#!/opt/local/bin/perl -w # -*- perl -*- =head1 NAME istas - Wildcard-plugin to monitor information from temperature and fan speed =head1 CONFIGURATION The possible wildcard values are the follwing: 'temp:', 'fan' So you would create symlinks to this plugin called istats_fan, istats_temp The plugins needs the following components configured: =over =item istats modules installed and loaded. =item istats program installed and in path. /usr/local/bin/istats =item Perl program installed and in path. /opt/local/bin/perl =back Note: Istats names are read from the output of the istats program. =head1 AUTHOR myos =head1 LICENSE GPLv2 =head1 MAGIC MARKERS #%# family=auto #%# capabilities=autoconf suggest =cut use strict; $ENV{'LANG'} = "C"; # Force parsable output from istats. $ENV{'LC_ALL'} = "C"; # Force parsable output from istats. #my $ISTATS = $ENV{'istats'} || 'istats'; my $ISTATS = "/usr/local/bin/istats"; # Example outputs from istats & matching regex parts: #--- CPU Stats --- #CPU temp: 29.5°C ▁▂▃▅▆▇ # #--- Fan Stats --- #Total fans in system: 1 #Fan 0 speed: 1701 RPM ▁▂▃▅▆▇ # #--- Battery Stats --- #No battery on system my %config = ( fan => { regex => qr/ ^ # String must start with: ([^:\n]*) # Match any non-whitespace char, except ':' and new line # (Match label as \$1) \s* # Zero or more spaces followed by : # : character \s* # Zero or more spaces followed by \+? # Zero or one '+' char. Note: This might not be needed # as FAN speeds don't have + signs in front of them. # This can be probably be removed as the # istats program never prints a + char in front of # RPM values. Verified in iStats v1.6.1 (\d+) # Match one or more digits (Match current value as \$2) \s # Exactly one space followed by RPM # RPM string /xm, # Use extended regular expressions and treat string as multiple lines. title => 'Fan speed', vlabel => 'RPM', warning => '2000', # wrning limit critical => '2100', # critical limit graph_args => '--base 1000 --lower-limit 1500' # initial graph lower value }, temp => { regex => qr/ ^ # String must start with: ([^:\n]*) # Match any non-whitespace char, except ':' and new line # (Match label as \$1) \s*:\s* # Zero or more spaces followed by ':' and # zero or more spaces followed by \+? # Zero or one '+' char followed by (-? # Match zero or one '-' char \d+ # Match one or more digits # (match the current voltage as \$2) followed by (?:\.\d+)?) # Zero or one match of '.' char followed by one or more digits. ° # Match degree char C # Match 'C' char /xm, # Use extended regular expressions and treat string as multiple lines. title => 'Temperature', vlabel => 'degrees Celsius', warning => '78', # warning limit critical => '100', # critical limit graph_args => '--base 1000 --lower-limit 25' # initial graph lower value }, ); if ( defined $ARGV[0] and $ARGV[0] eq 'autoconf' ) { # Now see if "istats" can run my $text = `$ISTATS 2>/dev/null`; if ($?) { if ($? == -1) { print "no (program $ISTATS not found)\n"; } else { print "no (program $ISTATS died)\n"; } exit 0; } unless ($text =~ /[° ]C/) { print "no (no temperature readings)\n"; exit 0; } print "yes\n"; exit 0; } if (defined $ARGV[0] and $ARGV[0] eq 'suggest') { my $text = `$ISTATS 2>/dev/null`; foreach my $func (keys %config) { print $func, "\n" if $text =~ $config{$func}->{regex}; } exit; } # plugin name's part after istats_ stored into $func # ex: istats_temp -> $func = 'temp' $0 =~ /istats_(.+)*$/; # switch with the link name istats_temp, istats_fan my $func = $1; my $text = `$ISTATS`; if (defined $ARGV[0] and $ARGV[0] eq 'config') { # print all unless defined $func; if (defined $func) { print "graph_title $config{$func}->{title}\n"; print "graph_vlabel $config{$func}->{vlabel}\n"; print "graph_args $config{$func}->{graph_args}\n"; print "graph_category istats\n"; my $istat = 1; while ($text =~ /$config{$func}->{regex}/g) { my ($label, undef, $max, $min) = ($1, $2, $3, $4); print "$func$istat.label $label\n"; print "$func$istat.warning $config{$func}->{warning}\n"; print "$func$istat.critical $config{$func}->{critical}\n"; $istat++; } } else { foreach my $func (keys %config) { print "graph_title $config{$func}->{title}\n"; print "graph_vlabel $config{$func}->{vlabel}\n"; print "graph_args $config{$func}->{graph_args}\n"; print "graph_args $config{$func}->{critical}\n"; print "graph_category istats\n"; my $istat = 1; while ($text =~ /$config{$func}->{regex}/g) { my ($label, undef, $max, $min) = ($1, $2, $3, $4); print "$func$istat.label $label\n"; print "$func$istat.warning $config{$func}->{warning}\n"; print "$func$istat.critical $config{$func}->{critical}\n"; $istat++; } } } exit 0; } # print all values unless defined $func; if (defined $func) { my $istat = 1; while ($text =~ /$config{$func}->{regex}/g) { print "$func$istat.value $2\n"; $istat++; } } else { foreach my $func (keys %config) { my $istat = 1; while ($text =~ /$config{$func}->{regex}/g) { print "$func$istat.value $2\n"; $istat++; } } }