diff --git a/CHANGELOG.md b/CHANGELOG.md index 47c8669..b3e6945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugin ## [Unreleased] +### Added +- metrics-interface-ssh.rb: new script to collect interface metrics remotely (@CoRfr) + ## [4.0.0] - 2019-04-18 ### Breaking Changes - Update minimum required ruby version to 2.3. Drop unsupported ruby versions. diff --git a/README.md b/README.md index 7aaebe2..dbc70a1 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ * bin/check-whois-domain-expiration-multi.rb * bin/check-whois-domain-expiration.rb * bin/metrics-interface.rb + * bin/metrics-interface-ssh.rb * bin/metrics-net.rb * bin/metrics-netif.rb * bin/metrics-netstat-tcp.rb diff --git a/bin/metrics-interface-ssh.rb b/bin/metrics-interface-ssh.rb new file mode 100755 index 0000000..5df2394 --- /dev/null +++ b/bin/metrics-interface-ssh.rb @@ -0,0 +1,124 @@ +#! /usr/bin/env ruby +# encoding: UTF-8 +# +# metrics-interface-ssh +# +# DESCRIPTION: +# unlike metrics-interface, this gather metrics from interfaces +# of an host reacheable through SSH. +# typically this can be used to track metrics for an host which is not +# able to run sensu directly (dd-wrt, etc ...) +# +# OUTPUT: +# metric data +# +# PLATFORMS: +# Linux +# +# DEPENDENCIES: +# gem: sensu-plugin +# +# USAGE: +# +# NOTES: +# +# LICENSE: +# Copyright 2012 Sonian, Inc +# Released under the same terms as Sensu (the MIT license); see LICENSE +# for details. +# + +require 'sensu-plugin/metric/cli' +require 'socket' +require 'net/ssh' + +# +# Interface Graphite +# +class InterfaceGraphite < Sensu::Plugin::Metric::CLI::Graphite + option :scheme, + description: 'Metric naming scheme, text to prepend to metric', + short: '-s SCHEME', + long: '--scheme SCHEME' + + option :excludeinterface, + description: 'List of interfaces to exclude', + short: '-x INTERFACE[,INTERFACE]', + long: '--exclude-interface', + proc: proc { |a| a.split(',') }, + default: ["lo"] + + option :includeinterface, + description: 'List of interfaces to include', + short: '-i INTERFACE[,INTERFACE]', + long: '--include-interface', + proc: proc { |a| a.split(',') } + + option :host, + description: 'Remove host', + short: '-h HOST', + long: '--host HOST', + default: '192.168.0.1' + + option :port, + description: 'Remote SSH port', + short: '-p PORT', + long: '--port PORT', + default: 22 + + option :user, + description: 'Remote SSH username', + short: '-u USER', + long: '--user USER', + default: 'root' + + def run + # Metrics borrowed from hoardd: https://github.com/coredump/hoardd + + metrics = %w[rxBytes + rxPackets + rxErrors + rxDrops + rxFifo + rxFrame + rxCompressed + rxMulticast + txBytes + txPackets + txErrors + txDrops + txFifo + txColls + txCarrier + txCompressed] + + output = nil + + Net::SSH.start(config[:host], config[:user], :port => config[:port]) do |ssh| + output = ssh.exec!("cat /proc/net/dev") + end + + return if not output + + if not config[:scheme] + config[:scheme] = "#{config[:host].tr('.','_')}.interface" + end + + output.each_line do |line| + interface, stats_string = line.scan(/^\s*([^:]+):\s*(.*)$/).first + next if config[:excludeinterface] && config[:excludeinterface].find { |x| line.match(x) } + next if config[:includeinterface] && !(config[:includeinterface].find { |x| line.match(x) }) + next unless interface + if interface.is_a?(String) + interface = interface.tr('.', '_') + end + + stats = stats_string.split(/\s+/) + next if stats == ['0'].cycle.take(stats.size) + + metrics.size.times { |i| output "#{config[:scheme]}.#{interface}.#{metrics[i]}", stats[i] } + end + + ok + end +end diff --git a/bin/metrics-interface.rb b/bin/metrics-interface.rb index 8503865..9f851b1 100755 --- a/bin/metrics-interface.rb +++ b/bin/metrics-interface.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true # -# interface-metrics +# metrics-interface # # DESCRIPTION: # diff --git a/sensu-plugins-network-checks.gemspec b/sensu-plugins-network-checks.gemspec index cc296dc..298411b 100644 --- a/sensu-plugins-network-checks.gemspec +++ b/sensu-plugins-network-checks.gemspec @@ -39,6 +39,7 @@ Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength s.add_runtime_dependency 'net-ping', '1.7.8' s.add_runtime_dependency 'whois', '>= 4.0' s.add_runtime_dependency 'whois-parser', '~> 1.0.0' + s.add_runtime_dependency 'net-ssh' s.add_development_dependency 'bundler', '~> 1.7' s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'