/application/models/system.php

https://github.com/transitiv/ninja · PHP · 227 lines · 159 code · 25 blank · 43 comment · 40 complexity · ac8e753b7fd8cff3757b6ad7cbf63123 MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Reads various nagios-related configuration files
  4. */
  5. class System_Model extends Model
  6. {
  7. /**
  8. * Fetch nagios base path as configured in config file
  9. * @return string 'config.nagios_base_path'
  10. */
  11. public static function get_nagios_base_path()
  12. {
  13. return Kohana::config('config.nagios_base_path');
  14. }
  15. /**
  16. * Reads a configuration file in the format variable=value
  17. * and returns it in an array.
  18. * lines beginning with # are considered to be comments
  19. * @param $config_file The configuration file to parse
  20. * @return Array of key => value type on success, false on errors
  21. */
  22. public static function parse_config_file($config_file) {
  23. $config_file = trim($config_file);
  24. if (empty($config_file)) {
  25. return false;
  26. }
  27. # check if we have a full path as input
  28. if (!file_exists($config_file)) {
  29. $base_path = self::get_nagios_base_path();
  30. $base_path = Kohana::config('config.nagios_etc_path') ? Kohana::config('config.nagios_etc_path') : $base_path.'/etc';
  31. $etc = strstr($base_path, '/etc') ? '' : '/etc/';
  32. # check that we have a trailing slash in path
  33. if (substr($base_path.$etc, -1, 1) != '/') {
  34. $etc .= '/';
  35. }
  36. $config_file = $base_path.$etc.$config_file;
  37. }
  38. if (!file_exists($config_file)) {
  39. return false;
  40. }
  41. $buf = file_get_contents($config_file);
  42. if($buf === false) return(false);
  43. $lines = explode("\n", $buf);
  44. $buf = '';
  45. $tmp = false;
  46. foreach($lines as $line) {
  47. // skip empty lines and non-variables
  48. $line = trim($line);
  49. if(!strlen($line) || $line{0} === '#') continue;
  50. $str = explode('=', $line);
  51. if(!isset($str[1])) continue;
  52. // preserve all values if a variable can be specified multiple times
  53. if(isset($options[$str[0]]) && $options[$str[0]] !== $str[1]) {
  54. if(!is_array($options[$str[0]])) {
  55. $tmp = $options[$str[0]];
  56. $options[$str[0]] = array($tmp);
  57. }
  58. $options[$str[0]][] = $str[1];
  59. continue;
  60. }
  61. $options[$str[0]] = $str[1];
  62. }
  63. return $options;
  64. }
  65. /**
  66. * Call parse_config_file() with cgi.cfg
  67. * and fetch user configuration options (authorized_for)
  68. */
  69. public function fetch_nagios_users()
  70. {
  71. $cgi_config = false;
  72. $base_path = self::get_nagios_base_path();
  73. $etc_path = Kohana::config('config.nagios_etc_path') ? Kohana::config('config.nagios_etc_path') : $base_path.'/etc';
  74. $cgi_config_file = $etc_path."/cgi.cfg";
  75. $user_data = false;
  76. $access_levels = array('authorized_for_system_information',
  77. 'authorized_for_configuration_information',
  78. 'authorized_for_system_commands',
  79. 'authorized_for_all_services',
  80. 'authorized_for_all_hosts',
  81. 'authorized_for_all_service_commands',
  82. 'authorized_for_all_host_commands');
  83. $cgi_config = self::parse_config_file($cgi_config_file);
  84. if(empty($cgi_config)) {
  85. return false;
  86. }
  87. foreach($cgi_config as $k => $v) {
  88. if(substr($k, 0, 14) === 'authorized_for') {
  89. $cgi_config[$k] = explode(',', $v);
  90. }
  91. }
  92. # fetch defined access data for users
  93. foreach ($access_levels as $level) {
  94. $users = $cgi_config[$level];
  95. foreach ($users as $user) {
  96. $user_data[$level][] = $user;
  97. }
  98. }
  99. return $user_data;
  100. }
  101. /**
  102. * Fetch authentication information
  103. * for a named user.
  104. * Use cached authorization data from session if available.
  105. */
  106. public function nagios_access($username=false)
  107. {
  108. $access = Session::instance()->get('nagios_access', null);
  109. $db = Database::instance();
  110. if (is_null($access)) {
  111. $access = Ninja_user_authorization_Model::get_auth_data($username);
  112. Session::instance()->set('nagios_access', $access);
  113. }
  114. return $access;
  115. }
  116. /**
  117. * Fetch status info from nagios log file
  118. */
  119. public function get_status_info($file = 'status.log', $section = 'programstatus')
  120. {
  121. if (empty($file))
  122. return false;
  123. if (!file_exists($file)) {
  124. $base_path = self::get_nagios_base_path();
  125. $file = $base_path.'/var/'.$file;
  126. }
  127. if (!file_exists($file)) {
  128. return false;
  129. }
  130. $fh = fopen($file, "r");
  131. if (!$fh)
  132. return false;
  133. $found_section = false;
  134. while ($raw_line = fgets($fh)) {
  135. $line = trim($raw_line);
  136. if (!strlen($line) || $line{0} === '#')
  137. continue;
  138. # this routine is only ever read to get one
  139. # section, so we can bail early when we've
  140. # found it and the section is done with
  141. if ($found_section && $line{0} === '}')
  142. break;
  143. if (!strcmp($line, $section.' {')) {
  144. $found_section = true;
  145. continue;
  146. }
  147. if ($found_section === true) {
  148. $ary = explode('=', $line);
  149. if (is_array($ary) && sizeof($ary)==2) {
  150. $data[$section][$ary[0]] = $ary[1];
  151. }
  152. }
  153. }
  154. fclose($fh);
  155. return $data;
  156. }
  157. /**
  158. * Extract values from status.log array returned from
  159. * get_status_info()
  160. */
  161. public function extract_stat_key($key=false, &$arr=false)
  162. {
  163. if (empty($key) || empty($arr))
  164. return false;
  165. $return = false;
  166. if (isset($arr[$key])) {
  167. $tmp = explode(',', $arr[$key]);
  168. if (is_array($tmp) && !empty($tmp)) {
  169. if (count($tmp) == 1) {
  170. $return = $tmp[0];
  171. } else {
  172. $return = $tmp;
  173. }
  174. } else {
  175. $return = $arr[$key];
  176. }
  177. }
  178. return $return;
  179. }
  180. /**
  181. * Fetch info on installed rpm packages
  182. * @param $filter A regular expression passed to 'grep'
  183. * @return array or false
  184. */
  185. public function rpm_info($filter = 'op5')
  186. {
  187. $filter = escapeshellarg(trim($filter));
  188. $rpm_info = false;
  189. $exec_str = '/bin/rpm -qa';
  190. $exec_str .= !empty($filter) ? '|grep '.$filter.'|sort' : '|sort';
  191. exec($exec_str, $output, $retval);
  192. if ($retval==0 && !empty($output)) {
  193. foreach ($output as $rpm) {
  194. $rpm_info .= $rpm."<br />";
  195. }
  196. if (!empty($rpm_info)) {
  197. return $rpm_info;
  198. }
  199. }
  200. return false;
  201. }
  202. }