108 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
|
|
|
6 |
# find our location and use it for searching for libraries
|
|
|
7 |
BEGIN {
|
|
|
8 |
use FindBin;
|
|
|
9 |
use File::Spec;
|
|
|
10 |
# uncomment the following to use libraries in our directory
|
|
|
11 |
# use lib File::Spec->catdir($FindBin::Bin);
|
|
|
12 |
# eval( 'use YAML::Tiny' );
|
|
|
13 |
}
|
|
|
14 |
|
|
|
15 |
# contains the directory our script is in
|
|
|
16 |
my $sourceDir = File::Spec->catdir($FindBin::Bin);
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
# define the version number
|
|
|
21 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
|
|
22 |
use version; our $VERSION = version->declare("v1.0.0");
|
|
|
23 |
use Data::Dumper;
|
|
|
24 |
|
|
|
25 |
# see https://perldoc.perl.org/Getopt/Long.html
|
|
|
26 |
use Getopt::Long;
|
|
|
27 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
|
|
28 |
Getopt::Long::Configure ("bundling");
|
|
|
29 |
|
|
|
30 |
# following variables modified by cli parameters
|
|
|
31 |
|
|
|
32 |
my $hostname = `hostname -f`; chomp $hostname;
|
|
|
33 |
my $version = 0;
|
|
|
34 |
my $help = 0;
|
|
|
35 |
my $dryRun = 0;
|
|
|
36 |
my $verbose = 0;
|
|
|
37 |
|
|
|
38 |
#################################################################################################
|
|
|
39 |
# Libraries
|
|
|
40 |
#################################################################################################
|
|
|
41 |
|
|
|
42 |
# simple display if --help is passed
|
|
|
43 |
sub help {
|
|
|
44 |
print <<END
|
|
|
45 |
$0 --verbose=x --dryrun --help --version --hostname=xxxxx
|
|
|
46 |
--verbose - set the verbosity (0-5).
|
|
|
47 |
--version - display version and exit
|
|
|
48 |
--hostname - explicitly set hostname. set as `hostname -f` if not set
|
|
|
49 |
|
|
|
50 |
Filter for ipmi sensor output which grabs power readings
|
|
|
51 |
|
|
|
52 |
Example Code
|
|
|
53 |
# get CSV file for local machine
|
|
|
54 |
ipmitool sensor | $0 > `hostname -f`.csv
|
|
|
55 |
|
|
|
56 |
# Gather several machines via ssh
|
|
|
57 |
ssh server_name_or_alias 'hostname -f ; sudo -S ipmitool sensor' > aaee && FNAM=$(head -1 aaee) ; sed '1d' aaee > \$FNAM.sensors
|
|
|
58 |
|
|
|
59 |
# With a directory full of `hostname -f`.sensors files,
|
|
|
60 |
# parse all into one CSV for CAMP | Attributes | Bulk Load
|
|
|
61 |
# parseSensors will automagically remove .sensors off of any hostname passed in
|
|
|
62 |
( echo -e "device\tattribute\tvalue" ; for i in `ls *.sensors` ; do $0 --hostname=\$i < "\$i" ; done ) > powerCalcs.csv
|
|
|
63 |
END
|
|
|
64 |
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
#################################################################################################
|
|
|
70 |
# Main Code
|
|
|
71 |
#################################################################################################
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
# handle any command line parameters that may have been passed in
|
|
|
75 |
|
|
|
76 |
GetOptions (
|
|
|
77 |
'help|h' => \$help,
|
|
|
78 |
'version' => \$version,
|
|
|
79 |
'verbose|v+' => \$verbose,
|
|
|
80 |
'hostname' => \$hostname
|
|
|
81 |
) or die "Error parsing command line\n";
|
|
|
82 |
|
|
|
83 |
if ( $help ) { &help() ; exit; }
|
|
|
84 |
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
|
|
|
85 |
|
|
|
86 |
if ( $hostname =~ m/(.*)\.sensors/ ) {
|
|
|
87 |
$hostname = $1;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
my @current;
|
|
|
91 |
my @voltage;
|
|
|
92 |
my @power;
|
|
|
93 |
my $power = 0;
|
|
|
94 |
|
|
|
95 |
while ( my $line = <> ) {
|
|
|
96 |
chomp $line;
|
|
|
97 |
my @fields = split( /\s*\|\s*/, $line );
|
|
|
98 |
next if $fields[1] eq 'na';
|
|
|
99 |
if ( $fields[0] =~ /Current.*\d*/ ) {
|
|
|
100 |
push @current, $fields[1];
|
|
|
101 |
print "$hostname\tPower $fields[0]\t$fields[1] $fields[2]\n";
|
|
|
102 |
} elsif ( $fields[0] =~ /Voltage.*\d*/ ) {
|
|
|
103 |
push @voltage, $fields[1];
|
|
|
104 |
print "$hostname\tPower $fields[0]\t$fields[1] $fields[2]\n";
|
|
|
105 |
} elsif ( $fields[0] eq 'Power Meter' ) {
|
|
|
106 |
print "$hostname\tPower Draw\t$fields[1] $fields[2]\n";
|
|
|
107 |
exit;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
for ( my $i = 0; $i < @current; $i++ ) {
|
|
|
112 |
$power += $current[$i] * $voltage[$i];
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
for ( my $i = 0; $i < @power; $i++ ) {
|
|
|
116 |
$power += $power[$i];
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
print "$hostname\tPower Draw\t$power Watts\n" if $power;
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
1;
|