# global variables our $dsl_cache_basicstats_time=0; # last time we got real data from device our $dsl_cache_basicstats_cache=10; # cache time, data older than this # is considered outdated (300 seconds) our @dsl_cache_basicstats_data=( # our array of basic stats 0, # time (last retrieved basic stats) 0, # snr_dn - signal noise margin 0, # snr_up 0, # att_dn - attenuation 0, # att_up 0, # pwr_dn - power (dBm) 0, # pwr_up 0, # capa_dn - line capacity 0, # capa_up 0, # rate_dn - actual rate 0, # rate_dn 0, # perc_dn 0, # perc_up ); sub dsl_cache_basicstats ($) { # making a new telnet connection each time a value is requested is a waste # of ressources and puts a significant load onto the device. # we retrieve all of the basic data at once, cache them and return # cached values instead of the real data if cached data is not older than # five minutes debug("dsl basicstats cache controller\n"); $current_time=time(); debug("current time: $current_time\n"); if ( ($dsl_cache_basicstats_time + $dsl_cache_basicstats_cache) > $current_time ) { debug("basic stats are not outdated - returning cached data\n"); } else { debug("basic stats are quite old - refreshing now...\n"); $comm=open_telnet(); if ( $commstatus == 9 ) { debug ("retrieving data from device\n"); @output=$comm->cmd("adsl info --stats"); if ( $commstatus == 9 ) { close_telnet($comm); $snr_dn=rwhite(substr(substr(@output[12],16),0,-5)); $snr_up=rwhite(substr(@output[12],25)); $att_dn=rwhite(substr(substr(@output[13],16),0,-5)); $att_up=rwhite(substr(@output[13],25)); $pwr_dn=rwhite(substr(substr(@output[14],16),0,-5)); $pwr_up=rwhite(substr(@output[14],25)); $capa_dn=rwhite(substr(substr(@output[15],16),0,-5)); $capa_up=rwhite(substr(@output[15],25)); $rate_dn=rwhite(substr(substr(@output[16],16),0,-5)); $rate_up=rwhite(substr(@output[16],25)); $perc_dn=100 * $rate_dn / $capa_dn; $perc_up=100 * $rate_up / $capa_up; our @dsl_cache_basicstats_data=( # our array of basic stats time(), # time $snr_dn, # snr_dn $snr_up, # snr_up $att_dn, # att_dn $att_up, # att_up $pwr_dn, # pwr_dn $pwr_up, # pwr_up $capa_dn, # capa_dn $capa_up, # capa_up $rate_dn, # rate_dn $rate_up, # rate_dn $perc_dn, # perc_dn $perc_up, # perc_up ); $dsl_cache_basicstats_time=time(); debug (@dsl_cache_basicstats_data."\n"); debug (" $dsl_cache_basicstats_data[0] $dsl_cache_basicstats_data[1] $dsl_cache_basicstats_data[2] $dsl_cache_basicstats_data[3] $dsl_cache_basicstats_data[4] $dsl_cache_basicstats_data[5] $dsl_cache_basicstats_data[6] $dsl_cache_basicstats_data[7] $dsl_cache_basicstats_data[8] $dsl_cache_basicstats_data[9] $dsl_cache_basicstats_data[10] $dsl_cache_basicstats_data[11] $dsl_cache_basicstats_data[12]\n"); } else { debug ("eeek! while retrieving data from device:\n"); debug ("connection problem. can't refresh, failing back to cached data\n"); } } else { debug ("eeek! while connecting to device:\n"); debug ("eeek! connection problem. can't refresh, failing back to cached data\n"); } } debug ("begin request processing\n"); $request=shift; debug ("request as given in argument 1: $request\n"); if ( $request == "snr" ) { debug("reqest: snr/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[1],$dsl_cache_basicstats_data[2]); } elsif ( $request == "att" ) { debug("reqest: att/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[3],$dsl_cache_basicstats_data[4]); } elsif ( $request == "pwr" ) { debug("reqest: pwr/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[5],$dsl_cache_basicstats_data[6]); } elsif ( $request == "capa" ) { debug("reqest: capa/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[7],$dsl_cache_basicstats_data[8]); } elsif ( $request == "rate" ) { debug("reqest: rate/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[9],$dsl_cache_basicstats_data[10]); } elsif ( $request == "perc" ) { debug("reqest: perc/$request\n"); my ($retval1,$retval2) = ($dsl_cache_basicstats_data[11],$dsl_cache_basicstats_data[12]); } else { debug ("fall through... oh my dear\n"); my ($retval1,$retval2) = ("error","unspecified request: $request"); } debug ("end request processing\n"); return ($retval1,$retval2); }