v_upd_sys_rrd_pgsql 3.9 KB

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