20 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
26 |
rodolico |
3 |
use strict;
|
2 |
rodolico |
4 |
|
26 |
rodolico |
5 |
our $VERSION = '1.0';
|
20 |
rodolico |
6 |
|
|
|
7 |
# Basic Unix module for sysinfo client
|
|
|
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2016-04-08
|
|
|
10 |
|
2 |
rodolico |
11 |
# gets additional systems information on Linux machine using some standard
|
|
|
12 |
# utilities
|
|
|
13 |
|
|
|
14 |
BEGIN {
|
|
|
15 |
push @INC, shift;
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
use library;
|
|
|
19 |
|
|
|
20 |
# the commands this script will use
|
|
|
21 |
my %commands = (
|
|
|
22 |
'free' => '',
|
|
|
23 |
'awk' => '',
|
|
|
24 |
'grep' => '',
|
|
|
25 |
'tail' => '',
|
|
|
26 |
'uname' => ''
|
|
|
27 |
);
|
|
|
28 |
|
|
|
29 |
# The files/directories the script will use
|
|
|
30 |
my %directory = (
|
|
|
31 |
'/proc/cpuinfo' => 0,
|
|
|
32 |
'/proc/uptime' => 0,
|
|
|
33 |
'/tmp/rodolico' => 0
|
|
|
34 |
);
|
|
|
35 |
|
|
|
36 |
# check the commands for validity
|
|
|
37 |
foreach my $command ( keys %commands ) {
|
|
|
38 |
$commands{$command} = &validCommandOnSystem( $command );
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
# check the file/directory exists
|
|
|
42 |
foreach my $dir ( keys %directory ) {
|
|
|
43 |
$directory{$dir} = 1 if (-e $dir);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
# category we will use for all values found
|
|
|
47 |
# see sysinfo for a list of valid categories
|
|
|
48 |
my $CATEGORY = 'system';
|
|
|
49 |
|
|
|
50 |
# make sure the commands will work before we run them.
|
|
|
51 |
if ( $commands{'awk'} && $commands{'grep'} ) { # we need grep and awk for all these
|
|
|
52 |
print "$CATEGORY\tmemory\t" . &cleanUp('', qx(free | grep Mem | awk '{print \$2}')) . "\n" if $commands{'free'};
|
|
|
53 |
if ( $directory{'/proc/cpuinfo'} ) { # and we need /proc/cpuinfo file for these
|
|
|
54 |
print "$CATEGORY\tnum_cpu\t" . &cleanUp('', qx(cat /proc/cpuinfo | grep processor | wc -l | awk '{print \$1}')) . "\n";
|
|
|
55 |
print "$CATEGORY\tcpu_speed\t" . &cleanUp('', qx(cat /proc/cpuinfo | grep MHz | tail -n1 | awk '{print \$4}')) . "\n";
|
|
|
56 |
print "$CATEGORY\tcpu_type\t" . &cleanUp('', qx(cat /proc/cpuinfo | grep vendor_id | tail -n 1 | awk '{print \$3}')) . "\n";
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
# all this needs is uname
|
|
|
60 |
print "$CATEGORY\tcpu_sub\t" . &cleanUp('', qx(uname -m)) . "\n" if $commands{'uname'};
|
|
|
61 |
|
|
|
62 |
# need /proc/uptime to get the uptime
|
|
|
63 |
if ( $directory{'/proc/uptime'} ) {
|
|
|
64 |
my $uptime = qx(cat /proc/uptime);
|
|
|
65 |
$uptime =~ m/(\d+)/;
|
|
|
66 |
$uptime = int($1); # uptime now has the up time in seconds
|
|
|
67 |
print "$CATEGORY\tlast_boot\t" . (time - $uptime) . "\n";
|
|
|
68 |
print "$CATEGORY\tuptime\t" . $uptime . "\n";
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
exit 0;
|