| 1 | #!/usr/bin/perl -w | ||||||||||
| 2 | |||||||||||
| 3 | # ----------------------------------------------------------------------------------------- | ||||||||||
| 4 | # Sys::Uptime | ||||||||||
| 5 | # ----------------------------------------------------------------------------------------- | ||||||||||
| 6 | # Wim De Hul 2004. | ||||||||||
| 7 | # | ||||||||||
| 8 | # This module shows the uptime, CPUload (1, 5, 15 min average) and the number of users. | ||||||||||
| 9 | # Additionally it is also capable of returning the number of CPU's. | ||||||||||
| 10 | # | ||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | package Sys::Uptime; | ||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | require Exporter; | |||||||||
| 16 | 1 | require DynaLoader; | |||||||||
| 17 | 1 | ▶ | use vars qw (@ISA $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | our @ISA = qw(Exporter DynaLoader); | |||||||||
| 19 | 1 | $VERSION = '0.01'; | |||||||||
| 20 | |||||||||||
| 21 | # Read the load average. | ||||||||||
| 22 | 1 | ▶ | sub loadavg { | ||||||||
|
Subroutine
loadavg: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 23 | # Read the values from /proc/loadavg and put them in an array. | ||||||||||
| 24 | 1 | ▶ | open FILE, "< /proc/loadavg" or die return ("Cannot open /proc/loadavg: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 1 | my ($avg1, $avg5, $avg15, undef, undef) = split / /, <FILE>; | |||||||||
| 26 | 1 | my @loadavg = ($avg1, $avg5, $avg15); | |||||||||
| 27 | 1 | close FILE; | |||||||||
| 28 | 1 | return (@loadavg); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | # Read the number of CPU's. | ||||||||||
| 32 | 1 | ▶ | sub cpunbr { | ||||||||
|
Subroutine
cpunbr: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 33 | # Read the data from /proc/cpuinfo and count the lines that start with processor blablabla :-) | ||||||||||
| 34 | 1 | ▶ | open FILE, "< /proc/cpuinfo" or die return ("Cannot open /proc/cpuinfo: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 1 | my $nbrcpu = scalar grep(/^processor\s+:/,<FILE>); | |||||||||
| 36 | 1 | close FILE; | |||||||||
| 37 | 1 | return ($nbrcpu); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | # Read the uptime. | ||||||||||
| 41 | 1 | ▶ | sub uptime { | ||||||||
|
Subroutine
uptime: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 42 | # Read the uptime in seconds from /proc/uptime, skip the idle time... | ||||||||||
| 43 | 1 | ▶ | open FILE, "< /proc/uptime" or die return ("Cannot open /proc/uptime: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 1 | my ($uptime, undef) = split / /, <FILE>; | |||||||||
| 45 | 1 | close FILE; | |||||||||
| 46 | 1 | return ($uptime); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Read the number of users. | ||||||||||
| 50 | 1 | ▶ | sub users { | ||||||||
|
Subroutine
users: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 51 | # I didn't find it in /proc, so here I use the uptime output... :-) | ||||||||||
| 52 | 1 | my $output = `uptime`; | |||||||||
| 53 | 0 | ▶ | if (!$output) { die return ("Cannot read uptime output: $!") } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | $output =~ /[0-9]+\s+user/; | |||||||||
| 55 | 1 | my ($users, undef) = split /\s+/, $&; | |||||||||
| 56 | 1 | return ($users); | |||||||||
| 57 | } | ||||||||||