Advertisement
luckpy

discovery.sla

Aug 29th, 2021
1,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.37 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # GLOBAL VARIABLES #######################################################################
  4. URL='http://IP/zabbix/api_jsonrpc.php' # URL/IP Zabbix.                                  #
  5. HEADER='Content-Type:application/json'                                                   #
  6. USER='"Admin"' # Enter your username.                                                    #
  7. PASS='"zabbix"' # CEnter your password.                                                  #
  8. # END ####################################################################################
  9.  
  10. # ARGUMENTS AND VARIABLES ################################################################
  11. SERVICEID=$2 # ARG $2 = Place SLA serviceid.                                             #
  12. PARENTID=$3  # ARG $3 = Place SLA PARENTID                                               #
  13. DAYS=$3      # ARG $3 = Enter the desired number of days for the SLA filter.             #
  14. DIA=`date +%s`                                                                           #
  15. TODAY=`date -d '' '+%s'`                                                                 #
  16. MONDAY=$([ $(date -d 'last mon' '+%V') -eq $(date '+%V') ] && date -d 'last mon' '+%s' || date -d '' '+%s')
  17. MONTH=`date -d $(date '+%Y-%m-01') '+%s'`                                               #
  18. VERSION="1.5.0"                                                                          #
  19. UPDATE="15/06/2018"                                                                      #
  20. # END ####################################################################################
  21.  
  22. # API AUTENTICATION ######################################################################
  23. autenticate()
  24. {
  25.  JSON='
  26. {
  27.     "jsonrpc": "2.0",
  28.     "method": "user.login",
  29.     "params": {
  30.         "user": '$USER',
  31.         "password": '$PASS'
  32.     },
  33.     "id": 0
  34. }'
  35.  curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" |cut -d '"' -f8
  36. }
  37. TOKEN=$(autenticate) # Token obtained by Zabbix
  38. # END ###################################################################################
  39.  
  40. # FUNCTION LLD ##########################################################################
  41. function lld
  42. {
  43. sla ()
  44. {
  45.   JSON='
  46.       {
  47.        "jsonrpc": "2.0",
  48.        "method": "service.get",
  49.        "params": {
  50.        "output": ["name","goodsla","serviceid","showsla","status"],
  51.        "selectTrigger":[ "triggerid","description","priority" ],
  52.        "selectParent":{ "name":[ "description" ],
  53.        "selectDependencies" : "serviceid",
  54.        "filter":{"name":["$PID"],
  55.        "serviceids":["self.serviceids"]
  56.          }
  57.      }
  58.    }
  59.  ,
  60. "auth": "'$TOKEN'",
  61.                  "id": 1}'
  62.  
  63.  
  64. command01=(`curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" |python3 -mjson.tool | jq '.result | map({name,serviceid,parentserviceid:.parent.serviceid?}) | map(select(.parentserviceid | test("'$SERVICEID'|'$PARENTID'"))) | map({name, serviceid})'| grep "serviceid" |cut -d ":" -f 2 |sed 's/ //g' |sed 's/[",]//g'`)
  65. command02=(`curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" |python3 -mjson.tool | jq '.result | map({name,serviceid,parentserviceid:.parent.serviceid?}) | map(select(.parentserviceid | test("'$SERVICEID'|'$PARENTID'"))) | map({name, serviceid})'| grep "name" |cut -d ":" -f 2 |sed 's/ //g' |sed 's/[",]//g'`)
  66. command03=(`curl -s -X POST -H "$HEADER" -d "$JSON" "$URL" |python3 -mjson.tool | jq '.result | map({goodsla,name,serviceid,parentserviceid:.parent.serviceid?})| map(select(.parentserviceid | test("'$SERVICEID'|'$PARENTID'")))| map({name, goodsla})' | grep "goodsla"| cut -d ":" -f2| sed 's/ //g'|sed 's/"//g'`)
  67.  
  68. length=${#command01[@]}
  69.  printf "{\n"
  70.  printf '\t'"\"data\":["
  71.   for ((i=0;i<$length;i++))
  72.    do
  73.     printf '\n\t\t{'
  74.     printf "\"{#SLAID}\":\"${command01[$i]}\", \"{#SLANAME}\":\"${command02[$i]}\", \"{#SLAGOOD}\":\"${command03[$i]}\"}"
  75.      if [ $i -lt $[$length-1] ];then
  76.      printf ','
  77.      fi
  78.    done
  79.  printf "\n\t]\n"
  80.  printf "}\n"
  81.  }
  82.  sla
  83. }
  84. # END ###################################################################################
  85.  
  86. # ARGUMENT OPTION $1              #
  87. ###################################
  88. case $1 in                        #
  89.     LLD) lld;                     #
  90.         ;;                        #
  91.         *)                        #
  92. # END #############################
  93.  
  94. exit ;;
  95. esac
  96. # END SCRIPT ###########################################################################
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement