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