v_update_sys_rrd_mysql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. update=$1
  11. period=${1-daily}
  12. # Importing variables
  13. source $VESTA/conf/vars.conf
  14. source $V_FUNC/shared.func
  15. #----------------------------------------------------------#
  16. # Action #
  17. #----------------------------------------------------------#
  18. # Switching on time period
  19. case $period in
  20. daily) start='-1d'; end='now'; grid='MINUTE:30:HOUR:1:HOUR:4:0:%H:%M';;
  21. weekly) start='-7d'; end='now'; grid='HOUR:8:DAY:1:DAY:1:0:%a %d';;
  22. monthly) start='-1m'; end='now'; grid='WEEK:1:WEEK:1:WEEK:1:0:%b %d';;
  23. yearly) start='-1y'; end='now'; grid='MONTH:1:YEAR:1:MONTH:2:2419200:%b';;
  24. *) exit $E_RRD ;;
  25. esac
  26. # Checking directory
  27. if [ ! -d "$V_RRD/db" ]; then
  28. mkdir $V_RRD/db
  29. fi
  30. # Parsing db hosts
  31. conf="$V_DB/mysql.conf"
  32. fields='$HOST'
  33. nohead=1
  34. hosts=$(shell_list)
  35. check_row=$(echo "$hosts" |wc -l)
  36. if [ 0 -eq "$check_row" ]; then
  37. exit
  38. fi
  39. # Parsing excludes
  40. for exclude in $(echo ${V_RRD_MYSQL_EXCLUDE//,/ }); do
  41. hosts=$(echo "$hosts" |grep -vw "$exclude" )
  42. done
  43. for host in $hosts; do
  44. # Checking database
  45. if [ ! -e "$V_RRD/db/mysql_$host.rrd" ]; then
  46. # Adding database
  47. rrdtool create $V_RRD/db/mysql_$host.rrd --step $V_RRD_STEP \
  48. DS:A:COUNTER:600:U:U \
  49. DS:S:COUNTER:600:U:U \
  50. RRA:AVERAGE:0.5:1:600 \
  51. RRA:AVERAGE:0.5:6:700 \
  52. RRA:AVERAGE:0.5:24:775 \
  53. RRA:AVERAGE:0.5:288:797 \
  54. RRA:MAX:0.5:1:600 \
  55. RRA:MAX:0.5:6:700 \
  56. RRA:MAX:0.5:24:775 \
  57. RRA:MAX:0.5:288:797
  58. fi
  59. if [ -z "$update" ]; then
  60. # Defining host credentials
  61. host_str=$(grep "HOST='$host'" $conf)
  62. for key in $host_str; do
  63. eval ${key%%=*}=${key#*=}
  64. done
  65. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  66. # Checking empty vars
  67. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]
  68. then
  69. echo "Error: config is broken"
  70. log_event 'debug' "$E_PARSING $V_EVENT"
  71. exit $E_PARSING
  72. fi
  73. # Parsing data
  74. status=$($sql "SHOW GLOBAL STATUS" 2>/dev/null); code="$?"
  75. if [ '0' -ne "$code" ]; then
  76. active=0
  77. slow=0
  78. else
  79. active=$(echo "$status"|grep 'Queries'|cut -f 2)
  80. slow=$(echo "$status"|grep 'Slow_queries'|cut -f 2)
  81. fi
  82. # Updating rrd
  83. rrdtool update $V_RRD/db/mysql_$host.rrd N:$active:$slow
  84. fi
  85. # Updating daily graph
  86. rrdtool graph $V_RRD/db/$period-mysql_$host.png \
  87. --imgformat PNG \
  88. --height="120" \
  89. --width="440" \
  90. --start "$start" \
  91. --end "$end" \
  92. --vertical-label "Queries" \
  93. --x-grid "$grid" \
  94. -c "BACK#484439" \
  95. -c "SHADEA#484439" \
  96. -c "SHADEB#484439" \
  97. -c "FONT#DDDDDD" \
  98. -c "CANVAS#202020" \
  99. -c "GRID#666666" \
  100. -c "MGRID#AAAAAA" \
  101. -c "FRAME#202020" \
  102. -c "ARROW#FFFFFF" \
  103. DEF:a=$V_RRD/db/mysql_$host.rrd:A:AVERAGE \
  104. DEF:s=$V_RRD/db/mysql_$host.rrd:S:AVERAGE \
  105. COMMENT:'\r' \
  106. LINE1:a#fefda0:"Queries"\
  107. GPRINT:a:'LAST: Current\:''%8.0lf' \
  108. GPRINT:a:'MIN: Min\:''%8.0lf' \
  109. GPRINT:a:'MAX: Max\:''%8.0lf\j' \
  110. AREA:s#f57900:"Slow " \
  111. GPRINT:s:'LAST:Current\:''%8.0lf' \
  112. GPRINT:s:'MIN:Min\:''%8.0lf' \
  113. GPRINT:s:'MAX:Max\:''%8.0lf\j' >/dev/null 2>/dev/null; result=$?
  114. if [ "$result" -ne 0 ]; then
  115. exit $E_RRD
  116. fi
  117. done
  118. #----------------------------------------------------------#
  119. # Vesta #
  120. #----------------------------------------------------------#
  121. exit