Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>log</title>
- <meta http-equiv=Content-Type content="text/html; charset=UTF-8">
- </head>
- <style>
- div.q {
- font-family: 'Monaco', 'Courier New', monospace;
- font-size: 10pt;
- margin: 12px 0;
- font-weight: normal;
- background: #f3f3f3; padding: 5px; border: 1px dotted #999;
- }
- hr { height: 0; border: 0; margin: 0; padding: 0; display: none; }
- </style>
- <body>
- <?php
- // версия с использованием сериализации
- // (делает код проще и понятнее)
- // сериализованные данные никогда не записываются извне, так что вроде безопасно
- $logfile = "log.txt";
- $hbfile = "hb.txt";
- $logentries = 40;
- $safe_delay = 3600;
- if (isset($_GET['clearall'])){
- $fp = fopen($logfile, "w");
- fclose($fp);
- }
- if (isset($_GET['t'])){ // записываем строчку в лог
- echo "<h3>accepted</h3>\n";
- touch($logfile);
- $entries = unserialize(file_get_contents($logfile));
- if ($entries === FALSE){ // in case of empty file
- for ($j = 0; $j < $logentries; $j++){
- $entries[$j]['timestamp'] = time();
- $entries[$j]['text'] = ' ';
- }
- }
- $entry['timestamp'] = time();
- $entry['text'] = trim($_GET['t']);
- $entries[] = $entry;
- unset($entries[0]);
- $entries = array_values($entries);
- file_put_contents($logfile, serialize($entries));
- }elseif(isset($_GET['hb'])){ // пришел сигнал heartbeat
- echo "<h3>hb</h3>\n";
- $s['hbtime'] = time();
- $s['hosts'] = array();
- if (isset($_GET['c'])){ // heartbeat от нового пингера
- $hosts = array();
- for ($k = 0; $k < intval($_GET['c']); $k++){
- $name = $_GET['n'.$k];
- $value = intval($_GET['ls'.$k]);
- // если у хоста несколько имен, нам важен факт online любого из них
- if (isset($hosts[$name])){
- if ($hosts[$name] == 0){ $hosts[$name] = $value; }
- }else{
- $hosts[$name] = $value;
- }
- }
- $s['hosts'] = $hosts;
- }
- file_put_contents($hbfile, serialize($s));
- //print_r($s);
- }else{ // нет параметров, выводим отчет
- touch($hbfile);
- $s = unserialize(file_get_contents($hbfile));
- if (FALSE !== $s){
- echo "<h3>";
- $l_time = time() - $s['hbtime'];
- if ($l_time < $safe_delay){
- echo "<font color=#66cc33>";
- }else{
- echo "<font color=#ff3333>";
- }
- echo "hb".(int)($l_time/60)."</font>";
- echo " ";
- foreach($s['hosts'] as $name=>$state){
- echo "$name<font color=".($state?"#66cc33>ON":"#ff3333>OFF")."</font> ";
- }
- echo "</h3>\n";
- }
- touch($logfile);
- $entries = unserialize(file_get_contents($logfile));
- //print_r($entries);
- if (FALSE != $entries){
- for ($j = $logentries-1; $j >= 0; $j --){
- echo "<div class=q>".htmlspecialchars($entries[$j]['text'])."</div><hr>\n";
- }
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement