c_smrrds.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #!/bin/sh
  2. # $Id: c_smrrds.sh,v 1.32 2005/10/18 19:58:13 dijkstra Exp $
  3. #
  4. # Copyright (c) 2001-2005 Willem Dijkstra
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. #
  11. # - Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # - Redistributions in binary form must reproduce the above
  14. # copyright notice, this list of conditions and the following
  15. # disclaimer in the documentation and/or other materials provided
  16. # with the distribution.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. # POSSIBILITY OF SUCH DAMAGE.
  30. # --- user configuration starts here
  31. INTERVAL=${INTERVAL:-5}
  32. # --- user configuration ends here
  33. OS=`uname -s`
  34. # addsuffix adds a suffix to each entry of a list (item|item|...)
  35. addsuffix() {
  36. list=$1'|'
  37. suffix=$2
  38. while [ `echo $list | grep '|'` ]; do
  39. newlist=$newlist'|'`echo $list | cut -f1 -d\|`$suffix
  40. list=`echo $list | cut -f2- -d\|`
  41. done
  42. echo $newlist | cut -b2-
  43. }
  44. create_rrd() {
  45. file=$1
  46. shift
  47. rrdtool create $file $RRD_ARGS $* $RRA_SETUP
  48. if [ "$?" = "0" -a -f $file ]; then
  49. echo "$file created"
  50. else
  51. echo "could not create $file"
  52. fi
  53. }
  54. # get arguments
  55. select_interval=""
  56. for i
  57. do
  58. case $i in
  59. interval)
  60. select_interval="yes"
  61. ;;
  62. child)
  63. child=1
  64. ;;
  65. oneday)
  66. config=$i
  67. # today only RRA setup:
  68. # - 1 day of 5 second samples = 17280 x 5 second samples
  69. RRA_SETUP=" RRA:AVERAGE:0.5:1:17280
  70. RRA:MAX:0.5:1:17280
  71. RRA:MIN:0.5:1:17280"
  72. if [ X"$child" = "X" ]; then
  73. echo "RRDs will only contain a single day of data"
  74. fi
  75. ;;
  76. *)
  77. if [ -n "$select_interval" ]; then
  78. INTERVAL=$i
  79. select_interval=""
  80. else
  81. args="$args $i"
  82. fi
  83. ;;
  84. esac
  85. done
  86. if [ X"$RRA_SETUP" = "X" ]; then
  87. # default RRA setup:
  88. # - 2 days of 5 second samples = 34560 x 5 second samples
  89. # - 14 days of 30 minutes samples = 672 x 360 x 5 second samples
  90. # - 50 days of 2 hour samples = 600 x 1440 x 5 second samples
  91. # - 600 days of 1 day samples = 600 x 17280 x 5 second samples
  92. RRA_SETUP=" RRA:AVERAGE:0.5:1:34560
  93. RRA:AVERAGE:0.5:360:672
  94. RRA:AVERAGE:0.5:1440:600
  95. RRA:AVERAGE:0.5:17280:600
  96. RRA:MAX:0.5:1:34560
  97. RRA:MAX:0.5:360:672
  98. RRA:MAX:0.5:1440:600
  99. RRA:MAX:0.5:17280:600
  100. RRA:MIN:0.5:1:34560
  101. RRA:MIN:0.5:360:672
  102. RRA:MIN:0.5:1440:600
  103. RRA:MIN:0.5:17280:600"
  104. fi
  105. # All interfaces and disks
  106. case ${OS} in
  107. FreeBSD)
  108. DISKS="ad|acd|afd|ast|sa|da|ar|cd|ch|md"
  109. INTERFACES="an|ar|ath|aue|awi|axe|bfe|bge|cm|cnw|cs|cue|dc|de|ed|el|em|ep|ex|fe|fwe|fxp|gem|gx|hme|ie|kue|lge|lnc|my|nge|pcn|ray|re|rl|rue|sf|sis|sk|sn|snc|ste|ti|tl|tx|txp|vge|vr|vx|wb|wi|xe|xl";
  110. VIRTUALINTERFACES="bridge|carp|enc|faith|gif|ppp|sl|sppp|strip|tun|vlan";
  111. diskcmd="mount | sed -n '/^\/dev/ s,/dev/\([a-z]*[0-9]\).*,\1,p' | sort -u"
  112. interfacecmd="ifconfig -l | sed 's/lo0//'"
  113. ;;
  114. Linux)
  115. DISKS="hda|hdb|hdc|hdd|sda|sdb|sdc|sdd"
  116. INTERFACES="eth"
  117. VIRTUALINTERFACES="sit"
  118. diskcmd="mount | sed -n '/^\/dev/ s,/dev/\([a-z]*[0-9]\).*,\1,p' | sort -u"
  119. interfacecmd="ifconfig -a| egrep -e \"^(\$INTERFACES) \" | cut -f1 -d\ | sort -u"
  120. ;;
  121. OpenBSD)
  122. DISKS="sd|cd|ch|rd|raid|ss|uk|vnc|wd"
  123. INTERFACES="an|ath|awi|be|bge|bm|cnw|dc|de|ec|ef|eg|el|em|ep|ex|fea|fpa|fxp|gem|gm|gre|hme|ie|kue|lc|le|lge|lmc|lo|ne|nge|ray|rl|qe|sf|sis|sk|sl|sm|ste|stge|ti|tl|tr|tx|txp|vme|vr|wb|we|wi|wx|xe|xl"
  124. VIRTUALINTERFACES="bridge|carp|enc|faith|gif|ppp|sppp|strip|tun|vlan";
  125. diskcmd="mount | sed -n '/^\/dev/ s,/dev/\([a-z]*[0-9]\).*,\1,p' | sort -u"
  126. partcmd="grep ffs /etc/fstab | sed -n '/^\/dev/ s,/dev/\([a-z]*[0-9]*[a-z]*\).*,\1,p' | sort -u"
  127. interfacecmd="ifconfig -a| egrep -e \"^(\$INTERFACES):\" | cut -f1 -d\: | sort -u"
  128. ;;
  129. NetBSD)
  130. DISKS="sd|cd|ch|rd|raid|ss|uk|vnc|wd"
  131. INTERFACES="ai|an|ate|ath|atw|awi|bce|bge|bicc|cnw|com|cs|depca|ec|ef|eg|el|elmc|en|ep|epic|esh|ex|fea|fmv|fpa|fxp|gsip|hme|ipw|iwi|ix|iy|lc|le|lmc|mbe|mhzc|mtd|ne|nele|ntwoc|pcn|ray|re|rtk|sf|sip|sk|skc|sm|ste|stge|ti|tl|tlp|tr|vge|vr|we|wi|wm|xi|xirc"
  132. VIRTUALINTERFACES="bridge|carp|enc|faith|gif|ppp|sppp|sl|strip|tun|vlan";
  133. diskcmd="mount | sed -n '/^\/dev/ s,/dev/\([a-z]*[0-9]\).*,\1,p' | sort -u"
  134. interfacecmd="ifconfig -l | sed 's/lo0//'"
  135. ;;
  136. esac
  137. DISKS=`addsuffix $DISKS [0-9]`
  138. INTERFACES=`addsuffix $INTERFACES [0-9]`
  139. VIRTUALINTERFACES=`addsuffix $VIRTUALINTERFACES \\.\\*`
  140. this=$0
  141. if [ X"$1$2$3$4$5$6$7$8$9" = "X" ]; then
  142. cat <<EOF
  143. Create rrd files for symux.
  144. Usage: `basename $0` [oneday] [interval <seconds>] all | cpu0 | mem |
  145. pf | pfq_<queue> | mbuf | debug | proc_<process> |
  146. <if> | <io> | sensor[0-25]
  147. Where:
  148. oneday = modify rrds to only contain one day of information
  149. seconds = modify rrds for non standard monitoring interval
  150. process = the name of a process as specified in sy{mon,mux}.conf
  151. e.g. proc(httpd) -> proc_httpd
  152. queue = the name of a queue as specified in sy{mon,mux}.conf
  153. e.g. pfq(root) -> pfq_root
  154. if= `echo $INTERFACES|
  155. awk 'BEGIN {FS="|"}
  156. {for (i=1; i<=NF; i++) {
  157. printf("%s|",$i);
  158. if ((i%6)==0) {
  159. printf("%s","\n\t")
  160. }
  161. }
  162. print " ";}'`
  163. io= `echo $DISKS|
  164. awk 'BEGIN {FS="|"}
  165. {for (i=1; i<=NF; i++) {
  166. printf("%s|",$i);
  167. if ((i%6)==0) {
  168. printf("%s","\n\t")
  169. }
  170. }
  171. print " ";}'`
  172. OpenBSD pre-3.5 disk statistics are available via the io1_<disk> argument.
  173. EOF
  174. exit 1;
  175. fi
  176. for i in $args
  177. do
  178. # add if_*.rrd if it is an interface
  179. if [ `echo $i | egrep -e "^($INTERFACES)$"` ]; then i=if_$i.rrd; fi
  180. if [ `echo $i | egrep -e "^($VIRTUALINTERFACES)$"` ]; then i=if_$i.rrd; fi
  181. # add io_*.rrd if it is a disk
  182. if [ `echo $i | egrep -e "^($DISKS)$"` ]; then i=io_$i.rrd; fi
  183. # add io_*.rrd if it is a disk
  184. if [ `echo $i | egrep -e "^($DISKS)[a-z]$"` ]; then i=df_$i.rrd; fi
  185. # add .rrd if it is a cpu, etc.
  186. if [ `echo $i | egrep -e "^(cpu[0-9]$|mem$|pf$|pfq_|mbuf$|debug$|proc_|sensor[0-9]$|sensor[0-9][0-9]$|io1_)"` ]; then i=$i.rrd; fi
  187. if [ -f $i ]; then
  188. echo "$i exists - ignoring"
  189. i="done"
  190. fi
  191. RRD_ARGS="--step=$INTERVAL --start=0"
  192. case $i in
  193. all)
  194. echo "Creating rrd files for {cpu0|df|mem|disks|interfaces|pf|mbuf}"
  195. sh $this interval $INTERVAL child $config cpu0 mem
  196. sh $this interval $INTERVAL child $config interfaces
  197. sh $this interval $INTERVAL child $config disks
  198. sh $this interval $INTERVAL child $config pf
  199. sh $this interval $INTERVAL child $config mbuf
  200. sh $this interval $INTERVAL child $config df
  201. ;;
  202. if|interfaces)
  203. # obtain all network cards
  204. sh $this child $config `eval $interfacecmd`
  205. ;;
  206. io|disks)
  207. # obtain all disks
  208. sh $this child $config `eval $diskcmd`
  209. ;;
  210. df)
  211. # obtain all ffs partitions
  212. sh $this child $config `eval $partcmd`
  213. ;;
  214. cpu[0-9].rrd)
  215. # Build cpu file
  216. create_rrd $i \
  217. DS:user:GAUGE:$INTERVAL:0:100 \
  218. DS:nice:GAUGE:$INTERVAL:0:100 \
  219. DS:system:GAUGE:$INTERVAL:0:100 \
  220. DS:interrupt:GAUGE:$INTERVAL:0:100 \
  221. DS:idle:GAUGE:$INTERVAL:0:100
  222. ;;
  223. df_*.rrd)
  224. # Build df file
  225. create_rrd $i \
  226. DS:blocks:GAUGE:$INTERVAL:0:U \
  227. DS:bfree:GAUGE:$INTERVAL:0:U \
  228. DS:bavail:GAUGE:$INTERVAL:0:U \
  229. DS:files:GAUGE:$INTERVAL:0:U \
  230. DS:ffree:GAUGE:$INTERVAL:0:U \
  231. DS:syncwrites:COUNTER:$INTERVAL:U:U \
  232. DS:asyncwrites:COUNTER:$INTERVAL:U:U
  233. ;;
  234. sensor*.rrd)
  235. # Build sensor file
  236. create_rrd $i \
  237. DS:value:GAUGE:$INTERVAL:-U:U
  238. ;;
  239. mem.rrd)
  240. # Build memory file
  241. create_rrd $i \
  242. DS:real_active:GAUGE:$INTERVAL:0:U \
  243. DS:real_total:GAUGE:$INTERVAL:0:U \
  244. DS:free:GAUGE:$INTERVAL:0:U \
  245. DS:swap_used:GAUGE:$INTERVAL:0:U \
  246. DS:swap_total:GAUGE:$INTERVAL:0:U
  247. ;;
  248. if_*.rrd)
  249. # Build interface files
  250. create_rrd $i \
  251. DS:ipackets:COUNTER:$INTERVAL:U:U DS:opackets:COUNTER:$INTERVAL:U:U \
  252. DS:ibytes:COUNTER:$INTERVAL:U:U DS:obytes:COUNTER:$INTERVAL:U:U \
  253. DS:imcasts:COUNTER:$INTERVAL:U:U DS:omcasts:COUNTER:$INTERVAL:U:U \
  254. DS:ierrors:COUNTER:$INTERVAL:U:U DS:oerrors:COUNTER:$INTERVAL:U:U \
  255. DS:collisions:COUNTER:$INTERVAL:U:U DS:drops:COUNTER:$INTERVAL:U:U
  256. ;;
  257. debug.rrd)
  258. # Build debug file
  259. create_rrd $i \
  260. DS:debug0:GAUGE:$INTERVAL:U:U DS:debug1:GAUGE:$INTERVAL:U:U \
  261. DS:debug2:GAUGE:$INTERVAL:U:U DS:debug3:GAUGE:$INTERVAL:U:U \
  262. DS:debug4:GAUGE:$INTERVAL:U:U DS:debug5:GAUGE:$INTERVAL:U:U \
  263. DS:debug6:GAUGE:$INTERVAL:U:U DS:debug7:GAUGE:$INTERVAL:U:U \
  264. DS:debug8:GAUGE:$INTERVAL:U:U DS:debug9:GAUGE:$INTERVAL:U:U \
  265. DS:debug10:GAUGE:$INTERVAL:U:U DS:debug11:GAUGE:$INTERVAL:U:U \
  266. DS:debug12:GAUGE:$INTERVAL:U:U DS:debug13:GAUGE:$INTERVAL:U:U \
  267. DS:debug14:GAUGE:$INTERVAL:U:U DS:debug15:GAUGE:$INTERVAL:U:U \
  268. DS:debug16:GAUGE:$INTERVAL:U:U DS:debug17:GAUGE:$INTERVAL:U:U \
  269. DS:debug18:GAUGE:$INTERVAL:U:U DS:debug19:GAUGE:$INTERVAL:U:U
  270. ;;
  271. proc_*.rrd)
  272. # Build proc file
  273. create_rrd $i \
  274. DS:number:GAUGE:$INTERVAL:0:U DS:uticks:COUNTER:$INTERVAL:0:U \
  275. DS:sticks:COUNTER:$INTERVAL:0:U DS:iticks:COUNTER:$INTERVAL:0:U \
  276. DS:cpusec:GAUGE:$INTERVAL:0:U DS:cpupct:GAUGE:$INTERVAL:0:100 \
  277. DS:procsz:GAUGE:$INTERVAL:0:U DS:rsssz:GAUGE:$INTERVAL:0:U
  278. ;;
  279. pf.rrd)
  280. # Build pf file
  281. create_rrd $i \
  282. DS:bytes_v4_in:DERIVE:$INTERVAL:0:U DS:bytes_v4_out:DERIVE:$INTERVAL:0:U \
  283. DS:bytes_v6_in:DERIVE:$INTERVAL:0:U DS:bytes_v6_out:DERIVE:$INTERVAL:0:U \
  284. DS:packets_v4_in_pass:DERIVE:$INTERVAL:0:U DS:packets_v4_in_drop:DERIVE:$INTERVAL:0:U \
  285. DS:packets_v4_out_pass:DERIVE:$INTERVAL:0:U DS:packets_v4_out_drop:DERIVE:$INTERVAL:0:U \
  286. DS:packets_v6_in_pass:DERIVE:$INTERVAL:0:U DS:packets_v6_in_drop:DERIVE:$INTERVAL:0:U \
  287. DS:packets_v6_out_pass:DERIVE:$INTERVAL:0:U DS:packets_v6_out_drop:DERIVE:$INTERVAL:0:U \
  288. DS:states_entries:GAUGE:$INTERVAL:0:U \
  289. DS:states_searches:DERIVE:$INTERVAL:0:U \
  290. DS:states_inserts:DERIVE:$INTERVAL:0:U \
  291. DS:states_removals:DERIVE:$INTERVAL:0:U \
  292. DS:counters_match:DERIVE:$INTERVAL:0:U \
  293. DS:counters_badoffset:DERIVE:$INTERVAL:0:U \
  294. DS:counters_fragment:DERIVE:$INTERVAL:0:U \
  295. DS:counters_short:DERIVE:$INTERVAL:0:U \
  296. DS:counters_normalize:DERIVE:$INTERVAL:0:U \
  297. DS:counters_memory:DERIVE:$INTERVAL:0:U
  298. ;;
  299. pfq_*.rrd)
  300. # Build pfq file
  301. create_rrd $i \
  302. DS:sent_bytes:COUNTER:$INTERVAL:0:U \
  303. DS:sent_packets:COUNTER:$INTERVAL:0:U \
  304. DS:drop_bytes:COUNTER:$INTERVAL:0:U \
  305. DS:drop_packets:COUNTER:$INTERVAL:0:U
  306. ;;
  307. mbuf.rrd)
  308. # Build mbuf file
  309. create_rrd $i \
  310. DS:totmbufs:GAUGE:$INTERVAL:0:U DS:mt_data:GAUGE:$INTERVAL:0:U \
  311. DS:mt_oobdata:GAUGE:$INTERVAL:0:U DS:mt_control:GAUGE:$INTERVAL:0:U \
  312. DS:mt_header:GAUGE:$INTERVAL:0:U DS:mt_ftable:GAUGE:$INTERVAL:0:U \
  313. DS:mt_soname:GAUGE:$INTERVAL:0:U DS:mt_soopts:GAUGE:$INTERVAL:0:U \
  314. DS:pgused:GAUGE:$INTERVAL:0:U DS:pgtotal:GAUGE:$INTERVAL:0:U \
  315. DS:totmem:GAUGE:$INTERVAL:0:U DS:totpct:GAUGE:$INTERVAL:0:100 \
  316. DS:m_drops:COUNTER:$INTERVAL:0:U DS:m_wait:COUNTER:$INTERVAL:0:U \
  317. DS:m_drain:COUNTER:$INTERVAL:0:U
  318. ;;
  319. io_*.rrd)
  320. # Build disk files
  321. create_rrd $i \
  322. DS:rxfer:COUNTER:$INTERVAL:U:U \
  323. DS:wxfer:COUNTER:$INTERVAL:U:U \
  324. DS:seeks:COUNTER:$INTERVAL:U:U \
  325. DS:rbytes:COUNTER:$INTERVAL:U:U \
  326. DS:wbytes:COUNTER:$INTERVAL:U:U
  327. ;;
  328. io1_*.rrd)
  329. # Build disk files
  330. create_rrd $i \
  331. DS:transfers:COUNTER:$INTERVAL:U:U \
  332. DS:seeks:COUNTER:$INTERVAL:U:U \
  333. DS:bytes:COUNTER:$INTERVAL:U:U
  334. ;;
  335. "done")
  336. # ignore
  337. ;;
  338. *)
  339. # Default match
  340. echo $i - unknown
  341. ;;
  342. esac
  343. done