aboutsummaryrefslogtreecommitdiff
path: root/ini_read.rb
blob: 9ae3f1ba2a70a47bbd5bb291b272e6781325b42d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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