Method: Perus::Pinger::Pinger#load_config

Defined in:
lib/perus/pinger/pinger.rb

#load_configObject


configuration




96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/perus/pinger/pinger.rb', line 96

def load_config
    if Pinger.options.system_id.nil?
        config_path = URI("systems/config_for_ip")
    else
        config_path = URI("systems/#{Pinger.options.system_id}/config")
    end

    config_url = (@server_uri + config_path).to_s

    # load the system config by requesting it from the perus server
    tries = 0
    json = begin
        JSON.parse(RestClient.get(config_url))
    rescue => e
        if tries < 5
            tries += 1
            sleep 2
            retry
        else
            raise e
        end 
    end

    json['metrics'] ||= []
    json['actions'] ||= []
    @system_id = json['id']

    if @system_id == 'nil'
        raise 'This system is unknown to the server'
    end

    # load metric and command modules based on the config
    json['metrics'].each do |config|
        begin
            if ::Perus::Pinger.const_defined?(config['type'])
                metric = ::Perus::Pinger.const_get(config['type'])
                @metric_errors[metric.name] ||= []
                @metrics << metric.new(config['options'])
            else
                @metric_errors[config['type']] = format_exception(e)
            end
        rescue => e
            @metric_errors[metric.name] << format_exception(e)
        end
    end

    json['actions'].each do |config|
        begin
            command = ::Perus::Pinger.const_get(config['type'])
            @actions << command.new(config['options'], config['id'])
        rescue => e
            if config['id']
                @action_results[config['id']] = format_exception(e)
            else
                puts 'Error - action does not have an associated id'
                p config
            end
        end
    end
end