File: quota_usage_.in

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (121 lines) | stat: -rw-r--r-- 3,285 bytes parent folder | download | duplicates (5)
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
#!@@PERL@@
#

=head1 NAME

quota_usage_ - Plugin to monitor quota on a specified device.

=head1 APPLICABLE SYSTEMS

Needs repquota and root privs

=head1 USAGE

Create Service Link /etc/munin/plugins/quota_usage_<dev>
for example C<quota_usage_hda3>. Use underscores instead of slashes, for
example to monitor C</dev/mapper/vol-foo>, name this C<quota_usage_mapper_vol-foo>

For backwards compatibility the prefix C<quota-usage_> is also acceptable
(instead of C<quota_usage_>).

This plugin uses the soft and hard quota data for warning and critical
levels respectively.

=head1 MAGIC MARKERS

  #%# family=manual

=head1 AUTHOR

Unknown author

Source: L<https://svn.koumbit.net/koumbit/trunk/munin-plugins/quota-usage>

L<http://munin-monitoring.org/attachment/ticket/143/quota-usage>

=head1 LICENSE

GPLv2

=cut

use strict;
use warnings;
use Munin::Plugin;
use File::Spec::Functions qw(splitdir);

# We do some magic to allow device strings with underscore to mean a /
# So to monitor /dev/mapper/vol-foo use "quota_usage_mapper_vol-foo" or
# "quota-usage_mapper_vol-foo". The latter was advertised as the default usage previously, but it
# is confusing since it does not match the name of the plugin exactly.
# See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=730030
my @path_tokens = splitdir($0);
my $basename = $path_tokens[-1];
# partial name of the device (it will be prefixed with "/dev/")
my $dev;
if ($basename =~ /^quota_usage_(.+)$/) {
	# The plugin was called with the basename "quota_usage_*".
	$dev = $1;
	$dev =~ s#_#/#g;
} else {
	# The plugin was called with the basename "quota-usage_*" (or another prefix without an
	# underscore).
	# This approach was documented for this plugin. But it conflicts with the common style of
	# using the exact plugin name (instead of a slight variation) for symlinks.
	my @tmp = split(/_/, $basename);
	shift @tmp;
	$dev = join("/", @tmp);
}
my %users;

open (REP, "/usr/sbin/repquota /dev/$dev |") or exit 22;
while (<REP>) {
	chomp;
	if (/^-+$/../^$/) {
		my @data = split(/ +/);

		next if !$data[0] || $data[0] =~ /^#/;

		if ( @data > 2 && $data[2] > 0 ) {
		  $users{$data[0]}[0] = $data[2]; # current usage
		  $users{$data[0]}[1] = $data[3]; # Soft quota
		  $users{$data[0]}[2] = $data[4]; # Hard quota
		}
	}
}
close REP;

my @users_by_usage = sort { $users{$b} <=> $users{$a} } keys(%users);

if ($ARGV[0] and $ARGV[0] eq "config") {
	print "graph_title Filesystem usage by user on $dev\n";
	print "graph_args --base 1024 --lower-limit 0\n";
	print "graph_vlabel bytes\n";
	print "graph_category disk\n";
	for my $user (@users_by_usage) {
		my ($uid, $name) = (getpwnam($user))[2,6];
		my $id = clean_fieldname($user);
		$name = $user if (length($name) < length($user));
		$name = (split(/,/, $name))[0];

		printf "%s.label %s\n", $id, $name;
		printf "%s.cdef %s,1024,*\n", $id, $id;

		if ($users{$user}[1] && $users{$user}[1] > 0) {
		  printf "%s.warning %s\n", $id, $users{$user}[1];
		}
		if ($users{$user}[2] && $users{$user}[2] > 0) {
		  printf "%s.critical %s\n", $id, $users{$user}[2];
		}

		if ($uid < 200) {
		  printf "%s.graph no\n", $id
		}
	}
} else {
	for my $user (@users_by_usage) {
		my $esc = $user;
		$esc = clean_fieldname($esc);
		printf "%s.value %s\n", $esc, $users{$user}[0];
	}
}