How to Monitor Memcached with Prometheus and Grafana


In this article, we will learn how to effectively monitor Memcached, a widely used in-memory key-value store for caching data in distributed systems. Monitoring its the performance and health is essential for ensuring the optimal operation of applications that depend on it. To achieve this, we’ll leverage the combined power of Prometheus, a robust monitoring and alerting toolkit, and Grafana, a versatile visualization platform. Together, these tools offer a comprehensive solution for monitoring Memcached.

Prerequisites

  • AWS Account with Ubuntu 22.04 LTS EC2 Instance.
  • Basic knowledge of AWS services, Prometheus and Grafana.

Step #1:Install Memcached on Ubuntu

First update the package repository.

sudo apt update 
How to Monitor Memcached with Prometheus and Grafana 1

Install the memcached using following command.

sudo apt install memcached
How to Monitor Memcached with Prometheus and Grafana 2

Step #2:Install Prometheus on Ubuntu

Download the latest version of Prometheus from the GitHub repository using the following command.

wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-amd64.tar.gz
How to Monitor Memcached with Prometheus and Grafana 3

After downloading the latest version of Prometheus binary, extract the downloaded archives.

tar xvfz prometheus-2.30.0.linux-amd64.tar.gz

The command extracts the contents of the file prometheus-2.30.0.linux-amd64.tar.gz

How to Monitor Memcached with Prometheus and Grafana 4

now navigate to the extracted directory using following command.

cd prometheus-2.30.0.linux-amd64
How to Monitor Memcached with Prometheus and Grafana 5

navigate to the /etc/systemd/system, this is where typically systemd unit files are located, which are used for managing services on Linux systems.

cd /etc/systemd/system
How to Monitor Memcached with Prometheus and Grafana 6

now lets create a service for Prometheus named prometheus.service.

sudo vi prometheus.service
How to Monitor Memcached with Prometheus and Grafana 7

add the following content into it.

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/home/ubuntu/prometheus-2.30.0.linux-amd64/prometheus --config.file=/home/ubuntu/prometheus-2.30.0.linux-amd64/prometheus.yml
Restart=always

[Install]
WantedBy=default.target
How to Monitor Redis with Prometheus and Grafana 10

save the file and close it.

Now our prometheus.service is created. After this reload the daemon service.

sudo systemctl daemon-reload
How to Monitor Memcached with Prometheus and Grafana 8

enable the Prometheus service.

sudo systemctl enable prometheus.service
How to Monitor Memcached with Prometheus and Grafana 9

And start the Prometheus service.

sudo systemctl start prometheus.service
How to Monitor Memcached with Prometheus and Grafana 10

now Verify the status of the Prometheus service using following command:

sudo systemctl status prometheus.service
How to Monitor Memcached with Prometheus and Grafana 11

If your service is running properly then you can run prometheus by running

Public IP:9090 which is default port for prometheus in url.

How to Monitor Redis with Prometheus and Grafana 13

Step #3:Install Grafana on Ubuntu

First import the GPG key used by the Grafana package.

wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
How to Monitor Memcached with Prometheus and Grafana 12

then add the Grafana repository to the APT sources.

sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
How to Monitor Memcached with Prometheus and Grafana 13

After adding the repository update the package lists.

sudo apt update
How to Monitor Memcached with Prometheus and Grafana 14

Once updating of package lists is complete, install the grafana.

sudo apt install grafana
How to Monitor Memcached with Prometheus and Grafana 15

Start the Grafana service.

sudo systemctl start grafana-server
How to Monitor Memcached with Prometheus and Grafana 16

Enable the Grafana service.

sudo systemctl enable grafana-server
How to Monitor Memcached with Prometheus and Grafana 17

and if everything works fine and your service is running properly then run grafana by running your Public IP:3000 which is default port of grafana in searchbar.

you will see the login page of grafana (UI) user interface

Grafana has admin as default username and password.

How to Monitor Redis with Prometheus and Grafana 19

it will ask for changing the password you can change it or skip the procedure.

How to Monitor Redis with Prometheus and Grafana 20

then you will see the welcome page of grafana.

How to Monitor Redis with Prometheus and Grafana 21

Step #4:Create User Group and User for Prometheus

Now after the installation of Prometheus and grafana is done lets create User Group and User for Prometheus.

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus
How to Monitor Memcached with Prometheus and Grafana 18

Step #5:Download and Install Memcached Exporter

Download the Memcached Exporter binary.

curl -s https://api.github.com/repos/prometheus/memcached_exporter/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
How to Monitor Memcached with Prometheus and Grafana 19

Extract the downloaded archive file.

