/** * 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']) { 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; } }
) temp WHERE MOD(temp.rownum, ' . $options['i'] . ') = ?;'; $doing = $options['n'] . "/" . $options['i']; $params[] = $options['n']; //print_vars($query); //print_vars($params); } if (!$where) { print_message("%n\nUSAGE:\n{$scriptname} [-drqV] [-i instances] [-n number] [-m module] [-h device]\n\nEXAMPLE:\n-h <device id> | <device hostname wildcard> Poll single device\n-h odd Poll odd numbered devices (same as -i 2 -n 0)\n-h even Poll even numbered devices (same as -i 2 -n 1)\n-h all Poll all devices\n-h new Poll all devices that have not had a discovery run before\n\n-i <instances> -n <id/number> Poll as instance <id/number> of <instances>\n Instance numbers start at 0. 0-3 for -i 4\n Example:\n -i 4 -n 0\n -i 4 -n 1\n -i 4 -n 2\n -i 4 -n 3\n\nOPTIONS:\n -h Device hostname, id or key odd/even/all/new.\n -i Poll instances count.\n -n Instance id (number), must start from 0 and to be less than instances count.\n -q Quiet output.\n -M Show globally enabled/disabled modules and exit.\n -V Show version and exit.\n\nDEBUGGING OPTIONS:\n -r Do not create or update RRDs\n -d Enable debugging output.\n -dd More verbose debugging output.\n -m Specify module(s) (separated by commas) to be run.\n\n%rInvalid arguments!%n", 'color', FALSE); exit; } if (isset($options['r'])) { $config['norrd'] = TRUE; } $cache['maint'] = cache_alert_maintenance(); rrdtool_pipe_open($rrd_process, $rrd_pipes); print_cli_heading("%WStarting polling run at " . date("Y-m-d H:i:s"), 0); $polled_devices = 0; if (!isset($query)) { $query = "SELECT `device_id` FROM `devices` WHERE `disabled` = 0 {$where} ORDER BY `device_id` ASC"; } foreach (dbFetch($query, $params) as $device) { $device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($device['device_id'])); poll_device($device, $options); $polled_devices++; } $poller_end = utime(); $poller_run = $poller_end - $poller_start; $poller_time = substr($poller_run, 0, 5); if ($polled_devices) { dbInsert(array('type' => 'poll', 'doing' => $doing, 'start' => $poller_start, 'duration' => $poller_time, 'devices' => $polled_devices), 'perf_times');
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_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; } }