Advertisement
METAJIJI

cURL in PHP with zabbix

Oct 8th, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('php_flag display_errors','on');
  4. ini_set('php_value error_reporting', E_ALL);
  5.  
  6. define('TMP_PATH', './tmp/');                                   //1. Папка для хранения изображений. Default: "./tmp/"
  7. define('IMG_PATH', './img/');                                   //1. Папка для хранения изображений. Default: "./img/"
  8. define('ZABBIX_URL', 'https://zabbix.univers.local');           //2. URL интерфейса Zabbix. Default: "http://host.local/zabbix/"
  9. define('ZABBIX_USER', 'тут_логин');                                 //3. Пользователь в Zabbix. Default: "admin"
  10. define('ZABBIX_PW', 'тут_паролька');                                  //4. Пароль в Zabbix. Default: "zabbix"
  11. $resources = array();
  12. $resources[] = array('url' => ZABBIX_URL . '/map.php?noedit=1&sysmapid=1');    // Карта сети
  13. //$resources[] = array('url' => ZABBIX_URL . 'chart2.php?graphid=414&sid=bab26b5059a13b11&width=1138&period=86400&stime=20120703112617&refresh=627');   // График температуры
  14.  
  15. // Инсталлирован ли у нас CURL?
  16. function_exists('curl_init') || die('CURL is not installed!');
  17.  
  18. // Логинимся скриптом в Zabbix
  19. $ch = curl_init();
  20. curl_setopt($ch, CURLOPT_URL, ZABBIX_URL . '/index.php');
  21. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  22. curl_setopt($ch, CURLOPT_POST, true); // использовать метод POST
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, array(
  24.                 'request'       => '',
  25.                 'password'      => ZABBIX_PW,
  26.                 'name'          => ZABBIX_USER,
  27.                 'enter'         => 'Sign in',
  28.                 'autologin'     => 1,
  29.         ));
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  32. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // не проверять SSL сертификат
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // не проверять Host SSL сертификата
  34. curl_setopt($ch, CURLOPT_COOKIEJAR, TMP_PATH . '/cookie.txt'); // Сохраняем куки в файл
  35. curl_setopt($ch, CURLOPT_COOKIEFILE, TMP_PATH . '/cookie.txt');
  36. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); // это необходимо, чтобы cURL не высылал заголовок на ожидание
  37.  
  38. $t = curl_exec($ch);
  39. curl_close($ch);
  40.  
  41.  
  42. $img='';
  43. foreach($resources as $k => $res) {
  44.     $ch = curl_init();
  45.     curl_setopt($ch, CURLOPT_URL, $res['url']);
  46.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  47.     curl_setopt($ch, CURLOPT_COOKIEJAR, TMP_PATH . '/cookie.txt');
  48.     curl_setopt($ch, CURLOPT_COOKIEFILE, TMP_PATH . '/cookie.txt');
  49.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );                   // required for https urls
  50.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  51.  
  52.     $file = curl_exec($ch);
  53.     if($file) file_put_contents(IMG_PATH . 'img' . $k . '.png', $file);
  54.     curl_close($ch);
  55.     $img .= "\t    " . '<img src="' . IMG_PATH . 'img' . $k . '.png' . '" alt="img' . $k . '" />' . "\n";
  56. }
  57. usleep(500000); // sleep for 0.5 sec
  58.  
  59. ?>
  60. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  61. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  62.     <head>
  63.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  64.         <title>Мониторинг критических параметров zabbix</title>
  65.     </head>
  66.     <body>
  67.         <div>
  68. <?=$img?>
  69.         </div>
  70.     </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement