Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@int128
Created July 23, 2012 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save int128/3162977 to your computer and use it in GitHub Desktop.
Save int128/3162977 to your computer and use it in GitHub Desktop.
Generate an image to show environment status
<?php
define('ZABBIX_AUTHKEY', '****');
define('FONT_PATH', '/usr/share/fonts/vlgothic/VL-PGothic-Regular.ttf');
//define('FONT_PATH', '/usr/share/fonts/dejavu/DejaVuSans.ttf');
define('FONT_SIZE', 24);
define('IMAGE_WIDTH', 300);
define('IMAGE_HEIGHT', 50);
function zabbix_query ($request) {
$channel = curl_init();
$options = array(
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_FAILONERROR => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/json-rpc'),
CURLOPT_URL => 'http://localhost/zabbix/api_jsonrpc.php',
CURLOPT_POSTFIELDS => $request
);
curl_setopt_array($channel, $options);
$result = curl_exec($channel);
curl_close($channel);
return json_decode($result, true);
}
function zabbix_query_items ($itemidarray) {
$itemids = implode(',', $itemidarray);
$authkey = ZABBIX_AUTHKEY;
return zabbix_query(<<<EOT
{
"method":"item.get",
"params":{"output":"extend","itemids":[$itemids]},
"id":1,
"auth":"$authkey",
"jsonrpc":"2.0"
}
EOT
);
}
function write_as_image ($message) {
$image = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
$foreground_color = imagecolorallocate($image, 0, 0, 0);
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);
imagettftext($image, FONT_SIZE, 0, 0, FONT_SIZE+10, $foreground_color, FONT_PATH, $message);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
$items = zabbix_query_items(array(/* item IDs: 1, 2, 3, ... */));
$message = sprintf(<<<EOT
%s W %.1f ℃
EOT
,
number_format(((float) $items['result'][0]['lastvalue'])),
(float) $items['result'][1]['lastvalue']
);
write_as_image($message);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment