#!/usr/bin/perl # s e n s o r s - e x t r a c t ---------------------- # author: Soup Nazi # version: 1.0.0 # description: parse the output of 'sensors' and # fetch what you want # t h i s m a c h i n e k i l l s f a s c i s t s - our $NAME = "sensors-extract"; our $VERSION = "1.0.0"; our $SCALE = "f"; my %args = @ARGV; print fetch_help() if !%args; my %cswt = ( -f => ["fetch_info", "\tFan.\n\tOptions:\tchassis, cpu, power.\n\tUsage:\t\t\$ ./sensor_utils.pl -f cpu\n"], -t => ["fetch_info", "\tTemperature.\n\tOptions:\tcpu, mb, power.\n\tUsage:\t\t\$ ./sensor_utils.pl -t cpu\n"], -h => ["fetch_help", "\tHelp.\n\tPrint help dialog.\n\tUsage:\t\t\$ ./$NAME -h\n"], ); my @cswt = keys %cswt; foreach my $key (keys %args) { # make sure we've been given valid command switches: if (!grep /$key/, @cswt) { print "Invalid argument: \"$key\".\nValid arguments:\n"; foreach my $arg (@cswt) { print "$arg $cswt{$arg}[1]"; } exit; } # good ... we have valid command switches, now to # run the prorgam and collect data: my $data = `sensors -$SCALE`; # remove -$SCALE for celsius { no strict 'refs'; print $cswt{$key}[0]($key, $args{$key}, $data); } } sub fetch_info { my ($type, $arg, $data) = @_; # valid arguments: my (@types, @valid); @types = qw(-f -t); @valid = qw(chassis cpu power) if $type eq $types[0]; @valid = qw(cpu mb power) if $type eq $types[1]; _sanity_check($arg, @valid); # good, now parse data according to use-case: my ($reg_ex1, $reg_ex2); ($reg_ex1 = "Chassis Fan:", $reg_ex2 = "\\(min") if $arg eq $valid[0] && $type eq $types[0]; ($reg_ex1 = "CPU Fan:", $reg_ex2 = "\\(min") if $arg eq $valid[1] && $type eq $types[0]; ($reg_ex1 = "Power Fan:", $reg_ex2 = "\\(min") if $arg eq $valid[2] && $type eq $types[0]; ($reg_ex1 = "\\(AMD\\):", $reg_ex2 = "\\(high") if $arg eq $valid[0] && $type eq $types[1]; ($reg_ex1 = "M/B Temp:", $reg_ex2 = "\\(high") if $arg eq $valid[1] && $type eq $types[1]; ($reg_ex1 = "Power Temp:", $reg_ex2 = "\\(high") if $arg eq $valid[2] && $type eq $types[1]; my ($junk, $outp) = split /$reg_ex1/, $data; my ($outp, $junk) = split /$reg_ex2/, $outp; $outp =~ s/^\s*//gsx; $outp =~ s/\s*$//gsx; return $outp; } sub fetch_help { my $help_msg =<## $ /usr/sbin/sensors-extract -t cpu #### $ /usr/sbin/sensors-extract -f power