File Coverage

blib/lib/App/Sys/Info.pm
Criterion Covered Total %
statement 199 251 79.2
branch 34 88 38.6
condition 12 38 31.5
subroutine 36 38 94.7
pod 3 3 100.0
total 284 418 67.9


line stmt bran cond sub pod time code
1             package App::Sys::Info;
2             $App::Sys::Info::VERSION = '0.26';
3 2     2   263953 use strict;
  2         4  
  2         84  
4 2     2   22 use warnings;
  2         5  
  2         179  
5              
6 2     2   14 use constant CP_UTF8 => 65_001;
  2         4  
  2         247  
7 2     2   13 use constant LAST_ELEMENT => -1;
  2         2  
  2         121  
8              
9 2     2   12 use Carp qw( croak );
  2         4  
  2         143  
10 2     2   1119 use Format::Human::Bytes;
  2         1211  
  2         76  
11 2     2   1105 use POSIX qw( locale_h );
  2         17759  
  2         15  
12 2     2   5290 use Text::Table qw();
  2         55246  
  2         125  
13 2     2   1530 use Time::Duration qw( duration_exact );
  2         5399  
  2         199  
14 2     2   2930 use Sys::Info qw();
  2         54285  
  2         95  
15 2     2   25 use Sys::Info::Constants qw( NEW_PERL OSID );
  2         4  
  2         206  
