lib/arborist/monitor/fping.rb
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 30 Aug 2017 13:48:56 -0700
changeset 3 1c195ed0a049
parent 1 69f2ba4d4d93
child 4 1801334b8dc4
permissions -rw-r--r--
Actually add the necessary files, this time.
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
#        include_down true
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
#        use :addresses
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
#        exec 'fping', '-e', '-t', '150'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
#        exec_callbacks( Arborist::Monitor::FPing )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
#    end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
require 'loggability'
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
require 'arborist/monitor' unless defined?( Arborist::Monitor )
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
# Parse Fping output for better batch ICMP checks.
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
#
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
module Arborist::Monitor::FPing
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
	extend Loggability
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
	log_to :arborist
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    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
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
	attr_accessor :identifiers
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
	def exec_arguments( nodes )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
		self.log.debug "Building fping arguments for %d nodes" % [ nodes.size ]
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
		self.identifiers = nodes.each_with_object({}) do |(identifier, props), hash|
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
			next unless props.key?( 'addresses' )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
			address = props[ 'addresses' ].first
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
			hash[ address ] = identifier
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
		end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
		return {} if self.identifiers.empty?
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
		return self.identifiers.keys
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
	end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
	def handle_results( pid, stdout, stderr )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
		# 8.8.8.8 is alive (32.1 ms)
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
		# 8.8.4.4 is alive (14.9 ms)
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
		# 8.8.0.1 is unreachable
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
		return stdout.each_line.with_object({}) do |line, hash|
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
			address, remainder = line.split( ' ', 2 )
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
			identifier = self.identifiers[ address ] or next
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
			if remainder =~ /is alive \((\d+\.\d+) ms\)/
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
				hash[ identifier ] = { rtt: Float( $1 ) }
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
			else
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
				hash[ identifier ] = { error: remainder.chomp }
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
			end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
		end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
	end
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
end # Arborist::Monitor::FPing
d99e1dffbc72 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72