Monitor the Heck Out Of Your Database
Postgres Open 2011
Josh Williams End Point Corporation
Data in... Data out...
What are we looking for? Why do we care?
Performance of the system Application throughput Is it dead about to die?
Different people care about different things
Devs
System Performance
Application Throughput
Ops
Is it about to die?
PHB's
Monitoring Postgres
Log Monitoring for errors Log Monitoring for query performance Control files / External commands Statistics from the database itself
Log Monitoring for Error Conditions
ERROR: division by zero FATAL: password authentication failed for user postgres PANIC: could not write to file pg_xlog/...: No space left on device tail_n_mail
tail_n_mail
http://bucardo.org/wiki/tail_n_mail Written in Perl, requires Net::SMTP::SSL or sendmail binary Slight misnomer Sample Config: tail tail_n_mail
tail_n_mail
Create a file: tail_n_mail.config:
EMAIL:postgres_OMG@endpoint.com FROM:postgres@server.local MAILSUBJECT:PGERRORREPORT FILE:/var/log/pgsql%Y%m%d.log INCLUDE:FATAL: INCLUDE:PANIC: INCLUDE:ERROR:
tail_n_mail
Create a file: ~/.tailnmailrc:
log_line_prefix:'%t[%p]:[%l1]%u@d' or pgmode:syslog
(Check your postgresql.conf)
tail_n_mail
Test: perl tail_n_mail tail_n_mail.config Schedule it to run every minute: *****perltail_n_mail quiettail_n_mail.config
tail_n_mail
Copy, edit tail_n_mail.config:
EMAIL:postgres_OMG@endpoint.com FROM:postgres@server.local MAILSUBJECT:PGERRORREPORT FILE:/var/log/pgsql%Y%m%d.log INCLUDE:FATAL: INCLUDE:PANIC: INCLUDE:ERROR: EXCLUDE:ERROR:duplicatekey ^valueviolatesuniqueconstraint
Log Monitoring for Query Performance
By default successful SQL isn't logged Set in postgresql.conf: log_statement = 'ddl' log_min_duration_statement = 200 log_line_prefix = '%t [%p]: [%l-1] %u@%d ' pgFouine pgsi
pgFouine
http://pgfouine.projects.postgresql.org/ Written in PHP (???) A little quirky, esp. regarding line prefix For scheduled task, write a wrapper script cat $logdir/postgres-$yesterday.log \ | bin/pgfouine -logtype stderr -memorylimit 512 > pgfouine-$yesterday.html
pgFouine
pgsi the Postgres System Impact report
http://bucardo.org/wiki/pgsi Written in Perl Little more tolerant of log_line_prefix Invocation similar, write a wrapper script bin/pgsi.pl --quiet \ --file=$logdir/postgres-$yesterday.log \ > pgsi-$yesterday.html
pgsi the Postgres System Impact report
External Commands
But that's OLD stuff. I want to see NOW. And besides, who relies on email anymore? check_postgres
check_postgres
Intended to plug into Nagios or similar Collection of several monitoring actions http://bucardo.org/wiki/check_postgres Written in Perl (seeing a pattern?) Requirements ~~ vary depending on action check_postgres.pl action=foo --include=specific-object --exclude=objects --warning=X --critical=Y
check_postgres Important Actions
--action=backends Connections, and % of max_connections Thresholds really flexible: %, remaining Count active connections with --noidle --action=bloat --db=bar Heuristics to figure out wasted space --include specific tables or indexes
check_postgres --action=bloat
Take a look at the query, search for: ## This was fun to write
check_postgres Important Actions
--action=query_time Long-running queries Complex thresholds: 2 for 10 minutes --action=txn_idle Long-running idle transactions Mostly the same code as query_time --action=txn_wraparound Transactions since datfrozenxid
check_postgres Additional Actions
Useful for warm (or hot) standby: --action=checkpoint How long since the last checkpoint Uses pg_controldata, no PG connection --action=archive_ready Number of unarchived WAL files --action=hot_standby_delay Must be able to connect to both servers
check_postgres Additional Actions
Sanity Checks: --action=database_size No default thresholds --action=relation_size --action=wal_files --action=last_autovacuum / autoanalyze May be a little tricky
check_postgres Additional Actions
--action=prepared_txns Age of prepared transactions --action=new_version_pg Something else? --action=custom_query Please let us know! --action=saneversion?
Live Metrics and Statistics
check_postgres does metrics, too! --output={nagios,mrtg,cacti,simple} Metrics collection: Graphite / Cacti Other metrics sources: Getting Statistics from Postgres
check_postgres Metrics Actions
--action=backends
check_postgres Metrics Actions
--action=backends
check_postgres Metrics Actions
--action=locks
POSTGRES_LOCKSOK:DB"postgres"totallocks:53| time=0.05sproduction.total=52 production.waiting=0postgres.total=1 postgres.waiting=0
check_postgres Metrics Actions
--action=database_size Or more specifically: relation_size --action=dbstats Cacti-friendly pg_stat_database view --action=hitratio Keep track of buffer cache hit rate
Getting Statistics from Postgres Directly
If you don't have Cacti, Nagios, Graphite... Take a look at pg_stat_database:
[local]:5434|postgres=#select*frompg_stat_database; datid|datname|numbackends|xact_commit|xact_rollback| +++++ 1|template1|0|0|0| 11939|template0|0|0|0| 11947|postgres|2|18006|0| 16393|foo|3|76|15| (4rows)
Getting Statistics from Postgres Directly
If you don't have Cacti, Nagios, Graphite... Take a look at pg_stat_user_tables:
[local]:5432|postgres=#select*frompg_stat_user_tables; relid|scheman|relname|seq_scan|seq_tup_read|idx_scan| ++++++ 36659|public|gs2|5|30000033|| 19736|public|baz|5|0|| 19739|public|gs|6|60000006|| (3rows)
Getting Statistics from Postgres Directly
If you don't have Cacti, Nagios, Graphite... Take a look at pg_stat_bgwriter:
[local]:5432|psql81=#select*frompg_stat_bgwriter; checkpoints_timed|checkpoints_req|buffers_checkpoint|bu... +++ 1871|26|52956| (1row)
INSERT INTO bgwriter_history SELECT now(), * FROM pg_stat_bgwriter; Poor man's metrics collection!
Other Things to Monitor
The OS environment, of course Hardware Drive / RAID status! Load balancer / Connection pool Replication The application!
Other Things: OS Environment
Nagios (etc) will have built-in system checks Never a bad idea! Also see: sysstat Periodic snapshots of CPU, network, etc Hardware: See what your vendor provides
Other Things: Application
Load Balancer or Connection Pooler: Should provide their own metrics pgBouncer? check_postgres! Application: Give Graphite a look correlation FTW!
Monitoring Postgres
Log Monitoring for errors Log Monitoring for query performance Control files / External commands Statistics from the database itself
Questions?
Slides:
http://joshwilliams.name/talks/monitoring/