#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = 'v1.1.0'; # Power Sensors for ipmi enabled servers # Author: R. W. Rodolico # Date: 2020-01-27 # # IPMI power module for sysinfo client # # Revision History # # v1.1 RWR 20230205 # cleaned up the detection for if we do not have driver installed on this system # ie, binary only, probably to connect to other machines # # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script # or, if run interactively, in the parent of the modules BEGIN { use FindBin; use File::Spec; # prepend the bin directory and its parent use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); eval( 'use library;' ); die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@; } ##### ##### Change these to match your needs ##### # 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 my $modulesList = { 'Data::Dumper' => undef, }; # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become # the full path (from which or where) my $commandsList = { 'ipmitool' => undef, }; # list of operating systems this module can be used on. my $osList = { # 'mswin32' => undef, 'freebsd' => undef, 'linux' => undef, }; # the category the return data should go into. See sysinfo for a list my $CATEGORY = 'system'; ##### ##### End of required ##### # some variables needed for our system my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages my @out; # temporary location for each line of output # Try to load the modules we need. If we can not, then make a list of missing modules for error message. for my $module ( keys %$modulesList ) { eval ( "use $module;" ); push @out, "$errorPrepend Could not load $module" if $@; } if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system push @out, "$errorPrepend Invalid Operating System"; } if ( !@out && ! validCommandOnSystem ( $commandsList ) ) { push @out, "$errorPrepend Can not find some commands needed"; } if ( !@out ) { # we made it, we have everything, so do the processing ##### ##### Your code starts here. Remember to push all output onto @out ##### # some systems have ipmitool installed simply for managing other machines # but do not have ipmi themselves if ( -e '/dev/ipmi0' || -e '/dev/ipmi/0' || -e '/dev/ipmidev/0' ) { my @temp = qx( ipmitool sensor 2>/dev/null ); chomp @temp; my @current; my @voltage; my @power; my $power = 0; my $i; while ( my $line = shift @temp ) { chomp $line; my @fields = split( /\s*\|\s*/, $line ); next if $fields[1] eq 'na'; if ( $fields[0] =~ /Current.*\d*/ ) { push @current, $fields[1]; print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n"; } elsif ( $fields[0] =~ /Voltage.*\d*/ ) { push @voltage, $fields[1]; print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n"; } elsif ( $fields[0] =~ m/Power Supply.*\d*/ && $fields[2] eq 'Watts' ) { push @power, $fields[1]; print "$CATEGORY\t$fields[0] Draw\t$fields[1] $fields[2]\n"; } elsif ( $fields[0] eq 'Power Meter' ) { print "$CATEGORY\tPower Draw\t$fields[1] $fields[2]\n"; exit; } } for ( $i = 0; $i < @current; $i++ ) { $power += $current[$i] * $voltage[$i]; } for ( $i = 0; $i < @power; $i++ ) { $power += $power[$i]; } push @out, "$CATEGORY\tPower Draw\t$power Watts" if $power; } ##### ##### Your code ends here. ##### } # If we are testing from the command line (caller is undef), print the results for debugging print join( "\n", @out ) . "\n" unless caller; # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return my $return = join( "\n", @out );