Skip to content

Instantly share code, notes, and snippets.

@manoot
Created February 1, 2012 18:09
Show Gist options
  • Save manoot/1718391 to your computer and use it in GitHub Desktop.
Save manoot/1718391 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:///zabbix/';
$z_user = 'admin';
$z_pass = '';
$z_img_path = "/usr/local/share/zabbix/custom_pages/tmp_images/";
$db_server = '127.0.0.1';
$db_user = '';
$db_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 = "name=" .$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;
foreach($array as $key=>$graphid) {
if(is_array($graphid)) {
CreatePDF($graphid);
} else {
if($key == 'graphid') {
$file = "./data.txt";
$image_name = $z_img_path .$trimmedsite ."_" .$graphid .".png";
$fh = fopen($file, 'a') or die("can't open file");
$stringData = "[" .$image_name ."]\n";
fwrite($fh, $stringData);
GraphImageById($graphid,$timeperiod,'750','150');
fclose($fh);
} else {
}
}
}
}
function CreatePDFAll($array) { global $z_image_path, $timeperiod, $data, $site, $trimmedsite;
foreach($array as $key=>$graphid) {
if(is_array($graphid)) {
echo "$key: $graphid\n";
CreatePDFAll($graphid);
} else {
if($key == 'graphid') {
#$file = "./data.txt";
$image_name = $z_img_path .$graphid .".png";
#$fh = fopen($file, 'a') or die("can't open file");
#$stringData = "[" .$image_name ."]\n";
#fwrite($fh, $stringData);
GraphImageById($graphid,$timeperiod,'750','150');
#fclose($fh);
echo "$key: $graphid";
} 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
$hostGraphs = ZabbixAPI::fetch_array('host','get',array('extendoutput'=>'shorten','select_graphs'=>'shorten','filter'=>array('host'=>$site)))
or die('Unable to get graphs: '.print_r(ZabbixAPI::getLastError(),true));
$hostsGraphs = ZabbixAPI::fetch_array('host','get',array('exendoutput'=>'shorten','select_graphs'=>'shorten'))
or die('Unable to get graphs: '.print_r(ZabbixAPI::getLastError(),true));
#save graphs to directory for selected host
$data = "./data.txt";
$fh = fopen($data, 'w');
$stringData = "1<Introduction>\n";
fwrite($fh, $stringData);
$stringData = "This is an automatically generated PDF file containing data on the .\n";
fwrite($fh, $stringData);
$stringData = "#NP\n";
fwrite($fh, $stringData);
$stringData = "2<Graphs>\n";
fwrite($fh, $stringData);
fclose($fh);
if($site == 'All') {
CreatePDFAll($hostsGraphs);
} else {
CreatePDF($hostGraphs);
}
?>
# OUTPUT OF CreatePDFAll
0: Array graphs: Array 1: Array graphs: Array 2: Array graphs: Array 3: Array graphs: Array 4: Array graphs: Array 5: Array graphs: Array 6: Array graphs: Array 7: Array graphs: Array 8: Array graphs: Array 9: Array graphs: Array 10: Array graphs: Array 11: Array graphs: Array 12: Array graphs: Array 13: Array graphs: Array 14: Array graphs: Array 15: Array graphs: Array 16: Array graphs: Array 17: Array graphs: Array 18: Array graphs: Array 19: Array graphs: Array 20: Array graphs: Array 21: Array graphs: Array 22: Array graphs: Array 23: Array graphs: Array 24: Array graphs: Array 25: Array graphs: Array 26: Array graphs: Array 27: Array graphs: Array 28: Array graphs: Array 29: Array graphs: Array 30: Array graphs: Array 31: Array graphs: Array 32: Array graphs: Array 33: Array graphs: Array 34: Array graphs: Array 35: Array graphs: Array 36: Array graphs: Array 37: Array graphs: Array 38: Array graphs: Array 39: Array graphs: Array 40: Array graphs: Array 41: Array graphs: Array 42: Array graphs: Array 43: Array graphs: Array 44: Array graphs: Array 45: Array graphs: Array 46: Array graphs: Array 47: Array graphs: Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment