Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run command on Zabbix agents?

I want to run a command on Zabbix agents:

  • Some simple unix commands, to obtain our reporting data.
  • When there is some processing required on the agent side.

There seem to be a variety approaches being talked about. So how to execute such commands on a Zabbix agent?

like image 583
Dreamcat4 Avatar asked Jun 14 '14 16:06

Dreamcat4


People also ask

How do I run a Zabbix server command?

Like any other item: go to Host Items, click 'Create a new one', the type will be Zabbix agent and a key: system. run and in the brackets you need to enter the command.


1 Answers

Run commands from the server directly from a new item.

First, set: EnableRemoteCommands=1 in the agent conf file (for all of your agents). To enable this feature.

Create a new item. A field on the "new item" page says 'key'. Enter:

system.run[command]

As the 'key' string. Where command is the command you want to be downloaded and run on the agent. Here is an example:

system.run[sysctl dev.cpu.0.temperature | cut -d ' ' -f 2 | tr -d C]

Perhaps you need to run something substantially more complex that is too long to fit in there? Then you will need to make a custom script. Put your custom scripts on a local webserver, or somewhere on the web.

Then you might set the item's key to:

system.run[ command -v script && script || wget script_url -O /path/to/script && script]

To fetch and download the missing script to the agent the first time it's executed. However that is a rather crude hack. Not very elegant.

A better way is to go to "Administration" --> "Scripts" in the menu. From there, you can create a new script to use in an item which may be configured to run on any of your agents.

Make a special custom item to re-run your script periodically (like a cron job). The job of the special script item is to update the agent with a collection of your other needed custom scripts.

Of course you could just write all of your custom scripts directly into zabbix's MYSQL database. And it is very tempting to do that. But be aware that then they'd be lost and vulnerable if your zabbix database ever gets fried or corrupted / lost. Zabbix databases always have a habit of growing large, unwieldy and out-of-control. So don't do that. Storing them separately somewhere else and under version control (git or subversion).

Once that's all sorted, we can finally go ahead and create further custom items to run your custom scripts. Again using:

system.run[script]

as the item's key just as before. Where 'script' is the command (plus any arguments), to execute your custom script locally on the agent.

like image 96
3 revs, 2 users 98% Avatar answered Oct 15 '22 06:10

3 revs, 2 users 98%