# postgresql

#     Get the PostgreSQL version
UserParameter=psql.version[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "show server_version"
#    Get the total number of Server Processes that are active
UserParameter=psql.server_processes[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "select sum(numbackends) from pg_stat_database"
#    Get the total number of commited transactions
UserParameter=psql.tx_commited[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "select sum(xact_commit) from pg_stat_database"
#    Get the total number of rolled back transactions
UserParameter=psql.tx_rolledback[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "select sum(xact_rollback) from pg_stat_database"

#    replication status
#    MASTER: pg_stat_replication view: gives the number of current replication streams
UserParameter=psql.replication_state[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -tc "select count(1) from pg_stat_replication where state = 'streaming' and application_name = 'walreceiver';"
#    Get the number of current connections to postgres
UserParameter=psql.current_connections[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "select count(*) from pg_stat_activity;"
#    max_connection parameter
UserParameter=psql.max_connections[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "show max_connections;"

# type of connections:
UserParameter=psql.connstate[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $2 -A -t -c "select count(*) from pg_stat_activity where state = '$1';"

# SLAVE replication lag in second, this can vary widely when there is no activity on the master
UserParameter=psql.replication_timelag[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -A -t -c "SELECT extract(EPOCH FROM (clock_timestamp() - pg_last_xact_replay_timestamp())) AS time_lag;"

# Discovery rule for databases
UserParameter=pgsql.db.discovery[*],/etc/zabbix/scripts/dyn_json.sh DBNAME $(/usr/bin/sudo -u postgres psql -p $1 -d postgres -A -t -c "SELECT datname FROM pg_database;"|grep -v template |grep -v postgres)

# Get the size of a Database (in bytes)
UserParameter=psql.db_size[*],/usr/bin/sudo -u postgres /usr/bin/psql -A -t -c "select pg_database_size('$1')"
# Get the size of the target table for a specific database ($1 = db, $2 = table)
UserParameter=psql.db_table_size[*],/usr/bin/sudo -u postgres psql -d $1 -A -t -c "select sum(pg_relation_size(inhrelid::regclass)) from pg_inherits where inhparent='$2'::regclass::oid;"
# Get the maximum number of connection for a specific database (-1 = no limit)
UserParameter=psql.db_datconnlimit[*],/usr/bin/sudo -u postgres /usr/bin/psql -A -t -c "select datconnlimit from pg_database where datname = '$1'"

####################################################################################################
# BEGIN pg_stat_database stats
# Get number of active connections for a specified database
UserParameter=psql.db_connections[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f3
# xact_commit
UserParameter=psql.db_xact_commit[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f4
# xact_rollback
UserParameter=psql.db_xact_rollback[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f5
# Number of disk blocks read in this database
UserParameter=psql.db_blks_read[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f6
# Number of Number of times disk blocks were found already in the buffer cache, so that a read was not necessary
UserParameter=psql.db_blks_hit[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f7
# Get number of tuples returned for a specified database
UserParameter=psql.db_returned[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f8
# Get number of tuples fetched for a specified database
UserParameter=psql.db_fetched[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f9
# Get number of tuples inserted for a specified database
UserParameter=psql.db_inserted[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f10
# Get number of tuples updated for a specified database
UserParameter=psql.db_updated[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f11
# Get number of tuples deleted for a specified database
UserParameter=psql.db_deleted[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f12
# Number of queries canceled due to conflicts with recovery in this database. (Conflicts occur only on standby servers; see pg_stat_database_conflicts for details.)
UserParameter=psql.db_conflicts[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f13
# Number of temporary files created by queries in this database. All temporary files are counted
UserParameter=psql.db_temp_files[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f14
# Total amount of data written to temporary files by queries in this database. All temporary files are counted
UserParameter=psql.db_temp_bytes[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f15
# Number of deadlocks detected in this database
UserParameter=psql.db_deadlocks[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f16
# Time spent reading data file blocks by backends in this database, in milliseconds
UserParameter=psql.db_blk_read_time[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f17
# Time spent writing data file blocks by backends in this database, in milliseconds
UserParameter=psql.db_blk_write_time[*],grep ";$1;" /dev/shm/zabbix-pg_stat_database-$2.cache| cut -d';' -f18
# END pg_stat_database stats
####################################################################################################


# generic (unused)
UserParameter=psql_stat_db[*],/usr/bin/sudo -u postgres /usr/bin/psql  -A -t -c "select $2 from pg_stat_database where datname = '$1'"

# create cache file
UserParameter=psql.pg_stat_database[*],/etc/zabbix/scripts/pg_stat_database.sh $1

# replication slot discovery, $1 = port
UserParameter=pgsql.replication_slots.discovery[*],/etc/zabbix/scripts/pg_replication_slots_discovery.sh $1
# replication slot info, $1 = port, $2 = slot_name, $3 = column
UserParameter=pgsql.replication_slots.info[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select $3 from pg_replication_slots where slot_name = '$2'"

# replications discovery, $1 = port
UserParameter=pgsql.stat_replication.discovery[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select client_addr from pg_stat_replication" | awk 'BEGIN{printf "{\"data\":["}; {printf c"{\"{#REPLICATION}\":\""$$1"\"}";c=","}; END{print "]}"}'
# replication info, $1 = port, $2 = client_addr, $3 = column
UserParameter=pgsql.stat_replication.info[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select $3 from pg_stat_replication where client_addr = '$2'"


####################################################################################################
# postgresql live configuration monitoring

# monitor the size of a postgres parameter that is dependant on the block_size (ex: shared_buffers)
UserParameter=pgsql.parameter.block_size.get[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select setting::bigint * current_setting('block_size')::bigint from pg_settings where name='$2';"
# monitor a parameter in its native unit (usually ms for time based parameters for example)
UserParameter=pgsql.parameter.get[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select setting from pg_settings where name='$2';"
# use a simple "show" for parameters that don't have strange unit (ex: autovacuum_freeze_max_age, etc)
UserParameter=pgsql.parameter.show[*],/usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "show $2;"


####################################################################################################
# get the number of CPU threads on the system
UserParameter=pgsql.nproc,nproc

# average duration of currently active statement, excluding the ones from replication and system users
UserParameter=pgsql.average_user_active_transaction_duration[*],echo -n 0 && /usr/bin/sudo -u postgres /usr/bin/psql -p $1 -Atc "select EXTRACT(EPOCH FROM avg(now() - xact_start)) from pg_stat_activity where state = 'active' and usename not like '%replica%' and usename not like '%pitr%' and usename <> 'postgres'"

# extract statitics from the postgres statistics collector

# pg_stat_slru
UserParameter=pgsql.stats.pg_stat_slru.discovery[*],/etc/zabbix/scripts/dyn_json.sh NAME $(sudo -u postgres psql -p $1 -Atc "SELECT name FROM pg_stat_slru")
UserParameter=pgsql.stats.pg_stat_slru[*],sudo -u postgres psql -p $1 -Atc "SELECT $3 from pg_stat_slru where name = '$2'"
# this item is the same as the previous one, but we want to display the rate with the previous item and the sum with this one
# and zabbix doesn't allow the usage of the same userparameter for different items
UserParameter=pgsql.stats.pg_stat_slru_sum[*],sudo -u postgres psql -p $1 -Atc "SELECT $3 from pg_stat_slru where name = '$2'"

# pg-backup.service status
UserParameter=systemd.unit.pg-backup,sudo systemctl is-failed pg-backup.service
