Get the App
SLTechnology News&Howtos  ›  Servers  › 

Zabbix-api, to be perfected

Shulou Source: shulou.com Published: 2022-06-02 19:02:06 10月03日 Update

Obtain the zabbix login authentication string (required) curl-X POST-H 'Content-Type: application/json'-d'{"jsonrpc": "2.0"," method ":" user.login "," params ": {" user ":" admin "," password ":" zabbix "}," auth ": null," id ": 0} 'http://192.168.74.133/api_jsonrpc.php

```returns a value

{

"jsonrpc": "2.0"

"result": "4f4a647dbb6d2d31a345fce2977425c5"

"id": 0

}

# # get all the host information. The return parameters of selectInterfaces can be found in https://www.zabbix.com/documentation/3.4/manual/api/reference/hostinterface/get

Curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc": "method": "host.get", "params": {"output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"]}, "id": 0, "auth": "4f4a647dbb6d2d31a345fce2977425c5"} 'http://192.168.74.133/api_jsonrpc.php

````returns values {"jsonrpc": "2.0"," result ": [{" hostid ":" 10084 "," host ":" Zabbix server "," interfaces ": [{" interfaceid ":" 1 "," ip ":" 127.0.0.1 "}]}, {" hostid ":" 10261 " "host": "zabbix-agent_134", "interfaces": [{"interfaceid": "2", "ip": "172.18.0.134"}]}, "id": 0} get all item information according to host_id curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc": "2.0" "method": "item.get", "params": {"output": "extend", "host": "Zabbix server"}, "id": 0, "auth": "4f4a647dbb6d2d31a345fce2977425c5"} 'http://192.168.74.133/api_jsonrpc.php

``returns a value that is directly intercepted because there are too many

{

"jsonrpc": "2.0"

"result": [{

"itemid": "23327"

"type": "0"

"snmp_community":

"snmp_oid":

"hostid": "10084"

"name": "Host name of zabbixagentd running"

"key": "agent.hostname"

"delay": "1h"

"history": "1w"

"trends": "0"

"status": "0"

"value_type": "1"

"trapper_hosts":

"units":

"snmpv3_securityname":

"snmpv3_securitylevel": "0"

"snmpv3_authpassphrase":

"snmpv3_privpassphrase":

# # get all the information of the item according to the specified item and sort by name Note: extend means all, and "sortfield": "name" is sorted by name

Curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc", "method": "item.get", "params": {"output": "extend", "host": "Zabbix server", "search": {"key_": "system.cpu.util [, nice]"}, "sortfield": "name"}, "id": 0, "auth": "4f4a647dbb6d2d31a345fce2977425c5"} 'http://192.168.74.133/api_jsonrpc.php

````returns values {"jsonrpc": "23302", "result": [{"itemid": "23302", "type": "0", "snmp_community": "", "snmp_oid": "", "hostid": "10084", "name": "CPU nice time", "key_": "system.cpu.util [, nice]" "delay": "1m", "history": "1w", "trends": "365d", "status": "0", "value_type": "0", "trapper_hosts": "", "units": "%", "snmpv3_securityname": "", "snmpv3_securitylevel": "0" "snmpv3_authpassphrase": "," snmpv3_privpassphrase ":", "formula": "," error ":", "lastlogsize": "0", "logtimefmt": "", "templateid": "17358", "valuemapid": "0", "params": "," ipmi_sensor ":" "authtype": "0", "username": "", "password": "," publickey ":", "privatekey": "", "mtime": "0", "flags": "0", "interfaceid": "1", "port": "" "description": "The time the CPU has spent running users' processes that have been niced.", "inventory_link": "0", "lifetime": "0", "snmpv3_authprotocol": "0", "snmpv3_privprotocol": "0", "state": "0", "snmpv3_contextname": "," evaltype ":" 0 " "jmx_endpoint": "," master_itemid ":" 0 "," timeout ":" 3s "," url ":"," query_fields ": []," posts ":"," status_codes ":" 200", "follow_redirects": "1", "post_type": "0" "http_proxy": "," headers ": []," retrieve_mode ":" 0 "," request_method ":" 0 "," output_format ":" 0 "," ssl_cert_file ":"," ssl_key_file ":", "ssl_key_password": "" "verify_peer": "0", "verify_host": "0", "allow_traps": "0", "lastclock": "0", "lastns": "0", "lastvalue": "0", "prevvalue": "0"}], "id": 0} returns only the most recent value of a particular item on a specific host However, the output value must be rendered as an array That is, ["lastvalue"] curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc": "method": "item.get", "params": {"output": ["lastvalue"], "host": "Zabbix server", "search": {"key_": "system.cpu.util [, nice]"}}, "id": 0, "auth": "4f4a647dbb6d2d31a345fce2977425c5"} 'http://192.168.74.133/api_jsonrpc.php

```returns a value

{

"jsonrpc": "2.0"

"result": [{

"itemid": "23302"

"lastvalue": "0"

}]

"id": 0

}

# # get all templates ID, arrange ```curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc": "2.0"," method ":" hostgroup.get "," params ": {" output ": [" groupid "," name "]," sortfield ":" groupid "}," id ": 0," auth ":" 4f4a647dbb6d2d31a345fce2977425c5 "} 'http://192.168.74.133/api_jsonrpc.php in ascending order of groupid

