v-update-sys-rrd-pgsql 4.3 KB

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