diff options
Diffstat (limited to 'ini_read.rb')
-rw-r--r-- | ini_read.rb | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/ini_read.rb b/ini_read.rb new file mode 100644 index 0000000..9ae3f1b --- /dev/null +++ b/ini_read.rb | |||
@@ -0,0 +1,91 @@ | |||
1 | require 'optparse' | ||
2 | require 'ostruct' | ||
3 | require "inifile" | ||
4 | |||
5 | class OptParse | ||
6 | def parse(args) | ||
7 | |||
8 | options = OpenStruct.new() | ||
9 | options.inifile = nil | ||
10 | |||
11 | opt_parser = OptionParser.new() do |opts| | ||
12 | |||
13 | opts.banner = "Usage: monitor [options]" | ||
14 | opts.separator "" | ||
15 | |||
16 | opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini| | ||
17 | options.inifile = ini | ||
18 | end | ||
19 | end | ||
20 | opt_parser.parse!(args) | ||
21 | return options | ||
22 | end | ||
23 | end | ||
24 | |||
25 | |||
26 | class Ini_read | ||
27 | attr_reader(:global,:sections) | ||
28 | # attr_reader :global | ||
29 | # attr_accessor :global | ||
30 | def initialize() | ||
31 | options = OptParse.new().parse(ARGV) | ||
32 | find_ini(options.inifile) | ||
33 | read_ini() | ||
34 | parse_config() | ||
35 | end | ||
36 | |||
37 | def find_ini(option_inifile) | ||
38 | if(not option_inifile.nil?) | ||
39 | @inifile = option_inifile | ||
40 | elsif(ENV.has_key?('MONITOR_RC')) | ||
41 | @inifile = ENV['MONITOR_RC'] | ||
42 | elsif(Process.uid == 0) | ||
43 | @inifile = "/etc/monitor.rc" | ||
44 | else | ||
45 | @inifile = ENV['HOME']+"/.monitorrc" | ||
46 | end | ||
47 | end | ||
48 | |||
49 | def read_ini() | ||
50 | @config = IniFile.load(@inifile, :default=>'global') | ||
51 | if(@config.nil?) | ||
52 | puts "Initialization file not found or not readable" | ||
53 | exit | ||
54 | end | ||
55 | end | ||
56 | |||
57 | def each_section() | ||
58 | return @sections | ||
59 | end | ||
60 | |||
61 | def parse_config() | ||
62 | @global = {} | ||
63 | @sections = {} | ||
64 | @config.each_section do |section| | ||
65 | sec = @config[section] | ||
66 | if(section == "global") | ||
67 | @global = sec | ||
68 | end | ||
69 | if(not sec.has_key?('Type')) | ||
70 | puts "Section incomplete, ignored: "+ section | ||
71 | next | ||
72 | elsif(not sec.has_key?('Command')) | ||
73 | puts "Section incomplete, ignored: "+ section | ||
74 | next | ||
75 | end | ||
76 | if(sec['Type'] == "continuous") | ||
77 | if(not sec.has_key?('Buffer')) | ||
78 | sec['Buffer'] = 1000 | ||
79 | end | ||
80 | end | ||
81 | if(sec['Type'] == "oneshot") | ||
82 | if(not sec.has_key?('Periodic')) | ||
83 | sec['Periodic'] = 600 | ||
84 | end | ||
85 | end | ||
86 | @sections[section] = sec | ||
87 | end | ||
88 | end | ||
89 | |||
90 | end | ||
91 | |||