1
0
mirror of https://gitlab.com/JKANetwork/CheckServer.git synced 2026-02-14 01:01:33 +01:00
Files
CheckServer/cronchk.php

89 lines
3.1 KiB
PHP
Executable File

<?php
require_once "functions.php";
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
$code = (int)$Param != 0 ? $Param : 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 ('$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);
}
//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($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");
}
}
//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)");
?>