File Coverage

blib/lib/Sys/Statistics/Linux/SysInfo.pm
Criterion Covered Total %
statement 107 111 96.4
branch 31 48 64.5
condition 8 19 42.1
subroutine 11 11 100.0
pod 2 2 100.0
total 159 191 83.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Sys::Statistics::Linux::SysInfo - Collect linux system information.
4              
5             =head1 SYNOPSIS
6              
7             use Sys::Statistics::Linux::SysInfo;
8              
9             my $lxs = Sys::Statistics::Linux::SysInfo->new;
10             my $info = $lxs->get;
11              
12             =head1 DESCRIPTION
13              
14             Sys::Statistics::Linux::SysInfo gathers system information from the virtual F filesystem (procfs).
15              
16             For more information read the documentation of the front-end module L.
17              
18             =head1 SYSTEM INFOMATIONS
19              
20             Generated by F
21             and F, F, F, F.
22              
23             hostname - The host name.
24             domain - The host domain name.
25             kernel - The kernel name.
26             release - The kernel release.
27             version - The kernel version.
28             memtotal - The total size of memory.
29             swaptotal - The total size of swap space.
30             uptime - The uptime of the system.
31             idletime - The idle time of the system.
32             pcpucount - The total number of physical CPUs.
33             tcpucount - The total number of CPUs (cores, hyper threading).
34             interfaces - The interfaces of the system.
35             arch - The machine hardware name (uname -m).
36              
37             # countcpus is the same like tcpucount
38             countcpus - The total (maybe logical) number of CPUs.
39              
40             C and C are really easy to understand. Both values
41             are collected from C. C is the number of physical
42             CPUs, counted by C. C is just the total number
43             counted by C.
44              
45             If you want to get C and C as raw value you can set
46              
47             $Sys::Statistics::Linux::SysInfo::RAWTIME = 1;
48             # or with
49             Sys::Statistics::Linux::SysInfo->new(rawtime => 1)
50              
51             =head1 METHODS
52              
53             =head2 new()
54              
55             Call C to create a new object.
56              
57             my $lxs = Sys::Statistics::Linux::SysInfo->new;
58              
59             =head2 get()
60              
61             Call C to get the statistics. C returns the statistics as a hash reference.
62              
63             my $info = $lxs->get;
64              
65             =head1 EXPORTS
66              
67             No exports.
68              
69             =head1 SEE ALSO
70              
71             B
72              
73             =head1 REPORTING BUGS
74              
75             Please report all bugs to .
76              
77             =head1 AUTHOR
78              
79             Jonny Schulz .
80              
81             =head1 COPYRIGHT
82              
83             Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.
84              
85             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
86              
87             =cut
88              
89             package Sys::Statistics::Linux::SysInfo;
90              
91 1     1   5 use strict;
  1         2  
  1         43  
92 1     1   5 use warnings;
  1         2  
  1         38  
93 1     1   6 use Carp qw(croak);
  1         2  
  1         1965  
