metrics-disk-usage.rb
4.05 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/env ruby
# encoding: UTF-8
#
# disk-usage-metrics
#
# DESCRIPTION:
# This plugin uses df to collect disk capacity metrics
# disk-usage-metrics.rb looks at /proc/stat which doesnt hold capacity metricss.
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: socket
#
# USAGE:
#
# NOTES:
# Based on disk-capacity-metrics.rb by bhenerey and nstielau
# The difference here being how the key is defined in graphite and the
# size we emit to graphite(now using megabytes). Also i dropped inode info.
# Using this as an example
# Filesystem Size Used Avail Use% Mounted on
# /dev/mapper/precise64-root 79G 3.5G 72G 5% /
# /dev/sda1 228M 25M 192M 12% /boot
# /dev/sdb1 99G 2G 97G 2% /media/sda1
# The keys with this plugin will be
# disk_usage.root, disk_usage.root.boot, and disk_usage.root.media.sda1
# instead of disk.dev.mapper.precise64-root, disk.sda1, and disk.sda2
#
# Use --flatten option to reduce graphite "tree" by using underscores rather
# then dots for subdirs. Also eliminates 'root' on mounts other than '/'.
# Keys with --flatten option would be
# disk_usage.root, disk_usage.boot, and disk_usage.media_sda1
#
# Mountpoints can be specifically included or ignored using -i or -I options:
# e.g. disk-usage-metric.rb -i ^/boot,^/media
#
# LICENSE:
# Copyright 2012 Sonian, Inc <chefs@sonian.net>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/metric/cli'
require 'socket'
#
# Disk Usage Metrics
#
class DiskUsageMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme, text to prepend to .$parent.$child',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.disk_usage"
option :ignore_mnt,
description: 'Ignore mounts matching pattern(s)',
short: '-i MNT[,MNT]',
long: '--ignore-mount',
proc: proc { |a| a.split(',') }
option :include_mnt,
description: 'Include only mounts matching pattern(s)',
short: '-I MNT[,MNT]',
long: '--include-mount',
proc: proc { |a| a.split(',') }
option :flatten,
description: 'Output mounts with underscore rather than dot',
short: '-f',
long: '--flatten',
boolean: true,
default: false
option :local,
description: 'Only check local filesystems (df -l option)',
short: '-l',
long: '--local',
boolean: true,
default: false
option :block_size,
description: 'Set block size for sizes printed',
short: '-B BLOCK_SIZE',
long: '--block-size BLOCK_SIZE',
default: 'M'
# Main function
#
def run
delim = config[:flatten] == true ? '_' : '.'
# Get disk usage from df with used and avail in megabytes
# #YELLOW
`df -PB#{config[:block_size]} #{config[:local] ? '-l' : ''}`.split("\n").drop(1).each do |line|
_, _, used, avail, used_p, mnt = line.split
unless %r{/sys[/|$]|/dev[/|$]|/run[/|$]} =~ mnt
next if config[:ignore_mnt] && config[:ignore_mnt].find { |x| mnt.match(x) }
next if config[:include_mnt] && !config[:include_mnt].find { |x| mnt.match(x) }
mnt = if config[:flatten]
mnt.eql?('/') ? 'root' : mnt.gsub(/^\//, '')
else
# If mnt is only / replace that with root if its /tmp/foo
# replace first occurance of / with root.
mnt.length == 1 ? 'root' : mnt.gsub(/^\//, 'root.')
end
# Fix subsequent slashes
mnt = mnt.gsub '/', delim
output [config[:scheme], mnt, 'used'].join('.'), used.gsub(config[:block_size], '')
output [config[:scheme], mnt, 'avail'].join('.'), avail.gsub(config[:block_size], '')
output [config[:scheme], mnt, 'used_percentage'].join('.'), used_p.delete('%')
end
end
ok
end
end