Linux Programmer | RHCE | RHCSA

Search This Blog

Zabbix

Zabbix API

1.  Get access token:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "user.login", "params": {"user": "Admin", "password": "xxxxx"}, "id": 1, "auth": null}' \
  "http://zabbix-link/zabbix/api_jsonrpc.php"

 

Output:
{"jsonrpc":"2.0","result":"87adffg007f7f2fg3b421056406dfretyde7cbe6f0a75","id":1}

 

2. Get host list:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "host.get", "params": {"output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"]}, "id": 2, "auth": "87adffg007f7f2fg3b421056406dfretyde7cbe6f0a75"}' \
  "zabbix-link/zabbix/api_jsonrpc.php"

 

2.1 Python program:

from zabbix_api import ZabbixAPI
 
# Connect to the Zabbix API
zapi = ZabbixAPI(server="http://zabbix-url/zabbix")
zapi.login("username", "xxxxx")
 
# Get all hosts from the Zabbix server
hosts = zapi.host.get({"output": ["host"]})
 
# Print the host names
for host in hosts:
    print(host["host"])
 

3. Add tags:

 curl --header "Content-Type: application/json" --request POST --data '{"jsonrpc": "2.0", "method": "host.update", "params": 
{"hostid": "10493","tags":  
[  
{ "tag": "tag1","value": "value1"},
{ "tag": "tag2","value": ""}

]},"auth": "87adffg007f7f2fg3b421056406dfretyde7cbe6f0a75","id": 1 }' "http://zabbix-link/zabbix/api_jsonrpc.php"

3. Add tags in zabbix host without deleting existing tags. ( Python program )

from zabbix_api import ZabbixAPI
 
# Connect to the Zabbix API
zapi = ZabbixAPI(server="http://zabbix-address/zabbix")
zapi.login("user-name", "password")
 
# Get the ID of the host you want to update
hosts = zapi.host.get({"filter": {"host": "host-name"}})
if hosts:
    host_id = hosts[0]["hostid"]
else:
    print("Host not found")
 
# Get the existing tags for the host
existing_tags = zapi.host.get({"hostids": host_id, "selectTags": "extend"})
existing_tags = existing_tags[0]["tags"]
 
# Add the new tag to the list of existing tags
new_tag = {"tag": "hostedby", "value": "xxxx"}
existing_tags.append(new_tag)
 
# Update the host with the new list of tags
zapi.host.update(
    {
        "hostid": host_id,
        "tags": existing_tags,
    }
)  

4. Create new tags in zabbix host ( Python program )

from zabbix_api import ZabbixAPI
 
# Connect to the Zabbix API
zapi = ZabbixAPI(server="http://your-zabbix-server.com/zabbix")
zapi.login("your-zabbix-api-username", "your-zabbix-api-password")
 
# Get the ID of the host you want to update
hosts = zapi.host.get({"filter": {"host": "your-host-name"}})
if hosts:
    host_id = hosts[0]["hostid"]
else:
    print("Host not found")
 
# Update the tags for the host
zapi.host.update(
    {
        "hostid": host_id,
        "tags": [
            {"tag": "tag1", "value": "value1"},
            {"tag": "tag2", "value": "value2"},
        ],
    }

 

5. Update tag in all host at a same time.

from zabbix_api import ZabbixAPI
import sys
# Connect to the Zabbix API
zapi = ZabbixAPI(server="http://zabbix-server/zabbix")
zapi.login("username", "password")
 
# Get all hosts from the Zabbix server
hosts = zapi.host.get({"output": ["host"]})
 
# Print the host names
for host in hosts:
#    print(host["host"])
    hostis=host["host"]
    # Get the ID of the host you want to update
    hosts = zapi.host.get({"filter": {"host": hostis}})
    if hosts:
        host_id = hosts[0]["hostid"]
    else:
        print("Host not found")
 
    print(hostis,host_id)
    # Get the existing tags for the host
    existing_tags = zapi.host.get({"hostids": host_id, "selectTags": "extend"})
    existing_tags = existing_tags[0]["tags"]
 
    # Add the new tag to the list of existing tags
    new_tag = {"tag": "xxxxx", "value": "xxxxx"}
    existing_tags.append(new_tag)
 
    # Update the host with the new list of tags
    zapi.host.update(
       {
           "hostid": host_id,
           "tags": existing_tags,
       }
    )
 

Upgrade Zabbix Version:

https://impurval.blogspot.com/p/upgrade-zabbix-60x-to-62x.html

No comments:

Post a Comment

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...