blib/lib/Sys/Uptime.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 80.8% 38 / 47 CC 5 SCAR 6.0
CC
5
Cov
86%
users
2.1
cpunbr
2.0
uptime
2.0
CRAP 1.8
↑ Summary
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
BranchTrueFalseTotal
open FILE, "< /proc/loadavg" or die +(return "Cannot open /proc/loadavg: $!") 1 0 1
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
BranchTrueFalseTotal
open FILE, "< /proc/cpuinfo" or die +(return "Cannot open /proc/cpuinfo: $!") 1 0 1
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
BranchTrueFalseTotal
open FILE, "< /proc/uptime" or die +(return "Cannot open /proc/uptime: $!") 1 0 1
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
BranchTrueFalseTotal
unless ($output) 0 1 1
54 1 $output =~ /[0-9]+\s+user/;
55 1 my ($users, undef) = split /\s+/, $&;
56 1 return ($users);
57 }
↑ Summary