Monitoring PostgreSQL replication lag using Stackdriver and Statsd
Posted on September 15, 2015
One of the important thing to monitor on the PostgreSQL is the streaming replication, it is very important to know the latency between the pg master and pg slave so we can make sure that our database are consistent
There’s a PostgreSQL functions to check the the WAL current status. for the master “pg_current_xlog_location()” and for slave “pg_last_xlog_replay_location()” this can provide the result of the current byte position (e.g. 0/D02E9A0 ) but not on a numerical value that is why we need to implement a script to convert the current byte position into integer and send the value to monitoring services like Stackdriver
Statsd
A network daemon runs on Node.js platform and listen for statistics, it send data on a backend service like Graphite over UDP or TCP. to learn more about Statsd kindly visit this Link
Set-up Statsd to send aggregated metrics on Stackdriver
Need to install the dependency for statsd (nodejs and npm)
Download the statsd and the stackdriver-stackd-backend then define the api-key of your stackdriver account of your statsd configuration
Create a stackdriver statsd config
To run statsd
Script to monitor the Write Ahead Log on Master
wal_master.py
Script to monitor the Write Ahead Log status on slave
wal_slave.py
You can checkout the python script for both master and slave here
Note:
Script dependencies
Python version 2.7
python-statsd
This will send the extracted data to Stackdriver and it will automatically populate the “wal_master_status” and the “wal_slave_status” on metric type option once adding a chart for your own dashboard
For more details about sending custom metrics on Stackdriver kindly check here