On Mon, 1 Mar 1999, Anand Buddhdev wrote:

> On Fri, Feb 26, 1999 at 12:47:33PM +1100, Peter Samuel wrote:
> 
> Hello Peter. I have one more suggestion for your program. It expects to
> read the queue by opening a pipe to /var/qmail/bin/qmail-qread. On my
> system I have qmail installed in /usr/local/qmail, so I had to change that
> setting by hand. Perhaps you can make the location of qmail-qread a variable,
> so that in case your script grows, it will not require too many changes.

Eventually these will be Makefile variables - set it once and all of
the programs will know where to find qmail binaries.

> 
> Your script is nice and useful. I'd also like to see your other scripts,
> ie. qstatus.

Follows:

Regards
Peter
----------
Peter Samuel                                [EMAIL PROTECTED]
Technical Consultant                        or at present:
Uniq Professional Services,                 [EMAIL PROTECTED]
a division of X-Direct Pty Ltd
Phone: +61 2 9206 3410                      Fax: +61 2 9281 1301

"If you kill all your unhappy customers, you'll only have happy ones left"

###########################################################################
#!/usr/local/bin/perl -w

use strict;

die("Only the superuser can run this program\n") if $>;

$| = 1;

my $one_shot = 0;
my $queued;
my $unprocessed;
my $locals;
my $remotes;
my $remoteprocs;
my $localprocs;
my $tcpto;
my $loadavg;
my $concurrencylocal;
my $concurrencyremote;

$one_shot = scalar @ARGV;

print << "EOF";
          queue             recipients      processes      tcpto     load
  processed/unprocessed    local/remote    local/remote      ips      avg
EOF

my $count = 0;
my @dial = ("|", "/", "-", "\\");

while (1)
{
    $concurrencylocal = &getconcurrencylocal;
    $concurrencyremote = &getconcurrencyremote;

    &getstats;

    if ($one_shot)
    {
        print "\n";
        exit(0);
    }

    sleep(10);
}

sub getstats
{
    my($sig) = @_;

    $unprocessed = 0;
    $locals = 0;
    $remotes = 0;
    $remoteprocs = 0;
    $localprocs = 0;
    $tcpto = 0;

    ++$count;

    &qstat;
    &qread;
    &processes;
    &tcpto;
    &uptime;

    printf("%s %9d %11d    %5d %6d    %2d/%-2d  %2d/%-2d    %5d    %5.2f\r",
        $dial[$count % 4],
        $queued,
        $unprocessed,
        $locals,
        $remotes,
        $localprocs,
        $concurrencylocal,
        $remoteprocs,
        $concurrencyremote,
        $tcpto,
        $loadavg
    );
}

sub processes
{
    open(PS, "ps -ef 2>/dev/null |");

    while(<PS>)
    {
        ++$remoteprocs if (/qmail-remote/);
        ++$localprocs if (/qmail-local/);
    }

    close(PS);
}

sub qread
{
    my @f;

    open(QREAD, "/var/qmail/bin/qmail-qread 2>/dev/null |");

    while(<QREAD>)
    {
        chomp;
        @f = split(' ');
        ++$remotes if ($f[0] eq "remote");
        ++$locals if ($f[0] eq "local");
    }

    close(QREAD);
}

sub qstat
{
    my @f;

    open(QSTAT, "/var/qmail/bin/qmail-qstat 2>/dev/null |");

    while(<QSTAT>)
    {
        chomp;
        @f = split(' ');

        $queued = $f[3] if (/queue:/);
        $unprocessed = $f[7] if (/not yet/);
    }

    close(QSTAT);
}

sub tcpto
{
    open(TCPTO, "/var/qmail/bin/qmail-tcpto 2>/dev/null |");

    while(<TCPTO>)
    {
        chomp;
        ++$tcpto;
    }

    close(TCPTO);
}

sub uptime
{
    my @f;

    open(UPTIME, "uptime 2>/dev/null |");

    while(<UPTIME>)
    {
        chomp;
        s/\s//g;
        @f = split(/,/);
        @f = split(/:/, $f[3]);
        $loadavg = $f[1];
    }
}

sub getconcurrencylocal
{
    my $file = "/var/qmail/control/concurrencylocal";
    my $default = 10;
    my $value;

    if (
        -e $file
        &&
        -f $file
        &&
        -r $file
       )
    {
        open(LOCAL, "$file");

        while (<LOCAL>)
        {
            chomp;
            $value = $_;
        }

        close(LOCAL);

        return $value if ($value =~ /^\d+$/);
        return $default;
    }
    else
    {
        return $default;
    }
}

sub getconcurrencyremote
{
    my $file = "/var/qmail/control/concurrencyremote";
    my $default = 20;
    my $value;

    if (
        -e $file
        &&
        -f $file
        &&
        -r $file
       )
    {
        open(LOCAL, "$file");

        while (<LOCAL>)
        {
            chomp;
            $value = $_;
        }

        close(LOCAL);

        return $value if ($value =~ /^\d+$/);
        return $default;
    }
    else
    {
        return $default;
    }
}

Reply via email to