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]


Reply via email to