-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatDB
More file actions
executable file
·63 lines (58 loc) · 1.64 KB
/
statDB
File metadata and controls
executable file
·63 lines (58 loc) · 1.64 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
export DBHOST="localhost"
declare -a Dbs=('postgres' 'gtb' 'serbia' 'chbs' 'mvisurgut' 'gtb_src' 'gtb_dst' ) #('gtb' 'postgres' 'serbia' 'chbs')
getFields(){
#Пример вызова cat /etc/fstab | getFields 1,2,3... или getFields ";" 1,2,3...
declare -a argv=`echo $@ | sed s/,/\ /g`
#echo "argv=${argv[@]}"
filtr=("")
if test $# -eq 0
then
echo -e "Example: cat /etc/fstab | getFields \";\" 1,2,3,... Arguments is number fields which you want view. Separator is option. cat /etc/fstab | getFields 1,2,3... "
elif test $# -eq 1
then
filtr=("awk '{print")
for arg in ${argv[@]}
do
filtr+=(`printf "$%s\" \"" $arg`)
done
filtr+=(`printf "}'"`)
elif test $# -eq 2
then
argv=`echo ${@:2:$(($#))} | sed s/,/\ /g`
filtr=("awk -F '$1' '{print")
for arg in ${argv[@]}
do
filtr+=(`printf "$%s\" \"" $arg`)
done
filtr+=(`printf "}'"`)
fi
#echo "${filtr[@]}"
local _stdin="$(< /dev/stdin)"
#echo "stdin: $_stdin"
execFiltr="`echo ${filtr[@]}`"
sudo sh -c "echo \"$_stdin\" | $execFiltr"
}
getConnections(){
for db in ${Dbs[@]}
do
echo "connections to $db"
command="SELECT * FROM pg_stat_activity;"
psql -h $DBHOST -d $db --command="$command" | getFields "|" 2,5,7,9,12,15,18
done
}
killConnections(){
#declare -a localDbs=('gtb' 'serbia' 'postgres')
#for db in ${localDbs[@]}
for db in ${Dbs[@]}
do
echo "kill all connections to $db"
command="SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='$db' AND pid <> pg_backend_pid() AND state='idle';"
psql -h $DBHOST -d $db --command="$command" > /dev/null
done
}
main(){
getConnections
killConnections
}
main