Prometheus: Apache Exporter – Install and Config – Ubuntu, CentOS

What is Apache Exporter? Apache Exporter is a Prometheus exporter for Apache metrics that exports Apache server status reports generated by mod_status with the URL of http://127.0.0.1/server-status/?auto.

In this article I’ll show how create Apache server status page, how to build Apache Exporter with go get and how to configure it using a systemd service manager on Ubuntu and CentOS.

Cool Tip: Install Node Exporter on Ubuntu and CentOS! Read More →

Enable Apache Server Status Page

To enable server-status reports, create the following configuration file:

# Ubuntu
$ sudo vi /etc/apache2/conf-available/server-status.conf

# CentOS
$ sudo vi /etc/httpd/conf.d/server-status.conf

With this contents:

ExtendedStatus on
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>

Restart Apache:

# Ubuntu
$ cd /etc/apache2/conf-enabled
$ sudo ln -s ../conf-available/server-status.conf server-status.conf && cd
$ sudo systemctl restart apache2

# CentOS
$ sudo systemctl restart httpd

Check the server-status page:

$ curl http://127.0.0.1/server-status

Install Go and Git

We need Go to compile apache_exporter from sources.

Cool Tip: GoLang Tutorial – Install Go on MacOS, Ubuntu, CentOS! Read More →

Download and install the latest version of Go for your platform from the official download page (the current version of Go is 1.11):

$ curl -O https://dl.google.com/go/go1.11.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xvzf go1.11.linux-amd64.tar.gz
$ echo "export GOPATH=$HOME/go" >> ~/.profile
$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
$ source ~/.profile
$ go version
go version go1.11 linux/amd64

We also need git package to download sources of apache_exporter from GitHub using go get command, otherwise we may get the error as follows:

go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/neezgee/apache_exporter: exec: “git”: executable file not found in $PATH

Install Git:

# Ubuntu
$ sudo apt install git

# CentOS
$ sudo yum install git

Apache Exporter: Install and Config

Create unprivileged apache_exporter user:

$ sudo useradd apache_exporter -s /sbin/nologin

Download and compile Apache Exporter:

$ go get -v github.com/Lusitaniae/apache_exporter
$ sudo cp ~/go/bin/apache_exporter /usr/sbin/

Create a systemd service file /etc/systemd/system/apache_exporter.service:

[Unit]
Description=Apache Exporter

[Service]
User=apache_exporter
EnvironmentFile=/etc/sysconfig/apache_exporter
ExecStart=/usr/sbin/apache_exporter $OPTIONS

[Install]
WantedBy=multi-user.target

Create a sysconfig file:

$ sudo mkdir -p /etc/sysconfig
$ sudo touch /etc/sysconfig/apache_exporter

Use this file to define options to provide, for example:

OPTIONS="-scrape_uri string 'http://127.0.0.1/server-status/?auto'"

To check the all available options, run:

$ /usr/sbin/apache_exporter --help

Reload systemd configuration and configure apache_exporter to auto-start on system boot:

$ sudo systemctl daemon-reload
$ sudo systemctl enable apache_exporter

Start the apache_exporter service:

$ sudo systemctl start apache_exporter

Once the Apache Exporter is installed, verify that metrics are being exported:

$ curl http://localhost:9117/metrics

Add Apache Exporter Target to Prometheus

Cool Tip: Install Prometheus using Docker on Ubuntu and CentOS! Read More →

Now all that’s left is to tell Prometheus server about the new target.

This needs to be done in the Prometheus config, as Apache Exporter just exposes metrics and Prometheus pulls them from the targets it knows about.

Open your Prometheus config file prometheus.yml, and add your machine to the scrape_configs section as follows:

scrape_configs:
  - job_name: 'apache'
    static_configs:
      - targets: ['$NODE_IP:9117']

Reload the Prometheus config, and you should now be able to access Apache metrics through Prometheus!