Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
153 rodolico 1
#!/usr/bin/env perl
2
use warnings;
3
use strict;  
4
 
5
# Description: Power Sensors for ipmi enabled servers
6
 
7
our $VERSION = '1.0';
8
 
9
# IPMI power module for sysinfo client
10
# Author: R. W. Rodolico
11
# Date:   2020-01-27
12
 
13
 
14
BEGIN {
15
   push @INC, shift;
16
}
17
 
18
use library;
19
 
20
# category we will use for all values found
21
# see sysinfo for a list of valid categories
22
my $CATEGORY = 'system';
23
 
24
# run the commands necessary to do whatever you want to do
25
# The library entered above has some helper routines
26
# validCommandOnSystem -- passed a name, returns the fully qualified path or
27
# '' if it does not exist
28
# cleanUp - passed a delimiter and a string, does the following (delimiter
29
# can be '')
30
#           chomps the string (removes trailing newlines)
31
#           removes all text BEFORE the delimiter, the delimiter, and any
32
# whitespace
33
#           thus, the string 'xxI Am x  a weird string' with a newline will
34
# become
35
#           'a weird string' with no newline
36
 
37
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
38
# item, name as recognized by sysinfo is the second and the value is
39
# the last one. For multiple entries, place on separate lines (ie, newline
40
# separated)
41
 
42
# check for commands we want to run
43
my %commands = ( 
44
                  'ipmitool' => ''
45
               );
46
 
47
# check the commands for validity
48
foreach my $command ( keys %commands ) {
49
   $commands{$command} = &validCommandOnSystem( $command );
50
}
51
 
52
# bail if we don't have the commands we need
53
exit 1 unless $commands{'ipmitool'};
54
 
55
my @temp = qx( $commands{'ipmitool'} sensor 2>/dev/null );
56
exit 2 unless @temp; # ipmitool installed, but driver not. Probably using to connect someplace else.
57
chomp @temp;
58
 
59
my @current;
60
my @voltage;
61
my @power;
62
my $power = 0;
63
my $i;
64
 
65
while ( my $line = shift @temp ) {
66
   chomp $line;
67
   my @fields = split( /\s*\|\s*/, $line );
68
   next if $fields[1] eq 'na';
69
   if ( $fields[0] =~ /Current.*\d*/ ) {
70
      push @current, $fields[1];
71
      print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n";
72
   } elsif ( $fields[0] =~ /Voltage.*\d*/ ) {
73
      push @voltage, $fields[1];
74
      print "$CATEGORY\tPower $fields[0]\t$fields[1] $fields[2]\n";
75
   } elsif ( $fields[0] =~ m/Power Supply.*\d*/ && $fields[2] eq 'Watts' ) {
76
      push @power, $fields[1];
77
      print "$CATEGORY\t$fields[0] Draw\t$fields[1] $fields[2]\n";
78
   } elsif ( $fields[0] eq 'Power Meter' ) {
79
      print "$CATEGORY\tPower Draw\t$fields[1] $fields[2]\n";
80
      exit;
81
   }
82
}
83
 
84
for ( $i = 0; $i < @current; $i++ ) {
85
   $power += $current[$i] * $voltage[$i];
86
}
87
 
88
for ( $i = 0; $i < @power; $i++ ) {
89
   $power += $power[$i];
90
}
91
 
92
print "$CATEGORY\tPower Draw\t$power Watts\n" if $power;
93
 
94
exit 0;