|
| 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