print_cli_data('Poller Time', $poller_time . " secs", 0); print_cli_data('Memory usage', formatStorage(memory_get_usage(TRUE), 2, 4) . ' (peak: ' . formatStorage(memory_get_peak_usage(TRUE), 2, 4) . ')', 0); $mysql_time = 0; foreach ($db_stats as $cmd => $count) { if (isset($db_stats[$cmd . '_sec'])) { $mysql_times[] = ucfirst(str_replace("fetch", "", $cmd)) . "[" . $count . "/" . round($db_stats[$cmd . '_sec'], 3) . "s]"; $mysql_time += $db_stats[$cmd . '_sec']; } } print_cli_data('MySQL Usage', implode(" ", $mysql_times) . ' (' . round($mysql_time, 3) . 's ' . round($mysql_time / $poller_time * 100, 3) . '%)', 0); $rrd_time = 0; foreach ($GLOBALS['rrdtool'] as $cmd => $data) { $rrd_times[] = $cmd . "[" . $data['count'] . "/" . round($data['time'], 3) . "s]"; $rrd_time += $data['time']; } print_cli_data('RRDTool Usage', implode(" ", $rrd_times) . ' (' . round($rrd_time, 3) . 's ' . round($rrd_time / $poller_time * 100, 3) . '%)', 0); $snmp_time = 0; foreach ($GLOBALS['snmp_stats'] as $cmd => $data) { $snmp_times[] = $cmd . "[" . $data['count'] . "/" . round($data['time'], 3) . "s]"; $snmp_time += $data['time']; } print_cli_data('SNMP Usage', implode(" ", $snmp_times) . ' (' . round($snmp_time, 3) . 's ' . round($snmp_time / $poller_time * 100, 3) . '%)', 0); } logfile($string); rrdtool_pipe_close($rrd_process, $rrd_pipes); unset($config); // Remove this for testing #print_vars(get_defined_vars()); echo "\n"; print_r($runtime_stats); // EOF
/** * Generates a graph file at $graph_file using $options * Opens its own rrdtool pipe. * * @return integer * @param string graph_file * @param string options */ function rrdtool_graph($graph_file, $options) { global $config, $debug; rrdtool_pipe_open($rrd_process, $rrd_pipes); if (is_resource($rrd_process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt if ($config['rrdcached']) { if (isset($config['rrdcached_dir']) && $config['rrdcached_dir'] !== false) { $options = str_replace($config['rrd_dir'] . '/', './' . $config['rrdcached_dir'] . '/', $options); $options = str_replace($config['rrd_dir'], './' . $config['rrdcached_dir'] . '/', $options); } fwrite($rrd_pipes[0], 'graph --daemon ' . $config['rrdcached'] . " {$graph_file} {$options}"); } else { fwrite($rrd_pipes[0], "graph {$graph_file} {$options}"); } fclose($rrd_pipes[0]); while (strlen($line) < 1) { $line = fgets($rrd_pipes[1], 1024); $data .= $line; } $return_value = rrdtool_pipe_close($rrd_process, $rrd_pipes); if ($debug) { echo '<p>'; if ($debug) { echo "graph {$graph_file} {$options}"; } echo '</p><p>'; echo "command returned {$return_value} ({$data})\n"; echo '</p>'; } return $data; } else { return 0; } }
function rrdtool_graph($graph_file, $options) { global $config; // Note, always use pipes, because standard command line has limits! if ($config['rrdcached']) { $cmd = 'graph --daemon ' . $config['rrdcached'] . " {$graph_file} {$options}"; } else { $cmd = "graph {$graph_file} {$options}"; } $GLOBALS['rrd_status'] = FALSE; $GLOBALS['exec_status'] = array('command' => $config['rrdtool'] . ' ' . $cmd, 'stdout' => '', 'exitcode' => -1); $start = microtime(TRUE); rrdtool_pipe_open($rrd_process, $rrd_pipes); if (is_resource($rrd_process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($rrd_pipes[0], $cmd); fclose($rrd_pipes[0]); $iter = 0; while (strlen($line) < 1 && $iter < 1000) { // wait for 10 milliseconds to loosen loop usleep(10000); $line = fgets($rrd_pipes[1], 1024); $stdout .= $line; $iter++; } $stdout = preg_replace('/(?:\\n|\\r\\n|\\r)$/D', '', $stdout); // remove last (only) eol unset($iter); $runtime = microtime(TRUE) - $start; // Check rrdtool's output for the command. if (preg_match('/\\d+x\\d+/', $stdout)) { $GLOBALS['rrd_status'] = TRUE; } else { $stderr = trim(stream_get_contents($rrd_pipes[2])); if (isset($config['rrd']['debug']) && $config['rrd']['debug']) { logfile('rrd.log', "RRD {$stderr}, CMD: " . $GLOBALS['exec_status']['command']); } } $exitcode = rrdtool_pipe_close($rrd_process, $rrd_pipes); $GLOBALS['exec_status']['exitcode'] = $exitcode; $GLOBALS['exec_status']['stdout'] = $stdout; $GLOBALS['exec_status']['stderr'] = $stderr; } else { $runtime = microtime(TRUE) - $start; $stdout = NULL; } $GLOBALS['exec_status']['runtime'] = $runtime; // Add some data to global array $graph_return $GLOBALS['graph_return']['status'] = $GLOBALS['rrd_status']; $GLOBALS['graph_return']['command'] = $GLOBALS['exec_status']['command']; $GLOBALS['graph_return']['filename'] = $graph_file; $GLOBALS['graph_return']['output'] = $stdout; $GLOBALS['graph_return']['runtime'] = $GLOBALS['exec_status']['runtime']; if (OBS_DEBUG) { print_message(PHP_EOL . 'RRD CMD[%y' . $cmd . '%n]', 'console', FALSE); $debug_msg = 'RRD RUNTIME[' . ($runtime > 0.1 ? '%r' : '%g') . round($runtime, 4) . 's%n]' . PHP_EOL; $debug_msg .= 'RRD STDOUT[' . ($GLOBALS['rrd_status'] ? '%g' : '%r') . $stdout . '%n]' . PHP_EOL; if ($stderr) { $debug_msg .= 'RRD STDERR[%r' . $stderr . '%n]' . PHP_EOL; } $debug_msg .= 'RRD_STATUS[' . ($GLOBALS['rrd_status'] ? '%gTRUE' : '%rFALSE') . '%n]'; print_message($debug_msg . PHP_EOL, 'console'); } return $stdout; }
function rrdtool_graph($graph_file, $options) { global $config, $debug; rrdtool_pipe_open($rrd_process, $rrd_pipes); if (is_resource($rrd_process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt if ($config['rrdcached']) { fwrite($rrd_pipes[0], "graph --daemon " . $config['rrdcached'] . " {$graph_file} {$options}"); } else { fwrite($rrd_pipes[0], "graph {$graph_file} {$options}"); } fclose($rrd_pipes[0]); $iter = 0; while (strlen($line) < 1 && $iter < 1000) { // wait for 10 milliseconds to loosen loop usleep(10000); $line = fgets($rrd_pipes[1], 1024); $data .= $line; $iter++; } unset($iter); $return_value = rrdtool_pipe_close($rrd_process, $rrd_pipes); if ($debug) { echo "<p>"; if ($debug) { echo "graph {$graph_file} {$options}"; } echo "</p><p>"; echo "command returned {$return_value} ({$data})\n"; echo "</p>"; } return $data; } else { return 0; } }
function rrdtool_graph_xport($options) { global $config; $debug = 1; header('Content-type: text/html'); if (substr_count($options, '--alt-autoscale-max') > 1) { $options = substr($options, strripos($options, '--alt-autoscale-max')); } $options = str_replace(array('--alt-autoscale-max ', '--rigid ', '-E'), '', $options); $options = preg_replace(array('/--(width|height)\\s[\\d]+/', '/-c\\s[^\\s]+/', '/--font\\s[^\\s]+/', '/--font-render-mode\\s[^\\s]+/', '/-R\\s[^\\s]+/'), '', $options); $xport = ""; if (preg_match_all("/(?<=\\s)DEF:[\\s]?([a-zA-Z]+)(?==)/", $options, $matches)) { $def = $matches[1]; foreach ($def as $d) { $xport .= "XPORT:" . $d . ":" . "\"" . str_replace(":", "", $d) . "\" "; } } rrdtool_pipe_open($rrd_process, $rrd_pipes); if (is_resource($rrd_process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($rrd_pipes[0], "xport {$graph_file} {$options} {$xport}"); fclose($rrd_pipes[0]); while (strlen($line) < 1) { // wait for 10 milliseconds to loosen loop usleep(10000); $line = fgets($rrd_pipes[1], 1024); $data .= $line; } $return_value = rrdtool_pipe_close($rrd_process, $rrd_pipes); return $data; } else { return 0; } }