v_upd_sys_rrd_pgsql 3.6 KB

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