Blame | Last modification | View Log | Download | RSS feed
#! /usr/bin/env perl
use strict;
use warnings;
# find our location and use it for searching for libraries
BEGIN {
use FindBin;
use File::Spec;
# uncomment the following to use libraries in our directory
# use lib File::Spec->catdir($FindBin::Bin);
# eval( 'use YAML::Tiny' );
}
# contains the directory our script is in
my $sourceDir = File::Spec->catdir($FindBin::Bin);
# define the version number
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
use version; our $VERSION = version->declare("v1.0.0");
use Data::Dumper;
# see https://perldoc.perl.org/Getopt/Long.html
use Getopt::Long;
# allow -vvn (ie, --verbose --verbose --dryrun)
Getopt::Long::Configure ("bundling");
# following variables modified by cli parameters
my $hostname = `hostname -f`; chomp $hostname;
my $version = 0;
my $help = 0;
my $dryRun = 0;
my $verbose = 0;
#################################################################################################
# Libraries
#################################################################################################
# simple display if --help is passed
sub help {
print <<END
$0 --verbose=x --dryrun --help --version --hostname=xxxxx
--verbose - set the verbosity (0-5).
--version - display version and exit
--hostname - explicitly set hostname. set as `hostname -f` if not set
Filter for ipmi sensor output which grabs power readings
Example Code
# get CSV file for local machine
ipmitool sensor | $0 > `hostname -f`.csv
# Gather several machines via ssh
ssh server_name_or_alias 'hostname -f ; sudo -S ipmitool sensor' > aaee && FNAM=$(head -1 aaee) ; sed '1d' aaee > \$FNAM.sensors
# With a directory full of `hostname -f`.sensors files,
# parse all into one CSV for CAMP | Attributes | Bulk Load
# parseSensors will automagically remove .sensors off of any hostname passed in
( echo -e "device\tattribute\tvalue" ; for i in `ls *.sensors` ; do $0 --hostname=\$i < "\$i" ; done ) > powerCalcs.csv
END
}
#################################################################################################
# Main Code
#################################################################################################
# handle any command line parameters that may have been passed in
GetOptions (
'help|h' => \$help,
'version' => \$version,
'verbose|v+' => \$verbose,
'hostname' => \$hostname
) or die "Error parsing command line\n";
if ( $help ) { &help() ; exit; }
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
if ( $hostname =~ m/(.*)\.sensors/ ) {
$hostname = $1;
}
my @current;
my @voltage;
my @power;
my $power = 0;
while ( my $line = <> ) {
chomp $line;
my @fields = split( /\s*\|\s*/, $line );
next if $fields[1] eq 'na';
if ( $fields[0] =~ /Current.*\d*/ ) {
push @current, $fields[1];
print "$hostname\tPower $fields[0]\t$fields[1] $fields[2]\n";
} elsif ( $fields[0] =~ /Voltage.*\d*/ ) {
push @voltage, $fields[1];
print "$hostname\tPower $fields[0]\t$fields[1] $fields[2]\n";
} elsif ( $fields[0] eq 'Power Meter' ) {
print "$hostname\tPower Draw\t$fields[1] $fields[2]\n";
exit;
}
}
for ( my $i = 0; $i < @current; $i++ ) {
$power += $current[$i] * $voltage[$i];
}
for ( my $i = 0; $i < @power; $i++ ) {
$power += $power[$i];
}
print "$hostname\tPower Draw\t$power Watts\n" if $power;
1;