-
Notifications
You must be signed in to change notification settings - Fork 15
/
metrics-snmp.rb
executable file
·95 lines (84 loc) · 2.56 KB
/
metrics-snmp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby
# SNMP Metrics
# ===
#
# This is a simple script to collect metrics from a SNMP OID value
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: snmp
#
# USAGE:
#
# check-snmp -h host -C community -O oid -p prefix -s suffix
#
# LICENSE:
# Copyright (c) 2013 Double Negative Limited
# Based on check-snmp.rb by Deepak Mohan Das <deepakmdass88@gmail.com>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'sensu-plugin/metric/cli'
require 'snmp'
# Class that collects and outputs SNMP metrics in graphite format
class SNMPGraphite < Sensu::Plugin::Metric::CLI::Graphite
option :host,
short: '-h host',
boolean: true,
default: '127.0.0.1',
required: true
option :community,
short: '-C snmp community',
boolean: true,
default: 'public'
option :objectid,
short: '-O OID',
default: '1.3.6.1.4.1.2021.10.1.3.1',
required: true
option :prefix,
short: '-p prefix',
description: 'prefix to attach to graphite path'
option :suffix,
short: '-s suffix',
description: 'suffix to attach to graphite path',
required: true
option :snmp_version,
short: '-v version',
description: 'SNMP version to use (SNMPv1, SNMPv2c (default))',
default: 'SNMPv2c'
option :graphite,
short: '-g',
description: 'Replace dots with underscores in hostname',
boolean: true
option :mibdir,
short: '-d mibdir',
description: 'Full path to custom MIB directory.'
option :mibs,
short: '-l mib[,mib,mib...]',
description: 'Custom MIBs to load (from custom mib path).',
default: ''
def run
mibs = config[:mibs].split(',')
begin
manager = SNMP::Manager.new(host: config[:host].to_s, community: config[:community].to_s, version: config[:snmp_version].to_sym)
if config[:mibdir] && !mibs.empty?
manager.load_modules(mibs, config[:mibdir])
end
response = manager.get([config[:objectid].to_s])
rescue SNMP::RequestTimeout
unknown "#{config[:host]} not responding"
rescue => e
unknown "An unknown error occured: #{e.inspect}"
end
config[:host] = config[:host].tr('.', '_') if config[:graphite]
response.each_varbind do |vb|
if config[:prefix]
output "#{config[:prefix]}.#{config[:host]}.#{config[:suffix]}", vb.value.to_f
else
output "#{config[:host]}.#{config[:suffix]}", vb.value.to_f
end
end
manager.close
ok
end
end