| 153 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
| 256 |
rodolico |
5 |
use version ; our $VERSION = 'v1.1.0';
|
|
|
6 |
|
|
|
7 |
# Power Sensors for ipmi enabled servers
|
| 153 |
rodolico |
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2020-01-27
|
| 203 |
rodolico |
10 |
#
|
| 256 |
rodolico |
11 |
# IPMI power module for sysinfo client
|
|
|
12 |
#
|
|
|
13 |
# Revision History
|
|
|
14 |
#
|
| 203 |
rodolico |
15 |
# v1.1 RWR 20230205
|
|
|
16 |
# cleaned up the detection for if we do not have driver installed on this system
|
|
|
17 |
# ie, binary only, probably to connect to other machines
|
| 256 |
rodolico |
18 |
#
|
| 153 |
rodolico |
19 |
|
| 256 |
rodolico |
20 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
|
|
21 |
# or, if run interactively, in the parent of the modules
|
| 153 |
rodolico |
22 |
BEGIN {
|
| 251 |
rodolico |
23 |
use FindBin;
|
|
|
24 |
use File::Spec;
|
| 256 |
rodolico |
25 |
# prepend the bin directory and its parent
|
|
|
26 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
|
|
27 |
eval( 'use library;' );
|
|
|
28 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 153 |
rodolico |
29 |
}
|
|
|
30 |
|
| 256 |
rodolico |
31 |
#####
|
|
|
32 |
##### Change these to match your needs
|
|
|
33 |
#####
|
| 153 |
rodolico |
34 |
|
| 256 |
rodolico |
35 |
# 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
|
|
|
36 |
my $modulesList = {
|
|
|
37 |
'Data::Dumper' => undef,
|
|
|
38 |
};
|
| 251 |
rodolico |
39 |
|
| 256 |
rodolico |
40 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
|
|
41 |
# the full path (from which or where)
|
|
|
42 |
my $commandsList = {
|
|
|
43 |
'ipmitool' => undef,
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
# list of operating systems this module can be used on.
|
|
|
47 |
my $osList = {
|
|
|
48 |
# 'mswin32' => undef,
|
|
|
49 |
'freebsd' => undef,
|
|
|
50 |
'linux' => undef,
|
|
|
51 |
};
|
|
|
52 |
|
|
|
53 |
# the category the return data should go into. See sysinfo for a list
|
| 153 |
rodolico |
54 |
my $CATEGORY = 'system';
|
|
|
55 |
|
| 256 |
rodolico |
56 |
#####
|
|
|
57 |
##### End of required
|
|
|
58 |
#####
|
| 153 |
rodolico |
59 |
|
| 256 |
rodolico |
60 |
# some variables needed for our system
|
|
|
61 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
|
|
62 |
my @out; # temporary location for each line of output
|
| 153 |
rodolico |
63 |
|
| 256 |
rodolico |
64 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
|
|
65 |
for my $module ( keys %$modulesList ) {
|
|
|
66 |
eval ( "use $module;" );
|
|
|
67 |
push @out, "$errorPrepend Could not load $module" if $@;
|
|
|
68 |
}
|
| 153 |
rodolico |
69 |
|
| 256 |
rodolico |
70 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
|
|
71 |
push @out, "$errorPrepend Invalid Operating System";
|
| 153 |
rodolico |
72 |
}
|
| 256 |
rodolico |
73 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
|
|
74 |
push @out, "$errorPrepend Can not find some commands needed";
|
|
|
75 |
}
|
|
|
76 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
|
|
77 |
#####
|
|
|
78 |
##### Your code starts here. Remember to push all output onto @out
|
|
|
79 |
#####
|
|
|
80 |
|
|
|
81 |
# some systems have ipmitool installed simply for managing other machines
|
|
|
82 |
# but do not have ipmi themselves
|
|
|
83 |
if ( -e '/dev/ipmi0' || -e '/dev/ipmi/0' || -e '/dev/ipmidev/0' ) {
|
| 153 |
rodolico |
84 |
|
| 256 |
rodolico |
85 |
my @temp = qx( ipmitool sensor 2>/dev/null );
|
|
|
86 |
chomp @temp;
|
| 153 |
rodolico |
87 |
|
| 256 |
rodolico |
88 |
my @current;
|
|
|
89 |
my @voltage;
|
|
|
90 |
my @power;
|
|
|
91 |
my $power = 0;
|
|
|
92 |
my $i;
|
| 203 |
rodolico |
93 |
|
| 256 |
rodolico |
94 |
while ( my $line = shift @temp ) {
|
|
|
95 |
chomp $line;
|
|
|
96 |
my @fields = split( /\s*\|\s*/, $line );
|
|
|
97 |
next if $fields[1] eq 'na';
|
|
|
98 |
if ( $fields[0] =~ /Current.*\d*/ ) {
|
|
|
99 |
push @current, $fields[1];
|
|
|
100 |
print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n";
|
|
|
101 |
} elsif ( $fields[0] =~ /Voltage.*\d*/ ) {
|
|
|
102 |
push @voltage, $fields[1];
|
|
|
103 |
print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n";
|
|
|
104 |
} elsif ( $fields[0] =~ m/Power Supply.*\d*/ && $fields[2] eq 'Watts' ) {
|
|
|
105 |
push @power, $fields[1];
|
|
|
106 |
print "$CATEGORY\t$fields[0] Draw\t$fields[1] $fields[2]\n";
|
|
|
107 |
} elsif ( $fields[0] eq 'Power Meter' ) {
|
|
|
108 |
print "$CATEGORY\tPower Draw\t$fields[1] $fields[2]\n";
|
|
|
109 |
exit;
|
|
|
110 |
}
|
|
|
111 |
}
|
| 153 |
rodolico |
112 |
|
| 256 |
rodolico |
113 |
for ( $i = 0; $i < @current; $i++ ) {
|
|
|
114 |
$power += $current[$i] * $voltage[$i];
|
|
|
115 |
}
|
| 153 |
rodolico |
116 |
|
| 256 |
rodolico |
117 |
for ( $i = 0; $i < @power; $i++ ) {
|
|
|
118 |
$power += $power[$i];
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
push @out, "$CATEGORY\tPower Draw\t$power Watts" if $power;
|
| 153 |
rodolico |
122 |
}
|
| 256 |
rodolico |
123 |
|
|
|
124 |
#####
|
|
|
125 |
##### Your code ends here.
|
|
|
126 |
#####
|
| 153 |
rodolico |
127 |
}
|
|
|
128 |
|
| 256 |
rodolico |
129 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
|
|
130 |
print join( "\n", @out ) . "\n" unless caller;
|
|
|
131 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
|
|
132 |
my $return = join( "\n", @out );
|
| 153 |
rodolico |
133 |
|