reports: Fix id of custom date selector
[ninja.git] / application / models / system.php
blobbc50209f52ee36a99f4785fe6981193190e50e11
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Reads various nagios-related configuration files
5 */
6 class System_Model extends Model
8 /**
9 * Fetch nagios base path as configured in config file
10 * @return string 'config.nagios_base_path'
12 public static function get_nagios_base_path()
14 return Kohana::config('config.nagios_base_path');
17 /**
18 * Fetch nagios etc path as configured in config file
19 * @return string directory where nagios.cfg can be found
21 public static function get_nagios_etc_path()
23 $etc = Kohana::config('config.nagios_etc_path') ? Kohana::config('config.nagios_etc_path') : (self::get_nagios_base_path().'/etc');
24 if (substr($etc, -1, 1) != '/') {
25 $etc .= '/';
27 return $etc;
30 /**
31 * Fetch nagios command pipe as configured by the
32 * nagios_pipe setting. If the pipe does not exist
33 * or is not writable, fetch pipe as configured by
34 * command_file in nagios.cfg.
35 * @return string path to the configured pipe
36 * */
37 public static function get_pipe()
39 $pipe = Kohana::config('config.nagios_pipe');
40 if( !file_exists($pipe) || !is_writable($pipe) ) {
41 $nagconfig = self::parse_config_file("nagios.cfg");
42 if (isset($nagconfig['command_file'])) {
43 $pipe = $nagconfig['command_file'];
46 return $pipe;
48 /**
49 * Reads a configuration file in the format variable=value
50 * and returns it in an array.
51 * lines beginning with # are considered to be comments
52 * @param $config_file The configuration file to parse
53 * @return Array of key => value type on success, false on errors
55 public static function parse_config_file($config_file) {
56 $config_file = trim($config_file);
57 if (empty($config_file)) {
58 return false;
61 # check if we have a full path as input
62 if (!file_exists($config_file)) {
63 $etc = self::get_nagios_etc_path();
64 $config_file = $etc.$config_file;
67 if (!file_exists($config_file)) {
68 return false;
70 $buf = file_get_contents($config_file);
71 if($buf === false) return(false);
73 $lines = explode("\n", $buf);
74 $buf = '';
76 $tmp = false;
77 foreach($lines as $line) {
78 // skip empty lines and non-variables
79 $line = trim($line);
80 if(!strlen($line) || $line{0} === '#') continue;
81 $str = explode('=', $line);
82 if(!isset($str[1])) continue;
84 // preserve all values if a variable can be specified multiple times
85 if(isset($options[$str[0]]) && $options[$str[0]] !== $str[1]) {
86 if(!is_array($options[$str[0]])) {
87 $tmp = $options[$str[0]];
88 $options[$str[0]] = array($tmp);
90 $options[$str[0]][] = $str[1];
91 continue;
93 $options[$str[0]] = $str[1];
96 return $options;
99 /**
100 * Fetch status info from nagios log file
102 public static function get_status_info($file = 'status.log', $section = 'programstatus')
104 if (empty($file))
105 return false;
107 if (!file_exists($file)) {
108 $base_path = self::get_nagios_base_path();
109 $file = $base_path.'/var/'.$file;
111 if (!file_exists($file)) {
112 return false;
115 $fh = fopen($file, "r");
116 if (!$fh)
117 return false;
118 $found_section = false;
119 while ($raw_line = fgets($fh)) {
120 $line = trim($raw_line);
121 if (!strlen($line) || $line{0} === '#')
122 continue;
124 # this routine is only ever read to get one
125 # section, so we can bail early when we've
126 # found it and the section is done with
127 if ($found_section && $line{0} === '}')
128 break;
130 if (!strcmp($line, $section.' {')) {
131 $found_section = true;
132 continue;
135 if ($found_section === true) {
136 $ary = explode('=', $line);
137 if (is_array($ary) && sizeof($ary)==2) {
138 $data[$section][$ary[0]] = $ary[1];
142 fclose($fh);
144 return $data;
148 * Extract values from status.log array returned from
149 * get_status_info()
151 public static function extract_stat_key($key=false, &$arr=false)
153 if (empty($key) || empty($arr))
154 return false;
155 $return = false;
156 if (isset($arr[$key])) {
157 $tmp = explode(',', $arr[$key]);
158 if (is_array($tmp) && !empty($tmp)) {
159 if (count($tmp) == 1) {
160 $return = $tmp[0];
161 } else {
162 $return = $tmp;
164 } else {
165 $return = $arr[$key];
168 return $return;
172 * Fetch info on installed rpm packages
173 * @param $filter A regular expression passed to 'grep'
174 * @return array or false
176 public function rpm_info($filter = 'op5')
178 $filter = escapeshellarg(trim($filter));
179 $rpm_info = false;
180 $exec_str = '/bin/rpm -qa';
181 $exec_str .= !empty($filter) ? '|grep '.$filter.'|sort' : '|sort';
182 exec($exec_str, $output, $retval);
183 if ($retval==0 && !empty($output)) {
184 foreach ($output as $rpm) {
185 $rpm_info .= $rpm."<br />";
187 if (!empty($rpm_info)) {
188 return $rpm_info;
191 return false;