v_upd_sys_rrd_pgsql 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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_PGSQL_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/pgsql_$host.rrd" ]; then
  36. # Adding database
  37. rrdtool create $V_RRD/db/pgsql_$host.rrd --step $V_RRD_STEP \
  38. DS:A:GAUGE:600:U:U \
  39. DS:T: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. export PGPASSWORD="$PASSWORD"
  56. sql="psql -h $HOST -U $USER -p $PORT -c"
  57. # Checking empty vars
  58. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]
  59. then
  60. echo "Error: config is broken"
  61. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  62. exit $E_PARSE_ERROR
  63. fi
  64. # Parsing data
  65. q='SELECT SUM(xact_commit + xact_rollback), SUM(numbackends)
  66. FROM pg_stat_database;'
  67. status=$($sql plsql -d postgres -c "$q" 2>/dev/null); code="$?"
  68. if [ '0' -ne "$code" ]; then
  69. active=0
  70. slow=0
  71. else
  72. active=$(echo "$status"|head -n 3|tail -n 1|awk '{print $3}')
  73. trans=$(echo "$status"|head -n 3 |tail -n 1|awk '{print $1}')
  74. fi
  75. # Updating rrd
  76. export PGPASSWORD='pgsql'
  77. rrdtool update $V_RRD/db/pgsql_$host.rrd N:$active:$trans
  78. fi
  79. # Updating rrd graph
  80. rrdtool graph $V_RRD/db/pgsql_$host.png \
  81. --imgformat PNG \
  82. --height="120" \
  83. --width="440" \
  84. --start "$rrd_start" \
  85. --end "$rrd_end" \
  86. --title "PostgreSQL Usage on $host" \
  87. --vertical-label "Queries" \
  88. --x-grid "$rrd_grid" \
  89. -c "BACK#484439" \
  90. -c "SHADEA#484439" \
  91. -c "SHADEB#484439" \
  92. -c "FONT#DDDDDD" \
  93. -c "CANVAS#202020" \
  94. -c "GRID#666666" \
  95. -c "MGRID#AAAAAA" \
  96. -c "FRAME#202020" \
  97. -c "ARROW#FFFFFF" \
  98. DEF:a=$V_RRD/db/pgsql_$host.rrd:A:AVERAGE \
  99. DEF:t=$V_RRD/db/pgsql_$host.rrd:T:AVERAGE \
  100. COMMENT:'\r' \
  101. LINE1:a#fefda0:"Queries "\
  102. GPRINT:a:'LAST: Current\:''%8.0lf' \
  103. GPRINT:a:'MIN: Min\:''%8.0lf' \
  104. GPRINT:a:'MAX: Max\:''%8.0lf\j' \
  105. LINE2:t#f57900:"Transactions" \
  106. GPRINT:t:'LAST:Current\:''%8.0lf' \
  107. GPRINT:t:'MIN:Min\:''%8.0lf' \
  108. GPRINT:t:'MAX:Max\:''%8.0lf\j' >/dev/null 2>/dev/null; result=$?
  109. if [ "$result" -ne 0 ]; then
  110. exit $E_RRD_FAILED
  111. fi
  112. done
  113. #----------------------------------------------------------#
  114. # Vesta #
  115. #----------------------------------------------------------#
  116. exit $OK