Ad Widget

Collapse

Run a curl command from local agent, alert on error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4h3XOZ2kDRWeD4SiHUIh
    Junior Member
    • Dec 2014
    • 2

    #1

    Run a curl command from local agent, alert on error

    I'd like to be able to run a curl command from a local agent (web server running Zabbix agent), and have it alert if the curl returns an error. I want it to work similar to a web check.

    I don't want to use a web check, unless it can be configured to run from the agent, instead of from the Zabbix server.

    The reason is that each customer will have a different URL, and I want to define the URL in the Zabbix template, so that all servers with the template applied will check for:

    curl https://${customer_name}.mydomain.com/myapplication

    ...where $customer_name is defined as an environment variable on each webserver.

    Otherwise, I have to create a different web check for each customer, and that just sounds laborious and tedious.

    Is there a way to do this in Zabbix?
  • jan.garaj
    Senior Member
    Zabbix Certified Specialist
    • Jan 2010
    • 506

    #2
    1.) run it on zabbix-server
    But define new macro with customer name per host e.g. $CUSTOMER_NAME
    Then you can define URL in standard Zabbix web check
    Code:
    https://{$CUSTOMER_NAME}.mydomain.com/myapplication
    Check $MACRO in manual https://www.zabbix.com/documentation...ed_by_location

    2.) run it from local zabbix-agent
    But use your environment variable (be sure that's environment variable also for zabbix user):
    Code:
    curl https://{$customer_name}.mydomain.com/myapplication
    I'll prefer 1st option, because in that case I don't need to deploy any new userparameters to zabbix-agent + I don't utilize zabbix-agent, but I use very quick and optimized webcheck code on the zabbix server.

    But I also use 2nd option to check localhost performance with timeout feature. My implementation:
    Code:
    webcheck[[url]http://127.0.0.1:80/index.html,5][/url]
    UserParameter=webcheck[*],responsetime=`/usr/bin/curl -s -o /dev/null --connect-timeout $2 --max-time $2 --write-out '%{time_total}' -k "$1"`; if [ $$? != "0" ]; then printf "0"; else printf "%f" $responsetime; fi
    Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
    My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

    Comment

    • 4h3XOZ2kDRWeD4SiHUIh
      Junior Member
      • Dec 2014
      • 2

      #3
      Thank you.

      I've got a curl bash script that can output the reponse code to either the shell, or standard error.

      However, no matter what, it only returns "1" to Zabbix.

      Right now, I'm just using the "date" command to test.

      #My_Script.sh
      date >> /var/log/zabbix/system_run_log

      #My Item
      system.run[My_Script.sh,nowait]

      #My Trigger, triggers if response is not equal to 200
      {My_Template:system.run[My_Script.sh,nowait].last(0)}#200

      #My Zabbix Log
      163026.771 Requested [system.run[My_Script.sh,nowait]]
      163026.771 Executing command 'My_Script.sh'
      163026.772 Sending back [1]
      ...
      163056.771 Requested [system.run[My_Script.sh,nowait]]
      163056.771 Executing command 'My_Script.sh'
      163056.772 Sending back [1]

      #My /var/log/zabbix/system_run_log
      Wed Dec 10 16:30:26 PST 2014
      Wed Dec 10 16:30:56 PST 2014

      So the script is running, it should be exiting with Status Code 0, but no matter what, the Zabbix item always shows a last value of "1".

      I've tried echo to standard out, and exit 0, exit 200, exit $myvariable. In all of these scenarios, the Zabbix Item value is always "1".

      What am I doing wrong?

      -CS

      Comment

      • jan.garaj
        Senior Member
        Zabbix Certified Specialist
        • Jan 2010
        • 506

        #4
        Manual is your friend:

        You are using nowait mode, so you don't wait for the output/exit code.
        If you use it wait mode, than you will see output (not exit code) from the command, so you output from your script should be exit code only "command &>/dev/null; echo $?" Or in your case script output should be http code.
        Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
        My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

        Comment

        Working...