Ad Widget

Collapse

Zabbix API help

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ActionParsnip
    Junior Member
    • Apr 2023
    • 6

    #1

    Zabbix API help

    Hi I've looked and looked with no joy. I have used the Zabbix API to get the hostgroup of a host

    Code:
    HostGroupID=`curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\":\"extend\",\"filter\":{\"name\":[\""$HOSTGROUP_NAME"\"]}},\"auth\":\""$api_key"\",\"id\":0}" http://$server/zabbix/api_jsonrpc.php | jq -r .result[0].groupid`
    This returns a numberic value and this can be even used to delete the hostgroup. No problem

    Code:
    curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"hostgroup.delete\",\"params\":[\""${HostGroupID}"\"],\"auth\":\""$api_key"\",\"id\":0}" http://$server/zabbix/api_jsonrpc.php
    Can someone please advise how I can use this HostGroupID variable or even the HOSTGROUP_NAME variable to see if the group is empty, please? If it is empty then I can run the delete command to tidy up. This ultimately is the goal.

    Thanks in advance
    -AndyW
  • Answer selected by ActionParsnip at 05-04-2023, 09:51.
    markfree
    Senior Member
    • Apr 2019
    • 868

    You could try the following.
    Copy the script, change the placeholders to your own, make it executable and run it like this.
    Code:
    ./zbx_del_empty_group.sh [GROUP NAME]

    Code:
    #!/bin/bash
    
    SERVER=[ZABBIX IP OR DOMAIN NAME]
    URL="http://$SERVER/zabbix/api_jsonrpc.php"
    API_TOKEN=[USER TOKEN]
    HOSTGROUP_NAME=$1
    
    PAYLOAD_GET='
    {
        "jsonrpc": "2.0",
        "method":"hostgroup.get",
        "params": {
            "filter":{
                "name":["'"$HOSTGROUP_NAME"'"]
            },
        "selectHosts": "count"
        },
        "auth":"'"$API_TOKEN"'",
        "id":0
    }'
    
    RESPONSE_GET=$(curl -s \
            -H 'Content-Type: application/json' \
            -H "Authorization: Bearer $api_key" \
            -d "$PAYLOAD_GET" \
            "$URL")
    
    HostGroupID=$(jq -r '.result[0].groupid' <<< $RESPONSE_GET)
    HostsCount=$(jq -r '.result[0].hosts' <<< $RESPONSE_GET)
    
    if [[ "$HostsCount" = "0" ]]; then
        PAYLOAD_DEL='
            {
            "jsonrpc": "2.0",
            "method":"hostgroup.delete",
            "params":["'"$HostGroupID"'"],
            "auth":"'"$API_TOKEN"'",
            "id":0
            }'
    
        RESPONSE_DEL=$(curl -s \
           -H 'Content-Type: application/json' \
           -H "Authorization: Bearer $api_key" \
           -d "$PAYLOAD_DEL" \
           "$URL")
    
        echo -e "\nEmpty host \"${HOSTGROUP_NAME}\" ($HostGroupID) deleted\n"
    else
        echo -e "\nHost \"${HOSTGROUP_NAME}\" is not empty!\n"
    fi
    
    exit 0​
    Note that I haven't treated any possible errors.

    Comment

    • LenR
      Senior Member
      • Sep 2009
      • 1005

      #2
      Add the selectHosts='count' option to your first query. That may not be the exact syntax, I use the Python DSL, so don't code JSONRPC directly. That will return the count of hosts in the group, delete if zero.

      Comment

      • markfree
        Senior Member
        • Apr 2019
        • 868

        #3
        You could try the following.
        Copy the script, change the placeholders to your own, make it executable and run it like this.
        Code:
        ./zbx_del_empty_group.sh [GROUP NAME]

        Code:
        #!/bin/bash
        
        SERVER=[ZABBIX IP OR DOMAIN NAME]
        URL="http://$SERVER/zabbix/api_jsonrpc.php"
        API_TOKEN=[USER TOKEN]
        HOSTGROUP_NAME=$1
        
        PAYLOAD_GET='
        {
            "jsonrpc": "2.0",
            "method":"hostgroup.get",
            "params": {
                "filter":{
                    "name":["'"$HOSTGROUP_NAME"'"]
                },
            "selectHosts": "count"
            },
            "auth":"'"$API_TOKEN"'",
            "id":0
        }'
        
        RESPONSE_GET=$(curl -s \
                -H 'Content-Type: application/json' \
                -H "Authorization: Bearer $api_key" \
                -d "$PAYLOAD_GET" \
                "$URL")
        
        HostGroupID=$(jq -r '.result[0].groupid' <<< $RESPONSE_GET)
        HostsCount=$(jq -r '.result[0].hosts' <<< $RESPONSE_GET)
        
        if [[ "$HostsCount" = "0" ]]; then
            PAYLOAD_DEL='
                {
                "jsonrpc": "2.0",
                "method":"hostgroup.delete",
                "params":["'"$HostGroupID"'"],
                "auth":"'"$API_TOKEN"'",
                "id":0
                }'
        
            RESPONSE_DEL=$(curl -s \
               -H 'Content-Type: application/json' \
               -H "Authorization: Bearer $api_key" \
               -d "$PAYLOAD_DEL" \
               "$URL")
        
            echo -e "\nEmpty host \"${HOSTGROUP_NAME}\" ($HostGroupID) deleted\n"
        else
            echo -e "\nHost \"${HOSTGROUP_NAME}\" is not empty!\n"
        fi
        
        exit 0​
        Note that I haven't treated any possible errors.

        Comment

        • ActionParsnip
          Junior Member
          • Apr 2023
          • 6

          #4
          Originally posted by markfree
          You could try the following.
          Copy the script, change the placeholders to your own, make it executable and run it like this.
          Code:
          ./zbx_del_empty_group.sh [GROUP NAME]

          Code:
          #!/bin/bash
          
          SERVER=[ZABBIX IP OR DOMAIN NAME]
          URL="http://$SERVER/zabbix/api_jsonrpc.php"
          API_TOKEN=[USER TOKEN]
          HOSTGROUP_NAME=$1
          
          PAYLOAD_GET='
          {
          "jsonrpc": "2.0",
          "method":"hostgroup.get",
          "params": {
          "filter":{
          "name":["'"$HOSTGROUP_NAME"'"]
          },
          "selectHosts": "count"
          },
          "auth":"'"$API_TOKEN"'",
          "id":0
          }'
          
          RESPONSE_GET=$(curl -s \
          -H 'Content-Type: application/json' \
          -H "Authorization: Bearer $api_key" \
          -d "$PAYLOAD_GET" \
          "$URL")
          
          HostGroupID=$(jq -r '.result[0].groupid' <<< $RESPONSE_GET)
          HostsCount=$(jq -r '.result[0].hosts' <<< $RESPONSE_GET)
          
          if [[ "$HostsCount" = "0" ]]; then
          PAYLOAD_DEL='
          {
          "jsonrpc": "2.0",
          "method":"hostgroup.delete",
          "params":["'"$HostGroupID"'"],
          "auth":"'"$API_TOKEN"'",
          "id":0
          }'
          
          RESPONSE_DEL=$(curl -s \
          -H 'Content-Type: application/json' \
          -H "Authorization: Bearer $api_key" \
          -d "$PAYLOAD_DEL" \
          "$URL")
          
          echo -e "\nEmpty host \"${HOSTGROUP_NAME}\" ($HostGroupID) deleted\n"
          else
          echo -e "\nHost \"${HOSTGROUP_NAME}\" is not empty!\n"
          fi
          
          exit 0​
          Note that I haven't treated any possible errors.
          Thanks for this man. This really helped. Its just knowing where to put the bits in the code. Inside one brace, outside another and so on. Thanks for the help. This is fab

          -AndyW

          Comment

          • ActionParsnip
            Junior Member
            • Apr 2023
            • 6

            #5
            Problem solved

            Comment

            Working...