📜 ⬆️ ⬇️

zabbix_sender over HTTP - how to send data to Zabbix via HTTP | S

In this article I will give a possible solution to the problem for many system administrators who use the Zabbix monitoring system. Especially useful for those who monitor various programs in Zabbix: telephony systems, various routine operations with the database, 1C (yes, yes, we are such perverts people with unusual thinking that we monitor 1C in Zabbix). I myself suffered from doing Powershell scripts, using zabbix_sender.exe for sending. It was a terrible time.

How we got here


I use this system not the first year. At the beginning, they used to monitor accessibility (aha, so the combine did icmp requests and that's it). Now we use almost all the functionality, maps, inventory, reports. Recently, monitoring of backups (not only Linux-systems, but also different Windows), monitoring of various programs (cash registers in retail), exchanges, synchronization began to be screwed. And each time you had to write scripts, because many programs could not run zabbix_sender.exe themselves with the necessary parameters (especially for 1C). Other scripts were written to monitor these scripts. The program exports something to the file, the script parses it, sends everything via zabbix_sender.

Every time I - as a system administrator - had to do all the scripts. 1C programmers simply wrote files, the logic was all on me. It did not suit me like any lazy system administrator .
I have always resented (especially at the beginning), why is there no web interface for sending values ​​to Zabbix? And now everything is easier - well, if not, then we will write!

Implementation


Excuses
I’ll say right away that I’m not a PHP programmer, so for intelligent programmers, the code can cause bleeding from the eye area. Also, it is not worthwhile to expose this solution to a large load, for each value a separate instance of zabbix_sender is started, if you need to send many values ​​at once, you may need to look in the API and set one session without a break. In this way, we get about 200 parameters per minute while the flight is great!

Let's get started! Create a zabbix_sender folder on the zabbix server (I have the / var / www / zabbix folder, don't forget about root-rights):
')
mkdir /var/www/zabbix/zabbix_sender chown www-data:www-data /var/www/zabbix/zabbix_sender 

Create an index.php file, content in
spoiler
 <?php function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; } //header("Content-type: text/xml; charset=windows-1251"); $server=$_GET['server']; $key=$_GET['key']; $value=$_GET['value']; $zabbix_server_address="zabbix.domain.com"; if (empty($server)){ echo "parametr SERVER is EMPTY"; $ip_address=get_client_ip(); $error_msg= date('Ymd H:i:s') . " - ZABBIX_SENDER[warning] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: parametr SERVER is EMPTY\n"; error_log($error_msg,3,"/var/log/apache2/zabbix_sender.log"); exit; } if (empty($key)){ echo "parametr KEY is EMPTY"; $ip_address=get_client_ip(); $error_msg= date('Ymd H:i:s') . " - ZABBIX_SENDER[warning] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: parametr KEY is EMPTY\n"; error_log($error_msg,3,"/var/log/apache2/zabbix_sender.log"); exit; } if ($value==""){ echo "parametr value is EMPTY"; $ip_address=get_client_ip(); $error_msg= date('Ymd H:i:s') . " - ZABBIX_SENDER[warning] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: parametr VALUE is EMPTY\n"; error_log($error_msg,3,"/var/log/apache2/zabbix_sender.log"); exit; } $exec_str="/usr/local/bin/zabbix_sender -z $zabbix_server_address -p 10051 -s ".escapeshellarg($server)." -k ". escapeshellarg($key)." -o ". escapeshellarg($value); exec($exec_str,$out, $err); if ($err==0){ echo "OK"; } else { //print to html echo "ERROR:"; echo "</br>"; echo "server=$server, key=$key, value=$value"; echo "</br>"; var_dump($out); echo "</br>"; var_dump($err); //Log error $ip_address=get_client_ip(); $error_msg= date('Ymd H:i:s') . " - ZABBIX_SENDER[error] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: zabbix_sender: $out[0]\n"; error_log($error_msg,3,"/var/log/apache2/zabbix_sender.log"); } 


To send the value. you just need to write in a browser (or in a program that can make an HTTP GET request) a request of the form zabbix.domain.com/zabbix_sender/index.php?server=myhost&key=testitem&value=11 . Where server is the host in zabbiks (case-sensitive name!), Key / value is the name and value of item.

More or for interested


The get_client_ip function takes the IP address of the client who sent it (we will write to the log for debugging on errors).

Only two possible errors. Error when running zabbix_sender, we write to the log (which is located in /var/www/apache2/zabbix_sender.log )
 ZABBIX_SENDER[error] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: zabbix_sender 

No any GET parameter
 ZABBIX_SENDER[warning] - FROM: $ip_address; HTTP_PARAM: server=$server, key=$key, value=$value; ERROR: parametr KEY is EMPTY 

Where $ ip_address is the IP address from which the value was sent, the other parameters are clear.

Monitor the monitoring system


Looking at the errors after the monitoring did not work - it is bad, so we will monitor the monitoring.
To do this, add to zabbix_agentd.conf:

 UserParameter=zabbix_sender_web_status_error, grep -q 'ZABBIX_SENDER\[error\]' /var/log/apache2/zabbix_sender.log; echo $?; UserParameter=zabbix_sender_web_status_warning, grep -q 'ZABBIX_SENDER\[warning\]' /var/log/apache2/zabbix_sender.log; echo $?; 

Restart the agent. In order for the logs to be cleared once a day (and the error hung only for one day), we check that there was a /etc/logrotate.d/apache2 file with the contents:

 /var/log/apache2/*.log { daily missingok rotate 52 compress delaycompress notifempty create 644 www-data www-data sharedscripts postrotate /etc/init.d/apache2 reload > /dev/null endscript } 

In the Zabbix Server host, add two item:

image
image

And two triggers:




At last


In total, we received a new API for sending values ​​to Zabbix. Now we give a link to programmers zabbix.domain.com/zabbix_sender/index.php?server=myhost&key=testitem&value=11 , create the necessary Templates, bind to the hosts ... Well, and we still do a lot of things, but without the zabbix_sender, and with the modern web API. Programmers can track the success or failure of the delivery themselves, if everything is good, the web server will return the “OK” page, if not, it will report which parameter is not. Well, if it's really hard, Zabbix will say that he has an error.

PS I want to hear constructive criticism, perhaps there are even similar solutions, but I did not find them.

Source: https://habr.com/ru/post/253799/


All Articles