v_update_sys_rrd_mysql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # info: updating MySQL rrd
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. update=$1
  8. period=${1-daily}
  9. # Importing variables
  10. source $VESTA/conf/vars.conf
  11. source $V_FUNC/shared.func
  12. #----------------------------------------------------------#
  13. # Action #
  14. #----------------------------------------------------------#
  15. # Switching on time period
  16. case $period in
  17. daily) start='-1d'; end='now'; grid='MINUTE:30:HOUR:1:HOUR:4:0:%H:%M';;
  18. weekly) start='-7d'; end='now'; grid='HOUR:8:DAY:1:DAY:1:0:%a %d';;
  19. monthly) start='-1m'; end='now'; grid='WEEK:1:WEEK:1:WEEK:1:0:%b %d';;
  20. yearly) start='-1y'; end='now'; grid='MONTH:1:YEAR:1:MONTH:2:2419200:%b';;
  21. *) exit $E_RRD ;;
  22. esac
  23. # Checking directory
  24. if [ ! -d "$V_RRD/db" ]; then
  25. mkdir $V_RRD/db
  26. fi
  27. # Parsing db hosts
  28. conf="$V_DB/mysql.conf"
  29. fields='$HOST'
  30. nohead=1
  31. hosts=$(shell_list)
  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 ${V_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 "$V_RRD/db/mysql_$host.rrd" ]; then
  43. # Adding database
  44. rrdtool create $V_RRD/db/mysql_$host.rrd --step $V_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 [ -z "$update" ]; 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 -P$PORT -e"
  63. # Checking empty vars
  64. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]
  65. then
  66. echo "Error: config is broken"
  67. log_event 'debug' "$E_PARSING $V_EVENT"
  68. exit $E_PARSING
  69. fi
  70. # Parsing data
  71. status=$($sql "SHOW GLOBAL STATUS" 2>/dev/null); code="$?"
  72. if [ '0' -ne "$code" ]; then
  73. active=0
  74. slow=0
  75. else
  76. active=$(echo "$status"|grep 'Queries'|cut -f 2)
  77. slow=$(echo "$status"|grep 'Slow_queries'|cut -f 2)
  78. fi
  79. # Updating rrd
  80. rrdtool update $V_RRD/db/mysql_$host.rrd N:$active:$slow
  81. fi
  82. # Updating daily graph
  83. rrdtool graph $V_RRD/db/$period-mysql_$host.png \
  84. --imgformat PNG \
  85. --height="120" \
  86. --width="440" \
  87. --start "$start" \
  88. --end "$end" \
  89. --title "MySQL Usage on $host ($period)" \
  90. --vertical-label "Queries" \
  91. --x-grid "$grid" \
  92. -c "BACK#484439" \
  93. -c "SHADEA#484439" \
  94. -c "SHADEB#484439" \
  95. -c "FONT#DDDDDD" \
  96. -c "CANVAS#202020" \
  97. -c "GRID#666666" \
  98. -c "MGRID#AAAAAA" \
  99. -c "FRAME#202020" \
  100. -c "ARROW#FFFFFF" \
  101. DEF:a=$V_RRD/db/mysql_$host.rrd:A:AVERAGE \
  102. DEF:s=$V_RRD/db/mysql_$host.rrd:S:AVERAGE \
  103. COMMENT:'\r' \
  104. LINE1:a#fefda0:"Queries"\
  105. GPRINT:a:'LAST: Current\:''%8.0lf' \
  106. GPRINT:a:'MIN: Min\:''%8.0lf' \
  107. GPRINT:a:'MAX: Max\:''%8.0lf\j' \
  108. AREA:s#f57900:"Slow " \
  109. GPRINT:s:'LAST:Current\:''%8.0lf' \
  110. GPRINT:s:'MIN:Min\:''%8.0lf' \
  111. GPRINT:s:'MAX:Max\:''%8.0lf\j' >/dev/null 2>/dev/null; result=$?
  112. if [ "$result" -ne 0 ]; then
  113. exit $E_RRD
  114. fi
  115. done
  116. #----------------------------------------------------------#
  117. # Vesta #
  118. #----------------------------------------------------------#
  119. exit