Ad Widget

Collapse

UserParameters, multiple items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragan979
    Member
    • Apr 2018
    • 49

    #1

    UserParameters, multiple items

    I read that using LLD i can return multiple values


    this is my custom script:

    Code:
    #!/bin/bash
    
    curl -s -H "Accept: application/json" -X GET "http://172.30.61.82:4440/api/20/project/demo/executions?authtoken=ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA"  | python -m json.tool > /tmp/1.json
    
    
    jq -r '.|[.executions[] | select(.job.name != null) ] select(.job.name|contains("test")) |  sort_by(.id)  | reverse  | .[0]  | [.status, .job.name, ."date-started".date, ."date-ended".date, .job.project] | @csv' /tmp/1.json > /tmp/1.csv
    
    
    sed 's/,/ /g' /tmp/1.csv>/tmp/st.txt
    
    
    
    while read -r status name startdate enddate project; do
    
    startdate=${startdate//\"/}
    stime=$(date -d "${startdate/T/ }" +%s)
    enddate=${enddate//\"/}
    etime=$(date -d "${enddate/T/ }" +%s)
    let elapsed=etime-stime
    
    if [ "$status" == "\"aborted\"" ] && [ "$elapsed" -gt 300 ]; then echo $project"-"$name-"Long-Run"
    elif [ "$status" == "\"aborted\"" ] && [ "$elapsed" -lt 300 ]; then echo $project"-"$name " Aborted"
    elif [ "$status" == "\"failed\"" ]; then echo $project"-"$name "failed-Demo"
    elif [ "$status" == "\"succeeded\"" ]; then echo $project"-"$name "success"
    elif [ "$status" == "\"succeeded\"" ] && [ "$elapsed" -gt 300 ]; then echo $project"-"$name" Takes too long"
    fi
    
    done</tmp/st.txt


    script output:

    Job "aa" failed

    I have another same script which monitors another job (different parameter), how to avoid creating multiple keys and triggers ?


    I read i need to create item prototype,but i'm confused what should be specified in Userparameters, what name i should specify for key and what changes in script should be specified ?
    Last edited by dragan979; 06-04-2018, 03:28.
  • aigars.kadikis
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Mar 2018
    • 208

    #2
    Hi, dragan979!

    If you have two scripts which differs only with one variable, job name in your case, then you can proceed with LLD.
    If this is the case then first you need to create another script which discovers job names only

    For example one way how to approach this thing is output all job names in separate lines like:
    Code:
    echo -e "job name one\nanother job name\nthird job"
    Once you can achieve this with curl+other tools you can prepare the JSON format for LLD by piping:
    Code:
    sed 's/^/{\"{#JOB}\":\"/;s/$/\"},/' | tr -cd '[:print:]' | sed 's/^/{\"data\":[/;s/,$/]}/'
    After connecting these two commands together we can get to the nice JSON string:
    Code:
    {"data":[{"{#JOB}":"job name one"},{"{#JOB}":"another job name"},{"{#JOB}":"third job"}]}
    At the end this will be UserParameter for job list discover:
    Code:
    UserParameter=job.list,echo -e "job name one\nanother job name\nthird job" | sed 's/^/{\"{#JOB}\":\"/;s/$/\"},/' | tr -cd '[:print:]' | sed 's/^/{\"data\":[/;s/,$/]}/'
    Discovery rule for job discover:
    Click image for larger version

Name:	job-discovery.png
Views:	4856
Size:	68.1 KB
ID:	356679

    And item prototype:

    {#JOB} will be your argument in Item Prototype:
    Click image for larger version

Name:	job-state-prototype.png
Views:	4774
Size:	149.0 KB
ID:	356680


    Underneath the hood you need to replace your "job name" in main script "get-job-state.sh" with $1

    Regards,
    Aigars

    Comment

    • dragan979
      Member
      • Apr 2018
      • 49

      #3
      Thanks for very detaile instructions, i created trigger prototype but alert is not raised: host is attaced to template error is "Value should be in JSON format"


      This is output i get


      Code:
      {"data":[{"{#JOB}":"aa"},{"{#JOB}":"test"}]}
      I validated it on https://jsonlint.com and it's valid



      no entry in agent log (set debug level to 5-no valid output), server log:

      I
      Code:
      n get_value() key:'job.list'
      192320:  3319:20180406:175403.321 In get_value_external() key:'job.list'
      192321:  3319:20180406:175403.321 In zbx_popen() command:'/usr/lib/zabbix/externalscripts/job.list'
      192990:  3320:20180406:175408.019 In substitute_key_macros() data:'job.list'
      192991:  3320:20180406:175408.019 End of substitute_key_macros():SUCCEED data:'job.list'


      Script is located on agent and on Zabbix server


      [UserParameter=job.list,echo -e "aa\ntest" | sed 's/^/{"{#JOB}":"/;s/$/"},/' | tr -cd '[print:]' | sed 's/^/{"data":[/;s/,$/]}/' | /var/lib/zabbix/get-job-state.sh

      Code:
       zabbix_get -s 172.30.61.82 -p 10050 -k job.list (ran from zabbix server against rundeck machine)
      "demo"-"aa" failed
      Job discovery


      Click image for larger version  Name:	1.PNG Views:	1 Size:	167.7 KB ID:	356712



      Item prototype



      Click image for larger version  Name:	1.PNG Views:	1 Size:	220.6 KB ID:	356713


      Trigger prototype



      Click image for larger version  Name:	1.PNG Views:	1 Size:	152.2 KB ID:	356714




      And getting this errorcopied script to /usr/lib/zabbix/externalscripts but don't know what jobs.list contains)
      Last edited by dragan979; 07-04-2018, 16:38.

      Comment

      Working...