v_update_sys_rrd_pgsql 4.2 KB

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