author | Mahlon E. Smith <mahlon@martini.nu> |
Wed, 30 Aug 2017 13:24:02 -0700 | |
changeset 2 | 1d4b6d1449ed |
parent 1 | 69f2ba4d4d93 |
child 4 | 1801334b8dc4 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- ruby -*- |
2 |
# vim: set noet nosta sw=4 ts=4 : |
|
3 |
# encoding: utf-8 |
|
4 |
# |
|
5 |
# This library parses fping output when provided a list of nodes to |
|
6 |
# monitor. Require it from your monitor file(s), and call the Arborist |
|
7 |
# exec() with this class -- that's it. |
|
8 |
# |
|
9 |
# require 'arborist/monitor/fping' |
|
10 |
# |
|
11 |
# Arborist::Monitor 'ping check' do |
|
12 |
# every 20.seconds |
|
13 |
# match type: 'host' |
|
14 |
# include_down true |
|
15 |
# use :addresses |
|
16 |
# exec 'fping', '-e', '-t', '150' |
|
17 |
# exec_callbacks( Arborist::Monitor::FPing ) |
|
18 |
# end |
|
19 |
||
20 |
||
21 |
require 'loggability' |
|
22 |
require 'arborist/monitor' unless defined?( Arborist::Monitor ) |
|
23 |
||
24 |
# Parse Fping output for better batch ICMP checks. |
|
25 |
# |
|
26 |
module Arborist::Monitor::FPing |
|
27 |
extend Loggability |
|
28 |
log_to :arborist |
|
29 |
||
30 |
# The version of this library. |
|
1
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
31 |
VERSION = '0.1.1' |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
32 |
|
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
33 |
# Always request the node addresses. |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
34 |
USED_PROPERTIES = [ :addresses ].freeze |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
35 |
|
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
36 |
### Return the properties used by this monitor. |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
37 |
def self::node_properties |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
38 |
return USED_PROPERTIES |
69f2ba4d4d93
Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
39 |
end |
0 | 40 |
|
41 |
attr_accessor :identifiers |
|
42 |
||
43 |
def exec_arguments( nodes ) |
|
44 |
self.log.debug "Building fping arguments for %d nodes" % [ nodes.size ] |
|
45 |
self.identifiers = nodes.each_with_object({}) do |(identifier, props), hash| |
|
46 |
next unless props.key?( 'addresses' ) |
|
47 |
address = props[ 'addresses' ].first |
|
48 |
hash[ address ] = identifier |
|
49 |
end |
|
50 |
||
51 |
return {} if self.identifiers.empty? |
|
52 |
return self.identifiers.keys |
|
53 |
end |
|
54 |
||
55 |
def handle_results( pid, stdout, stderr ) |
|
56 |
# 8.8.8.8 is alive (32.1 ms) |
|
57 |
# 8.8.4.4 is alive (14.9 ms) |
|
58 |
# 8.8.0.1 is unreachable |
|
59 |
||
60 |
return stdout.each_line.with_object({}) do |line, hash| |
|
61 |
address, remainder = line.split( ' ', 2 ) |
|
62 |
identifier = self.identifiers[ address ] or next |
|
63 |
||
64 |
if remainder =~ /is alive \((\d+\.\d+) ms\)/ |
|
65 |
hash[ identifier ] = { rtt: Float( $1 ) } |
|
66 |
else |
|
67 |
hash[ identifier ] = { error: remainder.chomp } |
|
68 |
end |
|
69 |
end |
|
70 |
end |
|
71 |
end # Arborist::Monitor::FPing |
|
72 |