'http://localhost:8080/nginx_status', // connection parameters for mysqladmin 'MySQL' => '--port=3306 --socket=/var/run/mysqld/mysqld.sock --user=**** --password=****', // connection parameters for pg_connect 'PostgreSQL' => "host='localhost' user='****' password='****'" ); /* if exec() function is disabled in PHP, then you can refresh data from cli with cron e.g. every minutes by the following command: /usr/bin/nice -n 10 /bin/su www-data -c '/usr/bin/php -f /{path}/{to}/monitor.php save' >/dev/null 2>&1 In this case turn TRUE the REFRESH_FROM_CRON constant */ define('REFRESH_FROM_CRON', FALSE); //define('TIMEZONE', 'America/New_York'); define('TIMEZONE', 'Europe/Budapest'); if (TIMEZONE) date_default_timezone_set(TIMEZONE); header('Content-Type: application/json; charset=utf-8'); $staticFile = __FILE__.'.txt'; define('SAVE', REFRESH_FROM_CRON && isset($_SERVER['argv']) && is_array($_SERVER['argv']) && in_array('save', $_SERVER['argv'])); if (REFRESH_FROM_CRON && !SAVE) { if (file_exists($staticFile)) echo file_get_contents($staticFile); die(); } function execute($command, $parameters = '', $trim = TRUE) { if (!empty($parameters)) $parameters = ' ' . $parameters; $output = $return = NULL; exec($command.$parameters, $output, $return); if ($return === 1 || $return === 127) { $command = trim(shell_exec("whereis -b {$command} | awk '{print $2}'")); $output = $return = NULL; if (!empty($command)) { exec($command.$parameters, $output, $return); } } if (!is_array($output)) return NULL; $output = implode("\n", $output); return $trim ? trim($output) : $output; } function lines2array($lines) { $lines = explode("\n", $lines); $array = array(); foreach ($lines as $line) { $line = preg_split('#\s+#siu', trim($line), 2); if (isset($line[1])) $array[$line[0]] = $line[1]; else $array[$line[0]] = NULL; } return $array; } function percent($value, $sum, $decimals=0) { if ($sum == 0) $value = $value > 0 ? 100 : 0; else $value = $value / $sum * 100; return sprintf('%.'.intval($decimals).'f', $value) . '%'; } $result = array(); // DateTime $result[] = array('DateTime', str_replace('T', ' ', date('c'))); // UpTime $data = execute('cut', '-d. -f1 /proc/uptime'); if (!empty($data)) { $result[] = array('UpTime', floor($data/60/60/24)." days, " . sprintf('%02d', $data/60/60%24) . ':' . sprintf('%02d', $data/60%60) . ':' . sprintf('%02d', $data%60)); } // LoadAVG $data = sys_getloadavg(); $result[] = array('LoadAVG', sprintf('%.2f', $data[0]) . ' ' . sprintf('%.2f', $data[1]) . ' '. sprintf('%.2f', $data[2])); // CPU /* DON'T USE THIS!! $data = execute('sar', "-P ALL 1 1 | grep 'Average' | grep -vE 'CPU|all' | awk '{print $8}'"); if (empty($data)) { $data = array('sysstat is not intalled') ; } else { $data = explode("\n", $data); foreach ($data as $i=>$cpu) $data[$i] = percent(100 - floatval($cpu), 100, 2); } $result[] = array('CPU', implode("\n", $data)); */ // CPU temperature $data = execute('sensors', '--no-adapter'); if (!empty($data)) { if (preg_match_all('#(coretemp.*?)(?:(?:\r?\n){2,}|$)#siu', $data, $cores)) { $cores = $cores[1]; foreach ($cores as $core) { $core = preg_replace('#^.*?(Core(?:\s+\d+)?):\s+[+]?(.*?)(?:\.\d+)?\s+([cf]).*$#siu', "$1\t$2°$3", $core); $core = explode("\t", $core, 2); $result[] = array("CPU {$core[0]}", $core[1]); } } } // Disc $data = execute('df', '-h --local | grep "^/dev/"'); if (!empty($data)) { $data = preg_split('/\r?\n+/siu', $data); foreach ($data as $i=>$d) { $d = preg_split('/\s+/siu', $d); $result[] = array("Disc {$i}", "{$d[5]} {$d[2]}/{$d[1]} {$d[4]}"); $temp = execute('hddtemp', "--wake-up {$d[0]}"); if (!empty($temp)) { $temp = preg_replace('#^.*?(\d+)\s+(\w)$#siu', '$1°$2', $temp); $result[] = array("Disc {$i} temperature", $temp); } } } // Memory $data = execute('free', '-t -m | grep "^Mem"'); if (!empty($data)) { $data = preg_split('/\s+/siu', $data); $data[2] -= $data[6]; // without cache $data = "{$data[2]}M/{$data[1]}M ".percent($data[2], $data[1]); $result[] = array('Memory', $data); } // Swap $data = execute('free', '-t -m | grep "^Swap"'); if (!empty($data)) { $data = preg_split('/\s+/siu', $data); $data = "{$data[2]}M/{$data[1]}M ".percent($data[2], $data[1]); $result[] = array('Swap', $data); } // Processes $result[] = array('Processes', execute('ps', 'aux --no-heading | wc -l')); // Users $data = execute('who', "| cut -d' ' -f1 | uniq -c | awk '{print $2\" (\"$1\")\"}' | sort"); $result[] = array('Users', empty($data) ? '-' : $data); // SMB users $data = execute('smbstatus', "-b | grep -E '^[0-9]+' | awk '{print $2}' | uniq -c | awk '{print $2\" (\"$1\")\"}' | sort"); $result[] = array('SMB users', empty($data) ? '-' : $data); // Apache // YOU MUST ENABLE Apache mod_status MODULE $apacheSettings = preg_replace('/^.*?(\w+)="([^"]*)".*$/miu', '$1 $2', trim(execute('apache2ctl', "-V | grep -E 'HTTPD_ROOT|SERVER_CONFIG_FILE|APACHE_MPM_DIR'"))); if (!empty($apacheSettings)) { $apacheSettings = lines2array($apacheSettings); $apacheModule = ''; if (preg_match('/prefork/', $apacheSettings['APACHE_MPM_DIR'])) $apacheModule = 'prefork'; if (preg_match('/worker/', $apacheSettings['APACHE_MPM_DIR'])) $apacheModule = 'worker'; if (preg_match('/event/', $apacheSettings['APACHE_MPM_DIR'])) $apacheModule = 'event'; if (empty($apacheModule)) $apacheModule = preg_replace('/^mpm_([^_]+)_module.*$/siu', '$1', trim(execute('apache2ctl', "-t -D DUMP_MODULES | grep 'mpm_[^_]*_module'"))); $apacheConfig = preg_replace('/#.*$/miu', '$1', execute('grep', " -v '^#' {$apacheSettings['HTTPD_ROOT']}/{$apacheSettings['SERVER_CONFIG_FILE']}")); $maxClients = intval(preg_replace('#^.*?.*$#siu', '$1', $apacheConfig)); $data = execute('apache2ctl', 'status | grep -E "([0-9]+)[^0-9]+([0-9]+) idle workers"'); if (!empty($data)) { $data = preg_replace('#^.*?(\d+)[^\d]+(\d)+[^\d]+$#siu', '$1/$2', $data); $data = explode('/', $data); $data = $data[0] . '/' . ($maxClients) . ' ' . percent($data[0], $maxClients); $result[] = array('Apache', $data); } unset($apacheConfig); } if (isset($CONFIG['Nginx'])) { // Nginx // YOU MUST ENABLE nginx_status on localhost // location /nginx_status { // stub_status on; // access_log off; // allow 127.0.0.1; // deny all; // } $nginxConfig = preg_replace('/#.*$/miu', '$1', execute('grep', " -E '^\s*(worker_connections|worker_processes)' /etc/nginx/nginx.conf")); $maxClients = intval(preg_replace('#^.*?worker_connections\s+(\d+).*?$#siu', '$1', $nginxConfig)) * intval(preg_replace('#^.*?worker_processes\s+(\d+).*?$#siu', '$1', $nginxConfig)); $nginxStatus = execute('wget', "-q -O - {$CONFIG['Nginx']} | grep 'Active connections'"); $activeClients = intval(preg_replace('#^.*?(\d+).*?$#siu', '$1', $nginxStatus)); $result[] = array('Nginx', "{$activeClients}/{$maxClients} ".percent($activeClients, $maxClients)); unset($nginxStatus); unset($nginxConfig); } // MySQL $variables = execute('mysqladmin ', "{$CONFIG['MySQL']} variables | grep 'max_connections' | grep '|' | sed 's/|/ /g' | awk '{print $1\" \"$2}'"); $status = execute('mysqladmin ', "{$CONFIG['MySQL']} extended-status | grep 'Threads_connected' | grep '|' | sed 's/|/ /g' | awk '{print $1\" \"$2}'"); if (!empty($variables) && !empty($status)) { $variables = lines2array($variables); $status = lines2array($status); $result[] = array('MySQL', "{$status['Threads_connected']}/{$variables['max_connections']} " . percent($status['Threads_connected'], $variables['max_connections'])); } unset($variables); unset($status); // PostgreSQL if (isset($CONFIG['PostgreSQL']) && function_exists('pg_connect')) { $s = ''; $conn = pg_connect($CONFIG['PostgreSQL']); if ($conn === FALSE) { $s = "Can't connect to database"; } else { $res = pg_query($conn, "SELECT (SELECT COUNT(*) from pg_stat_activity), setting FROM pg_settings WHERE name = 'max_connections'"); if ($res !== FALSE) { $row = pg_fetch_row($res); if (!empty($row)) { $s = "{$row[0]}/{$row[1]} " . percent($row[0], $row[1]); } unset($row); unset($res); } pg_close($conn); } unset($conn); if (empty($s)) $s = pg_last_error(); $result[] = array('PostgreSQL', $s); } $result = json_encode($result); if (SAVE) file_put_contents($staticFile, $result); else echo $result; ?>