v-update-sys-rrd-pgsql 4.1 KB

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