histogram: Make histograms crash less
[ninja.git] / application / helpers / showlog.php
blob8f01e237b18ad9658f5327b34a1e085e46534f02
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * showlog helper
4 */
5 class showlog_Core
7 /**
8 * Generate HTML-formatted representation of the on-disk nagios log
10 * @param $options A magical array of options to use - check source for more info
11 * @return HTML-representation of the log
13 public static function show_log_entries($options)
15 # default limit
16 $limit = 2500;
17 $cgi_cfg = false;
18 $etc_path = System_Model::get_nagios_etc_path();
19 $cgi_cfg = $etc_path . '/cgi.cfg';
21 $showlog = self::get_path();
22 $cmd = $showlog . " --cgi-cfg=" . $cgi_cfg;
24 foreach ($options as $k => $v) {
25 # support all the various 'hide' options
26 if (substr($k, 0, 4) === 'hide') {
27 $cmd .= ' --' . str_replace('_', '-', $k);
28 continue;
30 switch ($k) {
31 case 'state_type':
32 if (isset($v['hard']) && isset($v['soft'])) {
33 break;
35 if (isset($v['hard'])) {
36 $cmd .= ' --state-type=hard';
37 } elseif (isset($v['soft'])) {
38 $cmd .= ' --state-type=soft';
40 break;
41 case 'first': case 'last':
42 if (!empty($v)) {
43 $cmd .= " --$k=$v";
44 $limit = false;
46 break;
47 case 'time_format':
48 $cmd .= ' --' . str_replace('_', '-', $k) . '=' . $v;
49 break;
50 case 'host':
51 $cmd .= " --host='" . join("' --host='", $v) . "'";
52 break;
53 case 'service':
54 $cmd .= " --service='" . join("' --service='", $v) . "'";
55 break;
56 default:
57 break;
60 if (!empty($options['host_state_options'])) {
61 $cmd .= ' --host-states=' . join(array_keys($options['host_state_options']));
63 if (!empty($options['service_state_options'])) {
64 $cmd .= ' --service-states=' . join(array_keys($options['service_state_options']));
67 if (empty($options['parse_forward'])) {
68 $cmd .= ' --reverse';
70 # invoke a hard limit in case the user didn't set any.
71 # This will prevent php from exiting with an out-of-memory
72 # error, and will also stop users' browsers from hanging
73 # when trying to load a gargantually large page
74 if (empty($options['limit']) && $limit !== false) {
75 $cmd .= ' --limit=' . $limit;
78 # Add the proper image url.
79 $cmd .= " --image-url=" . url::base(false) .
80 'application/views/icons/16x16/';
82 if (!Auth::instance()->authorized_for('system_information')) {
83 $cmd .= ' --hide-process --hide-commands ';
86 $send_objects = false;
87 if (!Auth::instance()->authorized_for('hosts_view_all') || !Auth::instance()->authorized_for('services_view_all')) {
88 $send_objects = true;
89 $cmd .= ' --restrict-objects';
92 $cmd .= " --html";
94 $descriptorspec = array(
95 0 => array('pipe', 'r'),
96 1 => array('pipe', 'w'), // really, php, no "I don't want to see it, just send it to the browser"?
97 2 => array('pipe', 'a'),
100 $process = proc_open($cmd, $descriptorspec, $pipes);
101 if (is_resource($process)) {
102 if ($send_objects) {
103 $pool = new HostPool_Model();
104 $set = $pool->all();
105 $hosts = array();
106 foreach ($set->it(array('name')) as $obj) {
107 $hosts[$obj->get_name()] = $obj->get_name();
109 fwrite($pipes[0], implode(';', $hosts));
110 fwrite($pipes[0], "\n");
111 $pool = new ServicePool_Model();
112 $set = $pool->all();
113 $svc = array();
114 foreach ($set->it(array('host_name', 'description')) as $obj) {
115 $hname = $obj->get_host()->get_name();
116 if (!array_key_exists($hname, $hosts)) {
117 $svc[] = $hname;
118 $svc[] = $obj->get_description();
121 fwrite($pipes[0], implode(';', $svc));
123 fclose($pipes[0]);
124 while (!feof($pipes[1])) {
125 echo fgets($pipes[1], 1024);
127 fclose($pipes[1]);
128 proc_close($process);
129 } else {
130 echo "Couldn't run showlog binary";
135 * Get path to showlog executable
137 public static function get_path()
139 $showlog = Kohana::config('reports.showlog_path');
140 if (!file_exists($showlog) || !is_executable($showlog)) {
141 echo "Showlog program '$showlog' not installed or not executable.<br />\n";
142 return false;
144 return $showlog;