mirror of
https://gitlab.com/JKANetwork/CheckServer.git
synced 2026-02-22 04:54:06 +01:00
Fixing cronchk and adding JSON API check (WIP)
This commit is contained in:
54
cronchk.php
54
cronchk.php
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
require_once "functions.php";
|
||||
require_once "load.php";
|
||||
|
||||
function PING_IP($db_conn,$ID_C,$URL){
|
||||
function PING_IP($ID_C,$URL){
|
||||
if (strpos($URL, ":")){ //Si usa un puerto, dividir
|
||||
$host = explode(":", $URL)[0];
|
||||
$port = explode(":", $URL)[1];
|
||||
@@ -10,7 +10,7 @@ function PING_IP($db_conn,$ID_C,$URL){
|
||||
return $result; //Returns result
|
||||
}
|
||||
|
||||
function HTTP_CODE($db_conn,$ID_C,$URL,$Param){
|
||||
function HTTP_CODE($ID_C,$URL,$Param){
|
||||
$httpCode = httpCode($URL); //Code
|
||||
|
||||
$code = (int)$Param != 0 ? $Param : 200; //Establish the code test want to see
|
||||
@@ -23,7 +23,7 @@ function HTTP_CODE($db_conn,$ID_C,$URL,$Param){
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function DATABASE_CONN($db_conn,$ID_C,$URL){
|
||||
function DATABASE_CONN($ID_C,$URL){
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $URL);
|
||||
curl_setopt($ch, CURLOPT_HEADER, FALSE);
|
||||
@@ -39,10 +39,41 @@ function DATABASE_CONN($db_conn,$ID_C,$URL){
|
||||
}
|
||||
}
|
||||
|
||||
//WIP, doesnt compare right!
|
||||
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_CUSTOMREQUEST, 'POST');
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $JSONParams);
|
||||
}
|
||||
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);
|
||||
|
||||
$json_ret = json_decode($body,true);
|
||||
$json_param = json_decode($exceptedRes,true);
|
||||
$comp_ret =serialize($json_ret);
|
||||
$comp_param =serialize($json_param);
|
||||
if ($comp_ret == $comp_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
|
||||
@@ -50,15 +81,19 @@ while ($site = dbw_fetch_array($db_conn,$sites)){
|
||||
|
||||
switch ($site['ID_TC']) {
|
||||
case '1': //Ping to IP:Port
|
||||
$exitC = PING_IP($db_conn,$ID_C,$site['URL']);
|
||||
$exitC = PING_IP($ID_C,$site['url']);
|
||||
break;
|
||||
|
||||
case '2': //HttpCode
|
||||
$exitC = HTTP_CODE($db_conn,$ID_C,$site['URL'],$site['TCParam']);
|
||||
$exitC = HTTP_CODE($ID_C,$site['url'],$site['urlParam']);
|
||||
break;
|
||||
|
||||
case '4': //MySQL|Database connect
|
||||
$exitC = DATABASE_CONN($db_conn,$ID_C,$site['URL']);
|
||||
$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);
|
||||
@@ -70,13 +105,12 @@ while ($site = dbw_fetch_array($db_conn,$sites)){
|
||||
}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($db_conn,"maxTimeSave")*24*3600);
|
||||
$mintime = time()-(getSystemOpt("maxTimeSave")*24*3600);
|
||||
dbw_query($db_conn,"DELETE FROM CHKHIST WHERE `timestamp` < $mintime");
|
||||
|
||||
//Delete Checks stored for pages that not exist
|
||||
|
||||
Reference in New Issue
Block a user