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