| 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 | # This plugin uses the soft and hard quota data for warning and critical levels |
|---|
| 10 | # respectively. |
|---|
| 11 | # |
|---|
| 12 | # Parameters understood: |
|---|
| 13 | # |
|---|
| 14 | # config (required) |
|---|
| 15 | # |
|---|
| 16 | #%# family=manual |
|---|
| 17 | # |
|---|
| 18 | use strict; |
|---|
| 19 | use warnings; |
|---|
| 20 | |
|---|
| 21 | # We do some magic to allow device strings with underscore to mean a / |
|---|
| 22 | # So to monitor /dev/mapper/vol-foo use quota-usage_mapper_vol-foo |
|---|
| 23 | my @tmp = split(/_/, $0); |
|---|
| 24 | shift @tmp; |
|---|
| 25 | my $dev = join("/", @tmp); |
|---|
| 26 | my %users; |
|---|
| 27 | |
|---|
| 28 | open (REP, "/usr/sbin/repquota /dev/$dev |") or exit 22; |
|---|
| 29 | while (<REP>) { |
|---|
| 30 | chomp; |
|---|
| 31 | if (/^-+$/../^$/) { |
|---|
| 32 | my @data = split(/ +/); |
|---|
| 33 | if ( @data > 2 && $data[2] > 0 ) { |
|---|
| 34 | $users{$data[0]}[0] = $data[2]; # current usage |
|---|
| 35 | $users{$data[0]}[1] = $data[3]; # Soft quota |
|---|
| 36 | $users{$data[0]}[2] = $data[4]; # Hard quota |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | close REP; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | my @sorted_users; |
|---|
| 44 | @sorted_users = sort keys %users; # sorted by username |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | my $user; |
|---|
| 49 | my @value; |
|---|
| 50 | |
|---|
| 51 | if ($ARGV[0] and $ARGV[0] eq "config") { |
|---|
| 52 | print "graph_title Filesystem usage by user (quota based) on $dev\n"; |
|---|
| 53 | print "graph_args --base 1024 --lower-limit 0\n"; |
|---|
| 54 | print "graph_vlabel bytes\n"; |
|---|
| 55 | print "graph_category disk\n"; |
|---|
| 56 | |
|---|
| 57 | foreach (@sorted_users) { |
|---|
| 58 | my $user = $_; |
|---|
| 59 | my ($uid, $name) = (getpwnam($user))[2,6]; |
|---|
| 60 | $user =~ s/-/_/g; |
|---|
| 61 | $name = (split(/,/, $name))[0]; |
|---|
| 62 | printf "%s.label %s\n", $user, $name; |
|---|
| 63 | if ( $users{$user}[1] > 0 ) { |
|---|
| 64 | printf "%s.warning %s\n", $user, $users{$user}[1]; |
|---|
| 65 | } |
|---|
| 66 | if ( $users{$user}[2] > 0 ) { |
|---|
| 67 | printf "%s.critical %s\n", $user, $users{$user}[2]; |
|---|
| 68 | } |
|---|
| 69 | printf "%s.cdef %s,1024,*\n", $user, $user; |
|---|
| 70 | if (($uid < 500)) { |
|---|
| 71 | printf "%s.graph no\n", $user; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | } else { |
|---|
| 75 | foreach (@sorted_users) { |
|---|
| 76 | my $user = $_; |
|---|
| 77 | my $esc = $user; |
|---|
| 78 | $esc =~ s/-/_/g; |
|---|
| 79 | printf "%s.value %s\n", $esc, $users{$user}[0]; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | # vim: set ts=4 sw=4: |
|---|