cronchk.php 3.48 KB
Newer Older
1
<?php
2
require_once "load.php";
3

4
function PING_IP($ID_C,$URL){
5 6 7 8 9 10 11
	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
}
12

13
function HTTP_CODE($ID_C,$URL,$Param){
14
	$httpCode = httpCode($URL); //Code
15

16
	$code = (int)$Param != 0 ? $Param : 200; //Establish the code test want to see
17

18 19 20
	if ($httpCode == $code){ //Si es igual
		$ret = 0; //All right
	}else{ //Si no es igual (Incluye false)
JKA Network's avatar
JKA Network committed
21
		$ret = (int)$httpCode; //Code not 0 (And it's received code)
22 23 24
	}
	return $ret;
}
25

26
function DATABASE_CONN($ID_C,$URL){
27 28 29 30 31 32 33 34 35
	$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'){
JKA Network's avatar
JKA Network committed
36 37 38
		return 0;
	}else{ //Fail if it returns other than "OK".
		return 1;
39 40
	}
}
41

42 43 44
function JSON_API($ID_C,$URL,$params,$exceptedRes){
	$type = explode('|',$params)[0];
	$JSONParams = explode('|',$params)[1];
45

46 47 48 49
	$ch = curl_init();
	if ($type == 'GET'){
		curl_setopt($ch, CURLOPT_URL, $URL . $JSONParams);
	}else if ($type == 'POST'){
Kevin Puertas Ruiz's avatar
Kevin Puertas Ruiz committed
50
		curl_setopt($ch, CURLOPT_URL, $URL);
51 52 53 54 55 56 57 58 59 60 61
		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);
Kevin Puertas Ruiz's avatar
Kevin Puertas Ruiz committed
62
	if ($json_ret == $json_param){
63 64 65 66 67 68 69
		return 0;
	}else{
		return 1;
	}
}

$db_conn = getconn();
70 71 72
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
while ($site = dbw_fetch_array($db_conn,$sites)){
	$try = 0;
73
	$extitC = 0;
74 75 76 77 78 79 80
	$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
81
				$exitC = PING_IP($ID_C,$site['url']);
82
			break;
83

84
			case '2': //HttpCode
85
				$exitC = HTTP_CODE($ID_C,$site['url'],$site['urlParam']);
86
			break;
87

88
			case '4': //MySQL|Database connect
89 90 91 92 93
				$exitC = DATABASE_CONN($ID_C,$site['url']);
			break;

			case '5': //JSON GET | POST
				$exitC = JSON_API($ID_C,$site['url'],$site['urlParam'],$site['exceptedRes']);
94 95 96
			break;
		}
	}while($try <= 2 && $exitC == 1);
JKA Network's avatar
JKA Network committed
97 98 99 100 101 102 103 104
	$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')");
	}
105
		
106 107 108
}


109
//Delete old Checks (Based on time to save, put in secs)
110
$mintime = time()-(getSystemOpt("maxTimeSave")*24*3600);
111 112
dbw_query($db_conn,"DELETE FROM CHKHIST WHERE `timestamp` < $mintime");

113 114 115 116 117
//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)");


?>