Skip to content

Commit 75c118e

Browse files
author
Sergey V. Kravchuk
committed
[+] init
0 parents  commit 75c118e

File tree

4 files changed

+654
-0
lines changed

4 files changed

+654
-0
lines changed

Diff for: postgresql/detect_database_postgresql.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
LIST_DATABSE=`psql -t -c "select datname from pg_stat_database where datname not like 'template%'" postgres|sed '\$ d'|sed 's/ //g'`
3+
FIRST_ELEMENT=1
4+
5+
function json_head {
6+
printf "{";
7+
printf "\"data\":[";
8+
}
9+
10+
function json_end {
11+
printf "]";
12+
printf "}";
13+
}
14+
15+
function check_first_element {
16+
if [[ $FIRST_ELEMENT -ne 1 ]]; then
17+
printf ","
18+
fi
19+
FIRST_ELEMENT=0
20+
}
21+
22+
function databse_detect {
23+
json_head
24+
for dbname in $LIST_DATABSE
25+
do
26+
local dbname_t=$(echo $dbname| sed 's!\n!!g')
27+
check_first_element
28+
printf "{"
29+
printf "\"{#DBNAME}\":\"$dbname_t\""
30+
printf "}"
31+
done
32+
json_end
33+
}
34+
databse_detect

Diff for: postgresql/postgresql-stat.sh

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#! /bin/bash
2+
3+
rval=0
4+
sql=""
5+
6+
case $1 in
7+
'totalsize')
8+
sql="select sum(pg_database_size(datid)) as total_size from pg_stat_database"
9+
;;
10+
11+
'db_cache')
12+
# comprueba los parametros
13+
if [ ! -z $2 ]; then
14+
shift
15+
sql="select cast(blks_hit/(blks_read+blks_hit+0.000001)*100.0 as numeric(5,2)) as cache from pg_stat_database where datname = '$1'"
16+
fi
17+
;;
18+
19+
'db_success')
20+
# comprueba los parametros
21+
if [ ! -z $2 ]; then
22+
shift
23+
sql="select cast(xact_commit/(xact_rollback+xact_commit+0.000001)*100.0 as numeric(5,2)) as success from pg_stat_database where datname = '$1'"
24+
fi
25+
;;
26+
27+
'server_processes')
28+
sql="select sum(numbackends) from pg_stat_database"
29+
;;
30+
31+
'tx_commited')
32+
sql="select sum(xact_commit) from pg_stat_database"
33+
;;
34+
35+
'tx_rolledback')
36+
sql="select sum(xact_rollback) from pg_stat_database"
37+
;;
38+
39+
'db_size')
40+
# comprueba los parametros
41+
if [ ! -z $2 ]; then
42+
shift
43+
sql="select pg_database_size('$1')" #as size"
44+
fi
45+
;;
46+
47+
'db_connections')
48+
# comprueba los parametros
49+
if [ ! -z $2 ]; then
50+
shift
51+
sql="select numbackends from pg_stat_database where datname = '$1'"
52+
fi
53+
;;
54+
55+
'db_returned')
56+
# comprueba los parametros
57+
if [ ! -z $2 ]; then
58+
shift
59+
sql="select tup_returned from pg_stat_database where datname = '$1'"
60+
fi
61+
;;
62+
63+
'db_fetched')
64+
# comprueba los parametros
65+
if [ ! -z $2 ]; then
66+
shift
67+
sql="select tup_fetched from pg_stat_database where datname = '$1'"
68+
fi
69+
;;
70+
71+
'db_inserted')
72+
# comprueba los parametros
73+
if [ ! -z $2 ]; then
74+
shift
75+
sql="select tup_inserted from pg_stat_database where datname = '$1'"
76+
fi
77+
;;
78+
79+
'db_updated')
80+
# comprueba los parametros
81+
if [ ! -z $2 ]; then
82+
shift
83+
sql="select tup_updated from pg_stat_database where datname = '$1'"
84+
fi
85+
;;
86+
87+
'db_deleted')
88+
# comprueba los parametros
89+
if [ ! -z $2 ]; then
90+
shift
91+
sql="select tup_deleted from pg_stat_database where datname = '$1'"
92+
fi
93+
;;
94+
95+
'db_commited')
96+
# comprueba los parametros
97+
if [ ! -z $2 ]; then
98+
shift
99+
sql="select xact_commit from pg_stat_database where datname = '$1'"
100+
fi
101+
;;
102+
103+
'db_rolled')
104+
# comprueba los parametros
105+
if [ ! -z $2 ]; then
106+
shift
107+
sql="select xact_rollback from pg_stat_database where datname = '$1'"
108+
fi
109+
;;
110+
111+
'version')
112+
sql="version"
113+
;;
114+
*)
115+
echo "usage:"
116+
echo " $0 totalsize -- Check the total databases size."
117+
echo " $0 db_cache <dbname> -- Check the database cache hit ratio (percentage)."
118+
echo " $0 db_success <dbname> -- Check the database success rate (percentage)."
119+
echo " $0 server_processes -- Check the total number of Server Processes that are active."
120+
echo " $0 tx_commited -- Check the total number of commited transactions."
121+
echo " $0 tx_rolledback -- Check the total number of rolled back transactions."
122+
echo " $0 db_size <dbname> -- Check the size of a Database (in bytes)."
123+
echo " $0 db_connections <dbname> -- Check the number of active connections for a specified database."
124+
echo " $0 db_returned <dbname> -- Check the number of tuples returned for a specified database."
125+
echo " $0 db_fetched <dbname> -- Check the number of tuples fetched for a specified database."
126+
echo " $0 db_inserted <dbname> -- Check the number of tuples inserted for a specified database."
127+
echo " $0 db_updated <dbname> -- Check the number of tuples updated for a specified database."
128+
echo " $0 db_deleted <dbname> -- Check the number of tuples deleted for a specified database."
129+
echo " $0 db_commited <dbname> -- Check the number of commited back transactions for a specified database."
130+
echo " $0 db_rolled <dbname> -- Check the number of rolled back transactions for a specified database."
131+
echo " $0 version -- The PostgreSQL version."
132+
exit $rval
133+
;;
134+
esac
135+
136+
if [ "$sql" != "" ]; then
137+
if [ "$sql" == "version" ]; then
138+
psql --version|head -n1
139+
rval=$?
140+
else
141+
psql -t -c "$sql" postgres
142+
rval=$?
143+
fi
144+
fi
145+
146+
if [ "$rval" -ne 0 ]; then
147+
echo "ZBX_NOTSUPPORTED"
148+
fi
149+
150+
exit $rval

0 commit comments

Comments
 (0)