home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / iprates.zip / iprates.cmd next >
OS/2 REXX Batch file  |  1996-07-01  |  11KB  |  389 lines

  1. /*
  2. Graphical display of the TCP/IP connection showing:
  3.     Momentary and overall data recieve rate
  4.     Percent utilization (active time/total time)
  5.     Momentary and overall data send rate
  6.  
  7. Requires: TCP/IP and VREXX
  8.  
  9. Parameters: Single, numeric value representing the amount
  10. of time (seconds) to wait between TCP/IP poll operations.
  11. Missing or invalid(character) values result in default wait
  12. of 10 seconds.
  13.  
  14. John Ross Porter
  15. */
  16. SAY 'Graphical display of TCP/IP connection'
  17. SAY 'joropo@notes.cc.bellcore.com              1 July 1996'
  18.  
  19. PARSE ARG sltm .
  20.  
  21. /* Validate parameters */
  22. IF ( DATATYPE( sltm ) = "CHAR" ) THEN DO
  23.     sltm = 10
  24.     SAY "Default sleep time set (10 sec.)"
  25.     END
  26.  
  27. IF ( sltm < 1 ) THEN DO
  28.     sltm = 10
  29.     SAY "Default sleep time set (10 sec.)"
  30.     END
  31.  
  32. /* Variable/ counter initializations */    
  33. ttl_time = 0
  34. idl_time = 0
  35. atv_time = 0
  36.  
  37. first_bytrec = 0
  38. first_bytsnt = 0
  39.  
  40. last_bytrec = 0
  41. last_bytsnt = 0
  42.  
  43. /* initialize VREXX */
  44. IF RxFuncQuery( 'VInit' ) THEN DO                       
  45.     call RxFuncAdd 'VInit', 'VREXX', 'VINIT'
  46.     END
  47.  
  48. initcode = VInit()
  49. IF initcode = 'ERROR' THEN SIGNAL CLEANUP
  50.  
  51. SIGNAL ON HALT    NAME CLEANUP
  52. SIGNAL ON ERROR   NAME BUMMED
  53. SIGNAL ON SYNTAX  NAME BUMMED
  54. SIGNAL ON FAILURE NAME CLEANUP
  55.  
  56. /* check whether RxFuncs are loaded, if not, load them */    
  57. IF RxFuncQuery( 'SysLoadFuncs' ) THEN DO                       
  58.     CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  59.     CALL SysLoadFuncs                                        
  60.     END                                                      
  61.  
  62. /* Initialize VREXX window */
  63. CALL Init_Vwin
  64.  
  65. /* Start of polling loop */
  66. DO forever
  67.  
  68.     /* wait */
  69.     CALL syssleep sltm
  70.  
  71.     /* Ask for current byte counts */              
  72.     "@NETSTAT -n | RXQUEUE "
  73.  
  74.     /* pick out the poll data we want */
  75.     CALL RawData
  76.  
  77.     /* How long since last here? */
  78.     now = TIME( "Reset" )
  79.  
  80.     /* Time online (polling) */
  81.     ttl_time  = ttl_time + now
  82.    
  83.     IF ( bytrec = '' ) THEN
  84.         LEAVE
  85.  
  86.     IF ( now > 0 ) THEN DO /* have enough to start drawing */
  87.         /* rate since last poll and since polling started */
  88.         recv_rate = (bytrec - last_bytrec)  / now
  89.         send_rate = (bytsnt - last_bytsnt)  / now
  90.         rovr_rate = (bytrec - first_bytrec) / ttl_time
  91.         sovr_rate = (bytsnt - first_bytsnt) / ttl_time
  92.  
  93.         /* anything happen since we last looked? */
  94.         IF ( recv_rate = 0 & send_rate = 0 ) THEN
  95.             idl_time = idl_time + now
  96.         ELSE
  97.             atv_time = atv_time + now
  98.  
  99.         /* update display */
  100.         CALL VUpdate
  101.         END  /* Do */
  102.     ELSE DO /* first time around polling loop */
  103.         /* set base for overall rate calculations */
  104.         first_bytrec = bytrec
  105.         first_bytsnt = bytsnt
  106.         END  /* Do */
  107.  
  108.     /* new base for momentary rate calculations */
  109.     last_bytrec = bytrec
  110.     last_bytsnt = bytsnt
  111.     END /* forever */
  112.  
  113. /* done */
  114. SIGNAL CLEANUP
  115.  
  116. /*====================================================================
  117. In case of trouble 
  118.     (very useful/necessary while developing!)
  119. ====================================================================*/
  120. BUMMED:
  121. TRACE OFF
  122. SAY CONDITION('C') "signaled:" CONDITION('D') "at line" sigl
  123.  
  124. /*====================================================================
  125. MUST execute the following code NO MATTER WHAT 
  126. to gracefully shutdown VREXX
  127. ====================================================================*/
  128. CLEANUP:
  129. /* some final statistics */
  130. IF ( last_bytrec > 0 ) THEN DO
  131.     SAY "recieved:" FORMAT( last_bytrec, 9) "bytes",
  132.         "    Rate:" FORMAT( rovr_rate, 6, 1) "bytes/sec"
  133.     SAY "    sent:" FORMAT( last_bytsnt, 9) "bytes",
  134.         "    Rate:" FORMAT( sovr_rate, 6, 1) "bytes/sec"
  135.     SAY "    time:" FORMAT( ttl_time, 4, 2) "seconds",
  136.         " Utilization:" FORMAT( 100 * atv_time / ttl_time, 3, 2)"%"
  137.     END
  138. ELSE
  139.     SAY "No TCP/IP Connection present"
  140.  
  141. /* pause to keep final display until user dismisses */
  142. SAY "Enter to complete"
  143. PULL ans
  144.  
  145. /* terminate VREXX */
  146. CALL VExit
  147.  
  148. EXIT
  149. /*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
  150.  
  151. /*====================================================================
  152. initialize data structures for VREXX interface
  153.     define initial window
  154.     position, within window, of the three graphs
  155.     label graphs
  156.     initialize graph data
  157. ====================================================================*/
  158. Init_Vwin:
  159.  
  160. BackGround = 'BLACK'
  161.  
  162. win_pos.top    = 100
  163. win_pos.bottom = 75
  164. win_pos.left   = 0
  165. win_pos.right  = 25
  166. wid = VOpenWindow( 'TCP/IP Utilization', BackGround, win_pos)
  167.  
  168. np = 50
  169. M_ofst = 1
  170. X_ofst = 2
  171. W_ofst = 3
  172.  
  173. rpos.M_ofst = 0
  174. rpos.X_ofst = 10
  175. rpos.W_ofst = 384
  176.  
  177. upos.M_ofst = 100
  178. upos.X_ofst = 404
  179. upos.W_ofst = 192
  180.  
  181. spos.M_ofst = 0
  182. spos.X_ofst = 606
  183. spos.W_ofst = 384
  184.  
  185. Ymin = 10
  186. Ymax = 900
  187.  
  188. recv.0 = "GREEN"
  189. rovr.0 = "CYAN"
  190. util.0 = "YELLOW"
  191. send.0 = "RED"
  192. sovr.0 = "PINK"
  193.  
  194. CALL VForeColor wid, "WHITE"
  195. CALL Vsay       wid, 250, 500, "Wait..Wait..Wait"
  196. axis.0 = "WHITE"
  197. axis.1 = Ymax + 25
  198. axis.2 = Ymax
  199. axis.3 = axis.2
  200. axis.4 = axis.1
  201. axis.5 = Ymin
  202.  
  203. raxs.0 = 5
  204. raxs.1 = rpos.X_ofst
  205. raxs.2 = raxs.1
  206. raxs.3 = rpos.X_ofst + rpos.W_ofst
  207. raxs.4 = raxs.3
  208. raxs.5 = raxs.3
  209.  
  210. uaxs.0 = 4
  211. uaxs.1 = upos.X_ofst
  212. uaxs.2 = uaxs.1
  213. uaxs.3 = upos.X_ofst + upos.W_ofst
  214. uaxs.4 = uaxs.3
  215.  
  216. saxs.0 = 5
  217. saxs.1 = spos.X_ofst + spos.W_ofst
  218. saxs.2 = saxs.1
  219. saxs.3 = spos.X_ofst
  220. saxs.4 = saxs.3
  221. saxs.5 = saxs.3
  222.  
  223. pdelt = (Ymax - Ymin) / np
  224. DO i = 1 TO np
  225.     recv.i = rpos.X_ofst
  226.     rovr.i = rpos.X_ofst
  227.     util.i = upos.X_ofst
  228.     send.i = spos.X_ofst
  229.     sovr.i = spos.X_ofst
  230.     Y.i = Ymin + (i - 1) * pdelt
  231.     END     
  232.  
  233. RETURN
  234.  
  235. /*====================================================================
  236. (re)draw the graphs based upon current data
  237. ====================================================================*/
  238. VUpdate:
  239. CALL VClearWindow wid
  240.  
  241. CALL VForeColor  wid, axis.0
  242. CALL VDraw       wid, 'LINE', raxs, axis, raxs.0
  243. CALL VDraw       wid, 'LINE', uaxs, axis, uaxs.0
  244. CALL VDraw       wid, 'LINE', saxs, axis, saxs.0
  245.  
  246. CALL VForeColor  wid, rovr.0
  247. CALL Vsay        wid, rpos.X_ofst, Ymax + 1, "Recv"
  248.  
  249. CALL VForeColor  wid, util.0
  250. CALL Vsay        wid, upos.X_ofst, Ymax + 1, "Util"
  251.  
  252. CALL VForeColor  wid, sovr.0
  253. CALL Vsay        wid, spos.X_ofst, Ymax + 1, "Send"
  254.  
  255. /* new 'momentary rate' line */
  256. numax = Rate_Append( recv_rate,  "recv", "rpos", 0 )
  257. CALL VForeColor  wid,         recv.0
  258. CALL VSay        wid, rpos.X_ofst + rpos.W_ofst - 120, Ymax + 1,,
  259.                       FORMAT(rpos.M_ofst, ,0)
  260. CALL VDraw       wid, 'LINE', recv, Y, np
  261.  
  262. /* new 'overall rate' line */
  263. CALL Rate_Append rovr_rate,  "rovr", "rpos", numax
  264. CALL VForeColor  wid,         rovr.0
  265. CALL VDraw       wid, 'LINE', rovr, Y, np
  266.  
  267. /* new 'utilization' line */
  268. CALL Rate_Append 100 * (atv_time / ttl_time), "util", "upos", 0
  269. CALL VForeColor  wid,                          util.0
  270. CALL VDraw       wid, 'LINE',                  util, Y, np
  271.  
  272. /* new 'momentary' rate line */
  273. numax = Rate_Append( send_rate,  "send", "spos", 0 )
  274. CALL VForeColor  wid,         send.0
  275. CALL VSay        wid, spos.X_ofst + spos.W_ofst - 120, Ymax + 1,,
  276.                       FORMAT(spos.M_ofst, ,0)
  277. CALL VDraw       wid, 'LINE', send, Y, np
  278.  
  279. /* new 'overall' rate line */
  280. CALL Rate_Append sovr_rate,  "sovr", "spos", numax
  281. CALL VForeColor  wid,         sovr.0
  282. CALL VDraw       wid, 'LINE', sovr, Y, np
  283.  
  284. RETURN
  285.  
  286. /*====================================================================
  287. Very Busy Routine
  288.     called five times for each poll interval
  289.     updates and rescales if necessary the 'rate' data arrays
  290. ====================================================================*/
  291. Rate_Append: 
  292.  
  293. R  = ARG(1) /* rate value */
  294. A  = ARG(2) /* 'name' of rate array */
  295. P  = ARG(3) /* 'name' of chart position parameters */
  296. nm = ARG(4) /* is 1 if new maximum was set by prior call */
  297.  
  298. Rm = VALUE(P".M_ofst")  /* current rate maximum */
  299. Z  = VALUE(P".X_ofst")  /* graph 'zero' position */
  300. W  = VALUE(P".W_ofst")  /* graph width or 'max rate' offset */
  301.  
  302. /* have to work 'real hard' if new maximum rate present */
  303. IF ( R > Rm ) THEN DO
  304.     /* rate-rescale constants */
  305.     K1 = Rm / R
  306.     K2 = Z * (R - Rm) / R
  307.  
  308.     /* save new maximum */
  309.     CALL VALUE P".M_ofst", R
  310.     Rm = R
  311.  
  312.     readj = 1 /* flag on */
  313.     END  /* Do */
  314. ELSE
  315.     readj = 0 /* flag off - no readjustment necessary - whew */
  316.  
  317. /* scroll the values in the rate array */
  318. DO i = np TO 2 BY -1
  319.     j = i - 1
  320.     Aj = VALUE(A".j")
  321.     IF ( readj = 1 | nm = 1 ) THEN /* new maximum */
  322.         /* Dangerous - Dangerous */
  323.         CALL VALUE A".i",  Aj * K1 + K2
  324.         /* reusing K1 & K2 from last time here */
  325.     ELSE /* just copy */
  326.         CALL VALUE A".i", Aj
  327.     END
  328.  
  329. /*put 'newest' value into rate array */
  330. IF ( Rm > 0 ) THEN
  331.     CALL VALUE A".1", Z + ((R * W) / Rm)
  332. ELSE
  333.     CALL VALUE A".1", Z
  334.  
  335. RETURN( readj )
  336.  
  337. /*====================================================================
  338. remove results of NETSTAT command from queue 
  339. and parse out the values we want.
  340.             Sample Queue Data
  341. ----------------------------------------------------------------------
  342. Interface 10: Serial Interface ppp0
  343. physical address    000000000000      MTU 1500
  344.  
  345. speed 115200 bits/sec
  346. unicast packets received 7102
  347. broadcast packets received 0
  348. total bytes received 3446636
  349. unicast packets sent 6446
  350. broadcast packets sent 0
  351. total bytes sent 280569
  352. packets discarded on transmission 0
  353. packets discarded on reception 0
  354. received packets in error 0
  355. errors trying to send 0
  356. packets received in unsupported protocols 0
  357. ----------------------------------------------------------------------
  358. PULL translates lowercase to uppercase
  359. ====================================================================*/
  360. RawData: 
  361.  
  362. bytrec = ''
  363. bytsnt = ''
  364. st = 1
  365.  
  366. DO WHILE (queued() > 0)
  367.  
  368.     PULL li
  369.  
  370.     SELECT
  371.        WHEN ( st = 1 ) THEN DO
  372.           IF ( pos('TOTAL BYTES RECEIVED', li) > 0 ) THEN DO
  373.              st = 2
  374.              PARSE var li . . . bytrec .
  375.              END  /* Do */
  376.           END  /* Do */
  377.        WHEN ( st = 2 ) THEN DO
  378.           IF ( pos('TOTAL BYTES SENT', li) > 0 ) THEN DO
  379.              st = 3
  380.              PARSE var li . . . bytsnt .
  381.              END  /* Do */
  382.           END  /* Do */
  383.        OTHERWISE
  384.        END  /* select */
  385.     END /* do */
  386.  
  387. RETURN
  388.  
  389.