Rev 198 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use warnings;
use strict;
# Description: Power Sensors for ipmi enabled servers
# IPMI power module for sysinfo client
# Author: R. W. Rodolico
# Date: 2020-01-27
#
# 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
our $VERSION = '1.1';
BEGIN {
push @INC, shift;
}
use library;
# category we will use for all values found
# see sysinfo for a list of valid categories
my $CATEGORY = 'system';
# run the commands necessary to do whatever you want to do
# The library entered above has some helper routines
# validCommandOnSystem -- passed a name, returns the fully qualified path or
# '' if it does not exist
# cleanUp - passed a delimiter and a string, does the following (delimiter
# can be '')
# chomps the string (removes trailing newlines)
# removes all text BEFORE the delimiter, the delimiter, and any
# whitespace
# thus, the string 'xxI Am x a weird string' with a newline will
# become
# 'a weird string' with no newline
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
# item, name as recognized by sysinfo is the second and the value is
# the last one. For multiple entries, place on separate lines (ie, newline
# separated)
# check for commands we want to run
my %commands = (
'ipmitool' => ''
);
# check the commands for validity
foreach my $command ( keys %commands ) {
$commands{$command} = &validCommandOnSystem( $command );
}
# bail if we don't have the commands we need
exit 1 unless $commands{'ipmitool'};
# some systems have ipmitool installed simply for managing other machines
# but do not have ipmi themselves
exit 2 unless -e '/dev/ipmi0' || -e '/dev/ipmi/0' || -e '/dev/ipmidev/0';
my @temp = qx( $commands{'ipmitool'} sensor 2>/dev/null );
exit 2 unless @temp; # ipmitool installed, but driver not. Probably using to connect someplace else.
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];
}
print "$CATEGORY\tPower Draw\t$power Watts\n" if $power;
exit 0;