v-update-sys-rrd-net 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/bash
  2. # info: update network rrd
  3. # options: PERIOD
  4. #
  5. # The function is for updating network usage rrd database and graphic.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. period=${1-daily}
  11. # Includes
  12. source $VESTA/func/main.sh
  13. source $VESTA/conf/vesta.conf
  14. #----------------------------------------------------------#
  15. # Action #
  16. #----------------------------------------------------------#
  17. # Switching on time period
  18. case $period in
  19. daily) start='-1d'; end='now'; grid='MINUTE:30:HOUR:1:HOUR:4:0:%H:%M';;
  20. weekly) start='-7d'; end='now'; grid='HOUR:8:DAY:1:DAY:1:0:%a %d';;
  21. monthly) start='-1m'; end='now'; grid='WEEK:1:WEEK:1:WEEK:1:0:%b %d';;
  22. yearly) start='-1y'; end='now'; grid='MONTH:1:YEAR:1:MONTH:2:2419200:%b';;
  23. *) exit $E_RRD ;;
  24. esac
  25. # Checking directory
  26. if [ ! -d "$RRD/net" ]; then
  27. mkdir $RRD/net
  28. fi
  29. # Parsing network interfaces
  30. ndev=$(cat /proc/net/dev)
  31. ifaces=$(echo "$ndev" |grep : |cut -f 1 -d : | sed "s/ //g")
  32. # Parsing excludes
  33. if [ -z "$RRD_IFACE_EXCLUDE" ]; then
  34. RRD_IFACE_EXCLUDE='lo'
  35. fi
  36. for exclude in $(echo ${RRD_IFACE_EXCLUDE//,/ }); do
  37. ifaces=$(echo "$ifaces" |grep -vw "$exclude" )
  38. done
  39. for iface in $ifaces; do
  40. # Checking database
  41. if [ ! -e "$RRD/net/$iface.rrd" ]; then
  42. # Adding database
  43. rrdtool create $RRD/net/$iface.rrd --step $RRD_STEP \
  44. DS:RX:COUNTER:600:U:U \
  45. DS:TX:COUNTER:600:U:U \
  46. RRA:AVERAGE:0.5:1:600 \
  47. RRA:AVERAGE:0.5:6:700 \
  48. RRA:AVERAGE:0.5:24:775 \
  49. RRA:AVERAGE:0.5:288:797 \
  50. RRA:MAX:0.5:1:600 \
  51. RRA:MAX:0.5:6:700 \
  52. RRA:MAX:0.5:24:775 \
  53. RRA:MAX:0.5:288:797
  54. fi
  55. # Parsing device stats
  56. if [ "$period" = 'daily' ]; then
  57. x=$(echo "$ndev" |sed "s/^ *//" |grep "^$iface:" |sed "s/:/ /")
  58. rx=$(echo "$x" |awk '{print $2}')
  59. tx=$(echo "$x" |awk '{print $10}')
  60. # Updating rrd database
  61. rrdtool update $RRD/net/$iface.rrd N:$rx:$tx
  62. fi
  63. # Updating rrd graph
  64. rrdtool graph $RRD/net/$period-$iface.png \
  65. --imgformat PNG \
  66. --height="150" \
  67. --width="670" \
  68. --start "$start" \
  69. --end "$end" \
  70. --vertical-label "KBytes" \
  71. --x-grid "$grid" \
  72. -c "BACK#ffffff" \
  73. -c "SHADEA#ffffff" \
  74. -c "SHADEB#ffffff" \
  75. -c "FONT#555555" \
  76. -c "CANVAS#302c2d" \
  77. -c "GRID#666666" \
  78. -c "MGRID#AAAAAA" \
  79. -c "FRAME#302c2d" \
  80. -c "ARROW#FFFFFF" \
  81. DEF:inoctets=$RRD/net/$iface.rrd:RX:AVERAGE \
  82. DEF:outoctets=$RRD/net/$iface.rrd:TX:AVERAGE \
  83. "CDEF:in=inoctets,8,*" \
  84. "CDEF:out=outoctets,8,*" \
  85. COMMENT:'\r' \
  86. AREA:in#C8EA2E:"Input (rx) "\
  87. GPRINT:in:'LAST: Current\:''%8.0lf' \
  88. GPRINT:in:'MIN: Min\:''%8.0lf' \
  89. GPRINT:in:'MAX: Max\:''%8.0lf\j' \
  90. LINE1:out#1c74cd:"Output (tx)" \
  91. GPRINT:out:'LAST:Current\:''%8.0lf' \
  92. GPRINT:out:'MIN:Min\:''%8.0lf' \
  93. GPRINT:out:'MAX:Max\:''%8.0lf\j' &>/dev/null; result=$?
  94. if [ "$result" -ne 0 ]; then
  95. exit $E_RRD
  96. fi
  97. done
  98. #----------------------------------------------------------#
  99. # Vesta #
  100. #----------------------------------------------------------#
  101. exit