1
0
mirror of https://gitlab.com/JKANetwork/CheckServer.git synced 2026-02-15 09:41:33 +01:00
Files
CheckServer/cronchk.php
2018-01-16 09:36:53 +01:00

117 lines
3.5 KiB
PHP
Executable File

<?php
require_once "load.php";
function PING_IP($ID_C,$URL){
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
return $result; //Returns result
}
function HTTP_CODE($ID_C,$URL,$Param){
$httpCode = httpCode($URL); //Code
$code = (int)$Param != 0 ? $Param : 200; //Establish the code test want to see
if ($httpCode == $code){ //Si es igual
$ret = 0; //All right
}else{ //Si no es igual (Incluye false)
$ret = (int)$httpCode; //Code not 0 (And it's received code)
}
return $ret;
}
function DATABASE_CONN($ID_C,$URL){
$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'){
return 0;
}else{ //Fail if it returns other than "OK".
return 1;
}
}
function JSON_API($ID_C,$URL,$params,$exceptedRes){
$type = explode('|',$params)[0];
$JSONParams = explode('|',$params)[1];
$ch = curl_init();
if ($type == 'GET'){
curl_setopt($ch, CURLOPT_URL, $URL . $JSONParams);
}else if ($type == 'POST'){
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $JSONParams);
}
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);
$json_ret = json_decode($body,true);
$json_param = json_decode($exceptedRes,true);
if ($json_ret == $json_param){
return 0;
}else{
return 1;
}
}
$db_conn = getconn();
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
while ($site = dbw_fetch_array($db_conn,$sites)){
$try = 0;
$extitC = 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($ID_C,$site['url']);
break;
case '2': //HttpCode
$exitC = HTTP_CODE($ID_C,$site['url'],$site['urlParam']);
break;
case '4': //MySQL|Database connect
$exitC = DATABASE_CONN($ID_C,$site['url']);
break;
case '5': //JSON GET | POST
$exitC = JSON_API($ID_C,$site['url'],$site['urlParam'],$site['exceptedRes']);
break;
}
}while($try <= 2 && $exitC == 1);
$timestamp = time();
if ($exitC == 0){ //Check successful
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','0','$timestamp')");
}else if ($exitC == 1){ //Normal check failed
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`timestamp`) VALUES ('$ID_C','1','$timestamp')");
}else{ //Other code of check failed
dbw_query($db_conn, "INSERT INTO `CHKHIST` (`ID_C`,`code`,`errorText`,`timestamp`) VALUES ('$ID_C','1','$exitC','$timestamp')");
}
}
//Delete old Checks (Based on time to save, put in secs)
$mintime = time()-(getSystemOpt("maxTimeSave")*24*3600);
dbw_query($db_conn,"DELETE FROM CHKHIST WHERE `timestamp` < $mintime");
//Delete Checks stored for pages that not exist
dbw_query($db_conn, "DELETE FROM CHKHIST WHERE ID_C NOT IN (SELECT ID_C FROM CHECKS)");
?>