Class: HQ::GrapherIcingaPerfdata::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/grapher-icinga-perfdata/script.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args.



11
12
13
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 11

def args
  @args
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 12

def status
  @status
end

#stderrObject

Returns the value of attribute stderr.



15
16
17
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 15

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



14
15
16
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 14

def stdout
  @stdout
end

Instance Method Details

#mainObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 17

def main

	process_args

	read_config

	@args.each do
		|filename|
		process_file filename
	end

	@status = 0

end

#parse_data(rest) ⇒ Object



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
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 119

def parse_data rest

	return [] if rest =~ /^\s*$/

	regexp =
		/^
			(?:
				([^' ]+)
			|
				'((?:[^']|'')*)'
			) =
			(-?\d+(?:\.[\d]+)?)
			(?:\S*)
			(?:\s(.+))?
		$/x

	match_data = regexp.match rest

	return nil unless match_data

	name = match_data[1] || match_data[2]
	name.gsub! "''", "'"

	value = match_data[3]

	new_rest = match_data[4]

	return [
		[ name, value ],
		* new_rest ? parse_data(new_rest) : [],
	]

end

#process_argsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 32

def process_args

	@opts, @args =
		Tools::Getopt.process @args, [

		{ :name => :config,
			:required => true },

	]

end

#process_file(filename) ⇒ Object



76
77
78
79
80
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
116
117
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 76

def process_file filename

	File.open filename, "r" do
		|file_io|

		while line = file_io.gets

			timestamp_str, host, service, data_str =
				line.split ",", 4

			timestamp = timestamp_str.to_i

			data = Hash[
				parse_data(data_str),
			]

			mapping_key = {
				host: host,
				service: service,
			}

			mapping = @mappings[mapping_key]

			next unless mapping

			RRD::Wrapper.update \
				"--daemon",
				"%s:%s" % [
					@daemon_elem["host"],
					@daemon_elem["port"],
				],
				"%s.rrd" % mapping[:name],
				[
					timestamp_str,
					* mapping[:values].map { |name| data[name] || "U" },
				].join(":")

		end

	end

end

#read_configObject



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
# File 'lib/hq/grapher-icinga-perfdata/script.rb', line 44

def read_config

	config_doc =
		XML::Document.file @opts[:config]

	@config_elem =
		config_doc.root

	@daemon_elem =
		@config_elem.find_first("daemon")

	@mappings = Hash[
		@config_elem.find("mapping").map {
			|mapping_elem|
			[
				{
					host: mapping_elem["host"],
					service: mapping_elem["service"],
				},
				{
					name: mapping_elem["name"],
					values: mapping_elem.find("value").map {
						|value_elem|
						value_elem["name"]
					}
				},
			]
		}
	]

end