Download
  0: #! perl -w
  1: ###################################################################
  2: # TP-Link TD-W8920G Router Statistic
  3: ###################################################################
  4: # Copyright (c) Istvan Szikra, 2008-2011. All rights reserved.
  5: # If you want to do something with this just contact me at 
  6: #   e-mail: szikra.istvan@freemail.hu
  7: #   web: http://www.szikraistvan.hu/
  8: #        http://foton.szikraistvan.hu/blog/
  9: #
 10: # File Name   : TDW8920G.pl
 11: # Version     : 0.3
 12: # Revision    : 31
 13: # Editor Tabs : 4
 14: # Revision History / Change Log:
 15: #        https://foton.szikraistvan.hu/svn/lib/NET/mrtg/trunk
 16: ###################################################################
 17: 
 18: # Config:
 19: $routerIP="192.168.*.*";
 20: $routeruser="admin";
 21: $routerpassw="*****";
 22: 
 23: $maxspeed=2000000; # 2 Mbyte/s mrtg.cfg has to have the same value to detect overflow
 24: $maxchkinterval=6*60; # 5+1 minute
 25: 
 26: $tmpdir="tmp\\";
 27: $fnLAN=$tmpdir."LAN.html";
 28: $fnWAN=$tmpdir."WAN.html";
 29: $fnATM=$tmpdir."ATM.html";
 30: $fnIP=$tmpdir."IP.html";
 31: $fninfo=$tmpdir."info.html";
 32: $fnLANReset=$tmpdir."LANReset.html";
 33: $fnWANReset=$tmpdir."WANReset.html";
 34: $fnBOOT=$tmpdir."reboot.html";
 35: $fnADSL=$tmpdir."ADSL.html";
 36: 
 37: $CACHE_FILE = $tmpdir."router.cache";
 38: $LOG_FILE = $tmpdir."wg.log";
 39: #$LOG_FILE = "nul";
 40: 
 41: # Check parameters:
 42: if ((scalar(@ARGV) < 2) || ($ARGV[0] eq '') || ($ARGV[1] eq '')) {
 43:      print "Usage: $0 <interface number> <stat index>\n";
 44:      exit;
 45: }
 46: 
 47: ##############################
 48: # Functions:
 49: sub RetrieveStats(){
 50:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnLAN http://$routerIP/statsifc.html >NUL 2>NUL`;
 51:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnWAN http://$routerIP/statswan.cmd >NUL 2>NUL`;
 52:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnATM http://$routerIP/statsatm.cmd >NUL 2>NUL`;
 53: 
 54:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnIP http://$routerIP/wancfg.cmd?action=view >NUL 2>NUL`;
 55:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fninfo http://$routerIP/info.html >NUL 2>NUL`;
 56:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnADSL http://$routerIP/statsadsl.html >NUL 2>NUL`;
 57:   
 58: #  `curl -m 10 -u $routeruser:$routerpassw http://$routerIP/statsifc.html >$fnLAN 2>nul`;
 59: #  `curl -m 10 -u $routeruser:$routerpassw http://$routerIP/statswan.cmd >$fnWAN 2>nul`;
 60: #  `curl -m 10 -u $routeruser:$routerpassw http://$routerIP/statsatm.cmd >$fnATM 2>nul`;
 61: }
 62: 
 63: sub ExtractStats(){
 64:   open FCACHE, ">", $CACHE_FILE or die $!;
 65: 
 66:   open FLAN, $fnLAN or die $!;
 67: 	@llan=<FLAN>;
 68:   close FLAN;
 69: 	for( @llan[89..97, 121..129] ){
 70: 	  /^.*>(.*)<.*$/;
 71: 	  print FCACHE $1."\n";  
 72:     }
 73: 
 74:   open FWAN, $fnWAN or die $!;
 75: 	@lines=<FWAN>;
 76:   close FWAN;
 77: 	for( @lines[50, 54..61] ){
 78: 	  /^.*>(.*)<.*$/;
 79: 	  print FCACHE $1."\n";  
 80:     }
 81: 
 82:   open FATM, $fnATM or die $!;
 83: 	@lines=<FATM>;
 84:   close FATM;
 85: 	for( @lines[57, 73,75,77,79,74,76,78,80] ){
 86: 	  /^.*>([\w\s\d]+)<.*$/;
 87: 	  print FCACHE $1."\n";  
 88:     }
 89: 
 90: 
 91:   open FF, $fnIP or die $!;
 92: 	@lines=<FF>;
 93:   close FF;
 94: 	$lines[39] =~ /^.*>([\w\s\d\.]*)<.*$/;
 95:     print FCACHE $1."\n";  
 96: 
 97:   open FF, $fninfo or die $!;
 98: 	@lines=<FF>;
 99:   close FF;
100: 	$lines[109] =~ /^.*\'([\w\s\d\.]*)\'.*$/;
101:     print FCACHE "Default Gateway:\n";
102:     print FCACHE $1."\n";  
103: 	
104: 	for( @lines[118,119,122,123,133,134] ){
105: 	  /^.*>(.*)<.*$/;
106: 	  print FCACHE $1."\n";  
107:     }
108: 
109: # 2011.04.13 ADSL statistics
110:   open FF, $fnADSL or die $!;
111: 	@lines=<FF>;
112:   close FF;
113:     print FCACHE "ADSL:\n";
114: #	for( @lines[92,93,96,97,100,101,104,105,108,109] ){
115: 	for( @lines[87,92,102,107, 88,93,103,108] ){
116: 	  # SNR Margin (dB), Attenuation (dB), Attainable Rate (Kbps), Rate (Kbps)
117:       #print FCACHE $_."-";  
118: 	  if (/^.*>([0-9,\.]+)\&.*$/) {
119: 	    print FCACHE $1."\n";  
120: 	  } else {
121: 	    print FCACHE "0\n";  
122: 	  }
123:     }
124: 
125:   close FCACHE;  
126: }
127: 
128: # Fix OverFlow problems: Reset counters if greater than allowed, do not allow to overflow before the next update
129: # 1832910847/60/5        = 6.1 Mbyte/s
130: # (2^31-1832910847)/60/5 = 1.0 Mbyte/s
131: # 1427483648/5/60        = 4.758 Mbyte/s
132: # (2^31-1427483648)/5/60 = 2.400 Mbyte/s
133: sub ChkResetStats(){
134:   open FCACHE, $CACHE_FILE or die $!;
135:     @lcache=<FCACHE>; chop(@lcache);
136:   close FCACHE;
137:   $ovfprot= 2*1024*1024*1024 - $maxspeed*$maxchkinterval;
138:   #eth + wireless
139:   for ($i=1; $i<9; $i++){
140:     if ($lcache[$i]>=$ovfprot || $lcache[$i+9]>=$ovfprot) {
141:       # use undocumented feature:
142:       `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnLANReset http://$routerIP/statsifcreset.html >NUL 2>NUL`;
143:       last;
144:     }
145:   }
146:   #wan
147:   for ($i=1; $i<9; $i++){
148:     if ($lcache[$i+2*9]>=$ovfprot) {
149:       `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnWANReset http://$routerIP/statswanreset.html >NUL 2>NUL`;
150:       last;
151:     }
152:   }
153: }
154: 
155: sub ReBoot(){
156:   `wget --http-user=$routeruser --http-passwd=$routerpassw -o$LOG_FILE -O$fnBOOT http://$routerIP/rebootinfo.cgi >NUL`;
157: }
158: 
159: ##############################
160: # Main starts here:
161: 
162: # ha tobb mint egy perce volt letoltve a statisztika, akkor ujra letoltjuk
163: # redownload stats only if more than 1 minute has passed
164: if ( time() - (stat($CACHE_FILE))[9] > (1*60) ){	
165:     open FCACHE, $CACHE_FILE or die $!;
166:       @lcache=<FCACHE>;  chop(@lcache);  $nogateway = $lcache[38] eq ""; # previous gateway status
167:     close FCACHE;  
168: 
169: 	RetrieveStats();
170: 	ExtractStats();
171: 	ChkResetStats();
172: 
173:     open FCACHE, $CACHE_FILE or die $!;
174:       @lcache=<FCACHE>; chop(@lcache);
175:     close FCACHE;  
176:     if ($nogateway && ($lcache[38] eq "")) { ReBoot();} # + current gateway status
177: } else {
178: # else read from cache file
179:     open FCACHE, $CACHE_FILE or die $!;
180:       @lcache=<FCACHE>; chop(@lcache);
181:     close FCACHE;  
182: }
183: 
184: $descInOut="Received/Transmitted ";
185: @descr=("Bytes", "Packets", "Errors", "Drops");
186: 
187: $ifATM=3;
188: $atmdescInOut="In/Out ";
189: @atmdescr=("Octets", "Ucast Pkts", "Errors", "Discards");
190: 
191: $ifADSL=5;
192: $adslescInOut="Downstream/Upstream ";
193: @adsldescr=("SNR Margin (dB)", "Attenuation (dB)", "Attainable Rate (Kbps)", "Rate (Kbps)");
194: 
195: # interface, array index to line number:
196: $ifn=$ARGV[0];
197: $typen=$ARGV[1]%4; #0..3
198: $lnIN=$ifn*9+1+$typen;
199: $lnOUT=$lnIN+4;
200: 
201: #chop(@lcache);
202: $uptime = "??uptime??";
203: $in=$lcache[$lnIN];
204: $out=$lcache[$lnOUT];
205: #if ($in==""){ 	$in="0\n";}
206: #if ($out==""){	$out="0\n";}
207: #$_ = "0\n" unless length($_) foreach ($in,$out);
208: foreach($in,$out) {
209:  $_ = "0\n" unless length($_); 
210: }
211: 
212: if (($ifn == $ifADSL) && ($typen<=1)){
213:   $in=$in*10;   # fractional to integer
214:   $out=$out*10;
215: }
216: 
217: print $in."\n";
218: print $out."\n";
219: print $uptime."\n";
220: print substr($lcache[$ifn*9],0,-1)." ";
221: if ($ifn == $ifATM) {
222:   print $atmdescInOut.$atmdescr[$typen]."\n";
223: }elsif ($ifn == $ifADSL) {
224:   print $adslescInOut.$adsldescr[$typen]."\n";
225: }else{
226:   print $descInOut.$descr[$typen]."\n";
227: }