SQL Database Monitoring with CollectD and MySQL

May 26, 2015 No Comments collectd

database monitoring collectd

The CollectD DBI Plugin executes custom SQL queries to generate metrics from most relational databases. Over any interval of time, the result of SQL requests such as “SELECT count(*) FROM pageview” is reported. The difference between successive call results is the number of events recorded in the database over the specified time period.

Because it’s widely used, MySQL will be used for this example, but this also works for:

DBI Plugin Example

On our website, each time a user performs a pageview, edit or postcomment action we save it in the interaction table. A user can belong to one of the groups admin, user or guest. Using CollectD DBI Plugin and some custom SQL requests, we can monitor what is happening in our MySQL database.

(Optional) To get the exactly same result, download the database dump and import in MySQL:

Executing both SQL requests every second produces new monitoring metrics:

  • event_type.edit
  • event_type.pageview
  • event_type.postcomment
  • visitor.pageview

Install Plugin DBI for CollectD

To query our MySQL database with Plugin DBI we will need to recompile CollectD with some extra dependencies:

After the dependencies are installed, install CollectD as usual:

During CollectD compilation ./configure you should see:

Configure and Start the Plugin DBI

Edit the CollectD configuration, usually located at /opt/collectd/etc/collectd.conf

Now restart CollectD:

For troubleshooting, add this configuration:

And start CollectD in the foreground:

Metrics Dashboard

At this point, integrating with a monitoring dashboard should be easy. To record MySQL request results, we chose the excellent InfluxDB. For displaying the metrics from InfluxDB, we chose Grafana.

To graph the MySQL metrics you will need to:

  1. Install CollectD on Debian or CentOS
  2. Install InfluxDB from source (or from InfluxDB)
  3. Send CollectD metrics to InfluxDB
  4. Install and Configure Grafana with InfluxDB

Optional: You don’t have to use InfluxDB/Grafana. CollectD integrates with many time series databases. It is fairly easy to integrate with different time series databases / monitoring dashboards.

Fake some user activity in MySQL:

With more pageviews, the value should increase:

count MySQL request

We can use InfluxDB DIFFERENCE aggregator to highlight events accruing in real time.

difference MySQL request

Open the Grafana web UI. Replicate the InfluxDB request:
SELECT difference(value) FROM dbi_value WHERE type_instance=’visitor.pageview’ AND $timeFilter GROUP BY time($interval) ORDER ASC;

Grafana MySQL monitoring

Done! We can watch our pageviews per second update in real time. Proceed the exact same way for the postcomment, pageview and edit metrics.

Conclusion

Code monitoring with StatsD is easy to install and configure. It’s even easier since CollectD now embeds a StatsD implementation.
But making code changes is a bit painful.

With CollectD DBI Plugin, it becomes possible to monitor an application, or at least its output, without any code changes. Any SQL request that generate numbers can be used to produce new metrics.

It is definitively a good starting point into application monitoring since it requires no code changes. However, it can also be considered an advance technique for monitoring what’s really happening within the database.

Monitor & detect anomalies with Anomaly.io

SIGN UP
help with term papers