Ad Widget

Collapse

Request to Zabbix API or SQL (temperature report)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Connected
    Member
    • Mar 2015
    • 39

    #1

    Request to Zabbix API or SQL (temperature report)

    Hi ,

    I need to get info from Zabbix by SQL or API request.
    Info is - hosts with temperature alarm.
    I can't do this by trigger becouse there is many hosts ,
    and alarms are flapping (alarm / no alarm) in some cases.
    So that why I tried to do API request , but something go wrong.
    In this time I had this :

    Code:
    #Temperature DES 3200 get script
    #
    zbxUser=''
    zbxPass='' 
    
    zbxAPI='http://localhost/zabbix/api_jsonrpc.php'
    
    DATA=`date +%y%m%d%H%M%S`
    # Get auth token from zabbix
    curlOutput=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$zbxPass\", \"user\": \"$zbxUser\"}, \"jsonrpc\":\"2.0\", \"method\": \"user.login\", \"id\": 0}" $zbxAPI`
    authToken=`echo $curlOutput | sed -n 's/.*result":"\(.*\)",.*/\1/p'`
    #echo $authToken
    
    # Get all monitored and problem state triggers
    curlData="{\"jsonrpc\": \"2.0\", \"method\": \"template.get\", \"params\": {\"search\":{\"name\":\"D-Link DES3200\"}},\"output\":[\"template.id\"], \"auth\":\"$authToken\", \"id\": 1}"
    curlOutput=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "$curlData" $zbxAPI`
    echo "$curlOutput" > $DATA.log
    TEMPLATE=`cat $DATA.log| sed "s/,/,\n/g;s/}/\n/g"| grep "templateid" | sed s/[^0-9]//g`
    GET1=`echo "$TEMPLATE" | head -n1`
    echo GET1 = $GET1
    GET2=`echo "$TEMPLATE" | tail -n1`
    echo GET2 = $GET2
    rm $DATA.log
    
    
    #Get Item ID GET1
    curlData2="{\"jsonrpc\": \"2.0\", \"method\": \"item.get\", \"params\": {\"templateids\":\"$GET1\",\"search\":{\"name\":\"Temperature\"},\"searchWildcardsEnabled\":1,\"selectHosts\":[\"host\"]},\"output\":[\"lastvalue\"], \"auth\":\"$authToken\", \"id\": 1}"
    curlOutput2=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "$curlData2" $zbxAPI`
    echo "$curlOutput2" > ITEM2GET1.log
    ITEMIDGET1=`cat ITEM2GET1.log| sed "s/,/,\n/g;s/}/\n/g" | grep itemid | sed s/[^0-9]//g`
    echo Item ID GET1 = $ITEMIDGET1
    #Get Item ID GET2
    curlData3="{\"jsonrpc\": \"2.0\", \"method\": \"item.get\", \"params\": {\"templateids\":\"$GET2\",\"search\":{\"name\":\"Temperature\"},\"searchWildcardsEnabled\":1,\"selectHosts\":[\"host\"]},\"output\":[\"lastvalue\"], \"auth\":\"$authToken\", \"id\": 1}"
    curlOutput3=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "$curlData3" $zbxAPI`
    echo "$curlOutput3" > ITEM2GET2.log
    ITEMIDGET2=`cat ITEM2GET2.log| sed "s/,/,\n/g;s/}/\n/g" | grep itemid | sed s/[^0-9]//g`
    echo Item ID GET2 = $ITEMIDGET2
    rm *.log
    
    
    #Get host id GET1
    curlDataITEMIDGET1="{\"jsonrpc\": \"2.0\", \"method\": \"host.get\",\"params\": {\"monitored_hosts\":1}, \"output\":[\"hostids\"],\"filter\":{\"itemids\":\"$ITEMIDGET1\"}, \"auth\":\"$authToken\", \"id\": 1}"
    curlOutputITEMIDGET1=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "$curlDataITEMIDGET1" $zbxAPI`
    echo "$curlOutputITEMIDGET1" > curlOutputITEMIDGET1.log
    #Get host id GET2
    #curlDataITEMIDGET2="{\"jsonrpc\": \"2.0\", \"method\": \"host.get\",\"params\": {\"monitored_hosts\":1}, \"output\":[\"hostid\"],\"filter\":{\"itemids\":\"$ITEMIDGET2\"}, \"auth\":\"$authToken\", \"id\": 1}"
    #curlOutputITEMIDGET2=`curl -sS -i -X POST -H 'Content-Type: application/json-rpc' -d "$curlDataITEMIDGET2" $zbxAPI`
    #echo "$curlOutputITEMIDGET2" > curlOutputITEMIDGET2.log
    
    #rm *.log
    
    ####################
    So I had 2 Template IDs and 2 Item IDs, becouse I have 2 Templates D-Link DES3200 and D-Link DES3200_LOCAL .
    When I look to the curlOutputITEMIDGET1.log it consist not only host ID , there to many info , similiar to extended output.

    And now I don't know what to do next.
    Can anybody help me?

    The target is : show all hosts whith item temperature more then 50

    Zabbix version is 2.4
    Last edited by Connected; 28-06-2016, 20:55.
Working...