Jump to content

String manipulation for load average graph


pygmalion

Recommended Posts

I am logging server uptime in 5 minute intervals. The output appears as follows:

 

Sun Aug 31 23:30:02 CDT 2008

23:30:02 up 2 days, 14:20,  2 users,  load average: 0.64, 1.01, 2.72

Sun Aug 31 23:35:01 CDT 2008

23:35:01 up 2 days, 14:25,  2 users,  load average: 0.53, 0.89, 2.20

Sun Aug 31 23:40:01 CDT 2008

23:40:01 up 2 days, 14:30,  2 users,  load average: 1.01, 1.00, 1.88

 

I want to be able to select all of this data, plug it into a textarea, and manipulate the strings so that I'm left with only the crucial information required to make a dotted graph that records the load over time. I only want to keep the 1 minute load average and throw away the 5 and 15 minute averages and obviously I need to keep the date intact in order to chart things properly.

 

I am a php ultra-newbie so figuring out where to start with this is a bit of an enigma to me. I don't expect complete walk-throughs or anything, but anything pointing me in the right direction would be much appreciated!

You can a combination of split and/or strtok to get this done.  For instance, to get the date, split it by a space, and then the 1st, 2nd,  3rd, and 5th indices.  You skip 0 because that will be the day, skip 4 for the time zone. 

 

To get the 1 minute load, just do a split on "average: " followed by a split with a comma.  Then take the zero index.

 

That should get you somewhere.  If you get stuck, you var_dump to see what is actually happening.

 

 

That's the exact log output, which I am proceeding to paste into a textarea, i.e.

 

<form action="makegraph.php" method="post">

<textarea name="uptime" cols="100" rows="30"></textarea><br><br>

<input type="submit" name="submit" value="SUBMIT">
</form>

 

Can I split by a carriage return somehow so I can transform each line into a separate array element?

This is what I have so far, perhaps using an unnecessarily long amount of code, but it's working :)

 

<?

$u = $_POST["uptime"];
$u = ereg_replace("  ", " ", $u);
$newline = "\n";
$lines = explode($newline, $u);
$dtcounter = "0";
$lcounter = "0";

for ($i=0; $i <= (count($lines) - 1); $i++) {
if ($i%2 == 0) {
	$transform = explode(" ", $lines[$i]);
		for ($t=0; $t <= count($transform); $t++) {
			if ($t == "1" or $t == "2") { $dateandtime = $dateandtime . $transform[$t] . "|"; }
			if ($t == "3") { 
				$transform[$t] = substr($transform[$t], 0, -3);
				$dateandtime = $dateandtime . $transform[$t];
			}
		}
	$dt[$dtcounter] = $dateandtime;
	$dtcounter = $dtcounter + 1;
	$dateandtime = "";
}
if ($i%2 == 1) {
	$transform = explode(" ", $lines[$i]);
		for ($t=0; $t <= count($transform); $t++) {
			if ($t == "10") {
				$transform[$t] = substr($transform[$t], 0, -1);
				$load = $transform[$t];
			}
		}
	$l[$lcounter] = $load;
	$lcounter = $lcounter + 1;
	$load = "";
}
}

$numlines = ($lcounter - 1);

for ($x=0; $x <= $numlines; $x++) {
echo $dt[$x] . " ---> " . $l[$x] . "<br>";
}

?>

 

And it prints out the following (from the data posted in the OP):

 

Aug|31|23:30 ---> 0.64

Aug|31|23:35 ---> 0.53

Aug|31|23:40 ---> 1.01

 

The goal is to use the data from these two arrays to produce a graph tracking the 5 minute load average. I'm assuming it should it be easy enough from this point on?

 

If anyone is interested in honing their skills for fun I wouldn't mind becoming privy to more efficient ways of doing this... :)

  • 2 weeks later...

Alright, so I managed to finish this little project in a manner sufficient enough for my own needs, although the algorithms are very inefficiently coded I would imagine.

 

Here is what I ended up with:

 

<?

