1
0
mirror of https://gitlab.com/JKANetwork/CheckServer.git synced 2026-02-14 17:21:35 +01:00

First commit. Check if all is uploaded well.

This commit is contained in:
Kevin Puertas
2017-10-30 22:59:39 +01:00
parent f88dceff1b
commit 6b38aa6cf8
247 changed files with 27230 additions and 0 deletions

151
functions.php Executable file
View File

@@ -0,0 +1,151 @@
<?php
/* First, db connection */
require_once "connect.php";
require_once 'lib/loadTwig.php';
/** Function checkStatus checks last "Checks" and their return
Return can be:
A value from 0-1-2 for status. 3 for Out Of Service. 3 and 4 can be turned up manually (
0-Perfect
1-Little problems
2-Problems
3-Out of service
4-In maintenance
255-Not results recolected (grey)
*/
function checkStatus($ID_C){
global $db_conn;
//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);
//Return status
if ($result == 255){return 255;}
if ($result > 95 && $result <= 100){return 0;}
if ($result > 60 && $result <= 95){return 1;}
if ($result > 10 && $result <= 60){return 2;}
if ($result >= 0 && $result <= 10){return 3;} //If test fail much (90% fail)
}
function checkUptime($ID_C,$precision = 0){
global $db_conn;
$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];
$all = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND `timestamp` > $time")[0];
}else{ //Uptime of all time
$good = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND code == 0")[0];
$all = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C'")[0];
}
if ($all != 0){
$uptime = ($good/$all)*100;
return $uptime;
}else{
return 100;
}
}
/** Ping a WebSite */
function ping($host,$port = '80') {
$fP = fSockOpen($host, $port, $errno, $errstr, 3); //Host,port,Err,ErrString,Timeout(Sec)
//echo "Error:$errno";
return $fP ? "0" : "1";
}
/** Returns web root (ie. http://jkanetwork.com) */
function webRoot(){
$root = $_SERVER['HTTPS'] ? "https://" : "http://"; //Variable = condicion ? Verdadero : Falso
return $root . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
}
/** Returns a system option */
function getSystemOpt($sysopt){
global $db_conn;
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 */
function httpCode($url, $wait = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, $wait); //timeout in seconds
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $head ? $httpCode : FALSE;
}
/** Return a String of $lenght chars */
function RandomString($length)
{
//https://phpes.wordpress.com/2007/06/12/generador-de-una-cadena-aleatoria/
$source = 'abcdefghijklmnopqrstuvwxyz';
$source .= '1234567890';
if($length>0){
$rstr = "";
$source = str_split($source,1);
for($i=1; $i<=$length; $i++){
$num = mt_rand(1,count($source));
$rstr .= $source[$num-1];
}
}
return $rstr;
}
/** 1 if its logued, 0 if not */
function isLogued(){
if (isset($_COOKIE['SessionID'])){
return 1;
}else{
return 0;
}
}
function requireLogin(){
if (isLogued() == 0){
die('You dont have admin rights'); //This blocks edit or create nothing if its not logued
}
}
function nameFromIDC($ID_C){
global $db_conn;
return dbw_query_fetch_array($db_conn,"SELECT name FROM CHECKS WHERE ID_C='$ID_C'")[0];
}
function nameGroupFromIDG($ID_G){
global $db_conn;
return dbw_query_fetch_array($db_conn,"SELECT * FROM GROUPS WHERE ID_G='$ID_G'")['name'];
}
function IDGFromIDC($ID_C){
global $db_conn;
return dbw_query_fetch_array($db_conn,"SELECT ID_G FROM CHECKS WHERE ID_C='$ID_C'")['ID_G'];
}
/* This funtion returns the array with type of checks */
function arrayTypeChk(){
return array(
'1' => 'PING_IP',
'2' => 'HTTP_CODE',
'3' => 'VISIT_COUNT',
'4' => 'DATABASE');
}
/* This funtion returns the translated text of a type check */
function textTypeChk($typeChk){
global $T_;
$arr = array(
'1' => $T_['PING_IP'],
'2' => $T_['HTTP_CODE'],
'3' => $T_['VISIT_COUNT'],
'4' => $T_['DATABASE']);
return $arr[$typeChk];
}