Advertisement
ibragimovrinat

log.php

Aug 12th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>log</title>
  4. <meta http-equiv=Content-Type content="text/html; charset=UTF-8">
  5. </head>
  6.  
  7. <style>
  8. div.q {
  9.  font-family: 'Monaco', 'Courier New', monospace;
  10.  font-size: 10pt;
  11.  margin: 12px 0;
  12.  font-weight: normal;
  13.  background: #f3f3f3; padding: 5px; border: 1px dotted #999;
  14. }
  15. hr { height: 0; border: 0; margin: 0; padding: 0; display: none; }
  16. </style>
  17. <body>
  18. <?php
  19.     // версия с использованием сериализации
  20.     // (делает код проще и понятнее)
  21.     // сериализованные данные никогда не записываются извне, так что вроде безопасно
  22.    
  23.     $logfile = "log.txt";
  24.     $hbfile = "hb.txt";
  25.     $logentries = 40;
  26.     $safe_delay = 3600;
  27.  
  28.     if (isset($_GET['clearall'])){
  29.         $fp = fopen($logfile, "w");
  30.         fclose($fp);
  31.     }
  32.    
  33.     if (isset($_GET['t'])){         // записываем строчку в лог
  34.         echo "<h3>accepted</h3>\n";
  35.         touch($logfile);
  36.        
  37.         $entries = unserialize(file_get_contents($logfile));
  38.        
  39.         if ($entries === FALSE){        // in case of empty file
  40.             for ($j = 0; $j < $logentries; $j++){
  41.                 $entries[$j]['timestamp'] = time();
  42.                 $entries[$j]['text'] = ' ';                
  43.             }
  44.         }
  45.        
  46.         $entry['timestamp'] = time();
  47.         $entry['text'] = trim($_GET['t']);
  48.         $entries[] = $entry;
  49.         unset($entries[0]);
  50.         $entries = array_values($entries);
  51.         file_put_contents($logfile, serialize($entries));
  52.  
  53.     }elseif(isset($_GET['hb'])){     // пришел сигнал heartbeat
  54.         echo "<h3>hb</h3>\n";
  55.         $s['hbtime'] = time();
  56.         $s['hosts'] = array();
  57.         if (isset($_GET['c'])){             // heartbeat от нового пингера
  58.             $hosts = array();
  59.             for ($k = 0; $k < intval($_GET['c']); $k++){
  60.                 $name = $_GET['n'.$k];
  61.                 $value = intval($_GET['ls'.$k]);
  62.                 // если у хоста несколько имен, нам важен факт online любого из них
  63.                 if (isset($hosts[$name])){
  64.                     if ($hosts[$name] == 0){ $hosts[$name] = $value; }
  65.                 }else{
  66.                     $hosts[$name] = $value;
  67.                 }
  68.             }
  69.             $s['hosts'] = $hosts;
  70.         }
  71.         file_put_contents($hbfile, serialize($s));
  72.         //print_r($s);
  73.        
  74.     }else{      // нет параметров, выводим отчет
  75.        
  76.         touch($hbfile);
  77.         $s = unserialize(file_get_contents($hbfile));
  78.         if (FALSE !== $s){
  79.             echo "<h3>";
  80.  
  81.             $l_time = time() - $s['hbtime'];
  82.             if ($l_time < $safe_delay){
  83.                 echo "<font color=#66cc33>";
  84.             }else{
  85.                 echo "<font color=#ff3333>";
  86.             }
  87.             echo "hb".(int)($l_time/60)."</font>";
  88.             echo " ";
  89.            
  90.             foreach($s['hosts'] as $name=>$state){
  91.                 echo "$name<font color=".($state?"#66cc33>ON":"#ff3333>OFF")."</font> ";
  92.             }                
  93.             echo "</h3>\n";
  94.         }
  95.         touch($logfile);
  96.        
  97.         $entries = unserialize(file_get_contents($logfile));
  98.         //print_r($entries);
  99.         if (FALSE != $entries){
  100.             for ($j = $logentries-1; $j >= 0; $j --){
  101.                 echo "<div class=q>".htmlspecialchars($entries[$j]['text'])."</div><hr>\n";
  102.             }
  103.         }
  104.        
  105.     }
  106.  
  107. ?>
  108. </body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement