Skip to content

Instantly share code, notes, and snippets.

@manoot
Created January 30, 2012 19:37
Show Gist options
  • Save manoot/1706202 to your computer and use it in GitHub Desktop.
Save manoot/1706202 to your computer and use it in GitHub Desktop.
<?php
//////////
// THIS FILE DUMPS THE DATA TO DATA FILE
// (c) Travis Mathis - travisdmathis@gmail.com
// Zabbix Report Generator v0.1
//
// INCLUDES
require_once("ZabbixAPI.class.php");
// ERROR REPORTING
error_reporting(E_ALL);
set_time_limit(1800);
// VARIABLES
$timeperiod = $_GET['timePeriod'];
$site = $_GET['siteList'];
$trimmedsite = str_replace(" ", "_",$site);
//CONFIGURATION
$z_server = 'https://url/zabbix/';
$z_user = 'admin';
$z_pass = 'pass';
$z_img_path = "/usr/local/share/zabbix/custom_pages/tmp_images/";
$db_server = '127.0.0.1';
$db_user = 'root';
$db_pass = 'pass';
//NON CONFIGUREABLE
$z_tmp_cookies = "";
$z_url_index = $z_server ."index.php";
$z_url_graph = $z_server ."chart2.php";
$z_url_api = $z_server ."api_jsonrpc.php";
$z_login_data = "naime=" .$z_user ."&password=" .$z_pass ."&enter=Enter";
$data = "data";
// FUNCTIONS
function GraphImageById ($graphid, $period = 3600, $width, $height) { global $z_server, $z_user, $z_pass, $z_tmp_cookies, $z_url_index, $z_url_graph, $z_url_api, $z_img_path, $z_login_data, $trimmedsite;
// file names
$filename_cookie = $z_tmp_cookies ."zabbix_cookie_" .$graphid .".txt";
$image_name = $z_img_path .$trimmedsite ."_" .$graphid .".png";
//setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $z_url_index);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $z_login_data);
curl_setopt($ch, CURLOPT_COOKIEJAR, $filename_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $filename_cookie);
// login
curl_exec($ch);
// get graph
curl_setopt($ch, CURLOPT_URL, $z_url_graph ."?graphid=" .$graphid ."&width=" .$width ."&height=" .$height ."&period=" .$period);
$output = curl_exec($ch);
curl_close($ch);
// delete cookie
header("Content-type: image/png");
unlink($filename_cookie);
$fp = fopen($image_name, 'w');
fwrite($fp, $output);
fclose($fp);
header("Content-type: text/html");
}
function CreatePDF($array) { global $timeperiod, $data, $site, $trimmedsite, $z_img_path;
if(is_array($array)) {
foreach($array as $key=>$graphid) {
if(is_array($graphid)) {
CreatePDF($graphid);
} else {
if($key == 'graphid') {
$image_name = $z_img_path .$trimmedsite ."_" .$graphid .".png";
GraphImageById($graphid,$timeperiod,'750','150');
$fh = fopen($data, 'a') or die("can not open file");
$stringData = "2<" .$site .">";
fwrite($fh, $stringData);
$stringData = "I" .$image_name;
fwrite($fh, $stringData);
} else {
}
}
}
}
}
// Header
echo "<b>Host: </b>" .$site ."</br>";
echo "<b>Time Period: </b>" .$timeperiod ."</br></br>";
echo "<b><center> Please wait while I generate your report, you will be forwarded automatically upon completion </b></center>";
// Format $timeperiod into seconds
if($timeperiod == 'Hour'){
$timeperiod = '3600';
} elseif($timeperiod == 'Day'){
$timeperiod = '86400';
} elseif($timeperiod == 'Week'){
$timeperiod = '604800';
} elseif($timeperiod == 'Month'){
$timeperiod = '2678400';
} elseif($timeperiod == 'Year'){
$timeperiod = '31536000';
}
// get graphids
// Login to Zabbix API using ZabbixAPI.class.php
ZabbixAPI::debugEnabled(TRUE);
ZabbixAPI::login($z_server,$z_user,$z_pass)
or die('Unable to login: '.print_r(ZabbixAPI::getLastError(),true));
//fetch graph data host
$hosts = ZabbixAPI::fetch_array('host','get',array('extendoutput'=>'shorten','select_graphs'=>'shorten','filter'=>array('host'=>$site)))
or die('Unable to get hostids: '.print_r(ZabbixAPI::getLastError(),true));
#save graphs to directory for selected host
CreatePDF($hosts)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment