v_upd_sys_rrd_mysql 3.7 KB

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