1
0
mirror of https://gitlab.com/JKANetwork/CheckServer.git synced 2026-02-14 01:01:33 +01:00
Files
CheckServer/functions.php
2018-01-15 12:47:23 +01:00

181 lines
4.4 KiB
PHP
Executable File

<?php
//Functions
function getUptime($ID_C,$precision = 0){
$db_conn = getconn();
if ($precision == 0){
$time = time()-30*7*3600; //Last week
}else{
$time = $precision;
}
$res = dbw_query($db_conn,"SELECT code FROM CHKHIST WHERE ID_C='$ID_C' AND `timestamp` > $time");
$count = 0;
$err = 0;
while ($fila = dbw_fetch_array($db_conn,$res)){
$count++;
$err += $fila['code'];
}
if ($count == 0){
return '-1'; //No registers
}else{
if ($err != 0){
$uptime = (100 - round(($err/$count)*100,2));
return $uptime;
}else{
return 100;
}
}
}
function getStatus($ID_C){
$T_ = loadLang();
$db_conn = getconn();
$resql = dbw_query($db_conn,"SELECT code FROM CHKHIST WHERE ID_C='$ID_C' ORDER BY `timestamp` DESC LIMIT 5");
$err = 0;
while ($x = dbw_fetch_array($db_conn,$resql)){
if ($x['code'] != 0){
$err++;
}
}
switch ($err){
case 0:
case 1:
return array($T_['status_right'],'green');
break;
case 2:
return array($T_['status_lproblems'],'blue');
break;
case 3:
case 4:
return array($T_['status_problems'],'orange');
break;
default: // > 4
return array($T_['status_outofserv'],'red');
break;
}
}
/** 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 = isset($_SERVER['HTTPS']) ? "https://" : "http://"; //Variable = condicion ? Verdadero : Falso
return $root . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
}
/** Returns a system option */
function getSystemOpt($sysopt){
return dbw_query_fetch_array(getconn(),"SELECT * FROM SYS WHERE option = '$sysopt'")['value'];
}
/** Return HttpCode of page. Returns false if page is not found (Used in cron) */
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($_SESSION['UserID'])){
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){
return dbw_query_fetch_array(getconn(),"SELECT name FROM CHECKS WHERE ID_C='$ID_C'")[0];
}
function nameGroupFromIDG($ID_G){
return dbw_query_fetch_array(getconn(),"SELECT ID_G,name FROM GROUPS WHERE ID_G='$ID_G'")['name'];
}
function IDGFromIDC($ID_C){
return dbw_query_fetch_array(getconn(),"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',
5 => 'JSON_API');
}
/* This funtion returns the translated text of a type check */
function textTypeChk($typeChk){
$T_ = loadLang();
$arr = array(
1 => $T_['PING_IP'],
2 => $T_['HTTP_CODE'],
3 => $T_['VISIT_COUNT'],
4 => $T_['DATABASE'],
5 => $T_['JSON_API']
);
return $arr[$typeChk];
}
//This function is an alias to Twig render, with "standard args" added
function renderPage($page,$array = array()){
$T_ = loadLang(); //Load transactions
//Default params to send
$allarray = array(
'version' => VERSION,
'T_' => $T_,
);
foreach ($array as $key => $value) {
$allarray[$key] = $value;
}
require 'lib/loadTwig.php';
echo $twig->render($page, $allarray);
}
function loadLang(){
//Translations
require __DIR__."/assets/translations/en.php"; //Ever first English, and then your lang (Database)
require __DIR__."/assets/translations/".LANG.".php";
return $T_;
}