Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote (SSH) metrics interface gathering #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugin
### Added
- Updated asset build targets to support centos6

### 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This plugin provides native network instrumentation for monitoring and metrics c
* 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
Expand Down
136 changes: 136 additions & 0 deletions bin/metrics-interface-ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#! /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 :excludeinterfaceregex,
description: 'Regex matching interfaces to exclude',
short: '-X INTERFACE',
long: '--exclude-interface-regex'

option :includeinterfaceregex,
description: 'Regex matching interfaces to include',
short: '-I INTERFACE',
long: '--include-interface-regex'

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[:excludeinterfaceregex] && (interface =~ /#{config[:excludeinterfaceregex]}/)
next if config[:includeinterfaceregex] && (interface !~ /#{config[:includeinterfaceregex]}/)
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', '2.0.6'
s.add_runtime_dependency 'whois', '>= 4.0'
s.add_runtime_dependency 'whois-parser', '~> 1.2'
s.add_runtime_dependency 'net-ssh'

s.add_development_dependency 'bundler', '~> 2.1'
s.add_development_dependency 'github-markup', '~> 3.0'
Expand Down