94              
95             our $VERSION = '0.13';
96             our $RAWTIME = 0;
97              
98             sub new {
99 1     1 1 2 my $class = shift;
100 1 50       5 my $opts = ref($_[0]) ? shift : {@_};
101              
102 1         12 my %self = (
103             files => {
104             path => "/proc",
105             meminfo => "meminfo",
106             sysinfo => "sysinfo",
107             cpuinfo => "cpuinfo",
108             uptime => "uptime",
109             hostname => "sys/kernel/hostname",
110             domain => "sys/kernel/domainname",
111             kernel => "sys/kernel/ostype",
112             release => "sys/kernel/osrelease",
113             version => "sys/kernel/version",
114             netdev => "net/dev",
115             arch => [ "/bin/uname", "-m" ],
116             }
117             );
118              
119 1         2 foreach my $file (keys %{ $opts->{files} }) {
  1         5  
120 0         0 $self{files}{$file} = $opts->{files}->{$file};
121             }
122              
123 1         2 foreach my $param (qw(rawtime cpuinfo)) {
124 2 50       9 if ($opts->{$param}) {
125 0         0 $self{$param} = $opts->{$param};
126             }
127             }
128              
129 1         9 return bless \%self, $class;
130             }
131              
132             sub get {
133 1     1 1 2 my $self = shift;
134 1         2 my $class = ref $self;
135 1         6 my $file = $self->{files};
136 1         2 my $stats = { };
137              
138 1         2 $self->{stats} = $stats;
139              
140 1         4 $self->_get_common;
141 1         30 $self->_get_meminfo;
142 1         7 $self->_get_uptime;
143 1         4 $self->_get_interfaces;
144 1         5 $self->_get_cpuinfo;
145              
146 1         13 foreach my $key (keys %$stats) {
147 14         25 chomp $stats->{$key};
148 14         17 $stats->{$key} =~ s/\t+/ /g;
149 14         45 $stats->{$key} =~ s/\s+/ /g;
150             }
151              
152 1         23 return $stats;
153             }
154              
155             sub _get_common {
156 1     1   1 my $self = shift;
157 1         2 my $class = ref($self);
158 1         2 my $file = $self->{files};
159 1         3 my $stats = $self->{stats};
160              
161 1         2 for my $x (qw(hostname domain kernel release version)) {
162 5 50       21 my $filename = $file->{path} ? "$file->{path}/$file->{$x}" : $file->{$x};
163 5 50       153 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
164 5         39 $stats->{$x} = <$fh>;
165 5         41 close($fh);
166             }
167              
168 1 50       22 if (-x $file->{arch}->[0] ) {
169 1         2 my $cmd = join(" ", @{$file->{arch}});
  1         3  
170 1         4014 $stats->{arch} = `$cmd`;
171             }
172             }
173              
174             sub _get_meminfo {
175 1     1   10 my $self = shift;
176 1         7 my $class = ref($self);
177 1         8 my $file = $self->{files};
178 1         8 my $stats = $self->{stats};
179              
180 1 50       12 my $filename = $file->{path} ? "$file->{path}/$file->{meminfo}" : $file->{meminfo};
181 1 50       91 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
182              
183 1         855 while (my $line = <$fh>) {
184 42 100       187 if ($line =~ /^MemTotal:\s+(\d+ \w+)/) {
    100          
185 1         21 $stats->{memtotal} = $1;
186             } elsif ($line =~ /^SwapTotal:\s+(\d+ \w+)/) {
187 1         11 $stats->{swaptotal} = $1;
188             }
189             }
190              
191 1         20 close($fh);
192             }
193              
194             sub _get_cpuinfo {
195 1     1   3 my $self = shift;
196 1         6 my $class = ref($self);
197 1         6 my $file = $self->{files};
198 1         3 my $stats = $self->{stats};
199 1         2 my (%cpu, $phyid);
200              
201 1         2 $stats->{countcpus} = 0;
202              
203 1 50       7 my $filename = $file->{path} ? "$file->{path}/$file->{cpuinfo}" : $file->{cpuinfo};
204 1 50       35 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
205              
206 1         105 while (my $line = <$fh>) {
207 432 100       2097 if ($line =~ /^physical\s+id\s*:\s*(\d+)/) {
    100          
    100          
    50          
208 16         90 $phyid = $1;
209 16         103 $cpu{$phyid}{count}++;
210             } elsif ($line =~ /^core\s+id\s*:\s*(\d+)/) {
211 16         73 $cpu{$phyid}{cores}{$1}++;
212             } elsif ($line =~ /^processor\s*:\s*\d+/) { # x86
213 16         54 $stats->{countcpus}++;
214             } elsif ($line =~ /^# processors\s*:\s*(\d+)/) { # s390
215 0         0 $stats->{countcpus} = $1;
216 0         0 last;
217             }
218             }
219              
220 1         13 close($fh);
221              
222 1   50     5 $stats->{countcpus} ||= 1; # if it was not possible to match
223 1         3 $stats->{tcpucount} = $stats->{countcpus};
224 1   33     20 $stats->{pcpucount} = scalar keys %cpu || $stats->{countcpus};
225             }
226              
227             sub _get_interfaces {
228 1     1   7 my $self = shift;
229 1         2 my $class = ref($self);
230 1         5 my $file = $self->{files};
231 1         2 my $stats = $self->{stats};
232 1         3 my @iface = ();
233              
234 1 50       6 my $filename = $file->{path} ? "$file->{path}/$file->{netdev}" : $file->{netdev};
235 1 50       64 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
236 1         3 { my $head = <$fh>; }
  1         39  
237              
238 1         7 while (my $line = <$fh>) {
239 3 100       27 if ($line =~ /^\s*(\w+):/) {
240 2         14 push @iface, $1;
241             }
242             }
243              
244 1         13 close $fh;
245              
246 1         9 $stats->{interfaces} = join(", ", @iface);
247 1   50     9 $stats->{interfaces} ||= "";
248             }
249              
250             sub _get_uptime {
251 1     1   5 my $self = shift;
252 1         4 my $class = ref($self);
253 1         3 my $file = $self->{files};
254 1         2 my $stats = $self->{stats};
255              
256 1 50       10 my $filename = $file->{path} ? "$file->{path}/$file->{uptime}" : $file->{uptime};
257 1 50       45 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
258 1         26 ($stats->{uptime}, $stats->{idletime}) = split /\s+/, <$fh>;
259 1         13 close $fh;
260              
261 1 50 33     23 if (!$RAWTIME && !$self->{rawtime}) {
262 1         7 foreach my $x (qw/uptime idletime/) {
263 2         25 my ($d, $h, $m, $s) = $self->_calsec(sprintf('%li', $stats->{$x}));
264 2         11 $stats->{$x} = "${d}d ${h}h ${m}m ${s}s";
265             }
266             }
267             }
268              
269             sub _calsec {
270 2     2   4 my $self = shift;
271 2         5 my ($s, $m, $h, $d) = (shift, 0, 0, 0);
272 2 50 33     28 $s >= 86400 and $d = sprintf('%i',$s / 86400) and $s = $s % 86400;
273 2 100 66     35 $s >= 3600 and $h = sprintf('%i',$s / 3600) and $s = $s % 3600;
274 2 50 33     12 $s >= 60 and $m = sprintf('%i',$s / 60) and $s = $s % 60;
275 2         7 return ($d, $h, $m, $s);
276             }
277              
278             1;