v_upd_sys_rrd_pgsql 4.2 KB

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