diff options
author | Robert Rothenberg <rrwo@cpan.org> | 2020-03-30 09:32:00 +0100 |
---|---|---|
committer | Robert Rothenberg <rrwo@cpan.org> | 2020-03-30 09:32:00 +0100 |
commit | 0b0c12c6886478108f56f245882e8f2b5aeee28f (patch) | |
tree | 90ceb73c7a8fdf4340326f52467bf49294237ef9 | |
parent | c6b2db7e30a1307d4ddf02c98b0c9617b8cec047 (diff) |
Add ability to override path of uptime
-rw-r--r-- | Changes | 2 | ||||
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | lib/Sys/CpuLoad.pm | 22 |
3 files changed, 32 insertions, 6 deletions
@@ -4,6 +4,8 @@ Revision history for Perl extension {{$dist->name}}: [Enhancements] - Use IPC::Run3 for uptime. + - Add the ability to override the path of uptime. + 0.23 2020-03-29 13:09:50+01:00 Europe/London (TRIAL RELEASE) [Enhancements] - Support getloadavg for DragonFly BSD. @@ -60,10 +60,22 @@ Added in v0.22. Parse the output of uptime. -If the output cannot be parsed, it will return `undef`. +If the [uptime](https://metacpan.org/pod/uptime) executable cannot be found, or the output cannot be +parsed, it will return `undef`. Added in v0.22. +As of v0.24, you can override the executable path by setting +`$Sys::CpuLoad::UPTIME`, e.g. + +```perl +use Sys::CpuLoad 'uptime'; + +$Sys::CpuLoad::UPTIME = '/usr/bin/w'; + +@load = uptime(); +``` + # SEE ALSO [Sys::CpuLoadX](https://metacpan.org/pod/Sys::CpuLoadX) diff --git a/lib/Sys/CpuLoad.pm b/lib/Sys/CpuLoad.pm index f597cd0..c964b67 100644 --- a/lib/Sys/CpuLoad.pm +++ b/lib/Sys/CpuLoad.pm @@ -75,10 +75,22 @@ Added in v0.22. Parse the output of uptime. -If the output cannot be parsed, it will return C<undef>. +If the L<uptime> executable cannot be found, or the output cannot be +parsed, it will return C<undef>. Added in v0.22. +As of v0.24, you can override the executable path by setting +C<$Sys::CpuLoad::UPTIME>, e.g. + + use Sys::CpuLoad 'uptime'; + + no warnings 'once'; + + $Sys::CpuLoad::UPTIME = '/usr/bin/w'; + + @load = uptime(); + =cut sub proc_loadavg { @@ -98,17 +110,17 @@ sub proc_loadavg { return undef; ## no critic (ProhibitExplicitReturnUndef) } -my $uptime; +our $UPTIME; sub uptime { local %ENV = %ENV; $ENV{'LC_NUMERIC'} = 'POSIX'; # ensure that decimal separator is a dot - $uptime ||= which("uptime") or + $UPTIME ||= which("uptime") or return undef; ## no critic (ProhibitExplicitReturnUndef) - run3($uptime, \undef, \my $line); - if ( $line =~ /(\d+\.\d+)\s*,?\s+(\d+\.\d+)\s*,?\s+(\d+\.\d+)\s*$/ ) + run3($UPTIME, \undef, \my $line); + if ( $line =~ /(\d+\.\d+)\s*,?\s+(\d+\.\d+)\s*,?\s+(\d+\.\d+)\s*$/m ) { return ( $1, $2, $3 ); } |