| 29 | 
           rodolico | 
           1 | 
           #!/usr/bin/env perl
  | 
        
        
            | 
            | 
           2 | 
           use warnings;
  | 
        
        
           | 256 | 
           rodolico | 
           3 | 
           use strict;  
  | 
        
        
           | 29 | 
           rodolico | 
           4 | 
              | 
        
        
           | 256 | 
           rodolico | 
           5 | 
           use version ; our $VERSION = 'v1.3.0';
  | 
        
        
           | 29 | 
           rodolico | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           # IPMI network module for sysinfo client
  | 
        
        
            | 
            | 
           8 | 
           # Author: R. W. Rodolico
  | 
        
        
           | 256 | 
           rodolico | 
           9 | 
           # Date:   2016-09-17
  | 
        
        
            | 
            | 
           10 | 
           #
  | 
        
        
            | 
            | 
           11 | 
           # Enter some information about system here
  | 
        
        
            | 
            | 
           12 | 
           #
  | 
        
        
            | 
            | 
           13 | 
           # Revision History
  | 
        
        
            | 
            | 
           14 | 
           #
  | 
        
        
           | 57 | 
           rodolico | 
           15 | 
           # 20171124 RWR
  | 
        
        
            | 
            | 
           16 | 
           # Fixed where STDERR now goes to /dev/null
  | 
        
        
            | 
            | 
           17 | 
           #
  | 
        
        
           | 149 | 
           rodolico | 
           18 | 
           # 20191118 RWR v1.2.1
  | 
        
        
            | 
            | 
           19 | 
           # in some cases, ipmitool is used only to access other devices over the network
  | 
        
        
            | 
            | 
           20 | 
           # so it will not have information. Fixed so that if @temp is empty, will just exit
  | 
        
        
            | 
            | 
           21 | 
           #
  | 
        
        
           | 203 | 
           rodolico | 
           22 | 
           # 20230204 RWR v1.3.0
  | 
        
        
            | 
            | 
           23 | 
           # look for specific IPMI files to determine if the driver is installed
  | 
        
        
            | 
            | 
           24 | 
           # better than just checking @temp afterwards
  | 
        
        
            | 
            | 
           25 | 
           #
  | 
        
        
           | 29 | 
           rodolico | 
           26 | 
           # module to get network interface information for ipmi
  | 
        
        
            | 
            | 
           27 | 
           # requires ipmitool (runs ipmitool lan print)
  | 
        
        
            | 
            | 
           28 | 
           # NOTE: this takes the ipmitool output and parses it, so changes to 
  | 
        
        
           | 256 | 
           rodolico | 
           29 | 
           #       this output invalidates this module#
  | 
        
        
           | 29 | 
           rodolico | 
           30 | 
              | 
        
        
           | 256 | 
           rodolico | 
           31 | 
           # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
  | 
        
        
            | 
            | 
           32 | 
           # or, if run interactively, in the parent of the modules
  | 
        
        
           | 29 | 
           rodolico | 
           33 | 
           BEGIN {
  | 
        
        
           | 251 | 
           rodolico | 
           34 | 
              use FindBin;
  | 
        
        
            | 
            | 
           35 | 
              use File::Spec;
  | 
        
        
           | 256 | 
           rodolico | 
           36 | 
              # prepend the bin directory and its parent
  | 
        
        
            | 
            | 
           37 | 
              use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
  | 
        
        
           | 251 | 
           rodolico | 
           38 | 
              eval( 'use library;' );
  | 
        
        
           | 256 | 
           rodolico | 
           39 | 
              die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
  | 
        
        
           | 29 | 
           rodolico | 
           40 | 
           }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
           | 256 | 
           rodolico | 
           42 | 
           #####
  | 
        
        
            | 
            | 
           43 | 
           ##### Change these to match your needs
  | 
        
        
            | 
            | 
           44 | 
           #####
  | 
        
        
           | 29 | 
           rodolico | 
           45 | 
              | 
        
        
           | 256 | 
           rodolico | 
           46 | 
           # Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like
  | 
        
        
            | 
            | 
           47 | 
           my $modulesList = {
  | 
        
        
            | 
            | 
           48 | 
                   'Data::Dumper'     => undef,
  | 
        
        
            | 
            | 
           49 | 
              };
  | 
        
        
           | 29 | 
           rodolico | 
           50 | 
              | 
        
        
           | 256 | 
           rodolico | 
           51 | 
           # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
  | 
        
        
            | 
            | 
           52 | 
           # the full path (from which or where)
  | 
        
        
            | 
            | 
           53 | 
           my $commandsList = {
  | 
        
        
            | 
            | 
           54 | 
                   'ipmitool' => undef,
  | 
        
        
            | 
            | 
           55 | 
              };
  | 
        
        
           | 29 | 
           rodolico | 
           56 | 
              | 
        
        
           | 256 | 
           rodolico | 
           57 | 
           # list of operating systems this module can be used on.
  | 
        
        
            | 
            | 
           58 | 
           my $osList = {
  | 
        
        
            | 
            | 
           59 | 
           #         'mswin32' => undef,
  | 
        
        
            | 
            | 
           60 | 
                    'freebsd' => undef,
  | 
        
        
            | 
            | 
           61 | 
                    'linux'   => undef,
  | 
        
        
            | 
            | 
           62 | 
              };
  | 
        
        
           | 203 | 
           rodolico | 
           63 | 
              | 
        
        
           | 256 | 
           rodolico | 
           64 | 
           # the category the return data should go into. See sysinfo for a list
  | 
        
        
           | 29 | 
           rodolico | 
           65 | 
           my $CATEGORY = 'network';
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
           | 256 | 
           rodolico | 
           67 | 
           #####
  | 
        
        
            | 
            | 
           68 | 
           ##### End of required
  | 
        
        
            | 
            | 
           69 | 
           #####
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           # some variables needed for our system
  | 
        
        
            | 
            | 
           72 | 
           my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
  | 
        
        
            | 
            | 
           73 | 
           my @out; # temporary location for each line of output
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           # Try to load the modules we need. If we can not, then make a list of missing modules for error message.
  | 
        
        
            | 
            | 
           76 | 
           for my $module ( keys %$modulesList ) {
  | 
        
        
            | 
            | 
           77 | 
              eval ( "use $module;" );
  | 
        
        
            | 
            | 
           78 | 
              push @out, "$errorPrepend Could not load $module" if $@;
  | 
        
        
           | 29 | 
           rodolico | 
           79 | 
           }
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
           | 256 | 
           rodolico | 
           81 | 
           if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
  | 
        
        
            | 
            | 
           82 | 
               push @out, "$errorPrepend Invalid Operating System";
  | 
        
        
           | 29 | 
           rodolico | 
           83 | 
           }
  | 
        
        
           | 256 | 
           rodolico | 
           84 | 
           if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
  | 
        
        
            | 
            | 
           85 | 
              push @out, "$errorPrepend Can not find some commands needed";
  | 
        
        
            | 
            | 
           86 | 
           }
  | 
        
        
            | 
            | 
           87 | 
           if ( !@out ) { # we made it, we have everything, so do the processing
  | 
        
        
            | 
            | 
           88 | 
              #####
  | 
        
        
            | 
            | 
           89 | 
              ##### Your code starts here. Remember to push all output onto @out
  | 
        
        
            | 
            | 
           90 | 
              #####
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
              # some systems have ipmitool installed simply for managing other machines
  | 
        
        
            | 
            | 
           93 | 
              # but do not have ipmi themselves
  | 
        
        
            | 
            | 
           94 | 
              exit 1 unless -e '/dev/ipmi0' || -e '/dev/ipmi/0' || -e '/dev/ipmidev/0';
  | 
        
        
           | 29 | 
           rodolico | 
           95 | 
              | 
        
        
           | 256 | 
           rodolico | 
           96 | 
              my %storage = ( 
  | 
        
        
            | 
            | 
           97 | 
                              'address' => { 'key' => 'IP Address' }, 
  | 
        
        
            | 
            | 
           98 | 
                              'netmask' => { 'key' => 'Subnet Mask' },
  | 
        
        
            | 
            | 
           99 | 
                              'mac' => { 'key' => 'MAC Address' }
  | 
        
        
            | 
            | 
           100 | 
                            );
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
              my @temp = qx( ipmitool lan print 2> /dev/null );
  | 
        
        
            | 
            | 
           103 | 
              exit 2 unless @temp; # ipmitool installed, but driver not. Probably using to connect someplace else.
  | 
        
        
            | 
            | 
           104 | 
              chomp @temp;
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
              foreach my $line ( @temp ) {
  | 
        
        
            | 
            | 
           107 | 
                 my ( $key, $value ) = split( /\s+:\s+/, $line );
  | 
        
        
            | 
            | 
           108 | 
                 foreach my $test ( keys %storage ) {
  | 
        
        
            | 
            | 
           109 | 
                    next if defined( $storage{$test}{'value'} );
  | 
        
        
            | 
            | 
           110 | 
                    if ( $key eq $storage{$test}{'key'} ) {
  | 
        
        
            | 
            | 
           111 | 
                       $storage{$test}{'value'} = $value;
  | 
        
        
            | 
            | 
           112 | 
                       last;
  | 
        
        
            | 
            | 
           113 | 
                    } # if
  | 
        
        
            | 
            | 
           114 | 
                 } # foreach 
  | 
        
        
            | 
            | 
           115 | 
              }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
              foreach my $key ( keys %storage ) {
  | 
        
        
            | 
            | 
           118 | 
                 push @out, "$CATEGORY\tipmi\t$key\t$storage{$key}{'value'}";
  | 
        
        
            | 
            | 
           119 | 
              }
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
              #####
  | 
        
        
            | 
            | 
           122 | 
              ##### Your code ends here.
  | 
        
        
            | 
            | 
           123 | 
              #####
  | 
        
        
            | 
            | 
           124 | 
           }
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
           # If we are testing from the command line (caller is undef), print the results for debugging
  | 
        
        
            | 
            | 
           127 | 
           print join( "\n", @out ) . "\n" unless caller;
  | 
        
        
            | 
            | 
           128 | 
           # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
  | 
        
        
            | 
            | 
           129 | 
           my $return = join( "\n", @out );
  | 
        
        
            | 
            | 
           130 | 
              |