Help please - Uptime Logging PHP/xml
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Help please - Uptime Logging PHP/xml

Started by Psycho_Chihuahua, 19 February, 2007, 22:42:47

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Psycho_Chihuahua

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

bastya_elvtars

You can call /usr/bin/uptime with exec()
Everything could have been anything else and it would have just as much meaning.

Psycho_Chihuahua

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

bastya_elvtars

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.

Psycho_Chihuahua

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:

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



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

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

SMF spam blocked by CleanTalk