This one is for you @Miguel_Sensacion.
Below we have an example of a nasty little "green/red" PHP script written by yours truly, which attempts to automate our site's "Service Status Board," for our fellow non-IT people to know when service interruptions are occurring. We have sweet monitoring, the regular/normal people don't, so they use our service status board. I'll run it as a cron every 3-5 minutes.
Per standard sysadmin procedure, I've made sure to leave all the commented out debugging code and strictly use procedural and function programming to give all the good programmers and devs the full sysadmin treatment.
There is one outstanding tablepress library I need to include, but I'll do that Wednesday when I get back to work and put this into production. Then we'll never have update our status board again, until WordPress releases 4.9 and totally ravages our site again.
Lastly, when things slow down (LOL!) I'll probably write a quick function which actually authenticates into two of the services, because even though the page loads, tomcat listeners sometimes like to stop working and reverse proxies sometimes just don't want to forward traffic. That will be 2.0, for now this is enough for a hacked up script.
Enjoy!
PS - Code written using Vi on an Ubuntu 16.04 Laptop.
#!/usr/bin/php
//THIS SCRIPT UPDATES OUR SERVICE STATUS BOARD
#####################################################################################
# #
# DEFINE FUNCTIONS #
# #
#####################################################################################
//THIS FUNCTION CHECKS IF A URL IS "ONLINE" BY RETURNING "FALSE" IF THE HTTP HEADERS DON'T RETURN A 200
function URLIsValid($url)
{
// echo "checking " . $url;
$exists = true;
$file_headers = @get_headers($url); //gets headers from url
$headers = array('HTTP/1.1 200 OK','HTTP/1.0 404 Not Found', 'HTTP/1.0 403 Forbidden', 'HTTP/1.0 500 Internal Server Error'); //array of possible header
foreach($headers as $headerVal)
{
// echo $file_headers[0];
if($file_headers[0] != $headers[0])
{
$exists = false;
}
}
return $exists;
}
//THIS FUNCTION ITERATES THROUGH THE SITES AND GENERATES THE OUTPUT FOR THE TABLE
function runIt($sites, $urls)
{
$table_id = 1; //TABLEPRESS TABLE NUMBER ID
$table = TablePress::$model_table->load( $table_id ); //LOAD THAT TABLE INTO MEMORY
$numDown = 0; //HOW MANY SERVICES DOWN
$i=0; //COUNTER
$pattern='/^man|^unknown|^degraded/'; //MATCH PATTERNS WHICH INDICATED WE'VE MANUALLY UPDATED THE STATUS
//LOOP THROUGH EACH URL
foreach($urls as $url)
{
// $friendlyName=$sites[$url]; //TEMP ASSIGN THE FRIENDLY NAME FOR THE URL
$trValue=table[data][' . $i . ']; //GET THE HTML TAG FOR THIS ROW
// $trValue="<span class = \"ok status\">OK</span>";
//IF WE'VE MANUALLY UPDATED THE TABLE DON'T MESS WITH IT OTHERWISE PROCEED TO UPDATE TABLE
if (!preg_match($pattern, substr($trValue,15), $matches))
{
if (URLIsValid($url))//SEE IF WE'RE UP
{
// echo $friendlyName . " is up \n";
table[data][' . $i . '] = "<span class = \"ok status\">OK</span>";
// echo "\n";
}
else
{
// echo $friendlyName[$i] . " is down \n";
table[data][' . $i . '] = "<span class = \"do status\">DOWN</span>";
// echo "\n";
$numDown++;
}
}
$i++;
}
//GET THE VALUE OF THE NETWORK ROW
$trValue=table[data][' . 5 . '];
// $trValue="<span class = \"ok status\">OK</span>";
//IF WE'VE MANUALLY SET IT LEAVE IT ALONE
if (!preg_match($pattern, substr($trValue,15), $matches))
{
if($numDown == 2)//IF TWO SERVICES ARE DOWN THE NETWORK IS LIKELY DEGRADED
{
// echo "Network degraded \n";
table[data][' . 5 . '] = "<span class = \"de status\">Degraded</span>";
}
else if ($numDown > 2)//IF MORE THAN TWO ARE DOWN THE ISP IS PROBABLY DOWN
{
// echo "Network Down \n";
table[data][' . 5 . '] = "<span class = \"do status\">Down</span>";
}
else
{
// echo "Network Okay \n";
table[data][' . 5 . '] = "<span class = \"ok status\">OK</span>";
}
}
TablePress::$model_table->save( $table ); //UPDATE THAT TABLE
}
//DEFINE MAIN
function main()
{
//Hashmap of our sites
//Order is important here so make sure you put them in the correct or they appear on the table
$sites=[
"http://www.companyhomepage.com" => "Company_Site", //Company's Webpage
"https://mail.company.com"=>"Email", //Email
"https://application.oursite.com"=>"Hosted_App1", //Hosted App1
"https://emportal.oursite.com/lolz"=>"EmployeePortal", //Employee Portal
];
//GET JUST THE KEYS FROM ABOVE
foreach($sites as $key => $value)
{
$urls[]=$key;
}
//BUILD THE TABLE
runIt($sites, $urls);
}
#######################################################################
# #
# SHOW TIME #
# #
#######################################################################
main();
//AND.....WE CAN ALL GO HOME NOW