Author Topic: Help please - Uptime Logging PHP/xml  (Read 1897 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
Help please - Uptime Logging PHP/xml
« on: 19 February, 2007, 22:42:47 »
I have run myself up a wall here in trying to make my own uptime Sig.
All is working fine exept for one thing and that is logging of uptimes in some sort of way so i can show the uptime record of my system.

the code i'm using atm (jumbled together from stuff i found on the web) is attached here.

Does anyone have an idea on how to log the uptime - check on refresh to see if record uptime has changed? I've spent almost 5 days on this thing already.
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

PtokaX forum

Help please - Uptime Logging PHP/xml
« on: 19 February, 2007, 22:42:47 »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 730
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Help please - Uptime Logging PHP/xml
« Reply #1 on: 19 February, 2007, 22:54:28 »
You can call /usr/bin/uptime with exec()
Everything could have been anything else and it would have just as much meaning.

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
Re: Help please - Uptime Logging PHP/xml
« Reply #2 on: 19 February, 2007, 23:00:30 »
i do something similar already with "$uptime = exec("cat /proc/uptime");" which outputs the uptime in seconds and i reformat it to y d h m afterwards. thats not the problem. my problem is saving the longest uptime value so i can call it up with a variable. i have no idea on how to do that :(
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 730
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Help please - Uptime Logging PHP/xml
« Reply #3 on: 19 February, 2007, 23:02:03 »
Why do you cat with exec()? Opening /proc/uptime should be easier.
I canot help you with persistence, I know only a little PHP.
Everything could have been anything else and it would have just as much meaning.

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
Re: Help please - Uptime Logging PHP/xml
« Reply #4 on: 17 May, 2007, 13:54:10 »
i did it that way following one of the guidelines i found as it gives me the uptime in a way i can use it with my crappy linux knowledge lol

the end output is this /record uptime still handwritten though


This bit did the trick:

Code: [Select]
//Record Check & Setting
$fileSecs = file_get_contents("record", true);
if ($fileSecs<$uptimeSecs)   {
            $recwrite = fopen("record","w");
                                fwrite($recwrite, $uptimeSecs);
                                fclose($recwrite);
            }
$recordSecs = file_get_contents("record", true);

well it works and thats the main thing ;)
i have been able to make a neat little solution, misusing phpsysinfo to get more info's ;)

Code: [Select]
<?php
// Uptime-Banner
/*
Based on Uptime-Project Profile XML-Parser and modded by Tokolosh
*/
// Variablendefinition ------------------------------------------------------------------------------->>

// (Path to) Temp-File
$xmlpath "http://info.tokolosh.selfip.com/index.php?template=xml";
$config[tmp] = "uptime.dat";      //needs to be created and chmod 777
$uptime exec("cat /proc/uptime");
$uptime split(" ",$uptime);
$uptimeSecs $uptime[0];
$generated exec("date");
//Record Check & Setting
$fileSecs file_get_contents("record"true);
if (
$fileSecs<$uptimeSecs)   {
            
$recwrite fopen("record","w");
                                
fwrite($recwrite$uptimeSecs);
                                
fclose($recwrite);
            }
$recordSecs file_get_contents("record"true);
// <<------------------------------------------------------------------------------- Variablendefinition
// Programmablauf ------------------------------------------------------------------------------->>

function format_uptime($seconds) {
  
$hours $seconds/60/60%24;
  
$mins $seconds/60%60;
  
$secs $seconds%60;
  
$days floor($seconds/60/60/24);
  
$years floor($days/365);
  
$days $days - ($years*365);
  
// -- a rough approximation of month data that does not take into account the actual START date
  //list($months,$days) = explode("|",date('n|d',mktime(0,0,0,0,$days,0)));
  
if ($years 0) {
    
$uptimeString .= $years;
    
$uptimeString .= (($years == 1) ? " Y" " Y");
  }
/*
  if ($months > 0) {
    $uptimeString .= (($years > 0) ? " " : "") . $months;
    $uptimeString .= (($months == 1) ? "M" : "M");
  }
*/
  
if ($days 0) {
    
$uptimeString .= (($years || $months 0) ? " " "") . $days;
    
$uptimeString .= (($days == 1) ? " d" " d");
  }
  if (
$hours 0) {
    
$uptimeString .= (($years || $months || $days 0) ? " " "") . $hours;
    
$uptimeString .= (($hours == 1) ? " h" " h");
  }
  if (
$mins 0) {
    
$uptimeString .= (($years || $months || $days || $hours 0) ? " " "") . $mins;
    
$uptimeString .= (($mins == 1) ? " m" " m");
  }
  return 
$uptimeString;
}

if(@
filemtime($config[tmp]) <= time() - 900 || @filesize($config[tmp]) == 0)
{
   
$input_xml = @fopen($xmlpath,"r");
   
$input = @fread($input_xml,8192);
   @
fclose($input_xml);
   
   if(
$input)
   {
      
$tmpfile = @fopen($config[tmp],w);
      if(
$tmpfile)
      {
         @
fwrite($tmpfile,$input);
      }
      else
      {
         echo 
"Can not write to Temp-File. Please chmod Temp-File to 777.";
         exit;
      }
      @
fclose($tmpfile);
   }
   
   unset(
$input,$input_xml,$tmpfile);
}

$tmpfile = @fopen($config[tmp],r);
$input = @fread($tmpfile,8192);
if(
$input)
{
   
$parser xml_parser_create();
   
xml_parse_into_struct($parser,$input,$values);
   
xml_parser_free($parser);
   
   
$xml = array();
   
   foreach(
$values as $data)
   {
      if(
$data[tag] != "Vitals")
      {
         
$xml[strtolower($data[tag])] = $data[value];
      }
      if(
$data[tag] != "Memory")
      {
         
$xml[strtolower($data[tag])] = $data[value];
      }
      if(
$data[tag] != "CPU")
      {
         
$xml[strtolower($data[tag])] = $data[value];
      }
   }
}
else
{
   echo 
"Kann Temp-Datei $config[tmp] nicht lesen.";
   exit;
}
@
fclose($tmpfile);
unset(
$tmpfile,$input,$parser,$values,$data);

// <<------------------------------------------------------------------------------- Programmablauf

// Siehe fuer weitere Felder die XML-Datei.
header("Content-type: image/png");
$image imagecreatefrompng("uptime_leer.png");
$black imagecolorallocate($image255,255,255);
$uptime 'Uptime: ' .format_uptime($uptimeSecs);
$generated 'Generated: ' .exec("date");
ImageTTFText ($image10012019$black"arialbd.ttf","Host: {code.red}Tokolosh");
ImageTTFText ($image1002041$black"arialbd.ttf","Distro: $xml[distro]");
ImageTTFText ($image10024041$black"arialbd.ttf","Kernel: $xml[kernel]"); 
//ImageTTFText ($image, 10, 0, 240, 58, $black, "arialbd.ttf","BogoMips: $xml[bogomips]");
//ImageTTFText ($image, 10, 0, 240, 41, $black, "arialbd.ttf", $uptime);
ImageTTFText ($image1002075$black"arialbd.ttf","Cpu: $xml[model] @ $xml[cpuspeed] MHz"); 
ImageTTFText ($image1002058$black"arialbd.ttf"$uptime);
ImageTTFText ($image10024058$black"arialbd.ttf","Record Uptime: ".format_uptime($recordSecs));
ImageTTFText ($image10075140$black"arialbd.ttf"$generated);

imagepng($image);
imagedestroy($image);

?>

this gives out the following



if anyone know a better way of doing it, i am open for suggestions ;)
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

PtokaX forum

Re: Help please - Uptime Logging PHP/xml
« Reply #4 on: 17 May, 2007, 13:54:10 »