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