Files
VestaCP/bin/v_update_sys_rrd_pgsql

147 lines
4.2 KiB
Plaintext
Raw Normal View History

2011-10-17 16:58:46 +03:00
#!/bin/bash
# info: update PostgreSQL rrd
# options: period
#
# The function is for updating postgresql rrd database and graphic.
2011-10-17 16:58:46 +03:00
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
2011-10-18 12:51:59 +03:00
# Argument defenition
2011-12-15 00:19:04 +02:00
period=${1-daily}
2011-10-18 12:51:59 +03:00
2012-03-31 22:55:50 +03:00
# Includes
source $VESTA/conf/vesta.conf
2012-04-30 12:32:11 +03:00
source $VESTA/func/main.sh
2011-10-17 16:58:46 +03:00
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
2011-12-15 00:19:04 +02:00
# Switching on time period
case $period in
daily) start='-1d'; end='now'; grid='MINUTE:30:HOUR:1:HOUR:4:0:%H:%M';;
weekly) start='-7d'; end='now'; grid='HOUR:8:DAY:1:DAY:1:0:%a %d';;
monthly) start='-1m'; end='now'; grid='WEEK:1:WEEK:1:WEEK:1:0:%b %d';;
yearly) start='-1y'; end='now'; grid='MONTH:1:YEAR:1:MONTH:2:2419200:%b';;
2011-12-18 22:39:29 +02:00
*) exit $E_RRD ;;
2011-12-15 00:19:04 +02:00
esac
2011-10-17 16:58:46 +03:00
# Checking directory
2012-03-06 22:07:06 +02:00
if [ ! -d "$RRD/db" ]; then
mkdir $RRD/db
2011-10-17 16:58:46 +03:00
fi
# Parsing db hosts
2012-03-06 22:07:06 +02:00
conf="$VESTA/conf/pgsql.conf"
2011-10-17 16:58:46 +03:00
fields='$HOST'
nohead=1
hosts=$(shell_list)
2011-10-17 16:58:46 +03:00
check_row=$(echo "$hosts" |wc -l)
if [ 0 -eq "$check_row" ]; then
exit
fi
# Parsing excludes
2012-03-06 22:07:06 +02:00
for exclude in $(echo ${RRD_PGSQL_EXCLUDE//,/ }); do
2011-10-17 16:58:46 +03:00
hosts=$(echo "$hosts" |grep -vw "$exclude" )
done
for host in $hosts; do
# Checking database
2012-03-06 22:07:06 +02:00
if [ ! -e "$RRD/db/pgsql_$host.rrd" ]; then
2011-10-17 16:58:46 +03:00
# Adding database
2012-03-06 22:07:06 +02:00
rrdtool create $RRD/db/pgsql_$host.rrd --step $RRD_STEP \
2011-10-17 16:58:46 +03:00
DS:A:GAUGE:600:U:U \
DS:T:COUNTER:600:U:U \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797
fi
2012-04-28 22:44:22 +03:00
if [ "$period" = 'daily' ]; then
2011-10-18 12:51:59 +03:00
# Defining host credentials
host_str=$(grep "HOST='$host'" $conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
export PGPASSWORD="$PASSWORD"
sql="psql -h $HOST -U $USER -p $PORT -c"
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]
then
echo "Error: config is broken"
2012-03-31 22:55:50 +03:00
log_event "$E_PARSING" "$EVENT"
2011-12-18 22:39:29 +02:00
exit $E_PARSING
2011-10-18 12:51:59 +03:00
fi
# Parsing data
q='SELECT SUM(xact_commit + xact_rollback), SUM(numbackends)
FROM pg_stat_database;'
status=$($sql plsql -d postgres -c "$q" 2>/dev/null); code="$?"
if [ '0' -ne "$code" ]; then
active=0
slow=0
else
active=$(echo "$status"|head -n 3|tail -n 1|awk '{print $3}')
trans=$(echo "$status"|head -n 3 |tail -n 1|awk '{print $1}')
fi
# Updating rrd
export PGPASSWORD='pgsql'
2012-03-06 22:07:06 +02:00
rrdtool update $RRD/db/pgsql_$host.rrd N:$active:$trans
2011-10-17 16:58:46 +03:00
fi
2011-10-18 12:51:59 +03:00
# Updating rrd graph
2012-03-06 22:07:06 +02:00
rrdtool graph $RRD/db/$period-pgsql_$host.png \
2011-10-17 16:58:46 +03:00
--imgformat PNG \
--height="120" \
--width="440" \
2011-12-15 00:19:04 +02:00
--start "$start" \
--end "$end" \
2011-10-17 16:58:46 +03:00
--vertical-label "Queries" \
2011-12-15 00:19:04 +02:00
--x-grid "$grid" \
2012-04-28 22:44:22 +03:00
-c "BACK#7a766d" \
-c "SHADEA#7a766d" \
-c "SHADEB#7a766d" \
-c "FONT#FFFFFF" \
-c "CANVAS#302c2d" \
2011-10-17 16:58:46 +03:00
-c "GRID#666666" \
-c "MGRID#AAAAAA" \
2012-04-28 22:44:22 +03:00
-c "FRAME#302c2d" \
2011-10-17 16:58:46 +03:00
-c "ARROW#FFFFFF" \
2012-03-06 22:07:06 +02:00
DEF:a=$RRD/db/pgsql_$host.rrd:A:AVERAGE \
DEF:t=$RRD/db/pgsql_$host.rrd:T:AVERAGE \
2011-10-17 16:58:46 +03:00
COMMENT:'\r' \
LINE1:a#fefda0:"Queries "\
2011-10-18 12:51:59 +03:00
GPRINT:a:'LAST: Current\:''%8.0lf' \
2011-10-17 16:58:46 +03:00
GPRINT:a:'MIN: Min\:''%8.0lf' \
GPRINT:a:'MAX: Max\:''%8.0lf\j' \
LINE2:t#f57900:"Transactions" \
2011-10-18 12:51:59 +03:00
GPRINT:t:'LAST:Current\:''%8.0lf' \
2011-10-17 16:58:46 +03:00
GPRINT:t:'MIN:Min\:''%8.0lf' \
2012-03-06 22:07:06 +02:00
GPRINT:t:'MAX:Max\:''%8.0lf\j' &>/dev/null; result=$?
2011-10-18 12:51:59 +03:00
if [ "$result" -ne 0 ]; then
2011-12-18 22:39:29 +02:00
exit $E_RRD
2011-10-18 12:51:59 +03:00
fi
2011-10-17 16:58:46 +03:00
done
2011-10-18 12:51:59 +03:00
2011-10-17 16:58:46 +03:00
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
2011-11-01 12:27:45 +02:00
exit