0) { $uptimeString .= $days; $uptimeString .= (($days == 1) ? " Dia" : " Dias"); } if ($hours > 0) { $uptimeString .= (($days > 0) ? " " : "") . $hours; $uptimeString .= (($hours == 1) ? " Hora" : " Horas"); } if ($mins > 0) { $uptimeString .= (($days > 0 || $hours > 0) ? " " : "") . $mins; $uptimeString .= (($mins == 1) ? " Minuto" : " Minutos"); } if ($secs > 0) { $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? " " : "") . $secs; $uptimeString .= (($secs == 1) ? " Segundo" : " Segundos"); } return $uptimeString; } function lowermemory($Wertq) { if ($Wertq > 1073741824) { $Wertq = number_format($Wertq / 1073741824, 2, ".", ",") . " Tb"; } elseif ($Wertq > 1048576) { $Wertq = number_format($Wertq / 1048576, 2, ".", ",") . " Gb"; } elseif ($Wertq > 1024) { $Wertq = number_format($Wertq / 1024, 2, ".", ",") . " Mb"; } else { $Wertq = number_format($Wertq, 2, ".", ",") . " Kb"; } return $Wertq; } function getStat($_statPath) { if (trim($_statPath) == '') { $_statPath = '/proc/stat'; } ob_start(); passthru('cat ' . $_statPath); $stat = ob_get_contents(); ob_end_clean(); if (substr($stat, 0, 3) == 'cpu') { $parts = explode(" ", preg_replace("!cpu +!", "", $stat)); } else { return false; } $return = array(); $return['user'] = $parts[0]; $return['nice'] = $parts[1]; $return['system'] = $parts[2]; $return['idle'] = $parts[3]; return $return; } function getCpuUsage($_statPath = '/proc/stat') { $time1 = getStat($_statPath) or die("getCpuUsage(): couldn't access STAT path or STAT file invalid\n"); sleep(1); $time2 = getStat($_statPath) or die("getCpuUsage(): couldn't access STAT path or STAT file invalid\n"); $delta = array(); foreach ($time1 as $k => $v) { $delta[$k] = $time2[$k] - $v; } $deltaTotal = array_sum($delta); $percentages = array(); foreach ($delta as $k => $v) { $percentages[$k] = round($v / $deltaTotal * 100, 2); } return $percentages; } function ZahlenFormatieren($Wert) { if ($Wert > 1099511627776) { $Wert = number_format($Wert / 1099511627776, 2, ".", ",") . " TB"; } elseif ($Wert > 1073741824) { $Wert = number_format($Wert / 1073741824, 2, ".", ",") . " GB"; } elseif ($Wert > 1048576) { $Wert = number_format($Wert / 1048576, 2, ".", ",") . " MB"; } elseif ($Wert > 1024) { $Wert = number_format($Wert / 1024, 2, ".", ",") . " kB"; } else { $Wert = number_format($Wert, 2, ".", ",") . " Bytes"; } return $Wert; } function checkos() { if (substr(PHP_OS, 0, 3) == "WIN") { $osType = winosname(); $osbuild = php_uname('v'); $os = "windows"; } elseif (PHP_OS == "FreeBSD") { $os = "nocpu"; $osType = "FreeBSD"; $osbuild = php_uname('r'); } elseif (PHP_OS == "Darwin") { $os = "nocpu"; $osType = "Apple OS X"; $osbuild = php_uname('r'); } elseif (PHP_OS == "Linux") { $os = "linux"; $osType = "Linux"; $osbuild = php_uname('r'); } else { $os = "nocpu"; $osType = "Unknown OS"; $osbuild = php_uname('r'); } return $osType; } function winosname() { $wUnameB = php_uname("v"); $wUnameBM = php_uname("r"); $wUnameB = eregi_replace("build ", "", $wUnameB); if ($wUnameBM == "5.0" && ($wUnameB == "2195")) { $wVer = "Windows 2000"; } if ($wUnameBM == "5.1" && ($wUnameB == "2600")) { $wVer = "Windows XP"; } if ($wUnameBM == "5.2" && ($wUnameB == "3790")) { $wVer = "Windows Server 2003"; } if ($wUnameBM == "6.0" && (php_uname("v") == "build 6000")) { $wVer = "Windows Vista"; } if ($wUnameBM == "6.0" && (php_uname("v") == "build 6001")) { $wVer = "Windows Vista SP1"; } return $wVer; } if (PHP_OS == "WINNT") { $os = "windows"; $osbuild = php_uname('v'); } elseif (PHP_OS == "Linux") { $os = "linux"; $osbuild = php_uname('r'); } else { $os = "nocpu"; $osbuild = php_uname('r'); } while (true) { $envia_email = false; $email = ''; $email = $email . ''; $title = "Monitor do Sistema"; $portcheck = ".1"; $copyright = ""; $servername = "localhost"; $customos = ""; $name[1] = "Servidor Web"; $port[1] = "80"; $name[2] = "SSL Web"; $port[2] = "443"; $name[3] = "MySQL-Server"; $port[3] = "3306"; $name[4] = "FTP-Server"; $port[4] = "21"; $name[5] = "Asterisk"; $port[5] = "5038"; $name[6] = "PostGreSQL"; $port[6] = "5432"; $name[7] = "SSH"; $port[7] = "22"; $arr = array( array($name[1], $port[1]), array($name[2], $port[2]), array($name[3], $port[3]), array($name[4], $port[4]), array($name[5], $port[5]), array($name[6], $port[6]), array($name[7], $port[7]), ); { } if ($servername == "") { $theservername = $_SERVER['SERVER_NAME']; } else { $theservername = $servername; } if ($customos == "") { $osname = checkos(); } else { $os = "nocpu"; $osname = $customos; } if (php_sapi_name() == "apache2handler") { $httpapp = "Apache"; } else { $httpapp = php_sapi_name(); } if (PHP_OS == "WINNT") { $os = "windows"; $osbuild = php_uname('v'); } elseif (PHP_OS == "Linux") { $os = "linux"; $osbuild = php_uname('r'); } else { $os = "nocpu"; $osbuild = php_uname('r'); } connect_db(); $query = "SELECT * FROM pbx_monitora_sistema where nome_servico is not null and tipo_monitor='S' order by id desc"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { //VERIFICA SERVICOS DO SISTEMA $query2 = "SELECT * FROM pbx_servicos_sistema order by id asc"; $result2 = pg_query($query2); while ($dados2 = pg_fetch_array($result2)) { if (@fsockopen('127.0.0.1', $dados2['porta'], $errno, $errstr, $portcheck)) { } else { if (trim($dados2['servico']) == trim($dados['nome_servico'])) { $email = $email . ' '; $envia_email = true; } } flush(); } } pg_close(); $frei = disk_free_space("./"); $insgesamt = disk_total_space("./"); $belegt = $insgesamt - $frei; $prozent_belegt = 100 * $belegt / $insgesamt; //VERIFICACAO DE DISCOS $espacao_em_disco = ZahlenFormatieren($insgesamt); $espacao_em_uso = ZahlenFormatieren($belegt); $por_usada = round($prozent_belegt, "2"); $espaco_livre = ZahlenFormatieren($frei); connect_db(); $query = "SELECT * FROM pbx_monitora_sistema where nome_servico is not null and tipo_monitor='D' order by id desc"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $limite_disco = $dados['avisar_ao_chegar']; } pg_close(); if ($espaco_livre < $limite_disco) { $envia_email = true; $email = $email . ' '; } { if ($os == "windows") { $wmi = new COM("Winmgmts://"); $cpus = $wmi->execquery("SELECT * FROM Win32_Processor"); foreach ($cpus as $cpu) { $cpu->loadpercentage; } $por_cpu = round($cpu->loadpercentage, "2"); } if ($os == "linux") { $cpu = getCpuUsage(); //MONITOR CPU $cpulast = 100 - $cpu['idle']; $uso_cpu = round($cpulast, "0"); $por_cpu = round($cpulast, "2"); connect_db(); $query = "SELECT * FROM pbx_monitora_sistema where tipo_monitor='C' order by id desc"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $limite_cpu = $dados['avisar_ao_chegar']; } pg_close(); if ($por_cpu > $limite_cpu) { $envia_email = true; $email = $email . ' '; } } elseif ($os == "nocpu") { echo ""; } else { // } } { if ($os == "windows") { // } elseif ($os == "linux") { $mem = file_get_contents("/proc/meminfo"); if (preg_match('/MemTotal\:\s+(\d+) kB/', $mem, $matches)) { $insgesamtq = $matches[1]; } if (preg_match('/Buffers\:\s+(\d+) kB/', $mem, $matches)) { $buffertq = $matches[1]; } if (preg_match('/Cached\:\s+(\d+) kB/', $mem, $matches)) { $cachedamtq = $matches[1]; } unset($matches); if (preg_match('/MemFree\:\s+(\d+) kB/', $mem, $matches)) { $freiq = $matches[1]; } //MONITOR MEMÓRIA $Used = $insgesamtq - $freiq - $cachedamtq - $buffertq; $prozent_belegtq = 100 * $Used / $insgesamtq; $memoria_uso = lowermemory($Used); $memoria_total = lowermemory($insgesamtq); $por_total = round($prozent_belegtq, "2"); connect_db(); $query = "SELECT * FROM pbx_monitora_sistema where tipo_monitor='M' order by id desc"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $limite_mem = $dados['avisar_ao_chegar']; } pg_close(); if ($por_total > $limite_mem) { $envia_email = true; $email = $email . ' '; } } elseif ($os == "nocpu") { echo ""; } else { } } { if ($os == "windows") { $pagefile = 'c:\pagefile.sys'; function ifif($value, $true, $false) { if ($value == 0) { return $false; } else { return $true; } } $upsince = filemtime($pagefile); $gettime = (time() - filemtime($pagefile)); $days = floor($gettime / (24 * 3600)); $gettime = $gettime - ($days * (24 * 3600)); $hours = floor($gettime / (3600)); $gettime = $gettime - ($hours * (3600)); $minutes = floor($gettime / (60)); $gettime = $gettime - ($minutes * 60); $seconds = $gettime; $days = ifif($days != 1, $days . ' Dias', $days . ' Dia'); $hours = ifif($hours != 1, $hours . ' Horas', $hours . ' Hora'); $minutes = ifif($minutes != 1, $minutes . ' Minutos', $minutes . ' Minuto'); $seconds = ifif($seconds != 1, $seconds . ' Segundos', $seconds . ' Segundo'); if ($days == 0) { $days = ""; } if ($hours == 0) { $hours = ""; } if ($minutes == 0) { $minutes = ""; } if ($seconds == 0) { $seconds = ""; } //MONITOR TEMPO $tempo_operacao = $days . ' ' . $hours . ' ' . $minutes . ' ' . $seconds; $tempo_ligado = date('F jS, Y. h:i A', $upsince); } elseif ($os == "linux") { $uptime = exec("cat /proc/uptime"); $uptime = split(" ", $uptime); $uptimeSecs = $uptime[0]; $staticUptime = format_uptime($uptimeSecs); //$tempo_operacao = $days . ' ' . $hours . ' ' . $minutes . ' ' . $seconds; $email = $email . '
AVISOS DO SISTEMA
FORAM ENCONTRADAS AS SEGUINTES ANOMALIAS
O SERVIÇO:' . $dados2['servico'] . ' ESTÁ DESLIGADO!
O ESPAÇO USADO EM DISCO É DE :' . $por_usada . '% E O LIMITE É DE :' . $limite_disco . '%
O CPU ESTÁ OPERANDO Á ' . $por_cpu . '% E O LIMITE É DE :' . $limite_cpu . '%
A MEMÓRIA ESTÁ OPERANDO Á ' . $por_total . '% E O LIMITE É DE :' . $limite_mem . '%
O SERVIDOR ESTÁ LIGADO Á ' . $staticUptime; '%
'; $tempo_ligado = $staticUptime; } else { // } } flush(); if ($envia_email == true) { connect_db(); $query = "SELECT * FROM pbx_monitora_sistema where enviar_para is not null order by id desc limit 1"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $galera = $dados['enviar_para']; } pg_close(); require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); // mandar via SMTP connect_db(); $query = "SELECT * FROM pbx_conf_monitor"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $servidor_email = $dados['servidor_email']; $email_principal = $dados['email_principal']; $senha_principal = $dados['senha_principal']; $de_email = $dados['de_email']; $de_nome = $dados['de_nome']; $usuarios = $dados['usuarios']; $cabecalho = $dados['cabecalho']; $recebedores = $dados['recebedores']; } $query = "SELECT * FROM pbx_confMail where id='$servidor_email'"; $result = pg_query($query); while ($dados = pg_fetch_array($result)) { $senha = $dados['senha']; $usuario = $dados['usuarios']; $dominio = $dados['dominio']; } pg_close(); $mail->Host = $dominio; // Seu servidor smtp $mail->SMTPAuth = true; // smtp autenticado $mail->Username = $usuario; // usuário deste servidor smtp $mail->Password = $senha; // senha $mail->From = $de_email; $mail->FromName = $de_nome; $var_recpt = explode(';', $recebedores); for ($i = 0; count($var_recpt) > $i; $i++) { $mail->AddAddress($var_recpt[$i]); } $mail->IsHTML(true); // send as HTML $mail->Subject = $cabecalho; $mail->Body = $email; $mail->Send(); } sleep(60); } ?>