tar xvf memcached_exporter-*.linux-amd64.tar.gz
How to Monitor Memcached with Prometheus and Grafana 20

Move the binary to the appropriate directory.

sudo mv memcached_exporter-*.linux-amd64/memcached_exporter /usr/local/bin/
How to Monitor Memcached with Prometheus and Grafana 21

Confirm the installation of the exporter by running the following command.

memcached_exporter --version
How to Monitor Memcached with Prometheus and Grafana 22

Step #6: Configuring Memcached Exporter Service

Create a service file.

sudo vim /etc/systemd/system/memcached_exporter.service
How to Monitor Memcached with Prometheus and Grafana 23

Add the following content to the service file.

[Unit]
Description=Prometheus Memcached Exporter
Documentation=https://github.com/prometheus/memcached_exporter
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/memcached_exporter

SyslogIdentifier=memcached_exporter
Restart=always

[Install]
WantedBy=multi-user.target
How to Monitor Memcached with Prometheus and Grafana 24

Save the file and verify that the configuration is correct.

sudo systemctl daemon-reload
How to Monitor Memcached with Prometheus and Grafana 25

Enable and start the service.

sudo systemctl enable memcached_exporter
How to Monitor Memcached with Prometheus and Grafana 26
sudo systemctl start memcached_exporter
How to Monitor Memcached with Prometheus and Grafana 27

Confirm the service status.

systemctl status memcached_exporter
How to Monitor Memcached with Prometheus and Grafana 28

You can run the Public IP:9150 To view its web page.

How to Monitor Memcached with Prometheus and Grafana 29

Step #7:Configuring Memcached Exporter Endpoint in Prometheus Configuration

Navigate to the directory containing the Prometheus configuration file.

cd prometheus-2.30.0.linux-amd64
How to Monitor Memcached with Prometheus and Grafana 30

Edit the prometheus.yml file.

nano prometheus.yml
How to Monitor Memcached with Prometheus and Grafana 31

add the following block into it.

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
#Memcached Servers
  - job_name: "memcached"
    static_configs:
      - targets: ["localhost:9150"]
How to Monitor Memcached with Prometheus and Grafana 32

save the file and close it.

restart the Prometheus service.

sudo systemctl restart prometheus.service
How to Monitor Memcached with Prometheus and Grafana 33

Now go to Prometheus dashboard and click on status, select target, you can see our exporter are up and running.

How to Monitor Memcached with Prometheus and Grafana 34

Step #8:Setting up Grafana Dashboards for memcached Metrics

Now go back to the Grafana welcome page. Go to the Home in the left top corner.

Then go to the Connections and select the Data sources option.

How to Monitor Redis with Prometheus and Grafana 38

Search for Prometheus in the search bar and select it.

How to Monitor Redis with Prometheus and Grafana 39

In connection, in Prometheus server URL, give the server url on which our prometheus is running.

How to Monitor Memcached with Prometheus and Grafana 35

after this click on save and test button. You will see the message for prometheus being successfully queried.

How to Monitor Memcached with Prometheus and Grafana 36

now go to the ‘+‘ icon from up-right corner and select the New dashboard option

How to Monitor Redis with Prometheus and Grafana 42

Here you can start your own new dashboard by adding a visualization.

So click on + Add visualization option button.

Here you can also import dashboards.

How to Monitor Redis with Prometheus and Grafana 43

Select the Prometheus as Data source.

How to Monitor Redis with Prometheus and Grafana 44

You will see the edit panel page.

Now in the Query section add the A query

  • Metric: memcached_process_system_cpu_seconds_total
  • job: memcached
How to Monitor Memcached with Prometheus and Grafana 37

then add the B query

  • Metric: memcached_process_user_cpu_seconds_total
  • job: memcached
How to Monitor Memcached with Prometheus and Grafana 38

Click on run queries You will see the following output.

It’s in the Time series visualization, but there are many visualization option like Guage, stats, Bar graph etc.

How to Monitor Memcached with Prometheus and Grafana 39

Click ctrl+s to save the file. Give an appropriate Title for your dashboard the save it.

Conclusion:

In conclusion, today we’ve learned how to set up monitoring for Memcached using Prometheus and Grafana. Monitoring Memcached metrics allows you to gain insights into its performance, troubleshoot issues, and ensure the efficient operation of your caching infrastructure. With Prometheus collecting metrics and Grafana providing visualization capabilities, you have a powerful monitoring solution at your disposal to keep your Memcached instances running smoothly.

Related Articles:

Integrate Remote MySQL Database to Grafana

Reference:

Grafana Installation official page

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap