root / plugins / mail / procmail_ @ 430d68ff
Historique | Voir | Annoter | Télécharger (2,46 ko)
| 1 | dae1d3ef | Gunnar Wolf | #!/usr/bin/perl |
|---|---|---|---|
| 2 | use strict; |
||
| 3 | use warnings; |
||
| 4 | use YAML; |
||
| 5 | |||
| 6 | my (%conf, $state, $have_read, $totlines); |
||
| 7 | |||
| 8 | %conf = (logfile => $ENV{LOGFILE} || '/home/user/.procmail/log',
|
||
| 9 | state => $ENV{STATEFILE} || '/var/lib/munin/plugin-state/munin-plugin-procmail.state');
|
||
| 10 | |||
| 11 | $state = YAML::LoadFile($conf{state});
|
||
| 12 | $have_read = 0; |
||
| 13 | |||
| 14 | unless (-f $conf{logfile} and -r $conf{logfile}) {
|
||
| 15 | die "$conf{logfile} does not exist or is not readable!" ;
|
||
| 16 | } |
||
| 17 | |||
| 18 | if (@ARGV and $ARGV[0] eq 'autoconf') {
|
||
| 19 | print "yes\n"; |
||
| 20 | } elsif (@ARGV and $ARGV[0] eq 'config') {
|
||
| 21 | print config(); |
||
| 22 | } else {
|
||
| 23 | print fetch(); |
||
| 24 | # Update the offset, clear the numbers - Only when fetching! |
||
| 25 | $state->{offset} += $have_read;
|
||
| 26 | $state->{folders}{$_} = 0 foreach keys %{$state->{folders}};
|
||
| 27 | } |
||
| 28 | |||
| 29 | YAML::DumpFile($conf{state}, $state);
|
||
| 30 | exit 0; |
||
| 31 | |||
| 32 | sub config {
|
||
| 33 | # If config is ever called without having first a successful run, it should not die! |
||
| 34 | $state->{folders} ||= {};
|
||
| 35 | |||
| 36 | print "graph_title Destination folders for Postfix\n", |
||
| 37 | "graph_vlabel Received mails\n", |
||
| 38 | "graph_category Mail\n", |
||
| 39 | "graph_info Gives you the total mails stored by Postfix by folder\n"; |
||
| 40 | print "$_.label $_\n" foreach sort keys %{$state->{folders}};
|
||
| 41 | } |
||
| 42 | |||
| 43 | sub fetch {
|
||
| 44 | my ($fh); |
||
| 45 | |||
| 46 | if ($fh = open_log()) {
|
||
| 47 | $state->{folders} ||= {};
|
||
| 48 | |||
| 49 | while (my $folder = next_used_folder($fh)) {
|
||
| 50 | $state->{folders}{$folder}++;
|
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | print "$_.value $state->{folders}{$_}\n" foreach sort keys %{$state->{folders}};
|
||
| 55 | } |
||
| 56 | |||
| 57 | sub next_used_folder {
|
||
| 58 | my ($fh); |
||
| 59 | $fh = shift; |
||
| 60 | while (my $lin = <$fh>) {
|
||
| 61 | $have_read++; |
||
| 62 | next unless $lin =~ m!^\s*Folder: ([^\s/]+)!; |
||
| 63 | next unless $1; |
||
| 64 | return $1; |
||
| 65 | } |
||
| 66 | return undef; |
||
| 67 | } |
||
| 68 | |||
| 69 | sub open_log {
|
||
| 70 | my ($fh, $offset, $lines_to_read); |
||
| 71 | $offset = get_log_offset(); |
||
| 72 | get_log_size(); |
||
| 73 | |||
| 74 | $lines_to_read = $totlines - $offset; |
||
| 75 | open($fh, "tail -$lines_to_read $conf{logfile}|") or die $!;
|
||
| 76 | |||
| 77 | return $fh; |
||
| 78 | } |
||
| 79 | |||
| 80 | sub get_log_size {
|
||
| 81 | my $tot = `wc -l $conf{logfile}`;
|
||
| 82 | $tot =~ /^(\d+) /; |
||
| 83 | return $totlines = $1; |
||
| 84 | } |
||
| 85 | |||
| 86 | sub get_log_offset {
|
||
| 87 | my ($size); |
||
| 88 | # The offset is expressed as the number of lines to skip. We get to that |
||
| 89 | # point getting the total log size (get_log_size) and using tail for the |
||
| 90 | # difference. If the offset is larger than the file itself, we get it |
||
| 91 | # whole (it might have just been rotated). |
||
| 92 | $size = get_log_size(); |
||
| 93 | |||
| 94 | $state->{offset} ||= 0;
|
||
| 95 | $state->{offset} = 0 if $size < $state->{offset};
|
||
| 96 | |||
| 97 | return $state->{offset};
|
||
| 98 | } |
||
| 99 | |||
| 100 | sub hush {
|
||
| 101 | return unless $ENV{FOLLOW};
|
||
| 102 | warn @_; |
||
| 103 | } |
