Paul J Stevens wrote:
> How do you 'count' connections? lsof, netstat, ps??
>   
Attached find the script used:

I'm not saying it's _pretty_ (it's also from ~2.5 years ago) but it
gives the idea of what I'm doing.
#!/usr/bin/perl

use strict;
use warnings;
use IO::File;

my @users;
my $fh = IO::File->new('/var/run/dbmail-imapd.state', 'r');
for (1..3) {  #pop first 3 lines.
        <$fh>;
}
while(my $l = <$fh>) {
        
        $l =~ s/\\s+/ /g;
        #print \"\$l\n\";
        my @tokens = split(/\s+/, $l);
        my $user = $tokens[6];
        next if !defined($user) or $user =~ /User/ or $user eq '';
        push @users, $user;
}

my %userList;

foreach my $user (@users) {
        no warnings 'uninitialized';
        $userList{$user}++;
}

sub sendNotice($$) {
        my ($connections, $data) = @_;
        open((my $pipe), '|-', '/usr/sbin/sendmail -t');
        my @msg = (
        'From: [email protected]',
        'To: [email protected]',
        "Subject: automated restart of dbmail - $connections connections",
        '',
        "This is a note from perch that $connections connections were found on 
dbmail-imapd.",
        'We have restarted dbmail-imapd for you.',
        'Have a nice day',
        );
        print $pipe join("\n", @msg, "\n", $data);
        close $pipe;
}

my $total = 0;
my @rows;
foreach my $user (sort keys %userList) {
        #print "$user", (" ")x(30-length($user)), $userList{$user}, "\n";
        $total += $userList{$user};
        push @rows, [$user, $userList{$user}];
}

# The below garbled-looking print statement is a poor-man's prettyPrint() 
function
#print join("\n", map( { $_->[0] . (" ")x(30-length($_->[0])) . $_->[1] } 
reverse sort { $a->[1] <=> $b->[1] } @rows ) ), "\n";
print "Total: $total\n";
if($total > 200) {
        system("/etc/init.d/dbmail-imap stop; sleep 10; /etc/init.d/dbmail-imap 
start");
        my $data = join("\n", map( { $_->[0] . (" ")x(30-length($_->[0])) . 
$_->[1] } reverse sort { $a->[1] <=> $b->[1] } @rows ) );
        sendNotice($total, $data);
}

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
DBmail mailing list
[email protected]
http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail

Reply via email to