📜 ⬆️ ⬇️

Displaying the status of servers from Zabbix to the desktop

The Zabbix monitoring system provides excellent monitoring capabilities for servers running AIX, Linux, * BSD, Windows, Mac OS X, network equipment, Web applications, as well as any piece of hardware that supports SNMP or at least responds to ping. Zabbix is ​​free and is licensed under the GPL. The server part of the system is installed only under * nix.
You can configure sending notifications to email, jabber, sms when unwanted events occur, such as server crashes, excessive CPU usage, lack of disk space, etc. There is also a web interface with beautiful graphs and a network map.
But the sysadmin, as you know, is a lazy creature. Therefore, in order not to constantly climb into the web interface, it is advisable to bring some graphics and a network map directly to his desktop.

Handyman table

In this article, we consider Windows XP / 7 as a client machine, but by briefly finishing the script, you can also use it in Linux.
')


Principle of operation:
Zabbix creates the necessary graphics and network map that we want to display on the screen. The PHP script calls Zabbix once a minute, receives these images from it and generates one of them for the desktop. The picture is placed on a web server. Another script on the client machine periodically downloads this image and sets it as wallpaper.

Step 0. Install and configure Zabbix.
This question is already chewed in the documentation , we will not dwell on it.
The article uses version 1.8.2. In earlier versions, other image addresses are used, so if you have Zabbix 1.4 or 1.6, you will need to make adjustments to the script.

Step 1. Create the necessary graphics and network maps in Zabbix.
This step also should not cause difficulties.
Charts can be created on the Configuration - Hosts page, network maps - in Maps .

Step 2. We write a script that generates a desktop background image.
We use PHP, the extension with .URL to get images, the gd library and ImageMagick for working with images.
You must first create a user in Zabbix, under which the script will log into the system.
The script will also receive a graph of the download of the WAN interface of the router from cacti and the image from the webcam in the server room. The graph in cacti seems more visual than the Zabbix graph.
The result of the script will be a BMP file.

The script does not claim to universality, but it is easy to remake to fit your needs. Be sure to change the values ​​of the constants at the beginning of the file to the settings for your system.

