Class: Nagios::Config
- Inherits:
-
Object
- Object
- Nagios::Config
- Defined in:
- lib/nagios/config.rb
Overview
If you have more than one /etc/nagios* directories then only
Parser of the main Nagios configuration file – nagios.cfg.
Constructor parses Nagios’ main config file and returns an object: each configuration option’s value assigned to an instance variable and attribute reader method is created.
Can be used as:
require 'nagios/config'
nagios = Nagios::Config.new "lib/ruby-nagios/test/data/nagios.cfg"
nagios.log_file
=> "/var/log/nagios3/nagios.log"
nagios.status_file
=> "/var/cache/nagios3/status.dat"
Configuration of the module
Default search directory and file pattern (Dir.glob) is defined by Nagios::DEFAULT constant. It is set in config/default.rb
file.
first one will be used. For example, Debian can have both Nagios 2 and 3 installed. In the latter case configuration file is /etc/nagios3/nagios.cfg
.
Instance Attribute Summary collapse
-
#cfg_dir ⇒ Object
readonly
Special case for cfg_file and cfg_dir: they are Arrays.
-
#cfg_file ⇒ Object
readonly
Special case for cfg_file and cfg_dir: they are Arrays.
-
#configuration ⇒ Object
Hash holding all the configuration after parsing.
-
#path ⇒ Object
Path to main configuration file nagios.cfg.
Instance Method Summary collapse
-
#initialize(config_file = nil) ⇒ Config
constructor
Initialize configuration file path.
-
#parse ⇒ Object
Read and parse main Nagios configuration file
nagios.cfg
.
Constructor Details
#initialize(config_file = nil) ⇒ Config
Initialize configuration file path. Check existence and readability of the file, raise exception if not.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nagios/config.rb', line 47 def initialize config_file=nil @config = config_file || Dir.glob( Nagios::DEFAULT[:nagios_cfg_glob] ).first @path = @config @configuration ||= {} raise "No configuration file option and no files in #{ DEFAULT[:nagios_cfg_glob] } " unless @config raise "Configuration file #{@config} does not exist" unless File.exist? @config raise "Configuration file #{@config} is not readable" unless File.readable? @config end |
Instance Attribute Details
#cfg_dir ⇒ Object (readonly)
Special case for cfg_file and cfg_dir: they are Arrays
101 102 103 |
# File 'lib/nagios/config.rb', line 101 def cfg_dir @cfg_dir end |
#cfg_file ⇒ Object (readonly)
Special case for cfg_file and cfg_dir: they are Arrays
101 102 103 |
# File 'lib/nagios/config.rb', line 101 def cfg_file @cfg_file end |
#configuration ⇒ Object
Hash holding all the configuration after parsing. Additionally for every key in the configuration Hash method is created with the same name, which returns the value.
62 63 64 |
# File 'lib/nagios/config.rb', line 62 def configuration @configuration end |
#path ⇒ Object
Path to main configuration file nagios.cfg
66 67 68 |
# File 'lib/nagios/config.rb', line 66 def path @path end |
Instance Method Details
#parse ⇒ Object
Read and parse main Nagios configuration file nagios.cfg
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/nagios/config.rb', line 73 def parse idx=0 File.readlines(@config).map{ |l| l.sub(/\s*#.*$/,'')}.delete_if { |l| l=~ /^\s*$/}.each do |l| idx += 1 key,val = l.chomp.strip.split(/\s*=\s*/,2) raise "Incorrect configuration line ##{idx}: '#{l}'" unless key && val case key # There could be multiple entries for cfg_dir/file, so these # are Arrays. when /cfg_(file|dir)/ @configuration[key] ||= [] @configuration[key] << val instance_variable_set("@#{key}", (instance_variable_get("@#{key}") || []) << val ) else @configuration[key] = val instance_variable_set("@#{key}", val) instance_eval val =~ /^[\d\.-]+/ ? "def #{key}; return #{val}; end" : "def #{key}; return %Q{#{val}}; end" end end self end |