v_upd_sys_rrd_pgsql 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # info: updating PostgreSQL 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/pgsql.conf"
  23. fields='$HOST'
  24. nohead=1
  25. hosts=$(shell_list)
  26. check_row=$(echo "$hosts" |wc -l)
  27. if [ 0 -eq "$check_row" ]; then
  28. exit
  29. fi
  30. # Parsing excludes
  31. for exclude in $(echo ${V_RRD_PGSQL_EXCLUDE//,/ }); do
  32. hosts=$(echo "$hosts" |grep -vw "$exclude" )
  33. done
  34. for host in $hosts; do
  35. # Checking database
  36. if [ ! -e "$V_RRD/db/pgsql_$host.rrd" ]; then
  37. # Adding database
  38. rrdtool create $V_RRD/db/pgsql_$host.rrd --step $V_RRD_STEP \
  39. DS:A:GAUGE:600:U:U \
  40. DS:T:COUNTER:600:U:U \
  41. RRA:AVERAGE:0.5:1:600 \
  42. RRA:AVERAGE:0.5:6:700 \
  43. RRA:AVERAGE:0.5:24:775 \
  44. RRA:AVERAGE:0.5:288:797 \
  45. RRA:MAX:0.5:1:600 \
  46. RRA:MAX:0.5:6:700 \
  47. RRA:MAX:0.5:24:775 \
  48. RRA:MAX:0.5:288:797
  49. fi
  50. if [ -z "$1" ]; then
  51. # Defining host credentials
  52. host_str=$(grep "HOST='$host'" $conf)
  53. for key in $host_str; do
  54. eval ${key%%=*}=${key#*=}
  55. done
  56. export PGPASSWORD="$PASSWORD"
  57. sql="psql -h $HOST -U $USER -p $PORT -c"
  58. # Checking empty vars
  59. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]
  60. then
  61. echo "Error: config is broken"
  62. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  63. exit $E_PARSE_ERROR
  64. fi
  65. # Parsing data
  66. q='SELECT SUM(xact_commit + xact_rollback), SUM(numbackends)
  67. FROM pg_stat_database;'
  68. status=$($sql plsql -d postgres -c "$q" 2>/dev/null); code="$?"
  69. if [ '0' -ne "$code" ]; then
  70. active=0
  71. slow=0
  72. else
  73. active=$(echo "$status"|head -n 3|tail -n 1|awk '{print $3}')
  74. trans=$(echo "$status"|head -n 3 |tail -n 1|awk '{print $1}')
  75. fi
  76. # Updating rrd
  77. export PGPASSWORD='pgsql'
  78. rrdtool update $V_RRD/db/pgsql_$host.rrd N:$active:$trans
  79. fi
  80. # Updating rrd graph
  81. rrdtool graph $V_RRD/db/pgsql_$host.png \
  82. --imgformat PNG \
  83. --height="120" \
  84. --width="440" \
  85. --start "$rrd_start" \
  86. --end "$rrd_end" \
  87. --title "PostgreSQL Usage on $host" \
  88. --vertical-label "Queries" \
  89. --x-grid "$rrd_grid" \
  90. -c "BACK#484439" \
  91. -c "SHADEA#484439" \
  92. -c "SHADEB#484439" \
  93. -c "FONT#DDDDDD" \
  94. -c "CANVAS#202020" \
  95. -c "GRID#666666" \
  96. -c "MGRID#AAAAAA" \
  97. -c "FRAME#202020" \
  98. -c "ARROW#FFFFFF" \
  99. DEF:a=$V_RRD/db/pgsql_$host.rrd:A:AVERAGE \
  100. DEF:t=$V_RRD/db/pgsql_$host.rrd:T: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. LINE2:t#f57900:"Transactions" \
  107. GPRINT:t:'LAST:Current\:''%8.0lf' \
  108. GPRINT:t:'MIN:Min\:''%8.0lf' \
  109. GPRINT:t:'MAX:Max\:''%8.0lf\j' >/dev/null 2>/dev/null; result=$?
  110. if [ "$result" -ne 0 ]; then
  111. exit $E_RRD_FAILED
  112. fi
  113. done
  114. #----------------------------------------------------------#
  115. # Vesta #
  116. #----------------------------------------------------------#
  117. exit