<?php <br/>
<br/>
// , ! <br/>
<br/>
//1. <br/>
define ( 'TMP_PATH' , '/usr/local/share/zabbix/php/tmp/' ) ; <br/>
//2. URL - Zabbix <br/>
define ( 'ZABBIX_URL' , 'http://monitoring.local/' ) ; <br/>
//3. Zabbix <br/>
define ( 'ZABBIX_USER' , 'mon' ) ; <br/>
//4. Zabbix <br/>
define ( 'ZABBIX_PW' , 'qwerty' ) ; <br/>
//5. Cacti <br/>
define ( 'CACTI_URL' , 'http://monitoring.local/cacti/' ) ; <br/>
//6. Cacti <br/>
define ( 'CACTI_USER' , 'admin' ) ; <br/>
//7. Cacti <br/>
define ( 'CACTI_PW' , 'qwerty' ) ; <br/>
//8. <br/>
define ( 'WALLPAPER_WIDTH' , 1280 ) ; <br/>
//9. <br/>
define ( 'WALLPAPER_HEIGHT' , 1024 ) ; <br/>
//10. , . <br/>
// . <br/>
$resources = array ( ) ; <br/>
// <br/>
$resources [ ] = array ( 'url' => 'http://monitoring.local/map.php?noedit=1&sysmapid=2' , 'x' => 280 , 'y' => 0 ) ; <br/>
// <br/>
$resources [ ] = array ( 'url' => 'http://monitoring.local/chart2.php?graphid=494&width=1138&period=86400' , 'x' => 26 , 'y' => 400 ) ; <br/>
//- <br/>
$resources [ ] = array ( 'url' => 'http://192.168.4.18/axis-cgi/jpg/image.cgi?resolution=320x240' , 'x' => 960 , 'y' => 690 ) ; <br/>
// cacti <br/>
$resources [ ] = array ( 'url' => 'http://monitoring.local/cacti/graph_image.php?local_graph_id=5&rra_id=0&view_type=tree&graph_start=' . ( time ( ) - 86400 ) . '&graph_end=' . time ( ) , 'x' => 357 , 'y' => 690 ) ; <br/>
<br/>
// <br/>
// , , . <br/>
<br/>
$error = false ; <br/>
<br/>
//"" Zabbix <br/>
$ch = curl_init ( ) ; <br/>
curl_setopt ( $ch , CURLOPT_URL , ZABBIX_URL . '/index.php' ) ; <br/>
curl_setopt ( $ch , CURLOPT_POST , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_POSTFIELDS , array ( 'form' => '1' , 'form_refresh' => '1' , 'name' => ZABBIX_USER , 'password' => ZABBIX_PW , 'enter' => 'Enter' ) ) ; <br/>
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_COOKIEJAR , "./cookie.txt" ) ; // <br/>
curl_setopt ( $ch , CURLOPT_COOKIEFILE , "./cookie.txt" ) ; <br/>
<br/>
$t = curl_exec ( $ch ) ; <br/>
curl_close ( $ch ) ; <br/>
<br/>
<br/>
//"" cacti. , cacti <br/>
$ch = curl_init ( ) ; <br/>
curl_setopt ( $ch , CURLOPT_URL , CACTI_URL . '/graph_image.php' ) ; <br/>
curl_setopt ( $ch , CURLOPT_POST , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_POSTFIELDS , array ( 'action' => 'login' , 'login_username' => CACTI_USER , 'login_password' => CACTI_PW ) ) ; <br/>
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_COOKIEJAR , "./cookie.txt" ) ; <br/>
curl_setopt ( $ch , CURLOPT_COOKIEFILE , "./cookie.txt" ) ; <br/>
<br/>
$t = curl_exec ( $ch ) ; <br/>
curl_close ( $ch ) ; <br/>
<br/>
// <br/>
foreach ( $resources as $k => $res ) <br/>
{ <br/>
$ch = curl_init ( ) ; <br/>
curl_setopt ( $ch , CURLOPT_URL , $res [ 'url' ] ) ; <br/>
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ; <br/>
curl_setopt ( $ch , CURLOPT_COOKIEJAR , "./cookie.txt" ) ; <br/>
curl_setopt ( $ch , CURLOPT_COOKIEFILE , "./cookie.txt" ) ; <br/>
<br/>
$file = curl_exec ( $ch ) ; <br/>
if ( $file ) file_put_contents ( TMP_PATH . 'temp_img' . $k . '.tmp' , $file ) ; <br/>
else $error = true ; <br/>
curl_close ( $ch ) ; <br/>
} <br/>
<br/>
// "" <br/>
$wp = imagecreatetruecolor ( WALLPAPER_WIDTH , WALLPAPER_HEIGHT ) ; <br/>
<br/>
if ( ! $error ) <br/>
{ <br/>
// <br/>
<br/>
// <br/>
$bg = imagecolorallocate ( $wp , 58 , 110 , 165 ) ; <br/>
imagefill ( $wp , 0 , 0 , $bg ) ; <br/>
<br/>
// <br/>
$images = array ( ) ; <br/>
foreach ( $resources as $k => $res ) <br/>
{ <br/>
$im = imagecreatefromfile ( TMP_PATH . 'temp_img' . $k . '.tmp' ) ; <br/>
if ( ! $im ) <br/>
{ <br/>
$error = true ; <br/>
break ; <br/>
} <br/>
imagecopy ( $wp , $im , $res [ 'x' ] , $res [ 'y' ] , 0 , 0 , imagesx ( $im ) , imagesy ( $im ) ) ; <br/>
imagedestroy ( $im ) ; <br/>
} <br/>
imagepng ( $wp , TMP_PATH . 'temp_fin.png' ) ; <br/>
} <br/>
<br/>
if ( $error ) <br/>
{ <br/>
// , <br/>
$bg = imagecolorallocate ( $wp , 192 , 192 , 192 ) ; <br/>
imagefill ( $wp , 0 , 0 , $bg ) ; <br/>
} <br/>
<br/>
// PNG BMP ImageMagick <br/>
$imgk = new Imagick ( TMP_PATH . 'temp_fin.png' ) ; <br/>
$imgk -> pingImage ( TMP_PATH . 'temp_fin.png' ) ; <br/>
$imgk -> readImage ( TMP_PATH . 'temp_fin.png' ) ; <br/>
$imgk -> setImageCompression ( imagick :: COMPRESSION_NO ) ; <br/>
$imgk -> setImageFormat ( "bmp" ) ; <br/>
$imgk -> writeImage ( TMP_PATH . 'wp.bmp' ) ; <br/>
<br/>
<br/>
// php.net <br/>
function imagecreatefromfile ( $path ) <br/>
{ <br/>
$info = @ getimagesize ( $path ) ; <br/>
if ( ! $info ) return false ; <br/>
<br/>
$functions = array ( <br/>
IMAGETYPE_GIF => 'imagecreatefromgif' , <br/>
IMAGETYPE_JPEG => 'imagecreatefromjpeg' , <br/>
IMAGETYPE_PNG => 'imagecreatefrompng' , <br/>
IMAGETYPE_WBMP => 'imagecreatefromwbmp' , <br/>
IMAGETYPE_XBM => 'imagecreatefromwxbm' , <br/>
) ; <br/>
<br/>
if ( ! $functions [ $info [ 2 ] ] ) return false ; <br/>
<br/>
if ( ! function_exists ( $functions [ $info [ 2 ] ] ) ) return false ; <br/>
<br/>
return $functions [ $info [ 2 ] ] ( $path ) ; <br/>
} <br/>
?>