```returns a value

{

"jsonrpc": "2.0"

"result": [{

"groupid": "1"

"name": "Templates"

}, {

"groupid": "2"

"name": "Linux servers"

}, {

"groupid": "4"

"name": "Zabbix servers"

}, {

"groupid": "5"

"name": "Discovered hosts"

}, {

"groupid": "6"

"name": "Virtual machines"

}, {

"groupid": "7"

"name": "Hypervisors"

}, {

"groupid": "8"

"name": "Templates/Modules"

}, {

"groupid": "9"

"name": "Templates/Network devices"

}, {

"groupid": "10"

"name": "Templates/Operating systems"

}, {

"groupid": "11"

"name": "Templates/Server hardware"

}, {

Groupid: "12"

"name": "Templates/Applications"

}, {

"groupid": "13"

"name": "Templates/Databases"

}, {

"groupid": "14"

"name": "Templates/Virtualization"

}]

"id": 0

}

# # create a template based on groupid. The name of the template is Linux template3

Curl-H "Content-Type: application/json-rpc"-d'

{

"jsonrpc": "2.0"

"method": "template.create"

"params": {

"host": "Linux template3", # here is the name of the template

"groups": {

"groupid": 1

}

}

"auth": "4f4a647dbb6d2d31a345fce2977425c5"

"id": 0

} 'http://192.168.74.133/api_jsonrpc.php

````returns values {"jsonrpc": "2.0"," result ": {" templateids ": [" 10264 "]}," id ": 1} which templates are included according to GROUPID query Output template name curl-H "Content-Type: application/json-rpc"-d'{"jsonrpc": "method": "hostgroup.get", "params": {"output": ["host"], "groupids": 1, "selectTemplates": ["host"]}, "auth": "4f4a647dbb6d2d31a345fce2977425c5" "id": 0} 'http://192.168.74.133/api_jsonrpc.php

```returns a value

{

"jsonrpc": "2.0"

"result": [{

"groupid": "1"

"templates": [{

"host": "Linux template"

"templateid": "10262"

}, {

"host": "Linux template2"

"templateid": "10263"

}, {

"host": "Linux template3"

"templateid": "10264"

}]

}]

"id": 0

}

# # obtain CVM inventory information of specified CVM

Curl-H "Content-Type: application/json-rpc"-d'

{

"jsonrpc": "2.0", "method": "host.get", "params": {"output": "extend", "selectInventory": "extend", "filter": {"host": ["zabbix-agent_134"]}}, "id": 0, "auth": "4f4a647dbb6d2d31a345fce2977425c5"

} 'http://192.168.74.133/api_jsonrpc.php

````returns values {"jsonrpc": "10261", "result": [{"hostid": "10261", "proxy_hostid": "0", "host": "zabbix-agent_134", "status": "0", "disable_until": "0", "error": "", "available": "1" "errors_from": "0", "lastaccess": "0", "ipmi_authtype": "- 1", "ipmi_privilege": "2", "ipmi_username": "," ipmi_password ":", "ipmi_disable_until": "0", "ipmi_available": "0" "snmp_disable_until": "0", "snmp_available": "0", "maintenanceid": "0", "maintenance_status": "0", "maintenance_type": "0", "maintenance_from": "0", "ipmi_errors_from": "0", "snmp_errors_from": "0" "ipmi_error": "," snmp_error ":", "jmx_disable_until": "0", "jmx_available": "0", "jmx_errors_from": "0", "jmx_error": "", "name": "zabbix-agent_134", "flags": "0" "templateid": "0", "description": "", "tls_connect": "1", "tls_accept": "1", "tls_issuer": "", "tls_subject": "," tls_psk_identity ":", "tls_psk": "," proxy_address ":" "auto_compress": "1", "inventory": {"hostid": "10261", "inventory_mode": "0", "type": "123,123", "type_full": "123123", "name": "123123123", "alias": "123123" "os": "Linux", "os_full": "3123", "os_short": "," serialno_a ":", "serialno_b": "", "tag": "3123123", "asset_tag": "12312", "macaddress_a": "12312" "macaddress_b": "213123123", "hardware": "", "hardware_full": "," software ":", "software_full": "", "software_app_a": "," software_app_b ":", "software_app_c": "" "software_app_d": "," software_app_e ":", "contact": "," location ":", "location_lat": "", "location_lon": "," notes ":", "chassis": "" "model": "," hw_arch ":", "vendor": "," contract_number ":", "installer_name": "", "deployment_status": "," url_a ":", "url_b": "" "url_c": "," host_networks ":", "host_netmask": "," host_router ":", "oob_ip": "", "oob_netmask": "," oob_router ":", "date_hw_purchase": "" "date_hw_install": "," date_hw_expiry ":", "date_hw_decomm": "," site_address_a ":", "site_address_b": "", "site_address_c": "," site_city ":" "site_state": "," site_country ":", "site_zip": "," site_rack ":", "site_notes": "", "poc_1_name": "," poc_1_email ":" "poc_1_phone_a": "," poc_1_phone_b ":", "poc_1_cell": "," poc_1_screen ":", "poc_1_notes": "", "poc_2_name": "," poc_2_email ":" "poc_2_phone_a": "," poc_2_phone_b ":", "poc_2_cell": "," poc_2_screen ":", "poc_2_notes": ""}}], "id": 0}

In addition, it is troublesome to say that the markdown format of 51cto is really troublesome. I may put it on my blog when my server arrives.

Tags: Template name host information sort output ascending order blog character string meaning array method server format list part trouble service query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL OPPO Reno Docker Linux