| 1 |
#!/usr/bin/perl |
|---|
| 2 |
# |
|---|
| 3 |
# Plugin to monitor quota on a specified device. Needs repquota and root privs |
|---|
| 4 |
# |
|---|
| 5 |
# Usage: place in /etc/munin/node.d/quota-usage_<dev> (or link it there using |
|---|
| 6 |
# ln -s), for example quota-usage_hda3. Use underscores instead of slashes, for |
|---|
| 7 |
# example to monitor /dev/mapper/vol-foo, name this quota-usage_mapper_vol-foo |
|---|
| 8 |
# |
|---|
| 9 |
# Parameters understood: |
|---|
| 10 |
# |
|---|
| 11 |
# config (required) |
|---|
| 12 |
# |
|---|
| 13 |
#%# family=manual |
|---|
| 14 |
|
|---|
| 15 |
use strict; |
|---|
| 16 |
use warnings; |
|---|
| 17 |
|
|---|
| 18 |
# We do some magic to allow device strings with underscore to mean a / |
|---|
| 19 |
# So to monitor /dev/mapper/vol-foo use quota-usage_mapper_vol-foo |
|---|
| 20 |
my @tmp = split(/_/, $0); |
|---|
| 21 |
shift @tmp; |
|---|
| 22 |
my $dev = join("/", @tmp); |
|---|
| 23 |
my %users; |
|---|
| 24 |
|
|---|
| 25 |
open (REP, "/usr/sbin/repquota /dev/$dev |") or exit 22; |
|---|
| 26 |
while (<REP>) { |
|---|
| 27 |
chomp; |
|---|
| 28 |
if (/^-+$/../^$/) { |
|---|
| 29 |
my @data = split(/ +/); |
|---|
| 30 |
$users{$data[0]} = $data[2] if (@data > 2 && $data[2] > 0); |
|---|
| 31 |
} |
|---|
| 32 |
} |
|---|
| 33 |
close REP; |
|---|
| 34 |
|
|---|
| 35 |
my @users_by_usage = sort { $users{$b} <=> $users{$a} } keys(%users); |
|---|
| 36 |
|
|---|
| 37 |
if ($ARGV[0] and $ARGV[0] eq "config") { |
|---|
| 38 |
print "graph_title Filesystem usage by user on $dev\n"; |
|---|
| 39 |
print "graph_args --base 1024 --lower-limit 0\n"; |
|---|
| 40 |
print "graph_vlabel bytes\n"; |
|---|
| 41 |
print "graph_category disk\n"; |
|---|
| 42 |
for my $user (@users_by_usage) { |
|---|
| 43 |
my ($uid, $name) = (getpwnam($user))[2,6]; |
|---|
| 44 |
$name = $user if (length($name) < length($user)); |
|---|
| 45 |
$user =~ s/-/_/g; |
|---|
| 46 |
$name = (split(/,/, $name))[0]; |
|---|
| 47 |
printf "%s.label %s\n", $user, $name; |
|---|
| 48 |
printf "%s.cdef %s,1024,*\n", $user, $user; |
|---|
| 49 |
printf "%s.graph no\n", $user if ($uid < 1000); |
|---|
| 50 |
} |
|---|
| 51 |
} else { |
|---|
| 52 |
for my $user (@users_by_usage) { |
|---|
| 53 |
my $esc = $user; |
|---|
| 54 |
$esc =~ s/-/_/g; |
|---|
| 55 |
printf "%s.value %s\n", $esc, $users{$user}; |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
# vim: set ts=4 sw=4: |
|---|