1
0
mirror of https://gitlab.com/JKANetwork/CheckServer.git synced 2026-02-22 21:13:46 +01:00

Delete global vars, better standarized things

This commit is contained in:
2017-10-31 09:41:28 +01:00
parent 6b38aa6cf8
commit 0d3ee17bee
5 changed files with 84 additions and 73 deletions

View File

@@ -1,55 +1,74 @@
<?php
require_once "functions.php";
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
function PING_IP($db_conn,$ID_C,$URL,$timestamp){
if (strpos($URL, ":")){ //Si usa un puerto, dividir
$host = explode(":", $URL)[0];
$port = explode(":", $URL)[1];
}else{$host=$URL;}
$result = isset($port) ? ping($host,$port) : ping($host); //Ping IP with or without port
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','$result','$timestamp')");
return $result; //Returns result
}
function HTTP_CODE($db_conn,$ID_C,$URL,$Param,$timestamp){
$httpCode = httpCode($URL); //Code
while ($site = dbw_fetch_array($db_conn,$sites)){
$ID_C= $site['ID_C'];
$timestamp = time(); //Timestamp for saving time of checking.
unset($result); //Avoiding problems
switch ($site['ID_TC']) {
case '1': //Ping to IP:Port
unset($port); //Needed for the ?: if
$code = (int)$Param != 0 ? $Param : 200; //Establish the code test want to see
if (strpos($site['URL'], ":")){ //Si usa un puerto, dividir
$host = explode(":", $site['URL'])[0];
$port = explode(":", $site['URL'])[1];
}else{$host=$site['URL'];}
$result = isset($port) ? ping($host,$port) : ping($host); //Ping IP with or without port
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','$result','$timestamp')");
break;
case '2': //HttpCode
$httpCode = httpCode($site['URL']); //Code
$code = (int)$site['TCParam'] != 0 ? $site['TCParam'] : 200; //Establish the code test want to see
if ($httpCode == $code){ //Si es igual
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$site[ID_C]','0','$timestamp')");
}else{ //Si no es igual (Incluye false)
$httpCode = (int)$httpCode; //Force int
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`codeText`,`timestamp`) VALUES ('$site[ID_C]','1','$httpCode','$timestamp')");
}
break;
case '4': //MySQL|Database connect
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site['URL']);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 4); //timeout in seconds
$body = curl_exec($ch); //All file
curl_close($ch);
$result = htmlentities($body);
if($result == 'OK'){
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$site[ID_C]','0','$timestamp')");
}else{ //Fail if it's not ok.
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$site[ID_C]','1','$timestamp')");
}
break;
if ($httpCode == $code){ //Si es igual
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','0','$timestamp')");
$ret = 0; //All right
}else{ //Si no es igual (Incluye false)
$httpCode = (int)$httpCode; //Force int
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`codeText`,`timestamp`) VALUES ('$ID_C','1','$httpCode','$timestamp')");
$ret = 1;
}
return $ret;
}
function DATABASE_CONN($db_conn,$ID_C,$URL,$timestamp){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 4); //timeout in seconds
$body = curl_exec($ch); //All file
curl_close($ch);
$result = htmlentities($body);
if($result == 'OK'){
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','0','$timestamp')");
$ret = 0;
}else{ //Fail if it's not ok.
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','1','$timestamp')");
$ret = 1;
}
return $ret;
}
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
while ($site = dbw_fetch_array($db_conn,$sites)){
$try = 0;
$ID_C= $site['ID_C'];
do {
$try++; //This is for avoiding posible "second" problems in some checks that are working but doesn't answer to first time
set_time_limit (15); //This resets time limit of php, avoids 30 sec limit in some servers
switch ($site['ID_TC']) {
case '1': //Ping to IP:Port
$exitC = PING_IP($db_conn,$ID_C,$site['URL'],time());
break;
case '2': //HttpCode
$exitC = HTTP_CODE($db_conn,$ID_C,$site['URL'],$site['TCParam'],time());
break;
case '4': //MySQL|Database connect
$exitC = DATABASE_CONN($db_conn,$ID_C,$site['URL'],time());
break;
}
}while($try <= 2 && $exitC == 1);
}
@@ -58,8 +77,8 @@ $sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
while ($site = dbw_fetch_array($db_conn,$sites)){
$count = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C='$site[ID_C]'")[0]; //Count how much checks has a site
if ($count > getSystemOpt("maxChecksSave")){
$todelete = $count - getSystemOpt("maxChecksSave"); //How much to delete
if ($count > getSystemOpt($db_conn,"maxChecksSave")){
$todelete = $count - getSystemOpt($db_conn,"maxChecksSave"); //How much to delete
dbw_query($db_conn, "DELETE FROM CHKHIST WHERE ID_C='$site[ID_C]' ORDER BY `timestamp` ASC LIMIT $todelete");
}
}