X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ini_read.rb;fp=ini_read.rb;h=9ae3f1ba2a70a47bbd5bb291b272e6781325b42d;hb=332497b1a92f4257eea0c9257d041c8898c91c31;hp=0000000000000000000000000000000000000000;hpb=7a1b77020cc139cc9240cd2c7ed2ae8990613d66;p=perso%2FImmae%2FProjets%2FRuby%2FMonitor.git 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 @@ +require 'optparse' +require 'ostruct' +require "inifile" + +class OptParse + def parse(args) + + options = OpenStruct.new() + options.inifile = nil + + opt_parser = OptionParser.new() do |opts| + + opts.banner = "Usage: monitor [options]" + opts.separator "" + + opts.on( '-f', "-f ini_file", "chose a different initialization file") do |ini| + options.inifile = ini + end + end + opt_parser.parse!(args) + return options + end +end + + +class Ini_read + attr_reader(:global,:sections) +# attr_reader :global +# attr_accessor :global + def initialize() + options = OptParse.new().parse(ARGV) + find_ini(options.inifile) + read_ini() + parse_config() + end + + def find_ini(option_inifile) + if(not option_inifile.nil?) + @inifile = option_inifile + elsif(ENV.has_key?('MONITOR_RC')) + @inifile = ENV['MONITOR_RC'] + elsif(Process.uid == 0) + @inifile = "/etc/monitor.rc" + else + @inifile = ENV['HOME']+"/.monitorrc" + end + end + + def read_ini() + @config = IniFile.load(@inifile, :default=>'global') + if(@config.nil?) + puts "Initialization file not found or not readable" + exit + end + end + + def each_section() + return @sections + end + + def parse_config() + @global = {} + @sections = {} + @config.each_section do |section| + sec = @config[section] + if(section == "global") + @global = sec + end + if(not sec.has_key?('Type')) + puts "Section incomplete, ignored: "+ section + next + elsif(not sec.has_key?('Command')) + puts "Section incomplete, ignored: "+ section + next + end + if(sec['Type'] == "continuous") + if(not sec.has_key?('Buffer')) + sec['Buffer'] = 1000 + end + end + if(sec['Type'] == "oneshot") + if(not sec.has_key?('Periodic')) + sec['Periodic'] = 600 + end + end + @sections[section] = sec + end + end + +end +