$u = $_POST["uptime"];
$u = ereg_replace("  ", " ", $u);
$u = ereg_replace("0 min", "no", $u);
$u = ereg_replace("1 min", "no", $u);
$u = ereg_replace("2 min", "no", $u);
$u = ereg_replace("3 min", "no", $u);
$u = ereg_replace("4 min", "no", $u);
$u = ereg_replace("5 min", "no", $u);
$u = ereg_replace("6 min", "no", $u);
$u = ereg_replace("7 min", "no", $u);
$u = ereg_replace("8 min", "no", $u);
$u = ereg_replace("9 min", "no", $u);
$u = ereg_replace("10 min", "no", $u);
$u = ereg_replace("11 min", "no", $u);
$u = ereg_replace("12 min", "no", $u);
$u = ereg_replace("13 min", "no", $u);
$u = ereg_replace("14 min", "no", $u);
$u = ereg_replace("15 min", "no", $u);
$u = ereg_replace("16 min", "no", $u);
$u = ereg_replace("17 min", "no", $u);
$u = ereg_replace("18 min", "no", $u);
$u = ereg_replace("19 min", "no", $u);
$u = ereg_replace("20 min", "no", $u);
$u = ereg_replace("21 min", "no", $u);
$u = ereg_replace("22 min", "no", $u);
$u = ereg_replace("23 min", "no", $u);
$u = ereg_replace("24 min", "no", $u);
$u = ereg_replace("25 min", "no", $u);
$u = ereg_replace("26 min", "no", $u);
$u = ereg_replace("27 min", "no", $u);
$u = ereg_replace("28 min", "no", $u);
$u = ereg_replace("29 min", "no", $u);
$u = ereg_replace("30 min", "no", $u);
$u = ereg_replace("31 min", "no", $u);
$u = ereg_replace("32 min", "no", $u);
$u = ereg_replace("33 min", "no", $u);
$u = ereg_replace("34 min", "no", $u);
$u = ereg_replace("35 min", "no", $u);
$u = ereg_replace("36 min", "no", $u);
$u = ereg_replace("37 min", "no", $u);
$u = ereg_replace("38 min", "no", $u);
$u = ereg_replace("39 min", "no", $u);
$u = ereg_replace("40 min", "no", $u);
$u = ereg_replace("41 min", "no", $u);
$u = ereg_replace("42 min", "no", $u);
$u = ereg_replace("43 min", "no", $u);
$u = ereg_replace("44 min", "no", $u);
$u = ereg_replace("45 min", "no", $u);
$u = ereg_replace("46 min", "no", $u);
$u = ereg_replace("47 min", "no", $u);
$u = ereg_replace("48 min", "no", $u);
$u = ereg_replace("49 min", "no", $u);
$u = ereg_replace("50 min", "no", $u);
$u = ereg_replace("51 min", "no", $u);
$u = ereg_replace("52 min", "no", $u);
$u = ereg_replace("53 min", "no", $u);
$u = ereg_replace("54 min", "no", $u);
$u = ereg_replace("55 min", "no", $u);
$u = ereg_replace("56 min", "no", $u);
$u = ereg_replace("57 min", "no", $u);
$u = ereg_replace("58 min", "no", $u);
$u = ereg_replace("59 min", "no", $u);

$newline = "\n";
$lines = explode($newline, $u);
$dtcounter = "0";
$lcounter = "0";

for ($i=0; $i <= (count($lines) - 1); $i++) {

if ($i%2 == 0) {
	$transform = explode(" ", $lines[$i]);
		for ($t=0; $t <= count($transform); $t++) {
			if ($t == "1" or $t == "2") { $dateandtime = $dateandtime . $transform[$t] . "|"; }
			if ($t == "3") { 
				$transform[$t] = substr($transform[$t], 0, -3);
				$dateandtime = $dateandtime . $transform[$t];
			}
		}
	$dt[$dtcounter] = $dateandtime;
	$dtcounter = $dtcounter + 1;
	$dateandtime = "";
}

if ($i%2 == 1) {
	$transform = explode(" ", $lines[$i]);
		for ($t=0; $t <= count($transform); $t++) {
			if ($t == "10") {
				$transform[$t] = substr($transform[$t], 0, -1);
				$load = $transform[$t];
			}
		}
	$l[$lcounter] = $load;
	$lcounter = $lcounter + 1;
	$load = "";
}
}

$numlines = ($lcounter - 1);
$highestload = "0";

for ($x=0; $x <= $numlines; $x++) {
$loadcheck = $l[$x];
if ($loadcheck > $highestload) { $highestload = $loadcheck; }
}

$loadstack = (round($highestload / 10) + 1);

?>

<table border="0" cellpadding="0" cellspacing="0">
<tr><td valign="bottom">

<?

for ($x=0; $x <= 0; $x++) {
$grabdate = explode("|", $dt[$x]);
for ($t=0; $t <= count($grabdate); $t++) {
	if ($t == "0" or $t == "1") { $date = $date . $grabdate[$t]; }
}
}

echo $date;

?>

</td><td valign="bottom">

<?

for ($x=$loadstack; $x>=1; $x--) {
echo "<img src='l/" . $x . ".bmp'><br>";
}

?>

</td>
<td valign="bottom">

<?

for ($x=0; $x <= $numlines; $x++) {

$grabdate = explode("|", $dt[$x]);
for ($t=0; $t <= count($grabdate); $t++) {
	if ($t == "0" or $t == "1") { $newdate = $newdate . $grabdate[$t]; }
}
if ($date != $newdate) { 
	echo "</td></tr><tr><td colspan='3'><hr></td></tr><tr><td valign='bottom'>" . $newdate . "</td><td valign='bottom'>";
	for ($y=$loadstack; $y>=1; $y--) {
		echo "<img src='l/" . $y . ".bmp'><br>";
	}
	$date = $newdate;
	echo "</td><td valign='bottom'>";
}

$newdate = "";

$theload = round($l[$x]);
if ($theload == "0") { $theload = "0.5"; }
echo "<img src='d.bmp' width='2' height='" . (2 * $theload) . "'>";
}

?>

</td>
</tr></table>

 

I've attached a screenshot of the browser output this produces.

 

If anyone is interested in finding a better way to do this, by all means! Otherwise, thanks again for pointing me in the right direction.

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.