PtokaX forum

Stuff => Offtopic => Topic started by: Psycho_Chihuahua on 19 February, 2007, 22:42:47

Title: Help please - Uptime Logging PHP/xml
Post by: Psycho_Chihuahua 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.
Title: Re: Help please - Uptime Logging PHP/xml
Post by: b_e on 19 February, 2007, 22:54:28
You can call /usr/bin/uptime with exec()
Title: Re: Help please - Uptime Logging PHP/xml
Post by: Psycho_Chihuahua 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 :(
Title: Re: Help please - Uptime Logging PHP/xml
Post by: b_e 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.
Title: Re: Help please - Uptime Logging PHP/xml
Post by: Psycho_Chihuahua 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

(http://tokolosh.selfip.com/newut/uptime.png)
This bit did the trick:

//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 ;)

<?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 > 0 || $months > 0) ? " " : "") . $days;
   
$uptimeString .= (($days == 1) ? " d" : " d");
 }
 if (
$hours > 0) {
   
$uptimeString .= (($years > 0 || $months > 0 || $days > 0) ? " " : "") . $hours;
   
$uptimeString .= (($hours == 1) ? " h" : " h");
 }
 if (
$mins > 0) {
   
$uptimeString .= (($years > 0 || $months > 0 || $days > 0 || $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($image, 255,255,255);
$uptime = 'Uptime: ' .format_uptime($uptimeSecs);
$generated = 'Generated: ' .exec("date");
ImageTTFText ($image, 10, 0, 120, 19, $black, "arialbd.ttf","Host: {code.red}Tokolosh");
ImageTTFText ($image, 10, 0, 20, 41, $black, "arialbd.ttf","Distro: $xml[distro]");
ImageTTFText ($image, 10, 0, 240, 41, $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 ($image, 10, 0, 20, 75, $black, "arialbd.ttf","Cpu: $xml[model] @ $xml[cpuspeed] MHz");
ImageTTFText ($image, 10, 0, 20, 58, $black, "arialbd.ttf", $uptime);
ImageTTFText ($image, 10, 0, 240, 58, $black, "arialbd.ttf","Record Uptime: ".format_uptime($recordSecs));
ImageTTFText ($image, 10, 0, 75, 140, $black, "arialbd.ttf", $generated);

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

?>


this gives out the following

(http://tokolosh.selfip.com/uptime/uptime.png)

if anyone know a better way of doing it, i am open for suggestions ;)