<?php
mysql_connect("localhost", "foo", "bar");
mysql_select_db("rose_streams");
while(true)
{
$timestamp = time();
// echo("timestamp: $timestamp\n");
if ($timestamp % 10 == 0) {
$seconds = 10;
$lasttimestamp = $timestamp;
$pingtest = array();
exec("ping 4.2.2.2 -c 100 -f",$pingtest);
$pingtest = cleanarray(explode(" ",str_replace("%"," ",$pingtest[3])));
$packetloss = $pingtest[5];
// echo("packetloss: $packetloss\n");
$cpu = array();
exec("iostat -xtc 2 2",$cpu);
$cpu = cleanarray(explode(" ",$cpu[14]));
$l1 = 100 - $cpu[5];
$uptime = sys_getloadavg();
$l2 = $uptime[1];
$l3 = $uptime[2];
$output = array();
exec("cat /proc/meminfo",$output);
$out2 = cleanarray(explode(" ",$output[0]));
$out3 = cleanarray(explode(" ",$output[7]));
$privpct = ($out3[1] / $out2[1]) * 100;
$netstat = array();
exec("netstat -ns | grep \"connections established\"",$netstat);
$netstat = cleanarray(explode(" ",$netstat[0]));
$connections = $netstat[0];
// echo("conn: $connections\n");
$ifconfig = array();
exec("ifconfig eth0", $ifconfig);
$ifconfig = str_replace(":"," ",$ifconfig[7]);
$ifconfig = cleanarray(explode(" ",$ifconfig));
$rxraw = $ifconfig[2];
$txraw = $ifconfig[7];
// echo("rawlast: $rxrawlast; raw: $rxraw\n");
if($rxrawlast != "")
{
$rxtotal = $rxraw - $rxrawlast;
$txtotal = $txraw - $txrawlast;
$rxbytesec = round($rxtotal / $seconds,0);
$txbytesec = round($txtotal / $seconds,0);
$rxbitsec = $rxbytesec * 8;
$txbitsec = $txbytesec * 8;
$query = "INSERT INTO `rose_streams`.`serverstats10` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1', '$l2', '$l3', '$phyheld', '$phymax', '$privheld', '$privmax', '$privpct', '$privfail', '$connections', '$rxraw', '$txraw', '$rxtotal', '$txtotal', '$rxbytesec', '$txbytesec', '$rxbitsec', '$txbitsec','$packetloss');";
// echo($query."\n");
mysql_query($query);
$stale = time()-599;
mysql_query("DELETE FROM `rose_streams`.`serverstats10` WHERE `timestamp` < $stale");
$currentmin = date("i");
if($currentmin != $lastmin)
{
extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats10` WHERE `timestamp` > '".($timestamp - 60)."'")));
mysql_query("INSERT INTO `rose_streams`.`serverstats60` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
$stale = $timestamp-3599;
mysql_query("DELETE FROM `rose_streams`.`serverstats60` WHERE `timestamp` < $stale");
$lastmin = $currentmin;
if(($lastmin % 4) == 0)
{
extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats10` WHERE `timestamp` > '".($timestamp - 240)."'")));
mysql_query("INSERT INTO `rose_streams`.`serverstats4` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
$stale = $timestamp-14399;
mysql_query("DELETE FROM `rose_streams`.`serverstats4` WHERE `timestamp` < $stale");
}
$epochmins = round(time()/60,0);
if(($epochmins % 24) == 0)
{
extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats60` WHERE `timestamp` > '".($timestamp - 1440)."'")));
mysql_query("INSERT INTO `rose_streams`.`serverstats24` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
$stale = $timestamp-86399;
mysql_query("DELETE FROM `rose_streams`.`serverstats24` WHERE `timestamp` < $stale");
}
if(($epochmins % 168) == 0)
{
extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) AS `packetloss` FROM `serverstats4` WHERE 'timestamp` > '".($timestamp - 10080)."'")));
mysql_query("INSERT INTO `rose_streams`.`serverstats7` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
$stale = $timestamp-604799;
mysql_query("DELETE FROM `rose_streams`.`serverstats7` WHERE `timestamp` < $stale");
}
if(($epochmins % 744) == 0)
{
extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) AS `packetloss` FROM `serverstats24` WHERE `timestamp` > '".($timestamp - 44640)."'")));
mysql_query("INSERT INTO `rose_streams`.`serverstats31` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
$stale = $timestamp-2678399;
mysql_query("DELETE FROM `rose_streams`.`serverstats31` WHERE `timestamp` < $stale");
}
}
}
$rxrawlast = $rxraw;
$txrawlast = $txraw;
}
sleep(1.0);
}
function cleanarray($input)
{
foreach($input as $y)
{
if($y != "") $output[] = $y;
}
return($output);
}
?>