Collecting syslog-ng statistics to Graphite

syslog-ng makes available various types of statistics. Data is available in a couple of forms: emitted regularly from the internal() source of syslog-ng or obtained using the syslog-ng-ctl utility from the command line. Due to the format that the internal source or the “stats” option of syslog-ng-ctl uses, it is not easy to send statistical data to Graphite or anywhere else. The syslog-ng-ctl utility provides a flexible “query” option, (available in recent versions of syslog-ng) which uses an easy-to-parse output format. Using the “jo” utility by @JPMens, you can convert the output to JSON, which can then be parsed by syslog-ng and forwarded to Graphite.

Before you begin

In order to use the “query” option of syslog-ng-ctl, you need a recent enough version of syslog-ng. The query option appeared in syslog-ng version 3.10.1. If your distribution of choice provides an older version, check to see if there are any up-to-date packages from unofficial repositories.

The “jo” utility can create JSON output from a shell. Unfortunately, it is not yet available in most distributions. Luckily, however, it is really easy to compile. Source code together with documentation are available on GitHub:

You also need Graphite installed. It’s easiest to install Graphite in Docker as I described in one of my earlier blogs.

Figuring out what to collect

Using the query option of syslog-ng-ctl, you can fine-tune what information to collect. Issue the following command to list available fields:

[root@localhost ~]# syslog-ng-ctl query list,ElasticSearch_v2,syslog-ng,syslog-ng.dropped
[root@localhost ~]#

If you want to apply wildcards in your query, then use the “get” option. To receive all values, use ‘*’. For a subset of values, use part of the name as a prefix. For example:

[root@localhost ~]# syslog-ng-ctl query get 'center.*'
[root@localhost ~]#

You can read in depth about the different fields available in log statistics in the syslog-ng documentation at

Configuring syslog-ng

Originally, I tried the logger utility to get JSON data into syslog-ng but it failed for larger messages (more fields included). So I recommend using netcat for sending statistical data over a network port to syslog-ng.

In the following configuration example:

  1. We use port 514 to receive logs.
  2. We set the “no-parse” flag as by default, syslog-ng parses everything as syslog, but in this case we collect pure JSON messages.
  3. The JSON parser adds the “stat.” prefix to the name-value pairs found.
  4. A JSON file destination is defined for easy debugging. You can delete / comment it out once you have everything working with Graphite.
  5. You also need to change the IP address in the Graphite destination to reflect your local environment.
  6. Finally, the log path connects all of these components together.

source s_stat {
    tcp(ip( port(514) flags(no-parse));
parser p_json {
    json-parser (prefix("stat."));
destination d_json {
        template("$(format-json --scope nv-pairs)\n"));
destination d_graphite {
  tcp( ""
    template("$(graphite-output --key stat.* )")
log {

Collecting statistics

In order to have a periodic update of syslog-ng statistics in Graphite, you need to run syslog-ng-ctl regularly. Doing this by hand in the long term is boring so I recommend using cron to run syslog-ng-ctl and related commands. Before adding the commands to the scheduler, you should test the commands from a shell as root.

syslog-ng-ctl query get '*' | sed -e 's/[\.|\#|\,|\:]/_/g' | jo | nc 514

This command line gets all available statistical values from syslog-ng.

Here’s what we do:

  1. In order to have a flat and valid name space, we replace all special characters here with underscores using sed. You can fine-tune field names for your environment by changing the search expression.
  2. We pipe the results to jo to format it to JSON.
  3. Finally, we use netcat to send the results to syslog-ng to port 514 on the local host.
  4. Check /var/log/test.json and/or Graphite to see if logs arrive as expected:
    [root@localhost conf.d]# tail -1 /var/log/test.json 
  1. Once your configuration works, add the above command to cron. Open an editor:
crontab -e

The first five columns in the file define scheduling. If you want to collect statistics every minute, use the following line. Otherwise, check the crontab manual page. Depending on your cron configuration, you might need to add the full path to the applications. In my case:

* * * * * /usr/sbin/syslog-ng-ctl query get '*' | sed -e 's/[\.|\#|\,|\:]/_/g' | /usr/local/bin/jo | nc 514

  1. Save your cron configuration. Within a minute, you should see a new log message in /var/log/test.json and/or Graphite.

Other possibilities

Using the Graphite destination is just one of several possibilities. You can also send the statistics data to Elasticsearch and query it using Kibana, or send it to the Riemann destination for monitoring.

Related Content