Advertisement
Guest User

Zabbix-report

a guest
Mar 28th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2. // FUNCTIONS
  3. function tempdir($dir=false,$prefix='zabbix_report_') {
  4.     $tempfile=tempnam($dir,$prefix);
  5.     if (file_exists($tempfile)) { unlink($tempfile); }
  6.     $old_umask = umask(0);
  7.     mkdir($tempfile,0775);
  8.     umask($old_umask);
  9.     if (is_dir($tempfile)) { return $tempfile; }
  10. }
  11.  
  12. function GetGraphImageById ($graphs, $stime, $period = 3600, $width, $height, $filename) {
  13.     global $z_server, $z_user, $z_pass, $z_tmp_cookies, $z_url_index, $z_url_graph, $z_url_api, $z_login_data;
  14.     // file names
  15.     $filename_cookie = tempnam($z_tmp_cookies,"zabbix_cookie_");
  16.     //setup curl
  17.     $ch = curl_init();
  18.     curl_setopt($ch, CURLOPT_URL, $z_url_index);
  19.     curl_setopt($ch, CURLOPT_HEADER, false);
  20.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21.     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  22.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  23.     curl_setopt($ch, CURLOPT_POST, true);
  24.     curl_setopt($ch, CURLOPT_POSTFIELDS, $z_login_data);
  25.     curl_setopt($ch, CURLOPT_COOKIEJAR, $filename_cookie);
  26.     curl_setopt($ch, CURLOPT_COOKIEFILE, $filename_cookie);
  27.     // login   
  28.     $output=curl_exec($ch);
  29.     // get graph
  30.     // TODO: foreach ($graphs as $graphid) { $filename....
  31.         $graphid = $graphs;
  32.         //$image_file = $z_tmpimg_path ."/".$trimmed_hostname ."_" .$graphid .".png";
  33.         curl_setopt($ch, CURLOPT_URL, $z_url_graph ."?graphid=" .$graphid ."&width=" .$width ."&height=" .$height ."&period=" .$period ."&stime=" .$stime);
  34.         $output = curl_exec($ch);
  35.         curl_close($ch);
  36.         // delete cookie
  37.         unlink($filename_cookie);
  38.         $fp = fopen($filename, 'w');
  39.         fwrite($fp, $output);
  40.         fclose($fp);
  41.     //}
  42. }
  43.  
  44. function CreatePDF($hostarray) {
  45.     global $stime, $timeperiod, $tmp_pdf_data, $z_tmpimg_path, $debug;
  46.    
  47.     $valid_graph_names = array ('CPU load' , 'memory usage', 'disk space usage' );
  48.    
  49.     foreach($hostarray as $key=>$host) {
  50.         $hostid   = $hostarray[$key]['hostid'];
  51.         $hostname = $hostarray[$key]['name'];
  52.         $trimmed_hostname = str_replace(" ", "_",$hostname);
  53.  
  54.         if ($debug) { echo "<b>$hostname(id:$hostid)</b></br>\n"; }
  55.         $fh = fopen($tmp_pdf_data, 'a') or die("Can't open $tmp_pdf_data for writing!");
  56.         $stringData = "1<Graphs for ".$hostname.">\n";
  57.         fwrite($fh, $stringData);
  58.         fclose($fh);
  59.         #$hostGraphs = ZabbixAPI::fetch_array('graph','get',array('output'=>'extend','hostids'=>$hostid))
  60.         $hostGraphs = ZabbixAPI::fetch_array('graph','get',array('output'=>array('graphid','name'),'hostids'=>$hostid))
  61.             or die('Unable to get graphs: '.print_r(ZabbixAPI::getLastError(),true));
  62.         #var_dump($hostGraphs);
  63.         asort($hostGraphs);
  64.         foreach($hostGraphs as $graphkey=>$graphs) {
  65.             if(in_array($hostGraphs[$graphkey]['name'],$valid_graph_names)){
  66.             $graphid    = $hostGraphs[$graphkey]['graphid'];
  67.             $graphname  = $hostGraphs[$graphkey]['name'];
  68.             $image_file = $z_tmpimg_path ."/".$trimmed_hostname ."_" .$graphid .".png";
  69.             if ($debug) { echo "$graphname(id:$graphid)</br>\n"; }
  70.             $fh = fopen($tmp_pdf_data, 'a') or die("Can't open $tmp_pdf_data for writing!");
  71.             $stringData = "2<$graphname>\n";
  72.             fwrite($fh, $stringData);
  73.             $stringData = "[" .$image_file ."]\n";
  74.             fwrite($fh, $stringData);
  75.             GetGraphImageById($graphid,$stime,$timeperiod,'750','150',$image_file);
  76.             fclose($fh);
  77.             if ($debug) { ob_flush(); flush(); }
  78.         }
  79.     }
  80. }
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement