File Coverage

blib/lib/Sys/Uptime.pm
Criterion Covered Total %
statement 24 25 96.0
branch 4 8 50.0
condition n/a
subroutine 6 6 100.0
pod 0 4 0.0
total 34 43 79.0


line stmt bran cond sub pod time code
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     1   2617 use strict;
  1         2  
  1         55  
15             require Exporter;
16             require DynaLoader;
17 1     1   6 use vars qw (@ISA $VERSION);
  1         2  
  1         2837  
18             our @ISA = qw(Exporter DynaLoader);
19             $VERSION = '0.01';
20              
21             # Read the load average.
22             sub loadavg {
23             # Read the values from /proc/loadavg and put them in an array.
24 1 50   1 0 42 open FILE, "< /proc/loadavg" or die return ("Cannot open /proc/loadavg: $!");
25 1         29 my ($avg1, $avg5, $avg15, undef, undef) = split / /, ;
26 1         4 my @loadavg = ($avg1, $avg5, $avg15);
27 1         10 close FILE;
28 1         6 return (@loadavg);
29             }
30              
31             # Read the number of CPU's.
32             sub cpunbr {
33             # Read the data from /proc/cpuinfo and count the lines that start with processor blablabla :-)
34 1 50   1 0 72 open FILE, "< /proc/cpuinfo" or die return ("Cannot open /proc/cpuinfo: $!");
35 1         616 my $nbrcpu = scalar grep(/^processor\s+:/,);
36 1         44 close FILE;
37 1         5 return ($nbrcpu);
38             }
39              
40             # Read the uptime.
41             sub uptime {
42             # Read the uptime in seconds from /proc/uptime, skip the idle time...
43 1 50   1 0 33 open FILE, "< /proc/uptime" or die return ("Cannot open /proc/uptime: $!");
44 1         16 my ($uptime, undef) = split / /, ;
45 1         10 close FILE;
46 1         4 return ($uptime);
47             }
48              
49             # Read the number of users.
50             sub users {
51             # I didn't find it in /proc, so here I use the uptime output... :-)
52 1     1 0 5778 my $output = `uptime`;
53 1 50       35 if (!$output) { die return ("Cannot read uptime output: $!") }
  0         0  
54 1         39 $output =~ /[0-9]+\s+user/;
55 1         21 my ($users, undef) = split /\s+/, $&;
56 1         32 return ($users);
57             }