v-update-sys-rrd-mysql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # info: update MySQL rrd
  3. # options: PERIOD
  4. #
  5. # The function is for updating mysql 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/db" ]; then
  27. mkdir $RRD/db
  28. fi
  29. # Parsing db hosts
  30. conf="$VESTA/conf/mysql.conf"
  31. hosts=$(grep HOST $conf |awk '{print $1}' |cut -f 2 -d \')
  32. check_row=$(echo "$hosts" |wc -l)
  33. if [ 0 -eq "$check_row" ]; then
  34. exit
  35. fi
  36. # Parsing excludes
  37. for exclude in $(echo ${RRD_MYSQL_EXCLUDE//,/ }); do
  38. hosts=$(echo "$hosts" |grep -vw "$exclude" )
  39. done
  40. for host in $hosts; do
  41. # Checking database
  42. if [ ! -e "$RRD/db/mysql_$host.rrd" ]; then
  43. # Adding database
  44. rrdtool create $RRD/db/mysql_$host.rrd --step $RRD_STEP \
  45. DS:A:COUNTER:600:U:U \
  46. DS:S:COUNTER:600:U:U \
  47. RRA:AVERAGE:0.5:1:600 \
  48. RRA:AVERAGE:0.5:6:700 \
  49. RRA:AVERAGE:0.5:24:775 \
  50. RRA:AVERAGE:0.5:288:797 \
  51. RRA:MAX:0.5:1:600 \
  52. RRA:MAX:0.5:6:700 \
  53. RRA:MAX:0.5:24:775 \
  54. RRA:MAX:0.5:288:797
  55. fi
  56. if [ "$period" = 'daily' ]; then
  57. # Defining host credentials
  58. host_str=$(grep "HOST='$host'" $conf)
  59. for key in $host_str; do
  60. eval ${key%%=*}=${key#*=}
  61. done
  62. sql="mysql -h $HOST -u $USER -p$PASSWORD -e"
  63. # Checking empty vars
  64. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ]; then
  65. echo "Error: config is broken"
  66. log_event "$E_PARSING" "$ARGUMENTS"
  67. exit $E_PARSING
  68. fi
  69. # Parsing data
  70. status=$($sql "SHOW GLOBAL STATUS" 2>/dev/null); code="$?"
  71. if [ '0' -ne "$code" ]; then
  72. active=0
  73. slow=0
  74. else
  75. active=$(echo "$status"|grep 'Queries'|cut -f 2)
  76. slow=$(echo "$status"|grep 'Slow_queries'|cut -f 2)
  77. fi
  78. # Updating rrd
  79. rrdtool update $RRD/db/mysql_$host.rrd N:$active:$slow
  80. fi
  81. # Updating daily graph
  82. rrdtool graph $RRD/db/$period-mysql_$host.png \
  83. --imgformat PNG \
  84. --height="150" \
  85. --width="670" \
  86. --start "$start" \
  87. --end "$end" \
  88. --vertical-label "Queries" \
  89. --x-grid "$grid" \
  90. -c "BACK#ffffff" \
  91. -c "SHADEA#ffffff" \
  92. -c "SHADEB#ffffff" \
  93. -c "FONT#555555" \
  94. -c "CANVAS#302c2d" \
  95. -c "GRID#666666" \
  96. -c "MGRID#AAAAAA" \
  97. -c "FRAME#302c2d" \
  98. -c "ARROW#FFFFFF" \
  99. DEF:a=$RRD/db/mysql_$host.rrd:A:AVERAGE \
  100. DEF:s=$RRD/db/mysql_$host.rrd:S:AVERAGE \
  101. COMMENT:'\r' \
  102. LINE1:a#fefda0:"Queries"\
  103. GPRINT:a:'LAST: Current\:''%8.0lf' \
  104. GPRINT:a:'MIN: Min\:''%8.0lf' \
  105. GPRINT:a:'MAX: Max\:''%8.0lf\j' \
  106. AREA:s#f57900:"Slow " \
  107. GPRINT:s:'LAST:Current\:''%8.0lf' \
  108. GPRINT:s:'MIN:Min\:''%8.0lf' \
  109. GPRINT:s:'MAX:Max\:''%8.0lf\j' &>/dev/null; result=$?
  110. if [ "$result" -ne 0 ]; then
  111. exit $E_RRD
  112. fi
  113. done
  114. #----------------------------------------------------------#
  115. # Vesta #
  116. #----------------------------------------------------------#
  117. exit