Step 3. Add the script to cron.
It is advisable to store the script in a directory that is not published on the site. Otherwise, the script can be run from the browser, and the cookie file can be dragged off.
Add the script to cron:
# echo "*/1 * * * * root /usr/local/bin/php /usr/local/share/zabbix/get_image.php > /dev/null 2>&1" >> /etc/crontab
Zabbix, by default, updates the data every 30 seconds, so you can update the image once a minute.

Step 4. Install the automatic wallpaper change script on the client machine.
Create a VBS script:
For Windows XP
Dim res<br/>
Set oXMLHTTP = CreateObject( "MSXML2.XMLHTTP" ) <br/>
oXMLHTTP. Open "GET" , "http://monitoring.local/tmp/wp.bmp" , 0 ' <br/>
oXMLHTTP.Send<br/>
On Error Goto 0 <br/>
<br/>
Set oADOStream = CreateObject( "ADODB.Stream" ) <br/>
oADOStream.Mode = 3 <br/>
oADOStream. Type = 1 <br/>
oADOStream. Open <br/>
oADOStream.Write oXMLHTTP.responseBody <br/>
oADOStream.SaveToFile "C:\\wp.bmp" , 2 ' <br/>
Set oXMLHTTP = Nothing <br/>
Set oADOStream = Nothing <br/>
<br/>
<br/>
Dim WshShell<br/>
Set WshShell = WScript.CreateObject( "Wscript.Shell" )<br/>
WshShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper" , "" "C:\\wp.bmp" "" <br/>
WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters" , 1, True <br/>
<br/>
Set WshShell = Nothing


Windows Vista / Windows 7
Here the situation is more complicated. To change the wallpaper you need to call the WinAPI function. VBScript cannot do this, so we will have to write a small program in C ++.
The idea is taken from here .
At the end of the article there is a link to the compiled exe-schnick, but just in case I cite the source code:
#include <windows.h> <br/>
#include <stdio.h> <br/>
#include <iostream> <br/>
int main ( int argc, char ** argv ) <br/>
{ <br/>
if ( argc == 0 ) return 1 ; <br/>
SystemParametersInfo ( SPI_SETDESKWALLPAPER, 0 , ( PVOID ) argv [ 1 ] , SPIF_UPDATEINIFILE | SPIF_SENDCHANGE ) ; <br/>
return 0 ; <br/>
} <br/>

Now you need to replace the line in the VBS script for Windows XP
WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, True
on
WshShell.Run "C:\wallpaper.exe C:\wp.bmp", 1, True

Step 5. Add the VBS script to the task scheduler
It is necessary to run the script once a minute.

Done!

Project files - zabbix_wallpaper.zip .

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


All Articles