gen_util.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. i1=`/usr/local/bin/snmpget -Oq -v3 -l authPriv -u USER -a SHA -A PW -x AES -X PW 10.66.201.161 ifHCInOctets.1`
  27. o1=`/usr/local/bin/snmpget -Oq -v3 -l authPriv -u USER -a SHA -A PW -x AES -X PW 10.66.201.161 ifHCOutOctets.1`
  28. i2=`/usr/local/bin/snmpget -Oq -v3 -l authPriv -u USER -a SHA -A PW -x AES -X PW 10.66.201.162 ifHCInOctets.1`
  29. o2=`/usr/local/bin/snmpget -Oq -v3 -l authPriv -u USER -a SHA -A PW -x AES -X PW 10.66.201.162 ifHCOutOctets.1`
  30. bi1=1099215991115
  31. bo1=187042593328
  32. bi2=677250570
  33. bo2=218486766
  34. in1=`echo ${i1} | cut -f2 -d' '`
  35. out1=`echo ${o1} | cut -f2 -d' '`
  36. in2=`echo ${i2} | cut -f2 -d' '`
  37. out2=`echo ${o2} | cut -f2 -d' '`
  38. #echo "${in1} ${out1} ${in2} ${out2}"
  39. btotal=1287
  40. total=$(expr \( ${in1} + ${out1} + ${in2} + ${out2} \) / 1000000000)
  41. in1=$(expr ${in1} - ${bi1})
  42. out1=$(expr ${out1} - ${bo1})
  43. in2=$(expr ${in2} - ${bi2})
  44. out2=$(expr ${out2} - ${bo2})
  45. total=$(expr ${total} - ${btotal})
  46. oi1=0
  47. oi2=0
  48. oo1=0
  49. oo2=0
  50. ototal=0
  51. if [ -f /home/jclarke/cached_util.dat ]; then
  52. cache=$(cat /home/jclarke/cached_util.dat)
  53. oi1=$(echo ${cache} | cut -f1 -d' ')
  54. oi2=$(echo ${cache} | cut -f2 -d' ')
  55. oo1=$(echo ${cache} | cut -f3 -d' ')
  56. oo2=$(echo ${cache} | cut -f4 -d' ')
  57. ototal=$(echo ${cache} | cut -f5 -d' ')
  58. cp -f /home/jclarke/cached_util.dat /home/jclarke/cached_util.dat.old
  59. fi
  60. if [ ${in1} -lt ${oi1} ]; then
  61. in1=$(expr ${in1} + ${oi1})
  62. fi
  63. if [ ${in2} -lt ${oi2} ]; then
  64. in2=$(expr ${in2} + ${oi2})
  65. fi
  66. if [ ${out1} -lt ${oo1} ]; then
  67. out1=$(expr ${out1} + ${oo1})
  68. fi
  69. if [ ${out2} -lt ${oo2} ]; then
  70. out2=$(expr ${out2} + ${oo2})
  71. fi
  72. if [ ${total} -lt ${ototal} ]; then
  73. total=$(expr ${total} + ${ototal})
  74. fi
  75. echo "${in1} ${in2} ${out1} ${out2} ${total}" > /home/jclarke/cached_util.dat
  76. #echo "total = ${total}"
  77. date=`date`
  78. total=`/usr/local/bin/perl -MNumber::Format -e "\\\$x = new Number::Format(THOUSANDS_SEP => ' '); print \\\$x->format_number(${total})"`
  79. cat <<EOF > /usr/local/www/apache24/data/utilization.html
  80. <html>
  81. <head>
  82. <meta http-equiv="content-type" a="" content="text/html; charset=UTF-8">
  83. <meta http-equiv="refresh" content="15">
  84. <title>Cisco Live Melbourne Internet Usage (GigaBytes Transfered)</title>
  85. <style type="text/css">
  86. p.rightpar {text-align: right; color: white;}
  87. p.tabletitle {font-size: 30pt; color: white;}
  88. p.headtitle {font-size: 30pt; color: white; text-align: center;}
  89. body {font-family: "Trebuchet MS", Helvetica, sans-serif; background-color: black;}
  90. .BannerTable {color: black; table-layout: fixed;}
  91. .CLTable td {text-align: center; font-size: 10pt; width: 100px;}
  92. .MainTable {color: white; table-layout: fixed; border: 0px; width: 75%; background-color: #269E7A; }
  93. .MainTable td.bignum { font-size: 144pt; text-align: right; vertical-align: text-bottom;}
  94. .MainTable td.smallnum { font-size: 50pt; text-align: left; vertical-align: text-bottom;}
  95. .MainTable td.unit { font-size: 50pt; text-align: left; vertical-align: text-bottom;}
  96. .DeviceTable { color: white; table-layout: fixed; }
  97. </style>
  98. </head>
  99. <body>
  100. <center>
  101. <table class="BannerTable" align="center" border="0" cellpadding="2" cellspacing="2" width="100%">
  102. <tbody>
  103. <tr>
  104. <td valign="top" width="15%">
  105. <br/>
  106. </td>
  107. <td align="center" style="background-color: white;" valign="top"><img alt="NOCHeader" src="cl_logo.png" width="211" height="88">
  108. </td>
  109. <td valign="top" width="15%">
  110. <p class="rightpar">Dashboard Generated: ${date}<br/>Updated
  111. every FIVE minutes</p>
  112. </td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. </center>
  117. <p class="headtitle">CiscoLive Internet WAN Traffic (Total Traffic
  118. Amount In+Out)</p>
  119. <center>
  120. <table class="MainTable">
  121. <tbody>
  122. <tr>
  123. <td class="bignum" style="width: 30%">${total}</td>
  124. <td class="smallnum" style="width: 10%"></td>
  125. <td class="unit" style="width: 20%">Gigabytes</td>
  126. </tr>
  127. </tbody>
  128. </table>
  129. </center>
  130. <br/>
  131. <br/>
  132. <br/>
  133. <table class="DeviceTable" align="center" border="1"
  134. cellpadding="2" cellspacing="2" width="40%">
  135. <tr>
  136. <th>WAN Router</th>
  137. <th>Input Bytes</th>
  138. <th>Output Bytes</th>
  139. </tr>
  140. <tr>
  141. <td align="left">liveau-wan-gw1</td>
  142. <td align="right">${in1}</td>
  143. <td align="right">${out1}</td>
  144. </tr>
  145. <tr>
  146. <td align="left">liveau-wan-gw2</td>
  147. <td align="right">${in2}</td>
  148. <td align="right">${out2}</td>
  149. </tr>
  150. </table>
  151. <br/>
  152. <br/>
  153. <p class="headtitle">Counter began on Monday, March 6 at 16:00
  154. AEDT when Registration opened.</p>
  155. </body>
  156. </html>
  157. EOF