lib/arborist/monitor/fping.rb
author Mahlon E. Smith <mahlon@martini.nu>
Tue, 27 Mar 2018 14:30:38 -0700
changeset 6 f82534b40e06
parent 5 6cf2c60c80f7
child 7 919f139d2931
permissions -rw-r--r--
Remove accidental version bump (untagged), and debug output.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# -*- ruby -*-
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
# vim: set noet nosta sw=4 ts=4 :
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
# encoding: utf-8
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
#
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
# This library parses fping output when provided a list of nodes to
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# monitor.  Require it from your monitor file(s), and call the Arborist
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
# exec() with this class -- that's it.
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
#
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
#    require 'arborist/monitor/fping'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
#
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
#    Arborist::Monitor 'ping check' do
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
#        every 20.seconds
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
#        match type: 'host'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
#        exec 'fping', '-e', '-t', '150'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
#        exec_callbacks( Arborist::Monitor::FPing )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
#    end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
require 'loggability'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
require 'arborist/monitor' unless defined?( Arborist::Monitor )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
# Parse Fping output for better batch ICMP checks.
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
#
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
module Arborist::Monitor::FPing
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
	extend Loggability
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
	log_to :arborist
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
	# The version of this library.
6
f82534b40e06 Remove accidental version bump (untagged), and debug output.
Mahlon E. Smith <mahlon@martini.nu>
parents: 5
diff changeset
    29
	VERSION = '0.1.2'
1
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    30
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    31
	# Always request the node addresses.
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    32
	USED_PROPERTIES = [ :addresses ].freeze
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    33
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    34
	### Return the properties used by this monitor.
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    35
	def self::node_properties
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    36
		return USED_PROPERTIES
69f2ba4d4d93 Use the new #node_properties method.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    37
	end
0
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
	attr_accessor :identifiers
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
5
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    41
	### Arborist::Monitor API: Send addresses to the fping binary, after
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    42
	### creating a map to re-associate them back to identifiers.
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    43
	###
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    44
	def exec_input( nodes, io )
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    45
		self.log.debug "Building fping input for %d nodes" % [ nodes.size ]
0
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
		self.identifiers = nodes.each_with_object({}) do |(identifier, props), hash|
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
			next unless props.key?( 'addresses' )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
			address = props[ 'addresses' ].first
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
			hash[ address ] = identifier
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
		end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
5
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    52
		return if self.identifiers.empty?
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    53
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    54
		self.identifiers.keys.each{|ip| io.puts(ip) }
0
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
	end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
5
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    57
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    58
	### Parse fping output, return a hash of RTT data, along
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    59
	### with any detected errors.
6cf2c60c80f7 Provide fping addresses via pipe, instead of an argument list.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    60
	###
0
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
	def handle_results( pid, stdout, stderr )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
		# 8.8.8.8 is alive (32.1 ms)
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
		# 8.8.4.4 is alive (14.9 ms)
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
		# 8.8.0.1 is unreachable
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
		return stdout.each_line.with_object({}) do |line, hash|
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
			address, remainder = line.split( ' ', 2 )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
			identifier = self.identifiers[ address ] or next
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
			if remainder =~ /is alive \((\d+\.\d+) ms\)/
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
				hash[ identifier ] = { rtt: Float( $1 ) }
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
			else
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
				hash[ identifier ] = { error: remainder.chomp }
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
			end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
		end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
	end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
end # Arborist::Monitor::FPing
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78