Skip to content

Commit

Permalink
Add remote (SSH) metrics interface gathering
Browse files Browse the repository at this point in the history
This script, very similar to metrics-interface.rb, allows to collect
metrics on remote host by SSH.

This is meant to be able to collect metrics on host that cannot
necessarily run sensu client, such as small routers for instance.
  • Loading branch information
CoRfr committed Jul 15, 2019
1 parent 1bd41c9 commit c158644
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
124 changes: 124 additions & 0 deletions bin/metrics-interface-ssh.rb
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# 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
2 changes: 1 addition & 1 deletion bin/metrics-interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

#
# interface-metrics
# metrics-interface
#
# DESCRIPTION:
#
Expand Down
1 change: 1 addition & 0 deletions sensu-plugins-network-checks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit c158644

Please sign in to comment.