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