Advertisement
Guest User

监控脚本

a guest
Jun 26th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.51 KB | None | 0 0
  1. ####需要安装gawk
  2. ####建立数据库
  3. usr/bin/rrdtool create /xxxx/ping_wan.rrd \
  4. --step 60 \
  5. DS:pl:GAUGE:600:0:100 \
  6. DS:rtt:GAUGE:600:0:10000000 \
  7. RRA:AVERAGE:0.5:1:800 \
  8. RRA:AVERAGE:0.5:6:800 \
  9. RRA:AVERAGE:0.5:24:800 \
  10. RRA:AVERAGE:0.5:288:800 \
  11. RRA:MAX:0.5:1:800 \
  12. RRA:MAX:0.5:6:800 \
  13. RRA:MAX:0.5:24:800 \
  14. RRA:MAX:0.5:288:800
  15.  
  16. ####监控脚本 (每分钟执行)
  17. #!/bin/bash
  18. PING=/bin/ping
  19. COUNT=4
  20. DEADLINE=10
  21. ping_host() {
  22. local output=$($PING -q -n -c $COUNT -w $DEADLINE $1 2>&1)
  23. # notice $output is quoted to preserve newlines
  24. local temp=$(echo "$output"| awk '
  25. BEGIN {pl=100; rtt=0.1}
  26. /packets transmitted/ {
  27. match($0, /([0-9]+)% packet loss/, matchstr)
  28. pl=matchstr[1]
  29. }
  30. /^rtt/ {
  31. # looking for something like 0.562/0.566/0.571/0.024
  32. match($4, /(.*)\/(.*)\/(.*)\/(.*)/, a)
  33. rtt=a[2]
  34. }
  35. /unknown host/ {
  36. # no output at all means network is probably down
  37. pl=100
  38. rtt=0.1
  39. }
  40. END {print pl ":" rtt}
  41. ')
  42. RETURN_VALUE=$temp
  43. }
  44. # ping a host on the local lan
  45. ping_host xxxx
  46. /usr/bin/rrdtool update \
  47. /xxxx/ping_wan.rrd \
  48. --template \
  49. pl:rtt \
  50. N:$RETURN_VALUE
  51.  
  52. ####画图脚本
  53. #!/bin/sh
  54. cd /xxx
  55. echo "
  56. <HTML>
  57. <HEAD><TITLE>Round-Trip and Packet Loss Stats</TITLE></HEAD>
  58. <BODY>
  59. <H3>Hourly Round-Trip & Packetloss Stats(1min average)</H3>
  60. " > index.html
  61. rrdtool graph ping_wan_hour.png -h 225 -w 600 -a PNG \
  62. --imginfo '<IMG SRC=/stat/%s WIDTH=%lu HEIGHT=%lu >' \
  63. --start -3600 --end -60 --x-grid MINUTE:10:HOUR:1:MINUTE:30:0:%R \
  64. -v "Round-Trip Time (ms)" \
  65. --rigid \
  66. --lower-limit 0 \
  67. DEF:roundtrip=ping_wan.rrd:rtt:AVERAGE \
  68. DEF:packetloss=ping_wan.rrd:pl:AVERAGE \
  69. CDEF:PLNone=packetloss,0,2,LIMIT,UN,UNKN,INF,IF \
  70. CDEF:PL2=packetloss,2,8,LIMIT,UN,UNKN,INF,IF \
  71. CDEF:PL15=packetloss,8,15,LIMIT,UN,UNKN,INF,IF \
  72. CDEF:PL25=packetloss,15,25,LIMIT,UN,UNKN,INF,IF \
  73. CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
  74. CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF \
  75. CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF \
  76. AREA:roundtrip#4444ff:"Round Trip Time (millis)" \
  77. GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
  78. GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
  79. GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
  80. GPRINT:roundtrip:MIN:"Min\: %5.2lf\n" \
  81. AREA:PLNone#6c9bcd:"0-2%":STACK \
  82. AREA:PL2#00ffae:"2-8%":STACK \
  83. AREA:PL15#ccff00:"8-15%":STACK \
  84. AREA:PL25#ffff00:"15-25%":STACK \
  85. AREA:PL50#ffcc66:"25-50%":STACK \
  86. AREA:PL75#ff9900:"50-75%":STACK \
  87. AREA:PL100#ff0000:"75-100%":STACK \
  88. COMMENT:"(Packet Loss Percentage)"
  89. echo "
  90. <img src="ping_wan_hour.png"><br>
  91. <br>
  92. <H3>Daily Round-Trip & Packetloss Stats(1min average)</H3>
  93. " >> index.html
  94. rrdtool graph ping_wan_day.png -h 225 -w 600 -a PNG \
  95. --imginfo '<IMG SRC=/stats/%s WIDTH=%lu HEIGHT=%lu >' \
  96. --lazy --start -86400 --end -60 --x-grid MINUTE:30:HOUR:1:HOUR:2:0:%H \
  97. -v "Round-Trip Time (ms)" \
  98. --rigid \
  99. --lower-limit 0 \
  100. DEF:roundtrip=ping_wan.rrd:rtt:AVERAGE \
  101. DEF:packetloss=ping_wan.rrd:pl:AVERAGE \
  102. CDEF:PLNone=packetloss,0,2,LIMIT,UN,UNKN,INF,IF \
  103. CDEF:PL2=packetloss,2,8,LIMIT,UN,UNKN,INF,IF \
  104. CDEF:PL15=packetloss,8,15,LIMIT,UN,UNKN,INF,IF \
  105. CDEF:PL25=packetloss,15,25,LIMIT,UN,UNKN,INF,IF \
  106. CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
  107. CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF \
  108. CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF \
  109. AREA:roundtrip#4444ff:"Round Trip Time (millis)" \
  110. GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
  111. GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
  112. GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
  113. GPRINT:roundtrip:MIN:"Min\: %5.2lf\n" \
  114. AREA:PLNone#6c9bcd:"0-2%":STACK \
  115. AREA:PL2#00ffae:"2-8%":STACK \
  116. AREA:PL15#ccff00:"8-15%":STACK \
  117. AREA:PL25#ffff00:"15-25%":STACK \
  118. AREA:PL50#ffcc66:"25-50%":STACK \
  119. AREA:PL75#ff9900:"50-75%":STACK \
  120. AREA:PL100#ff0000:"75-100%":STACK \
  121. COMMENT:"(Packet Loss Percentage)"
  122. echo "
  123. <img src="ping_wan_day.png"><br>
  124. <br>
  125. <H3>Weekly Round-Trip & Packetloss Stats(1min average)</H3>
  126. " >> index.html
  127. rrdtool graph ping_wan_week.png -h 225 -w 600 -a PNG \
  128. --imginfo '<IMG SRC=/stats/%s WIDTH=%lu HEIGHT=%lu >' \
  129. --lazy --start -604800 --end -1800 \
  130. -v "Round-Trip Time (ms)" \
  131. --rigid \
  132. --lower-limit 0 \
  133. DEF:roundtrip=ping_wan.rrd:rtt:AVERAGE \
  134. DEF:packetloss=ping_wan.rrd:pl:AVERAGE \
  135. CDEF:PLNone=packetloss,0,2,LIMIT,UN,UNKN,INF,IF \
  136. CDEF:PL2=packetloss,2,8,LIMIT,UN,UNKN,INF,IF \
  137. CDEF:PL15=packetloss,8,15,LIMIT,UN,UNKN,INF,IF \
  138. CDEF:PL25=packetloss,15,25,LIMIT,UN,UNKN,INF,IF \
  139. CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
  140. CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF \
  141. CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF \
  142. AREA:roundtrip#4444ff:"Round Trip Time (millis)" \
  143. GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
  144. GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
  145. GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
  146. GPRINT:roundtrip:MIN:"Min\: %5.2lf\n" \
  147. AREA:PLNone#6c9bcd:"0-2%":STACK \
  148. AREA:PL2#00ffae:"2-8%":STACK \
  149. AREA:PL15#ccff00:"8-15%":STACK \
  150. AREA:PL25#ffff00:"15-25%":STACK \
  151. AREA:PL50#ffcc66:"25-50%":STACK \
  152. AREA:PL75#ff9900:"50-75%":STACK \
  153. AREA:PL100#ff0000:"75-100%":STACK \
  154. COMMENT:"(Packet Loss Percentage)"
  155. echo "
  156. <img src="ping_wan_week.png"><br>
  157. <br>
  158. <H3>Monthly Round-Trip & Packetloss Stats(1min average)</H3>
  159. " >> index.html
  160. rrdtool graph ping_wan_month.png -h 225 -w 600 -a PNG \
  161. --imginfo '<IMG SRC=/stats/%s WIDTH=%lu HEIGHT=%lu >' \
  162. --lazy --start -2592000 --end -7200 \
  163. -v "Round-Trip Time (ms)" \
  164. --rigid \
  165. --lower-limit 0 \
  166. DEF:roundtrip=ping_wan.rrd:rtt:AVERAGE \
  167. DEF:packetloss=ping_wan.rrd:pl:AVERAGE \
  168. CDEF:PLNone=packetloss,0,2,LIMIT,UN,UNKN,INF,IF \
  169. CDEF:PL2=packetloss,2,8,LIMIT,UN,UNKN,INF,IF \
  170. CDEF:PL15=packetloss,8,15,LIMIT,UN,UNKN,INF,IF \
  171. CDEF:PL25=packetloss,15,25,LIMIT,UN,UNKN,INF,IF \
  172. CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
  173. CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF \
  174. CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF \
  175. AREA:roundtrip#4444ff:"Round Trip Time (millis)" \
  176. GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
  177. GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
  178. GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
  179. GPRINT:roundtrip:MIN:"Min\: %5.2lf\n" \
  180. AREA:PLNone#6c9bcd:"0-2%":STACK \
  181. AREA:PL2#00ffae:"2-8%":STACK \
  182. AREA:PL15#ccff00:"8-15%":STACK \
  183. AREA:PL25#ffff00:"15-25%":STACK \
  184. AREA:PL50#ffcc66:"25-50%":STACK \
  185. AREA:PL75#ff9900:"50-75%":STACK \
  186. AREA:PL100#ff0000:"75-100%":STACK \
  187. COMMENT:"(Packet Loss Percentage)"
  188. echo "
  189. <img src="ping_wan_month.png"><br>
  190. <br>
  191. <H3>Yearly Round-Trip & Packetloss Stats(1min average)</H3>
  192. " >> index.html
  193. rrdtool graph ping_wan_year.png \
  194. --imginfo '<IMG SRC=/stats/%s WIDTH=%lu HEIGHT=%lu >' \
  195. --lazy --start -31536000 --end -86400 -h 225 -w 600 -a PNG \
  196. -v "Round-Trip Time (ms)" \
  197. --rigid \
  198. --lower-limit 0 \
  199. DEF:roundtrip=ping_wan.rrd:rtt:AVERAGE \
  200. DEF:packetloss=ping_wan.rrd:pl:AVERAGE \
  201. CDEF:PLNone=packetloss,0,2,LIMIT,UN,UNKN,INF,IF \
  202. CDEF:PL2=packetloss,2,8,LIMIT,UN,UNKN,INF,IF \
  203. CDEF:PL15=packetloss,8,15,LIMIT,UN,UNKN,INF,IF \
  204. CDEF:PL25=packetloss,15,25,LIMIT,UN,UNKN,INF,IF \
  205. CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
  206. CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF \
  207. CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF \
  208. AREA:roundtrip#4444ff:"Round Trip Time (millis)" \
  209. GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
  210. GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
  211. GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
  212. GPRINT:roundtrip:MIN:"Min\: %5.2lf\n" \
  213. AREA:PLNone#6c9bcd:"0-2%":STACK \
  214. AREA:PL2#00ffae:"2-8%":STACK \
  215. AREA:PL15#ccff00:"8-15%":STACK \
  216. AREA:PL25#ffff00:"15-25%":STACK \
  217. AREA:PL50#ffcc66:"25-50%":STACK \
  218. AREA:PL75#ff9900:"50-75%":STACK \
  219. AREA:PL100#ff0000:"75-100%":STACK \
  220. COMMENT:"(Packet Loss Percentage)"
  221. echo "
  222. <img src="ping_wan_year.png"><br>
  223. <br>
  224. </BODY>
  225. </HTML>
  226. " >> index.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement