Re: new plugin: check_system_load

2008-03-12 Thread Mark Powell

On Sat, 8 Mar 2008, Steve Kemp wrote:


 Having said that though executing uptime on each incoming connection
might increase resource usage at a time when the system is already
loaded - but I'm certain that doing that will be a lot less resource
intensive than running clamav, spambayes, and all the other tests that
are typically conducted...


Indeed. However, why do it at all when you don't need to?
  Cheers.

--
Mark Powell - UNIX System Administrator - The University of Salford
Information Services Division, Clifford Whitworth Building,
Salford University, Manchester, M5 4WT, UK.
Tel: +44 161 295 6843  Fax: +44 161 295 5888  www.pgp.com for PGP key


Re: new plugin: check_system_load

2008-03-08 Thread Peter Eisch
On Sat, 8 Mar 2008 16:08:10 +0100
Juerd Waalboer [EMAIL PROTECTED] wrote:

 Peter Eisch skribis 2008-03-07 16:17 (-0600):
$self-{_args}-{uptime} = '/usr/bin/uptime'
if (! defined $self-{_args}-{uptime});
  (...)
my $res = `$self-{_args}-{uptime}`;
 
 Especially during high load, forking and executing uptime may not be
 such a wise thing to do, depending on how well your platform handles
 this under high load.

True.  The goal is to avoid swamping the system.  

I'm happy to take patches that keep the plugin universal and gather the
stats better.


Re: new plugin: check_system_load

2008-03-08 Thread Steve Kemp
On Sat Mar 08, 2008 at 10:02:12 -0600, Peter Eisch wrote:

 I'm happy to take patches that keep the plugin universal and gather the
 stats better.

  How about something like this:

sub getLoad
{
#
#  Use the module if we can.
#
my $test  = use Sys::CpuLoad;;
eval( $test );

if ( $@ )
{
#
#  Fall back to running process.
#
my $res = `uptime`;
if ($res =~ /aver\S+: (\d+\.\d+)/)
{
return( int( $1 ) );
}
}
else
{
my @loads = Sys::CpuLoad::load();
return( int($loads[0]) );
}

# fail?
return undef;
}

Steve
-- 



Re: new plugin: check_system_load

2008-03-08 Thread Brian Szymanski
I started cleaning this up to use a block, not string eval, and pull the
check out of getload so it needn't be called as often... But really, if
your load is too high, do you want to be exec'ing uptime on every
call? I sure wouldn't. I say just require Sys::CpuLoad, warn and return
DECLINED if it's unavailable.

eval { require Sys::CpuLoad };
my $_has_sys_cpuload = !$@;

sub getLoad {
  unless($_has_sys_cpuload) {
$self-log(LOGWARN, you must install Sys::CpuLoad to use this
module!);
return -1; #or undef or whatever
  }
  return int((Sys::CpuLoad::load())[0]);
}

Steve Kemp wrote:
 On Sat Mar 08, 2008 at 10:02:12 -0600, Peter Eisch wrote:

   
 I'm happy to take patches that keep the plugin universal and gather the
 stats better.
 

   How about something like this:

 sub getLoad
 {
 #
 #  Use the module if we can.
 #
 my $test  = use Sys::CpuLoad;;
 eval( $test );

 if ( $@ )
 {
 #
 #  Fall back to running process.
 #
 my $res = `uptime`;
 if ($res =~ /aver\S+: (\d+\.\d+)/)
 {
 return( int( $1 ) );
 }
 }
 else
 {
 my @loads = Sys::CpuLoad::load();
 return( int($loads[0]) );
 }

 # fail?
 return undef;
 }

 Steve
   


-- 
Brian Szymanski
email:  [EMAIL PROTECTED]
skype:  xbrianskix
cell:   +1 202 747 4019
jabber: [EMAIL PROTECTED]
aim:xbrianskix
msn:[EMAIL PROTECTED]




Re: new plugin: check_system_load

