function checkProcess1(){ $unix=new unix(); $pid=$unix->PIDOF_PATTERN("bin/process1"); if($pid<5){return null;} $process1=$unix->PROCCESS_TIME_MIN($pid); $mem=$unix->PROCESS_MEMORY($pid); Myevents("process1: $pid ($process1 mn) memory:$mem Mb",__FUNCTION__); if($mem>30){ @copy("/var/log/artica-postfix/process1.debug","/var/log/artica-postfix/process1.killed".time().".debug"); system("/bin/kill -9 $pid"); $unix->send_email_events( "artica process1 (process1) Killed", "Process1 use too much memory $mem MB","watchdog"); } if($process1>2){ @copy("/var/log/artica-postfix/process1.debug","/var/log/artica-postfix/process1.killed".time().".debug"); system("/bin/kill -9 $pid"); $unix->send_email_events( "artica process1 (process1) Killed", "Process1 run since $process1 Pid: $pid and exceed 2 minutes live","watchdog"); } }
function php_cgi_array() { $unix = new unix(); $pgrep = $unix->find_program("pgrep"); $php_cgi = $unix->find_program("php-cgi"); $cmd = "{$pgrep} -l -f \"{$php_cgi}\" 2>&1"; exec("{$pgrep} -l -f \"{$php_cgi}\" 2>&1", $results); writelogs_framework("{$cmd} ->" . count($results) . " line", __FUNCTION__, __FILE__, __LINE__); while (list($num, $ligne) = each($results)) { if (preg_match("#\\/pgrep#", $ligne)) { continue; } if (preg_match("#([0-9]+)\\s+#", $ligne, $re)) { $pid = $re[1]; $PPID = $unix->PPID_OF($pid); $rss0 = $unix->PROCESS_MEMORY($pid, true); $vm0 = $unix->PROCESS_CACHE_MEMORY($pid, true); $TTL = $unix->PROCESS_TTL_TEXT($pid); $PPID2 = $unix->PPID_OF($PPID); if ($PPID2 > 0) { if ($PPID2 != $pid) { $PPID = $PPID2; } } $ARRAY[$PPID][$pid]["RSS"] = $unix->PROCESS_MEMORY($pid, true); $ARRAY[$PPID][$pid]["VM"] = $unix->PROCESS_CACHE_MEMORY($pid, true); $ARRAY[$PPID][$pid]["TTL"] = $unix->PROCESS_TTL_TEXT($pid, true); } } echo "<articadatascgi>" . base64_encode(serialize($ARRAY)) . "</articadatascgi>"; }
function GetMemoriesOf($pid) { $unix = new unix(); $rss = $unix->PROCESS_MEMORY($pid, true); $vm = $unix->PROCESS_CACHE_MEMORY($pid, true); exec("pgrep -P {$pid}", $results); $count = 0; while (list($num, $ligne) = each($results)) { $ligne = trim($ligne); if ($ligne < 1) { continue; } $count = $count + 1; $rss = $rss + $unix->PROCESS_MEMORY($ligne, true); $vm = $vm + $unix->PROCESS_CACHE_MEMORY($ligne, true); } if ($count == 0) { $count = 1; } $l[] = "master_pid={$pid}"; $l[] = "master_memory={$rss}"; //$l[]="master_cached_memory=$vm"; $l[] = "processes_number={$count}"; return implode("\n", $l); }
function instance_memory() { $instance_id = $_GET["instance_id"]; $pidfile = "/var/run/mysqld/mysqld{$instance_id}.pid"; $unix = new unix(); $pid = multi_get_pid($instance_id); writelogs_framework("{$pidfile} -> {$pid}", __FUNCTION__, __FILE__, __LINE__); if ($unix->process_exists($pid)) { $rss = $unix->PROCESS_MEMORY($pid, true); $vm = $unix->PROCESS_CACHE_MEMORY($pid, true); } echo "<articadatascgi>" . base64_encode(serialize(array($rss, $vm))) . "</articadatascgi>"; }
function make_execute() { $unix = new unix(); $array = array(); exec('pgrep -l -f "make\\s+" 2>&1', $results); while (list($path, $line) = each($results)) { if (preg_match("#^([0-9]+)\\s+(.+?)#", $line, $re)) { if (preg_match("#pgrep#", $re[2])) { continue; } if (strlen($re[2]) < 5) { continue; } if ($unix->process_exists($re[1])) { $mem = $unix->PROCESS_MEMORY($re[1]); if ($mem > 1) { $array[] = "PID: {$re[1]} " . FormatBytes($mem) . "<br>{$re[2]}"; } } } } if (count($array) == 0) { return; } $package = @implode(" ", $array); //$link="<center><a href='#' OnClick=javascript:Loadjs('squid.newbee.php?yes'); style='font-weight:bolder;text-decoration:underline;font-size:11px'>{APP_SQUID}</a></center>"; $tpl = new templates(); $title = $tpl->_ENGINE_parse_body("{COMPILING_SOFTWARES}"); $html[] = "\$.jGrowl(\""; $html[] = "<table>"; $html[] = "<tr>"; $html[] = "<td width=1% valign='top'>"; $html[] = "<img src=img/software-task-48.png>"; $html[] = "</td>"; $html[] = "<td valign=top>"; $html[] = "<span style=color:red;font-size:16px>{$title}"; $html[] = "</span><hr>"; $html[] = $tpl->_ENGINE_parse_body("<span style=font-size:13px>{COMPILING_SOFTWARES_TEXT}</span><p style=font-size:14px;color:red>{$package}</p>", "samba.index.php"); $html[] = "\","; $html[] = "{header: '{$title}',life:25000});"; echo implode("", $html) . "\n"; }
function mod_status($servername = null) { $unix = new unix(); if ($servername == "_default_") { return; } $unix->hostname_g(); $hostname = $unix->hostname_g(); $hosnenc = md5($unix->hostname_g()); $conf[] = "# Status from " . $unix->hostname_g(); $conf[] = "<Location >"; $curl = $unix->find_program("curl"); $TEMP_FILE = $unix->FILE_TEMP(); $cmdline = "{$curl} --interface 127.0.0.1 http://localhost/{$hosnenc}/{$hosnenc}-status >{$TEMP_FILE}"; if ($GLOBALS["VERBOSE"]) { echo $cmdline . "\n"; } shell_exec($cmdline); $q = new mysql(); $pid = array(); $access = null; $total_traffic = null; $total_traffic_unit = null; $traffic_sec = 0; $traffic_request = 0; $request_s = 0; $UPTIME = null; $total_mem = 0; $datas = explode("\n", @file_get_contents($TEMP_FILE)); @unlink($TEMP_FILE); while (list($num, $ligne) = each($datas)) { if ($GLOBALS["VERBOSE"]) { echo "Parsing line...`{$ligne}`\n"; } if (preg_match("#<dt>Server uptime:\\s+(.+?)</dt>#", $ligne, $re)) { $UPTIME = trim($re[1]); if ($GLOBALS["VERBOSE"]) { echo "*************\nUPTIME = {$UPTIME}\n"; } continue; } if (preg_match("#Total accesses:\\s+([0-9]+)\\s+-\\s+Total Traffic:\\s+([0-9\\.]+)\\s+([a-zA-Z]+)#i", $ligne, $re)) { $access = $re[1]; $total_traffic = $re[2]; $total_traffic = UnitToBytes($total_traffic, strtoupper($re[3])); if ($GLOBALS["VERBOSE"]) { echo "*************\n{$access} - {$total_traffic}\n"; } continue; } if (preg_match("#([0-9\\.]+)\\s+requests\\/sec\\s+-\\s+([0-9\\.]+)\\s+(.+)\\/second\\s+-\\s+([0-9\\.]+)\\s+(.+?)\\/request#", $ligne, $re)) { $request_s = $re[1]; if (substr($request_s, 0, 1) == ".") { $request_s = "0{$request_s}"; } $traffic_sec = UnitToBytes($re[2], $re[3]); $traffic_request = UnitToBytes($re[4], $re[5]); continue; } if (preg_match("#<td><b>[0-9]+-[0-9]+</b></td><td>([0-9]+)</td><td>#", $ligne, $re)) { $pid[$re[1]] = $re[1]; } } if (count($pid) > 0) { while (list($num, $ligne) = each($pid)) { $total = $unix->PROCESS_MEMORY($num, true); if ($GLOBALS["VERBOSE"]) { echo "Current PID: {$num} {$total} KB\n"; } $total_mem = $total_mem + $total; } } if ($GLOBALS["VERBOSE"]) { echo "\n\n ***************************************************************************************************************"; echo "Access: {$access} total-traffic:{$total_traffic} bytes UPTIME={$UPTIME} Total memory used: {$total_mem} Bytes\n"; echo "Access: requests/seconds: {$request_s} traffic/sec:{$traffic_sec} trafic per request:{$traffic_request} bytes:\n"; echo "\n\n ***************************************************************************************************************"; } if (!is_numeric($total_traffic)) { if ($GLOBALS["VERBOSE"]) { echo "No traffic return null\n"; } return; } $UPTIME = str_replace("</td>", "", $UPTIME); $UPTIME = str_replace("</dt>", "", $UPTIME); if ($GLOBALS["VERBOSE"]) { echo "MEMORY: " . FormatBytes($total_mem) . "\n"; } $HASH["total_traffic"] = $total_traffic; $HASH["total_mem"] = $total_mem * 1024; $HASH["requests_second"] = $request_s; $HASH["traffic_second"] = $traffic_sec; $HASH["traffic_request"] = $traffic_request; $HASH["UPTIME"] = $UPTIME; $table_name = "apache_stats_" . date('Ym'); $q = new mysql(); $sql = "SELECT AVG(total_traffic) as total_traffic,\n\t\t\tAVG(total_memory) as total_memory,\n\t\t\tAVG(requests_second) as requests_second,\n\t\t\tAVG(traffic_second) as traffic_second,\n\t\t\tAVG(traffic_request) as traffic_request FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["AVG"]["total_traffic"] = $ligne["total_traffic"]; $HASH["AVG"]["total_memory"] = $ligne["total_memory"]; $HASH["AVG"]["requests_second"] = $ligne["requests_second"]; $HASH["AVG"]["traffic_second"] = $ligne["traffic_second"]; $HASH["AVG"]["traffic_request"] = $ligne["traffic_request"]; $sql = "SELECT MAX(total_traffic) as total_traffic FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["MAX"]["total_traffic"] = $ligne["total_traffic"]; $sql = "SELECT MAX(total_memory) as total_memory FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["MAX"]["total_memory"] = $ligne["total_memory"]; $sql = "SELECT MAX(requests_second) as requests_second FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["MAX"]["requests_second"] = $ligne["requests_second"]; $sql = "SELECT MAX(traffic_second) as traffic_second FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["MAX"]["traffic_second"] = $ligne["traffic_second"]; $sql = "SELECT MAX(traffic_request) as traffic_request FROM `{$table_name}`"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_events")); $HASH["MAX"]["traffic_request"] = $ligne["traffic_request"]; if ($GLOBALS["VERBOSE"]) { print_r($HASH); } @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/APACHE_HASH", serialize($HASH)); @chmod("/usr/share/artica-postfix/ressources/logs/web/APACHE_HASH", 0755); return "('{$hostname}','{$total_traffic}','{$total_mem}','{$request_s}','{$traffic_sec}','{$traffic_request}','{$UPTIME}')"; }
function clamd() { $bin_path = $GLOBALS["CLASS_UNIX"]->find_program("clamd"); $EnableClamavDaemon = $GLOBALS["CLASS_SOCKETS"]->GET_INFO("EnableClamavDaemon"); if ($bin_path == null) { return null; } $pid_path = GetVersionOf("clamd-pid"); $master_pid = trim(@file_get_contents($pid_path)); if ($EnableClamavDaemon == null) { $EnableClamavDaemon = 1; } if (is_file("/etc/artica-postfix/KASPERSKY_WEB_APPLIANCE")) { $EnableClamavDaemon = 0; } if (!$GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) { $master_pid = $GLOBALS["CLASS_UNIX"]->PIDOF($bin_path); } $l[] = "[CLAMAV]"; $l[] = "service_name=APP_CLAMAV"; $l[] = "master_version=" . GetVersionOf("clamav"); $l[] = "service_cmd=clamd"; $l[] = "service_disabled={$EnableClamavDaemon}"; $l[] = "pid_path={$pid_path}"; $l[] = "binpath={$bin_path}"; $l[] = "family=system"; $l[] = "watchdog_features=1"; $l[] = ""; if ($EnableClamavDaemon == 0) { if ($GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) { $GLOBALS["CLASS_UNIX"]->THREAD_COMMAND_SET("/etc/init.artica-postfix stop clamd"); } } if ($EnableClamavDaemon == 0) { $l[] = ""; return implode("\n", $l); return; } if (!$GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) { WATCHDOG("APP_CLAMAV", "clamd"); $l[] = ""; return implode("\n", $l); return; } $l[] = GetMemoriesOf($master_pid); $l[] = ""; $unix = new unix(); $timeFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; $timeTimeFile = $unix->file_time_min($timeFile); if ($timeTimeFile > 5) { @unlink($timeFile); @file_put_contents($timeFile, time()); $ClamavRefreshDaemonTime = $GLOBALS["CLASS_SOCKETS"]->GET_INFO("ClamavRefreshDaemonTime"); $ClamavRefreshDaemonMemory = $GLOBALS["CLASS_SOCKETS"]->GET_INFO("ClamavRefreshDaemonMemory"); if (!is_numeric($ClamavRefreshDaemonMemory)) { $ClamavRefreshDaemonMemory = 350; } if (!is_numeric($ClamavRefreshDaemonTime)) { $ClamavRefreshDaemonTime = 60; } $ClamavRefreshDaemonTime = $ClamavRefreshDaemonTime - 1; $rss = $unix->PROCESS_MEMORY($master_pid, false); $vm = $unix->PROCESS_CACHE_MEMORY($master_pid, false); $time = time(); if (!is_dir("/var/log/artica-postfix/clamd-mem")) { @mkdir("/var/log/artica-postfix/clamd-mem", 755, true); } $sql = "('" . date('Y-m-d H:i:s') . "','{$rss}','{$vm}')"; @file_put_contents("/var/log/artica-postfix/clamd-mem/{$time}", $sql); if ($ClamavRefreshDaemonMemory > 0) { if ($rss > $ClamavRefreshDaemonMemory) { $unix->send_email_events("Reboot ClamAV Antivirus Daemon", " ClamAV Antivirus Daemon memory {$rss}MB exceed {$ClamavRefreshDaemonMemory}MB", "system"); $cmd = trim("{$GLOBALS["nohup"]} /etc/init.d/artica-postfix restart clamd >/dev/null 2>&1 &"); shell_exec($cmd); } } $time = $unix->PROCCESS_TIME_MIN($master_pid); if ($time > $ClamavRefreshDaemonTime) { $unix->send_email_events("Reboot ClamAV Antivirus Daemon", " ClamAV Antivirus Daemon TTL {$time} minutes exceed {$ClamavRefreshDaemonTime} minutes", "system"); events("Reboot clamd daemon"); $cmd = trim("{$GLOBALS["nohup"]} /etc/init.d/artica-postfix restart clamd >/dev/null 2>&1 &"); shell_exec($cmd); } } return implode("\n", $l); return; }
function mod_status($servername) { $servername = trim($servername); if ($servername == "_default_") { return; } $freeweb = new freeweb($servername); $dir_www = $freeweb->WORKING_DIRECTORY; $unix = new unix(); $q = new mysql(); $pid = array(); $dirMD = md5($servername); if ($GLOBALS["VERBOSE"]) { echo "Testing {$dir_www}/.htaccess\n"; } if (is_file("{$dir_www}/.htaccess")) { if ($GLOBALS["VERBOSE"]) { echo "mod_status_htaccess({$dir_www}/.htaccess,{$dirMD})\n"; } mod_status_htaccess("{$dir_www}/.htaccess", $dirMD); } $curl = new ccurl("http://{$servername}/{$dirMD}/{$dirMD}-status", true); $access = null; $total_traffic = null; $total_traffic_unit = null; $traffic_sec = 0; $traffic_request = 0; $request_s = 0; $UPTIME = null; $total_mem = 0; $datas = $curl->GetFile("/tmp/{$servername}.html"); $datas = explode("\n", @file_get_contents("/tmp/{$servername}.html")); while (list($num, $ligne) = each($datas)) { if (preg_match("#Server uptime:\\s+(.+)#", $ligne, $re)) { $UPTIME = trim($re[1]); continue; } if (preg_match("#Total accesses:\\s+([0-9]+)\\s+-\\s+Total Traffic:\\s+([0-9]+)\\s+([a-zA-Z]+)#", $ligne, $re)) { $access = $re[1]; $total_traffic = $re[2]; $total_traffic_unit = strtoupper($re[3]); if ($total_traffic_unit == "KB") { $total_traffic = $total_traffic * 1024; } if ($total_traffic_unit == "MB") { $total_traffic = $total_traffic * 1024000; } if ($total_traffic_unit == "GB") { $total_traffic = $total_traffic * 1024000000; } if ($total_traffic_unit == "TB") { $total_traffic = $total_traffic * 10240000000000; } continue; } if (preg_match("#([0-9\\.]+)\\s+requests\\/sec\\s+-\\s+([0-9]+)\\s+(.+)\\/second\\s+-\\s+([0-9]+)\\s+(.+?)\\/request#", $ligne, $re)) { $request_s = $re[1]; if (substr($request_s, 0, 1) == ".") { $request_s = "0{$request_s}"; } $traffic_sec = $re[2]; $traffic_sec_unit = strtoupper($re[3]); if ($traffic_sec_unit == "KB") { $traffic_sec = $traffic_sec * 1024; } if ($traffic_sec_unit == "MB") { $traffic_sec = $traffic_sec * 1024000; } if ($traffic_sec_unit == "GB") { $traffic_sec = $traffic_sec * 1024000000; } if ($traffic_sec_unit == "TB") { $traffic_sec = $traffic_sec * 10240000000000; } $traffic_request = $re[4]; $traffic_request_unit = strtoupper($re[5]); if ($traffic_request_unit == "KB") { $traffic_request = $traffic_request * 1024; } if ($traffic_request_unit == "MB") { $traffic_request = $traffic_request * 1024000; } if ($traffic_request_unit == "GB") { $traffic_request = $traffic_request * 1024000000; } if ($traffic_request_unit == "TB") { $traffic_request = $traffic_request * 10240000000000; } continue; } if (preg_match("#<td><b>[0-9]+-[0-9]+</b></td><td>([0-9]+)</td><td>#", $ligne, $re)) { $pid[$re[1]] = $re[1]; } } if (count($pid) > 0) { while (list($num, $ligne) = each($pid)) { $mem = $unix->PROCESS_MEMORY($num, true) + $unix->PROCESS_CACHE_MEMORY($num, true); $total_mem = $total_mem + $mem; } } if ($GLOBALS["VERBOSE"]) { echo "Access: {$access} total-traffic:{$total_traffic} bytes UPTIME={$UPTIME} Total memory used: {$total_mem} Bytes\n"; echo "Access: requests/seconds: {$request_s} traffic/sec:{$traffic_sec} trafic per request:{$traffic_request} bytes:\n"; } if (!is_numeric($total_traffic)) { if ($GLOBALS["VERBOSE"]) { echo "No traffic return null\n"; } return; } $UPTIME = str_replace("</td>", "", $UPTIME); $UPTIME = str_replace("</dt>", "", $UPTIME); $query = "('{$servername}','{$total_traffic}','{$total_mem}','{$request_s}','{$traffic_sec}','{$traffic_request}','{$UPTIME}')"; if ($GLOBALS["VERBOSE"]) { echo "{$query}\n"; } $GLOBALS["MODSTATUSQ"][] = $query; // voir //http://www.apache.org/server-status }