mirror of
https://gitlab.com/JKANetwork/CheckServer.git
synced 2026-02-15 01:31:37 +01:00
70 lines
2.6 KiB
PHP
Executable File
70 lines
2.6 KiB
PHP
Executable File
<?php
|
|
require_once "functions.php";
|
|
|
|
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//Delete old Checks (Based on user input)
|
|
$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
|
|
dbw_query($db_conn, "DELETE FROM CHKHIST WHERE ID_C='$site[ID_C]' ORDER BY `timestamp` ASC LIMIT $todelete");
|
|
}
|
|
}
|
|
//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)");
|
|
|
|
|
|
?>
|