2008-03-08 Thread Steve Kemp
On Sat Mar 08, 2008 at 15:21:48 -0500, Brian Szymanski wrote:

 I started cleaning this up to use a block, not string eval, and pull the
 check out of getload so it needn't be called as often... But really, if
 your load is too high, do you want to be exec'ing uptime on every
 call? I sure wouldn't. I say just require Sys::CpuLoad, warn and return
 DECLINED if it's unavailable.

  Good idea.  Personally I'm always using the Sys::Cpuload module now,
 so I'll bow out of further discussion.  (will update the code on my
 site/page to reflect the current module shortly.)

  Having said that though executing uptime on each incoming connection
 might increase resource usage at a time when the system is already
 loaded - but I'm certain that doing that will be a lot less resource
 intensive than running clamav, spambayes, and all the other tests that
 are typically conducted...

Steve
-- 


Re: new plugin: check_system_load

2008-03-08 Thread Matt Sergeant

On 8-Mar-08, at 3:21 PM, Brian Szymanski wrote:


eval { require Sys::CpuLoad };
my $_has_sys_cpuload = !$@;


You might find this meme useful:

use constant HAVE_CPULOAD = eval { require Sys::CpuLoad };


Re: new plugin: check_system_load

2008-03-07 Thread Peter Eisch
On 3/7/08 3:39 PM, Steve Kemp [EMAIL PROTECTED] wrote:

 
 This seems like rather an obvious idea:  Reject new
 connections if the system load is too high.  But surprisingly
 there seem to be no plugins which I could find implementing it.
 

I had posted this one to the list back a couple years ago.  It works on
linux, bsd'en and solaris.  Maybe on hpux too.

peter



#!/usr/bin/perl

=head1 NAME

loadcheck

=head1 DESCRIPTION

Only takes email transactions if the system load is at or below a
specified level.  

=head1 CONFIG

max_load

  This is the 1 minute system load where we won't take transactions
if our load is higher than this value.  (Default: 7)

uptime

  The path to the command 'uptime' if different than the default.
(Default: /usr/bin/uptime)

Example:

loadcheck max_load 7 uptime /usr/bin/uptime

=over 4

=head1 AUTHOR

Written by Peter Eisch [EMAIL PROTECTED].

=cut

my $VERSION = 0.01;

sub register {
  my ($self, $qp, @args) = @_;

  %{$self-{_args}} = @args;

  $self-{_args}-{max_load} = 7
  if (! defined $self-{_args}-{max_load});

  $self-{_args}-{uptime} = '/usr/bin/uptime'
  if (! defined $self-{_args}-{uptime});
  
  $self-register_hook(connect, loadcheck);
}

sub loadcheck {
  my ($self, $transaction) = @_;

  my $hiload = 0;

  my $cmd = $self-{_args}-{uptime};
  #10:33AM  up  2:06, 1 user, load averages: 6.55, 3.76, 2.48
  # 12:29am  2 users,  load average: 0.05, 0.05, 0.06
  # 12:30am  up 5 days, 12:43,  1 user,  load average: 0.00, 0.00, 0.00

  my $res = `$self-{_args}-{uptime}`;
  if ($res =~ /aver\S+: (\d+\.\d+)/) {
  $hiload = $1;
  }

  if ($hiload  $self-{_args}-{max_load}) {
  $self-log(LOGERROR, local load too high: $hiload);
  return DENYSOFT;
  }

  return (DECLINED, continuing with load: $hiload);
}




Re: new plugin: check_system_load

2008-03-07 Thread Matt Sergeant
Note - you'd be better off using Linux::SysInfo (on Linux, obviously)  
to save the backtick call.


On 7-Mar-08, at 4:39 PM, Steve Kemp wrote:



  This seems like rather an obvious idea:  Reject new
 connections if the system load is too high.  But surprisingly
 there seem to be no plugins which I could find implementing it.

  So here's mine:

http://mail-scanning.com/qpsmtpd/system-load

  This reads /etc/qpsmtpd/max_system_load, and if the system load
 is equal or higher than the value contained within it it is rejected.


   Comments welcome, and I'm happy for it to be used in the core,  
or in

  contrib if there is any interest.

Steve
--
http://mail-scanning.com/





Re: new plugin: check_system_load

2008-03-07 Thread Steve Kemp
On Fri Mar 07, 2008 at 17:48:22 -0500, Matt Sergeant wrote:

 Note - you'd be better off using Linux::SysInfo (on Linux, obviously) to 
 save the backtick call.

  Noted.  I'll update the code to use that where possible.  (Right now
 I figured portability would be a good thing - although I realise my
 regexp fails there!)

Steve
--