16              
17             my($NEED_CHCP, $OLDCP);
18              
19             BEGIN {
20 2     2   17 no strict qw( refs );
  2         4  
  2         214  
21 2     2   11 foreach my $id ( qw( info os cpu fhb meta NA ) ) {
22 12     23   128 *{ $id } = sub () { return shift->{$id} };
  12         6801  
  23         149  
23             }
24             }
25              
26             END {
27 1 50 33 1   2131 _chcp( $OLDCP ) if $NEED_CHCP && $OLDCP;
28             }
29              
30             sub _chcp {
31 0   0 0   0 my $enc = shift || croak 'No encoding specified';
32 0         0 system chcp => $enc, '2>nul', '1>nul';
33 0         0 return;
34             }
35              
36             sub new {
37 1     1 1 3 my $class = shift;
38 1         11 my $i = Sys::Info->new;
39 1         11 my $loc = do {
40 1         11 my $rv;
41             eval {
42 1         15 $rv = setlocale( LC_CTYPE );
43 1         6 1;
44 1 50       3 } or do {
45 0   0     0 my $error = $@ || 'Unknown error';
46 0         0 warn "Unable to collect the locale information: $error";
47 0         0 $rv = '';
48             };
49 1         3 $rv;
50             };
51 1         7 my $self = {
52             LOCALE => $loc,
53             NA => 'N/A',
54             info => $i,
55             os => $i->os,
56             cpu => $i->device('CPU'),
57             fhb => Format::Human::Bytes->new,
58             };
59 1         23042 $self->{meta} = { $self->{os}->meta };
60 1         68229 bless $self, $class;
61 1         4 return $self;
62             }
63              
64             sub run {
65 1     1 1 270504 my $self = __PACKAGE__->new;
66 1   33     6 $NEED_CHCP = $self->os->is_winnt && $ENV{PROMPT};
67 1         9 my @probe = $self->probe;
68              
69 1         17 $self->_init_encoding;
70              
71 1         28 my $tb = Text::Table->new( q{}, q{} );
72 1         949 $tb->load( @probe );
73              
74 1 50       1877 print "\n", $tb or croak "Unable to orint to STDOUT: $!";
75 1         26604 return;
76             }
77              
78             sub probe {
79 1     1 1 3 my $self = shift;
80 1         3 my @rv = eval { $self->_probe(); };
  1         4  
81 1 50       7 croak "Error fetching information: $@" if $@;
82 1         9 return @rv;
83             }
84              
85             sub _init_encoding {
86 1     1   7 my $self = shift;
87 1 50       7 if ( $NEED_CHCP ) {
88             ## no critic (InputOutput::ProhibitBacktickOperators)
89 0         0 chomp($OLDCP = (split /:\s?/xms, qx(chcp))[LAST_ELEMENT]);
90             # try to change the command line encoding to unicode
91 0 0       0 _chcp( CP_UTF8 ) if $OLDCP;
92 0         0 if ( NEW_PERL ) {
93 0         0 my $eok = eval q{ binmode STDOUT, ':utf8'; 1; };
94             }
95             }
96 1         3 return;
97             }
98              
99             sub _probe {
100 1     1   3 my $self = shift;
101 1         3 my $meta = $self->meta;
102 1         3 my $NA = $self->NA;
103 1         7 my $i = $self->info;
104 1         3 my $os = $self->os;
105 1         5 my $pt = $os->product_type;
106 1         6 my $proc = $self->_processors;
107 1         16 my $tz = $os->tz;
108 1         110 my $driver = 'Sys::Info::Driver::' . OSID;
109 1         7 my @rv;
110              
111 1         21 push @rv,
112             [ 'Sys::Info Version' =>
113             sprintf(
114             '%s (%s: %s - Base: %s)',
115             Sys::Info->VERSION,
116             OSID,
117             $driver->VERSION,
118             Sys::Info::Base->VERSION,
119             )
120             ],
121             [ 'Perl Version' => $i->perl_long ],
122             [ 'Host Name' => $os->host_name ],
123             [ 'OS Name' => $self->_os_name ],
124             [ 'OS Version' => $self->_os_version ],
125             ;
126              
127 1         28 my $manu = $meta->{manufacturer};
128 1         3 my $bt = $meta->{build_type};
129              
130 1 50       19 push @rv, [ 'OS Manufacturer' => $manu ] if $manu;
131 1 50       4 push @rv, [ 'OS Configuration' => $pt ] if $pt;
132 1 50       4 push @rv, [ 'OS Build Type' => $bt ] if $bt;
133              
134 1         5 $self->_bitness( \@rv );
135 1         5 $self->_current_user( \@rv );
136 1         417 $self->_registered( \@rv, $meta );
137              
138 1         12 my $pid = $meta->{product_id};
139 1         11 my $tick = $os->tick_count;
140 1         204 my $st = $meta->{system_type};
141              
142 1 50       5 push @rv, [ 'Product ID' => $pid ] if $pid;
143              
144 1         6 $self->_install_date( \@rv );
145              
146 1 50       22 push @rv, [ 'System Up Time' => duration_exact($tick) ] if $tick;
147              
148 1         124 $self->_manufacturer( \@rv, $meta );
149              
150 1 50       12 push @rv, [ 'System Type' => $st ] if $st;
151 1 50       5 push @rv, [ 'Processor(s)' => $proc ] if $proc;
152              
153 1         5 $self->_proc_meta( \@rv );
154 1         6 $self->_bios_version( \@rv );
155 1         24 $self->_directories( \@rv, $meta );
156              
157 1         19 my $loc = $self->{LOCALE};
158              
159 1 50       6 push @rv, [ 'System Locale' => $loc ] if $loc;
160 1 50       5 push @rv, [ 'Input Locale' => $loc ] if $loc;
161 1 50       3 push @rv, [ 'Time Zone' => $tz ] if $tz;
162              
163 1         7 $self->_memory( \@rv, $meta );
164 1         5 $self->_vm( \@rv );
165              
166 1         28 my $domain = $os->domain_name;
167 1         10724 my $logon = $os->logon_server;
168 1         36 my $ip = $os->ip;
169 1         5661 my $page = $meta->{page_file_path};
170              
171 1 50       15 push @rv, [ 'Page File Location(s)' => $page ] if $page;
172 1 50       12 push @rv, [ 'Domain' => $domain ] if $domain;
173 1 50       9 push @rv, [ 'Logon Server' => $logon ] if $logon;
174 1 50       11 push @rv, [ 'IP Address' => $ip ] if $ip;
175              
176 1         19 $self->_cdkey( \@rv );
177              
178 1         62 return @rv;
179             }
180              
181             sub _registered {
182 1     1   4 my($self, $rv, $meta) = @_;
183 1 50       4 return if ! $self->os->is_windows;
184 0         0 my $owner = $meta->{owner};
185 0         0 my $org = $meta->{organization};
186 0 0       0 push @{ $rv }, [ 'Registered Owner' => $owner ] if $owner;
  0         0  
187 0 0       0 push @{ $rv }, [ 'Registered Organization' => $org ] if $org;
  0         0  
188 0         0 return;
189             }
190              
191             sub _directories {
192 1     1   4 my($self, $rv, $meta) = @_;
193 1         4 my $win = $meta->{windows_dir};
194 1         2 my $sys = $meta->{system_dir};
195 1         2 my $boot = $meta->{boot_device};
196 1 50       5 push @{ $rv }, [ 'Windows Directory' => $win ] if $win;
  0         0  
197 1 50       3 push @{ $rv }, [ 'System Directory' => $sys ] if $sys;
  0         0  
198 1 50       3 push @{ $rv }, [ 'Boot Device' => $boot ] if $boot;
  0         0  
199 1         2 return;
200             }
201              
202             sub _manufacturer {
203 1     1   6 my($self, $rv, $meta) = @_;
204 1 50       4 return if ! $self->os->is_windows;
205 0         0 my $manu = $meta->{system_manufacturer};
206 0         0 my $model = $meta->{system_model};
207 0 0       0 push @{ $rv }, [ 'System Manufacturer' => $manu ] if $manu;
  0         0  
208 0 0       0 push @{ $rv }, [ 'System Model' => $model ] if $model;
  0         0  
209 0         0 return;
210             }
211              
212             sub _cdkey {
213 1     1   8 my($self, $rv) = @_;
214 1         16 my $os = $self->os;
215 1 50       22 return if ! $os->is_windows;
216              
217 0         0 my $cdkey = $os->cdkey;
218 0         0 my $okey = $self->_office_cdkey;
219 0 0       0 push @{ $rv }, [ 'Windows CD Key' => $cdkey ] if $cdkey;
  0         0  
220 0 0       0 push @{ $rv }, [ 'Microsoft Office CD Key' => $okey ] if $okey;
  0         0  
221 0         0 return;
222             }
223              
224             sub _current_user {
225 1     1   3 my($self, $rv_ref) = @_;
226 1         4 my $os = $self->os;
227 1   50     12 my $user = $os->login_name || return;
228 0         0 my $real = $os->login_name( real => 1 );
229              
230 0 0 0     0 return if ! $user || ! $real;
231              
232 0 0 0     0 my $display = $real && ($real ne $user) ? qq{$real ($user)} : $user;
233 0 0       0 $display .= $os->is_root ? q{ is an administrator} : q{};
234 0         0 push @{ $rv_ref }, [ 'Current User', $display ];
  0         0  
235              
236 0         0 return;
237             }
238              
239             sub _proc_meta {
240 1     1   3 my $self = shift;
241 1         2 my $data = shift;
242 1         4 my @cpu = $self->cpu->identify;
243 1   50     24 my $prop = $cpu[0] || {};
244 1         3 my $load = $self->cpu->load;
245 1         235 my $L1 = $prop->{L1_cache}{max_cache_size};
246 1         3 my $L2 = $prop->{L2_cache}{max_cache_size};
247 1         3 my $sock = $prop->{socket_designation};
248 1         3 my $id = $prop->{processor_id};
249 1         2 my @rv;
250              
251             my $check_lc = sub {
252 2   50 2   6 my $ref = shift || return;
253 2 100       4 return if ! ${ $ref };
  2         6  
254 1 50       2 ${ $ref } .= q{ KB} if ${ $ref } !~ m{\sKB\z}xms;
  0         0  
  1         9  
255 1         2 return;
256 1         6 };
257              
258 1         5 $check_lc->( \$L1 );
259 1         3 $check_lc->( \$L2 );
260              
261 1 50       20 push @rv, qq{Load : $load} if $load;
262 1 50       3 push @rv, qq{L1 Cache: $L1} if $L1;
263 1 50       5 push @rv, qq{L2 Cache: $L2} if $L2;
264 1 50       4 push @rv, qq{Package : $sock} if $sock;
265 1 50       4 push @rv, qq{ID : $id} if $id;
266              
267 1         2 my $buf = q{ } x 2**2;
268 1         5 push @{$data}, [ q{ }, $buf . $_ ] for @rv;
  2         9  
269 1         8 return;
270             }
271              
272             sub _processors {
273 1     1   3 my $self = shift;
274 1         4 my $cpu = $self->cpu;
275 1         5 my $name = scalar $cpu->identify;
276 1         6023 my $rv = sprintf '%s ~%sMHz', $name, $cpu->speed;
277 1         39 $rv =~ s{\s+}{ }xmsg;
278 1         4 return $rv;
279             }
280              
281             sub _memory {
282 1     1   2 my($self, $rv, $meta) = @_;
283 1         7 push @{ $rv },
284             map {
285 4         61 [ $_->[0], $self->_mb( $_->[1] ) ]
286             }
287             [ 'Total Physical Memory' => $meta->{physical_memory_total} ],
288             [ 'Available Physical Memory' => $meta->{physical_memory_available} ],
289             [ 'Virtual Memory: Max Size' => $meta->{page_file_total} ],
290 1         3 [ 'Virtual Memory: Available' => $meta->{page_file_available} ],
291             ;
292 1         4 return;
293             }
294              
295             sub _vm {
296 1     1   3 my($self, $rv_ref) = @_;
297 1   50     3 my $tot = $self->meta->{page_file_total} || return;
298 0   0     0 my $av = $self->meta->{page_file_available} || return;
299 0         0 push @{ $rv_ref },
  0         0  
300             [ 'Virtual Memory: In Use' => $self->_mb( $tot - $av ) ]
301             ;
302 0         0 return;
303             }
304              
305             sub _mb {
306 4     4   25 my $self = shift;
307 4   100     14 my $kb = shift || return $self->NA;
308 2         6 return $self->fhb->base2( $kb, 2 );
309             }
310              
311             sub _os_name {
312 1     1   84 my $self = shift;
313 1         3 return $self->os->name( long => 1, edition => 1 );
314             }
315              
316             sub _os_version {
317 1     1   30 my $self = shift;
318 1         3 my $os = $self->os;
319 1         7 return $os->version . q{.} . $os->build;
320             }
321              
322             sub _office_cdkey {
323 0     0   0 my $self = shift;
324 0         0 my @office = $self->os->cdkey( office => 1 );
325 0 0       0 return @office ? $office[0] : undef;
326             }
327              
328             sub _bitness {
329 1     1   3 my($self, $rv_ref) = @_;
330 1   50     3 my $cpu = $self->cpu->bitness || q{??};
331 1   50     150 my $os = $self->os->bitness || q{??};
332 1         22 push @{ $rv_ref }, [ 'Running on' => qq{${cpu}bit CPU & ${os}bit OS} ];
  1         4  
333 1         3 return;
334             }
335              
336             sub _install_date {
337 1     1   3 my($self, $rv_ref) = @_;
338 1   50     4 my $date = $self->meta->{install_date} || return;
339 1         3 push @{ $rv_ref }, [ 'Original Install Date' => scalar localtime $date ];
  1         33  
340 1         3 return;
341             }
342              
343             sub _bios_version {
344 1     1   3 my($self, $rv_ref) = @_;
345 1         2 local $@;
346 1         3 my $bv = eval { $self->info->device('bios')->version; };
  1         4  
347 1 50 33     523 return if $@ || ! $bv;
348 0         0 push @{ $rv_ref }, [ 'BIOS Version' => $bv ];
  0         0  
349 0         0 return;
350             }
351              
352             1;
353              
354             __END__
355              
356             =pod
357              
358             =encoding UTF-8
359              
360             =head1 NAME
361              
362             App::Sys::Info
363              
364             =head1 VERSION
365              
366             version 0.26
367              
368             =head1 SYNOPSIS
369              
370             Run C<psysinfo> from the command line.
371              
372             =head1 DESCRIPTION
373              
374             The output is similar to I<systeminfo> windows command.
375              
376             =head1 NAME
377              
378             App::Sys::Info - Application of Sys::Info to gather information from the system
379              
380             =head1 METHODS
381              
382             =head2 cpu
383              
384             =head2 fhb
385              
386             =head2 info
387              
388             =head2 meta
389              
390             =head2 new
391              
392             =head2 NA
393              
394             =head2 nf
395              
396             =head2 os
397              
398             =head2 probe
399              
400             =head2 run
401              
402             =head1 AUTHOR
403              
404             Burak Gursoy
405              
406             =head1 COPYRIGHT AND LICENSE
407              
408             This software is copyright (c) 2010 by Burak Gursoy.
409              
410             This is free software; you can redistribute it and/or modify it under
411             the same terms as the Perl 5 programming language system itself.
412              
413             =cut