v_update_sys_rrd_net 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 defenition
  10. update=$1
  11. period=${1-daily}
  12. # Importing variables
  13. source $VESTA/conf/vars.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 "$V_RRD/net" ]; then
  27. mkdir $V_RRD/net
  28. fi
  29. # Parsing network interfaces
  30. ifaces=$(cat /proc/net/dev |grep : |cut -f 1 -d : |sed -e "s/ //g")
  31. # Parsing excludes
  32. for exclude in $(echo ${V_RRD_IFACE_EXCLUDE//,/ }); do
  33. ifaces=$(echo "$ifaces" |grep -vw "$exclude" )
  34. done
  35. for iface in $ifaces; do
  36. # Checking database
  37. if [ ! -e "$V_RRD/net/$iface.rrd" ]; then
  38. # Adding database
  39. rrdtool create $V_RRD/net/$iface.rrd --step $V_RRD_STEP \
  40. DS:RX:COUNTER:600:U:U \
  41. DS:TX:COUNTER:600:U:U \
  42. RRA:AVERAGE:0.5:1:600 \
  43. RRA:AVERAGE:0.5:6:700 \
  44. RRA:AVERAGE:0.5:24:775 \
  45. RRA:AVERAGE:0.5:288:797 \
  46. RRA:MAX:0.5:1:600 \
  47. RRA:MAX:0.5:6:700 \
  48. RRA:MAX:0.5:24:775 \
  49. RRA:MAX:0.5:288:797
  50. fi
  51. # Parsing device stats
  52. if [ -z "$update" ]; then
  53. raw_iface=$(grep "$iface:" /proc/net/dev |sed -e "s/:/ /")
  54. rx=$(echo "$raw_iface" |awk '{print $2}')
  55. tx=$(echo "$raw_iface" |awk '{print $10}')
  56. # Updating rrd database
  57. rrdtool update $V_RRD/net/$iface.rrd N:$rx:$tx
  58. fi
  59. # Updating rrd graph
  60. rrdtool graph $V_RRD/net/$period-$iface.png \
  61. --imgformat PNG \
  62. --height="120" \
  63. --width="440" \
  64. --start "$start" \
  65. --end "$end" \
  66. --vertical-label "KBytes" \
  67. --x-grid "$grid" \
  68. -c "BACK#484439" \
  69. -c "SHADEA#484439" \
  70. -c "SHADEB#484439" \
  71. -c "FONT#DDDDDD" \
  72. -c "CANVAS#202020" \
  73. -c "GRID#666666" \
  74. -c "MGRID#AAAAAA" \
  75. -c "FRAME#202020" \
  76. -c "ARROW#FFFFFF" \
  77. DEF:inoctets=$V_RRD/net/$iface.rrd:RX:AVERAGE \
  78. DEF:outoctets=$V_RRD/net/$iface.rrd:TX:AVERAGE \
  79. "CDEF:in=inoctets,8,*" \
  80. "CDEF:out=outoctets,8,*" \
  81. COMMENT:'\r' \
  82. AREA:in#C8EA2E:"Input (rx) "\
  83. GPRINT:in:'LAST: Current\:''%8.0lf' \
  84. GPRINT:in:'MIN: Min\:''%8.0lf' \
  85. GPRINT:in:'MAX: Max\:''%8.0lf\j' \
  86. LINE1:out#1c74cd:"Output (tx)" \
  87. GPRINT:out:'LAST:Current\:''%8.0lf' \
  88. GPRINT:out:'MIN:Min\:''%8.0lf' \
  89. GPRINT:out:'MAX:Max\:''%8.0lf\j' >/dev/null 2>/dev/null; result=$?
  90. if [ "$result" -ne 0 ]; then
  91. exit $E_RRD
  92. fi
  93. done
  94. #----------------------------------------------------------#
  95. # Vesta #
  96. #----------------------------------------------------------#
  97. exit