IvanFaQerstein IvanFaQerstein - 5 months ago 22
JSON Question

Is it possible to execute system.run[] with Zabbix 3.0 JSON-RPC API?

I'm trying to remotely stop/start services using

systemctl
inside Zabbix's
system.run[]
request/item but it doesn't seem to work.

I'm using Zabbix 3.0 JSON-RPC API and my JSON looks like this:

{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"filter": {
"host": "host-name",
"key_": "system.run[sudo systemctl stop nginx.service]"
}
},
"id": 1,
"auth": "my-token"
}


Result:

{"jsonrpc":"2.0","result":[],"id":1}


But I'm not too sure about validity of this request because all the information I've seen on
system.run[]
so far was related to zabbix_get. Is it even possible to execute
system.run[]
this way? What am I doing wrong?

This is obviously just filtering items but I have no idea how to replicate what zabbix_get does using Zabbix JSON-RPC API. There is no information I could find about this.

This works well for gathering data, tho:

{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"filter": {
"host": "host-name",
"key_": "vm.memory.size[used]"
}
},
"id": 1,
"auth": "my-token"
}


Result:

{
"jsonrpc": "2.0",
"result": [
{
"itemid": "455",
"type": "0",
"snmp_community": "",
"snmp_oid": "",
"hostid": "12241",
"name": "Used memory",
"key_": "vm.memory.size[used]",
"delay": "60",
"history": "90",
"trends": "365",
"status": "0",
"value_type": "3",
"trapper_hosts": "",
"units": "B",
"multiplier": "0",
"delta": "0",
"snmpv3_securityname": "",
"snmpv3_securitylevel": "0",
"snmpv3_authpassphrase": "",
"snmpv3_privpassphrase": "",
"formula": "1",
"error": "",
"lastlogsize": "0",
"logtimefmt": "",
"templateid": "106",
"valuemapid": "0",
"delay_flex": "",
"params": "",
"ipmi_sensor": "",
"data_type": "0",
"authtype": "0",
"username": "",
"password": "",
"publickey": "",
"privatekey": "",
"mtime": "0",
"flags": "0",
"interfaceid": "2",
"port": "",
"description": "",
"inventory_link": "0",
"lifetime": "30",
"snmpv3_authprotocol": "0",
"snmpv3_privprotocol": "0",
"state": "0",
"snmpv3_contextname": "",
"evaltype": "0",
"lastclock": "1466142275",
"lastns": "142277413",
"lastvalue": "3971121455",
"prevvalue": "3971001230"
}
],
"id": 1
}


If someone managed to execute
system.run[]
using JSON-RPC API, please, share your solution.
Thank you.

Answer

No, there seem to be a few things wrong. First, the Zabbix API is JSON-RPC (not REST). Second, the item.get method is primarily used to get item configuration from the server.

To request item values from an agent (and this is how remote commands are implemented with the system.run item key), you can use the already mentioned zabbix_get:

$ zabbix_get -s host-name -k "system.run[sudo systemctl stop nginx.service]"

Note that when you say "This works well for gathering data", you are not telling Zabbix to collect data at that point - it just returns you some data that is already in the database. In the case of remote commands, the best you could get would be "1" that indicates that last time this remote command was sent to the agent successfully.