v-update-sys-rrd-pgsql 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. hosts=$(grep HOST $conf |awk '{print $1}' |cut -f 2 -d \')
  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 ${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 "$RRD/db/pgsql_$host.rrd" ]; then
  43. # Adding database
  44. rrdtool create $RRD/db/pgsql_$host.rrd --step $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 [ "$period" = 'daily' ]; 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 -c"
  64. # Checking empty vars
  65. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ]; then
  66. echo "Error: config is broken"
  67. log_event "$E_PARSING" "$ARGUMENTS"
  68. exit $E_PARSING
  69. fi
  70. # Parsing data
  71. q='SELECT SUM(xact_commit + xact_rollback), SUM(numbackends)
  72. FROM pg_stat_database;'
  73. status=$($sql psql -d postgres -c "$q" 2>/dev/null); code="$?"
  74. if [ '0' -ne "$code" ]; then
  75. active=0
  76. slow=0
  77. else
  78. active=$(echo "$status"|head -n 3|tail -n 1|awk '{print $3}')
  79. trans=$(echo "$status"|head -n 3 |tail -n 1|awk '{print $1}')
  80. fi
  81. # Updating rrd
  82. export PGPASSWORD='pgsql'
  83. rrdtool update $RRD/db/pgsql_$host.rrd N:$active:$trans
  84. fi
  85. # Updating rrd graph
  86. rrdtool graph $RRD/db/$period-pgsql_$host.png \
  87. --imgformat PNG \
  88. --height="150" \
  89. --width="670" \
  90. --start "$start" \
  91. --end "$end" \
  92. --vertical-label "Queries" \
  93. --x-grid "$grid" \
  94. -c "BACK#ffffff" \
  95. -c "SHADEA#ffffff" \
  96. -c "SHADEB#ffffff" \
  97. -c "FONT#555555" \
  98. -c "CANVAS#302c2d" \
  99. -c "GRID#666666" \
  100. -c "MGRID#AAAAAA" \
  101. -c "FRAME#302c2d" \
  102. -c "ARROW#FFFFFF" \
  103. DEF:a=$RRD/db/pgsql_$host.rrd:A:AVERAGE \
  104. DEF:t=$RRD/db/pgsql_$host.rrd:T:AVERAGE \
  105. COMMENT:'\r' \
  106. LINE1:a#fefda0:"Queries "\
  107. GPRINT:a:'LAST: Current\:''%8.0lf' \
  108. GPRINT:a:'MIN: Min\:''%8.0lf' \
  109. GPRINT:a:'MAX: Max\:''%8.0lf\j' \
  110. LINE2:t#f57900:"Transactions" \
  111. GPRINT:t:'LAST:Current\:''%8.0lf' \
  112. GPRINT:t:'MIN:Min\:''%8.0lf' \
  113. GPRINT:t:'MAX:Max\:''%8.0lf\j' &>/dev/null; result=$?
  114. if [ "$result" -ne 0 ]; then
  115. exit $E_RRD
  116. fi
  117. done
  118. #----------------------------------------------------------#
  119. # Vesta #
  120. #----------------------------------------------------------#
  121. exit