v-update-sys-rrd-mysql 4.0 KB

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