#!/usr/local/bin/perl # # Get one airtalk reading. # Check if the cache file is up to date. If not get a new copy from # metal1's web server. Then search it for the reading. # # by Doke Scott, 2008.7.7 # # $Header: /home/metal1.nss/usra/monitor/airtalk/RCS/airtalk_get_one_reading,v 1.4 2008/07/22 20:07:54 doke Exp $ use vars qw( $cachedir $cachetime $remoteurl ); $remoteurl = "http://metal1.nss/airtalk"; $cachedir = "/var/tmp"; $cachetime = 300; # seconds cache is good for ################## use warnings; use strict; use Getopt::Long; use vars qw( $host $statpat $verbose $help ); ############### sub usage { my( $rc ) = @_; warn "Usage: $0 -H -s \n"; exit $rc; } Getopt::Long::Configure ("bundling"); GetOptions( 'H=s' => \$host, 's=s' => \$statpat, 'v+' => \$verbose, 'h' => \$help, ); &usage( 0 ) if ( $help ); &usage( 1 ) if ( ! $host ); &usage( 1 ) if ( ! $statpat ); get_reading( $statpat ); exit 0; ################# sub get_reading { my( $statpat ) = @_; my( $cachefile, $device, $location, $type, $thresh, $reading, $error, $last ); $cachefile = "$cachedir/$host.readings"; if ( ! -e $cachefile || -M _ > ( $cachetime / 86400 ) ) { refresh_cache(); } if ( ! open( fH, $cachefile ) ) { die "can't open $cachefile: $!\n"; } #Device Location Type Thresh. Reading Last #-------------------------------------------------------------- # 1-1 MORRIS POWER OFF CPAMS TD CLOSED OK OK # 1-2 MORRIS EXCESS RUN CPAMS TD CLOSED OK OK # 1-3 HULLIHEN POWER OFF CPAMS TD CLOSED OK OK # 1-4 HULLIHEN EXCESS RUN CPAMS TD CLOSED OK OK # 2-1 MORRIS DRYER 018A CPA/30 9.0 11.1 10.7 # 2-2 HULLIHEN DRYER 007 CPA/30 9.0 10.5 10.2 # 2-3 MANIFOLD FLOW CF/47.5 65.0 61.5 50.0 # 2-4 MANIFOLD PRESSURE CPA/30 9.0 10.1 9.9 # 2-5 WOLF BASEMENT CPA/30 4.0 5.6 5.6 # 2-6 DUPONT COMM ROOM CPA/30 4.0 5.7 5.7 # 2-7 SHARP LAB BASEMENT CPA/30 7.0 8.9 8.9 # 2-8 EVANS BASEMENT CPA/30 4.0 5.1 5.1 # 2-9 BROWN MH-NC28MH CPA/30 4.0 4.8 4.8 # 2-10 MITCHELL MH-NC18MH CPA/30 8.0 9.2 9.2 # 2-11 ALISON BASEMENT CPA/30 8.0 8.6 8.6 # 2-12 CANNON MH-NC40MH CPA/30 7.0 7.2 7.2 # 2-13 ALISON MH-NC33MH CPA/30 8.0 8.8 8.7 # 2-14 MORRIS MH-NC32MH CPA/30 7.5 8.4 8.4 # 2-15 ------- ------ ------ ------ # 2-16 ------- ------ ------ ------ # 3-1 mySwitch_50 # errors look like this # 2-3 MANIFOLD FLOW CF/47.5 65.0 66.5 * 62.0 # 2-6 DUPONT COMM ROOM CPA/30 4.0 OPEN SHORT # 2-12 CANNON MH-NC40MH CPA/30 7.0 BAD TD BAD TD while ( ) { chomp; if ( m!^ \s* (\d-\d+) \s+ (.*?) \s+ (CPAMS\sTD|CPA/\d+|CF/[\d\.]+|-+) \s+ (OK|[\d\.]+|OPEN|CLOSED|SHORT|BAD\sTD|-+) \s+ (OK|[\d\.]+|OPEN|CLOSED|SHORT|BAD\sTD|-+) \s* (\*)? \s+ (OK|[\d\.]+|OPEN|CLOSED|SHORT|BAD\sTD|-+) \s* $!ix ) { ( $device, $location, $type, $thresh, $reading, $error, $last ) = ( $1, $2, $3, $4, $5, $6, $7 ); #print ">$device, $location, $type, $thresh, $reading, $last\n"; if ( $location =~ m/$statpat/i ) { if ( $reading =~ m/^[\d\.]+$/ ) { # good as is } elsif ( $reading eq 'OK' ) { $reading = 1.0; } elsif ( $reading eq 'CLOSED' ) { $reading = 2.0; } elsif ( $reading eq 'OPEN' ) { $reading = -2.0; } elsif ( $reading eq 'SHORT' ) { $reading = -3.0; } elsif ( $reading eq 'BAD TD' ) { $reading = -4.0; } else { $reading = -1.0; } print "$reading\n"; return; } } } print "-1.0 not found\n"; } sub refresh_cache { my( $cachefile, $url, $cmd ); $cachefile = "$cachedir/$host.readings"; unlink $cachefile; $url = "$remoteurl/$host.readings"; $cmd = "/usr/sfw/bin/wget -q -O '$cachefile' '$url'"; system( $cmd ); if ( ! -e $cachefile || -M _ > ( $cachetime / 86400 ) ) { die "couldn't refresh $cachefile\n"; } }