forked from Pixelmatic/sensu-plugins-bandwidth-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics-bandwidth-usage.rb
63 lines (53 loc) · 1.2 KB
/
metrics-bandwidth-usage.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
#! /usr/bin/env ruby
# encoding: UTF-8
#
# metrics-bandwidth-usage
#
# DESCRIPTION:
# This plugins uses ifstat to collect bandwidth usage on eth0 interface.
# Metrics are in KB/s.
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: socket
#
# USAGE:
# ./metrics-bandwidth-usage.rb
#
# LICENSE:
# Copyright 2016 Pixelmatic, Inc <[email protected]>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/metric/cli'
require 'socket'
class BandwidthUsageMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme, text to prepend to .$parent.$child',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.bandwidth_usage"
def run
metrics = metrics_hash
metrics.each do |k, v|
output "#{config[:scheme]}.#{k}", v
end
ok
end
def metrics_hash
metrics = {}
i = 0
`ifstat -i eth0 1 1`.each_line do |line|
i += 1
next if i < 3
metrics['download'] = line.strip.split(/\s+/)[0].to_f
metrics['upload'] = line.strip.split(/\s+/)[1].to_f
end
metrics
end
end