-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpg-connections.sh
More file actions
executable file
·33 lines (26 loc) · 1.11 KB
/
pg-connections.sh
File metadata and controls
executable file
·33 lines (26 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
set -e
if [ $# -ne 2 ] ; then
echo "Usage: $(basename $0) OpenbravoProperties_Path Connection_Number_Threshold"
exit 1
fi
OBPROPS=$1
CON_NUM_THRES=$2
DBNAME=$(awk -F = '/^bbdd.sid/ {print $2}' $OBPROPS)
DBUSER=$(awk -F = '/^bbdd.user/ {print $2}' $OBPROPS)
DBPASS=$(awk -F = '/^bbdd.pass/ {print $2}' $OBPROPS)
DBPORT=$(awk -F = '/^bbdd.url/ {print $2}' $OBPROPS | sed 's/.*:\([0-9]*\)/\1/')
DBHOST=$(awk -F = '/^bbdd.url/ {print $2}' $OBPROPS | cut -d':' -f3 | sed 's#//##')
DATESTAMP=$( date +"%Y-%m-%d" )
LOGFILE="pg_conn-${DATESTAMP}.log"
run_pg_command()
{
PGPASSWORD=$DBPASS psql -d $DBNAME -U $DBUSER -h $DBHOST -p $DBPORT -t -c "$*" 2>&1 ;
}
NUM_CON=$( run_pg_command "select count(*) from pg_stat_activity" )
echo -ne "\n[$(date)] Open connections $NUM_CON " >> $LOGFILE
if [ $NUM_CON -gt $CON_NUM_THRES ] ; then
TIMESTAMP=$( date +"%Y-%m-%d-%H%M%S" )
echo -n " --- saving connection info to ${TIMESTAMP}.csv" >> $LOGFILE
run_pg_command "copy (select now(), pid, xact_start, query_start, state_change, waiting, state, query from pg_stat_activity) to STDOUT with csv header" > ${TIMESTAMP}.csv
fi