-
Notifications
You must be signed in to change notification settings - Fork 37
/
parser.rb
46 lines (38 loc) · 1.22 KB
/
parser.rb
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
require 'json'
class CollectdData
# ALL PLUGIN CHECKS ARE EXPECTED TO RETURN AN ARRAY OF HASHES OR AN EMPTY ARRAY
Dir[File.dirname(__FILE__) + '/plugins/*.rb'].each do |file|
f = File.basename(file).gsub(/\.rb/, '')
require "backstop/collectd/plugins/#{f}"
end
attr_accessor :data
def initialize(data)
self.data = data
end
def parse
base = parse_base
plugin = parse_plugin
plugin.map {|p| p.merge base}
end
# extract cloud, slot, and id
def parse_base
hostname = data['host'].gsub('DOT','.').gsub('DASH', '-')
parts = hostname.split('.')
id = parts.last
slot = parts[-2]
cloud = parts.first(parts.size-2).join('.')
measure_period = (data['interval'] || 10).to_i
{id: id, slot: slot, cloud: cloud, measure_period: measure_period, measure_time: data['time']}
end
# extract the juicy bits, but do it dynamically
# we check for the existence of a predefined method called parse_plugin_PLUGIN
# if it exists, we dispatch. If it doesn't, we return an empty array
def parse_plugin
plugin = data['plugin']
method = "parse_plugin_#{plugin}".to_sym
unless self.respond_to? method
method = :parse_plugin_generic
end
send(method)
end
end