Ticket #143: quota-usage

File quota-usage, 2.5 kB (added by raszi, 2 months ago)

working with the new munin

Line 
1 #!/usr/bin/perl -W
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
60                         my ($login, $pass, $uid, $gid, $name, $home, $gecos) = getpwnam($user) or next;
61             next if (($uid < 500));
62
63                         my $esc = $user;
64             $esc =~ s/-/_/g;
65
66                         my $fullname = "";
67                         if (defined($name) && length($name) > 0) {
68                                 $fullname = $name;
69                         } elsif (defined($gecos) && length($gecos) > 0) {
70                                 $fullname = (split(/,/, $gecos))[0];
71                         }
72
73             printf "%s.label %s\n", $esc, $fullname;
74             if ( $users{$user}[1] > 0 ) {
75                 printf "%s.warning %s\n", $esc, $users{$user}[1];
76             }
77             if ( $users{$user}[2] > 0 ) {
78                 printf "%s.critical %s\n", $esc, $users{$user}[2];
79             }
80             printf "%s.cdef %s,1024,*\n", $esc, $esc;
81         }
82 } else {
83         foreach (@sorted_users) {
84             my $user = $_;
85             my $esc = $user;
86             $esc =~ s/-/_/g;
87             printf "%s.value %s\n", $esc, $users{$user}[0];
88         }
89 }
90
91 # vim: set ts=4 sw=4:
92