Class: Perus::Pinger::Pinger

Inherits:
Object
  • Object
show all
Defined in:
lib/perus/pinger/pinger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_path = DEFAULT_PINGER_OPTIONS_PATH) ⇒ Pinger

Returns a new instance of Pinger.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/perus/pinger/pinger.rb', line 49

def initialize(options_path = DEFAULT_PINGER_OPTIONS_PATH)
    Pinger.options.load(options_path, DEFAULT_PINGER_OPTIONS)

    # cache urls on initialisation since the urls depend on values known
    # at startup and that won't change over the object lifetime
    config_path = URI("systems/#{Pinger.options.system_id}/config")
    pinger_path = URI("systems/#{Pinger.options.system_id}/ping")
    server_uri  = URI(Pinger.options.server)

    @config_url = (server_uri + config_path).to_s
    @pinger_url = (server_uri + pinger_path).to_s

    @metrics = []
    @metric_results = {}
    @metric_errors = {}

    @actions = []
    @action_results = {}
    @late_actions = []
end

Class Method Details

.optionsObject



45
46
47
# File 'lib/perus/pinger/pinger.rb', line 45

def self.options
    @options ||= Perus::Options.new
end

Instance Method Details

#add_to_payload(payload, field, results) ⇒ Object


response




160
161
162
163
164
165
166
167
168
169
# File 'lib/perus/pinger/pinger.rb', line 160

def add_to_payload(payload, field, results)
    results.each do |name, val|
        next unless val.instance_of?(File)
        uuid = SecureRandom.uuid
        results[name] = {file: uuid}
        payload[uuid] = val
    end

    payload[field] = JSON.dump(results)
end

#cleanupObject


cleanup




197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/perus/pinger/pinger.rb', line 197

def cleanup
    @metrics.each do |metric|
        begin
            metric.cleanup
        rescue => e
            puts 'Error running metric cleanup'
            puts format_exception(e)
        end
    end

    @actions.each do |action|
        begin
            action.cleanup
        rescue => e
            puts 'Error running action cleanup'
            puts format_exception(e)
        end
    end

    @late_actions.each do |code|
        begin
            code.call
        rescue => e
            puts 'Error running late action'
            puts format_exception(e)
        end
    end
end

#format_exception(e) ⇒ Object


run




120
121
122
123
124
125
126
# File 'lib/perus/pinger/pinger.rb', line 120

def format_exception(e)
    if e.backtrace.empty?
        e.inspect
    else
        "#{e.inspect}\n#{e.backtrace.first}"
    end
end

#load_configObject


configuration




81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/perus/pinger/pinger.rb', line 81

def load_config
    # load the system config by requesting it from the perus server
    json = JSON.parse(RestClient.get(@config_url))
    json['metrics'] ||= []
    json['actions'] ||= []

    # 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

#runObject



70
71
72
73
74
75
76
# File 'lib/perus/pinger/pinger.rb', line 70

def run
    load_config
    run_metrics
    run_actions
    send_response
    cleanup
end

#run_actionsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/perus/pinger/pinger.rb', line 139

def run_actions
    @actions.each do |action|
        begin
            result = action.run

            if result.instance_of?(Proc)
                @late_actions << result
                result = true
            end

            @action_results[action.id] = result

        rescue => e
            @action_results[action.id] = format_exception(e)
        end
    end
end

#run_metricsObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/perus/pinger/pinger.rb', line 128

def run_metrics
    @metrics.each do |metric|
        begin
            result = metric.run
            @metric_results.merge!(result)
        rescue => e
            @metric_errors[metric.class.name] << format_exception(e)
        end
    end
end

#send_responseObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/perus/pinger/pinger.rb', line 171

def send_response
    # prepare the response and replace file results with a reference
    # to the uploaded file. files are sent as top level parameters in
    # the payload, while metric and action results are sent as a json
    # object with a reference to these files.
    payload = {}
    add_to_payload(payload, 'metrics', @metric_results)
    add_to_payload(payload, 'actions', @action_results)

    # metric_errors is created with a key for each metric type. most
    # metrics should run without any errors, so remove these entries
    # before adding errors to the payload.
    @metric_errors.reject! {|metric, errors| errors.empty?}
    add_to_payload(payload, 'metric_errors', @metric_errors)

    begin
        RestClient.post(@pinger_url, payload)
    rescue => e
        puts 'Ping failed with exception'
        puts format_exception(e)
    end
end