1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
4 * Reads various nagios-related configuration files
6 class System_Model
extends Model
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');
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) != '/') {
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
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'];
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)) {
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)) {
70 $buf = file_get_contents($config_file);
71 if($buf === false) return(false);
73 $lines = explode("\n", $buf);
77 foreach($lines as $line) {
78 // skip empty lines and non-variables
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];
93 $options[$str[0]] = $str[1];
100 * Fetch status info from nagios log file
102 public static function get_status_info($file = 'status.log', $section = 'programstatus')
107 if (!file_exists($file)) {
108 $base_path = self
::get_nagios_base_path();
109 $file = $base_path.'/var/'.$file;
111 if (!file_exists($file)) {
115 $fh = fopen($file, "r");
118 $found_section = false;
119 while ($raw_line = fgets($fh)) {
120 $line = trim($raw_line);
121 if (!strlen($line) ||
$line{0} === '#')
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} === '}')
130 if (!strcmp($line, $section.' {')) {
131 $found_section = true;
135 if ($found_section === true) {
136 $ary = explode('=', $line);
137 if (is_array($ary) && sizeof($ary)==2) {
138 $data[$section][$ary[0]] = $ary[1];
148 * Extract values from status.log array returned from
151 public static function extract_stat_key($key=false, &$arr=false)
153 if (empty($key) ||
empty($arr))
156 if (isset($arr[$key])) {
157 $tmp = explode(',', $arr[$key]);
158 if (is_array($tmp) && !empty($tmp)) {
159 if (count($tmp) == 1) {
165 $return = $arr[$key];
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));
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)) {