mirror of
https://gitlab.com/JKANetwork/CheckServer.git
synced 2026-02-15 09:41:33 +01:00
Delete global vars, better standarized things
This commit is contained in:
@@ -25,7 +25,7 @@ if (isset($_COOKIE['SessionID'])){
|
||||
|
||||
}
|
||||
|
||||
$lang=getSystemOpt('lang');
|
||||
$lang=getSystemOpt($db_conn,'lang');
|
||||
//Translations
|
||||
require_once __DIR__."/assets/translations/en.php"; //Ever first English, and then your lang (Database)
|
||||
require_once __DIR__."/assets/translations/$lang.php";
|
||||
|
||||
111
cronchk.php
111
cronchk.php
@@ -1,55 +1,74 @@
|
||||
<?php
|
||||
require_once "functions.php";
|
||||
|
||||
$sites = dbw_query($db_conn,"SELECT * FROM CHECKS");
|
||||
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
|
||||
|
||||
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
|
||||
$code = (int)$Param != 0 ? $Param : 200; //Establish the code test want to see
|
||||
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,8 +77,8 @@ $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
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,13 +13,12 @@ require_once 'lib/loadTwig.php';
|
||||
4-In maintenance
|
||||
255-Not results recolected (grey)
|
||||
*/
|
||||
function checkStatus($ID_C){
|
||||
global $db_conn;
|
||||
function checkStatus($db_conn,$ID_C){
|
||||
//First, check if the status is marked to any manual value
|
||||
$manualPage = dbw_query_fetch_array($db_conn,"SELECT ID_C,manStatus FROM CHECKS WHERE ID_C='$ID_C'")['manStatus'];
|
||||
if ($manualPage != NULL){return $manualPage;}
|
||||
//If it's not manual-setted, see real status
|
||||
$result = checkUptime($ID_C);
|
||||
$result = checkUptime($db_conn,$ID_C);
|
||||
//Return status
|
||||
if ($result == 255){return 255;}
|
||||
if ($result > 95 && $result <= 100){return 0;}
|
||||
@@ -28,8 +27,7 @@ function checkStatus($ID_C){
|
||||
if ($result >= 0 && $result <= 10){return 3;} //If test fail much (90% fail)
|
||||
}
|
||||
|
||||
function checkUptime($ID_C,$precision = 0){
|
||||
global $db_conn;
|
||||
function checkUptime($db_conn,$ID_C,$precision = 0){
|
||||
$time = $precision;
|
||||
if ($precision != 0){
|
||||
$good = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND code == 0 AND `timestamp` > $time")[0];
|
||||
@@ -61,10 +59,8 @@ function webRoot(){
|
||||
}
|
||||
|
||||
/** Returns a system option */
|
||||
function getSystemOpt($sysopt){
|
||||
global $db_conn;
|
||||
function getSystemOpt($db_conn,$sysopt){
|
||||
return dbw_query_fetch_array($db_conn,"SELECT * FROM SYS WHERE option = '$sysopt'")['value'];
|
||||
|
||||
}
|
||||
|
||||
/** Return HttpCode of page. Returns false if page is not found */
|
||||
@@ -116,18 +112,15 @@ function requireLogin(){
|
||||
}
|
||||
}
|
||||
|
||||
function nameFromIDC($ID_C){
|
||||
global $db_conn;
|
||||
function nameFromIDC($db_conn,$ID_C){
|
||||
return dbw_query_fetch_array($db_conn,"SELECT name FROM CHECKS WHERE ID_C='$ID_C'")[0];
|
||||
}
|
||||
|
||||
function nameGroupFromIDG($ID_G){
|
||||
global $db_conn;
|
||||
function nameGroupFromIDG($db_conn,$ID_G){
|
||||
return dbw_query_fetch_array($db_conn,"SELECT * FROM GROUPS WHERE ID_G='$ID_G'")['name'];
|
||||
}
|
||||
|
||||
function IDGFromIDC($ID_C){
|
||||
global $db_conn;
|
||||
function IDGFromIDC($db_conn,$ID_C){
|
||||
return dbw_query_fetch_array($db_conn,"SELECT ID_G FROM CHECKS WHERE ID_C='$ID_C'")['ID_G'];
|
||||
}
|
||||
|
||||
@@ -140,8 +133,7 @@ function arrayTypeChk(){
|
||||
'4' => 'DATABASE');
|
||||
}
|
||||
/* This funtion returns the translated text of a type check */
|
||||
function textTypeChk($typeChk){
|
||||
global $T_;
|
||||
function textTypeChk($T_,$typeChk){
|
||||
$arr = array(
|
||||
'1' => $T_['PING_IP'],
|
||||
'2' => $T_['HTTP_CODE'],
|
||||
|
||||
@@ -22,7 +22,7 @@ while ($onechk = dbw_fetch_array($db_conn,$results)){
|
||||
case 4: //MySQL
|
||||
|
||||
if (dbw_query_fetch_array($db_conn, "SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$idchk'")[0] != 0){
|
||||
$chks[$idchk]['status'] = checkStatus($idchk); //Check status of a site
|
||||
$chks[$idchk]['status'] = checkStatus($db_conn,$idchk); //Check status of a site
|
||||
$chks[$idchk]['dateLastChk'] = date('d/m H:i',dbw_query_fetch_array($db_conn, "SELECT `timestamp` FROM CHKHIST WHERE ID_C = '$idchk' ORDER BY `timestamp` DESC")['timestamp']);
|
||||
|
||||
$chks[$idchk]['failedLastChk'] = dbw_query_fetch_array($db_conn, "SELECT `code` FROM CHKHIST WHERE ID_C = '$idchk' ORDER BY `timestamp` DESC LIMIT 0,1")['code'];
|
||||
@@ -33,7 +33,7 @@ while ($onechk = dbw_fetch_array($db_conn,$results)){
|
||||
}
|
||||
|
||||
$ID_TC = $chks[$idchk]['ID_TC'];
|
||||
$chks[$idchk]['nameCheck'] = textTypeChk($ID_TC);
|
||||
$chks[$idchk]['nameCheck'] = textTypeChk($T_,$ID_TC);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
16
panel.php
16
panel.php
@@ -38,7 +38,7 @@ switch($page){
|
||||
$ID_G = $chks[$idchk]['ID_G']; //For nameGroup
|
||||
$chks[$idchk]['nameGroup'] = dbw_query_fetch_array($db_conn,"SELECT * FROM GROUPS WHERE ID_G='$ID_G'")['name'];
|
||||
$ID_TC = $chks[$idchk]['ID_TC'];
|
||||
$chks[$idchk]['nameTCheck'] = textTypeChk($ID_TC);
|
||||
$chks[$idchk]['nameTCheck'] = textTypeChk($T_,$ID_TC);
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ switch($page){
|
||||
foreach(arrayTypeChk() as $key => $value){
|
||||
$tchecks[] = array(
|
||||
'ID_TC' =>$key,
|
||||
'name' => textTypeChk($key)
|
||||
'name' => textTypeChk($T_,$key)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ switch($page){
|
||||
$news[$ID_N]['mdtext'] = $result['text']; //Raw text
|
||||
}
|
||||
|
||||
echo $twig->render('panel/p_news.twig', array('T_' => $T_, 'mpage' => 'news', 'allnews' => $news));
|
||||
echo $twig->render('panel/p_news.twig', array('T_' => $T_, 'you' => $you, 'mpage' => 'news', 'allnews' => $news));
|
||||
break;
|
||||
|
||||
case 'newnews':
|
||||
@@ -216,7 +216,7 @@ switch($page){
|
||||
$sys[$syso['option']] = $syso['value'];
|
||||
}
|
||||
|
||||
echo $twig->render('panel/p_settings.twig', array('T_' => $T_, 'mpage' => 'settings', 'sys' => $sys));
|
||||
echo $twig->render('panel/p_settings.twig', array('T_' => $T_, 'you' => $you, 'mpage' => 'settings', 'sys' => $sys));
|
||||
break;
|
||||
|
||||
case 'users':
|
||||
@@ -280,9 +280,9 @@ switch($page){
|
||||
if ($failssql != false){
|
||||
while ($fail = dbw_fetch_array($db_conn,$failssql)){ //Create array
|
||||
$fails[] = array (
|
||||
'name' => nameFromIDC($fail['ID_C']),
|
||||
'groupName' => nameGroupFromIDG(IDGFromIDC($fail['ID_C'])),
|
||||
'typeCheck' => textTypeChk($fail['ID_TC']),
|
||||
'name' => nameFromIDC($db_conn,$fail['ID_C']),
|
||||
'groupName' => nameGroupFromIDG($db_conn,IDGFromIDC($db_conn,$fail['ID_C'])),
|
||||
'typeCheck' => textTypeChk($T_,$fail['ID_TC']),
|
||||
'errorText' => $fail['errorText'],
|
||||
'date' => date('j/n/Y',$fail['timestamp']),
|
||||
'hour' => date('H:i',$fail['timestamp'])
|
||||
@@ -292,7 +292,7 @@ switch($page){
|
||||
$fails = array(); //Empty
|
||||
}
|
||||
|
||||
echo $twig->render('panel/p_index.twig', array('T_' => $T_, 'mpage' => 'index', 'data' => $data,'fails' => $fails)); //Render
|
||||
echo $twig->render('panel/p_index.twig', array('T_' => $T_, 'you' => $you, 'mpage' => 'index', 'data' => $data,'fails' => $fails)); //Render
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user