Published on 2019-08-29· Updated on 2024-01-25
The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.
As a developer, you would often find yourself concerned about the operational status of your services. If you're also responsible for server infrastructure, you might worry about factors like RAM and disk utilization, as well as the risk of system overload leading to downtime. Monitoring these basic metrics is just the tip of the iceberg, with numerous other parameters requiring constant attention in daily operations.
Manually monitoring all these variables around the clock can be quite challenging. This tutorial will guide you through setting up Prometheus as a versatile monitoring system to simplify the process. You will also learn how to use its exporter to specify and retrieve the metrics for tracking.
Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability. It is used for collecting and storing time-series data, particularly metrics about various aspects of your systems and applications. Prometheus provides a flexible query language, powerful alerting capabilities, and various integrations to help monitor and manage the health and performance of your infrastructure and services. Get more details here.
Prometheus scrapes the data with the timestamp, which can be stored on a server and accessed using the PromQL. Prometheus has a superb support API, making it easy to integrate with any resource in the data center. Prometheus includes third-party libraries that make it more powerful.
Prometheus can be installed and tested on a server with a minimum configuration of:
There are multiple ways to install Prometheus. You can use Prometheus docker image or any of the configuration management systems like Ansible, Chef, puppet, and SaltStack. For more information, visit the official installation guide here.
Prometheus Docker Image
Here is the link to download the docker image for Prometheus.
It also contains pre-compiled binaries and is one of the easiest methods to set up.
Visit the Prometheus download page. You will see a list of pre-compiled binaries for Linux, and Windows that you can download according to your OS. The installation for Linux OS is explained below.
OR
You can execute the below command in your Linux terminal:
wget https://github.com/prometheus/prometheus/releases/download/v2.11.1/prometheus-2.11.1.linux-amd64.tar.gz
You will see a file with name prometheus-2.11.1.linux-amd64.tar.gz will get downloaded.
tar -xvzf prometheus-2.11.1.linux-amd64.tar.gz
$ mv prometheus-2.11.1.linux-amd64 prometheus $ cd prometheus/$ ll
Folder contains below file:
./prometheus
Visit localhost:9090 on your web browser:
Your Prometheus should be up and running now.
In the prometheus/ folder, you will notice a folder named ‘data.’ Prometheus stores metrics in this ‘/data’ folder.
You can get all metric lists by hitting the URL to localhost:9090/metrics.
Prometheus stores its data on disk in a custom time-series format, and it relies on LevelDB as its underlying storage engine. LevelDB is a high-performance, lightweight key-value storage library developed by Google. This combination of Prometheus's custom time-series format and LevelDB as the storage engine enables efficient and scalable storage and retrieval of time-series data, making it suitable for monitoring and alerting purposes. You can check more details on storage.
Here is a sample production command:
~/prometheus/prometheus --storage.tsdb.path=/var/lib/prometheus/data/ --web.external-url=http://myurl.com:9090
--storage.tsdb.path: Specify the path where you want to save Prometheus data.
--web.external-url: You can use this option if you want to bind your address with your URL.
You may get the below error if your folder doesn’t have appropriate permission:
level=error ts=2019-08-06T14:25:19.791Z caller=main.go:731 err="opening storage failed: lock DB directory: open /var/lib/lock: permission denied"
You can try appending “sudo” to your command OR you can give appropriate permission to your folder.
1) Create a file:
/etc/systemd/system/prometheus.service
2) Just paste below code:
[Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network-online.target [Service] User=root Restart=on-failure #Change this line if you download the #Prometheus on different path user ExecStart=~/prometheus/prometheus --storage.tsdb.path=/var/lib/prometheus/data/ --web.external-url=http://myurl.com:9090 [Install] WantedBy=multi-user.target
Save and exit.
3) Reload the Systemctl Daemon:
sudo systemctl daemon-reload
4) Start the Prometheus service:
sudo systemctl start prometheus
Till now you learned how to do basic Prometheus setup. Now, you will learn, how to set up Prometheus exporter.
EExporters can be any scripts or services that will fetch specific metrics from your system and provide data in Prometheus format. There are two primary methods for fetching and storing metrics in Prometheus:
Both methods provide flexibility in collecting and storing metrics, depending on your specific monitoring needs and the nature of the data you want to track.
Now let us set up a node exporter to fetch your server metrics such as RAM/DISK/CPU utilization, network, io, etc.
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz $ tar -xvzf node_exporter-0.18.1.linux-amd64.tar.gz $ mv node_exporter-0.18.1.linux-amd64 node_exporter $ cd node_exporter $ ./node_exporter
You should be able to see the below output once the node exporter starts:
Just visit to localhost:9100/metrics
Create a file in below path:
/etc/systemd/system/node-exporter.service
Just paste below code:
[Unit] Description=Node exporter After=network-online.target [Service] User=root Restart=on-failure #Change this line if you download the #Prometheus on different path user ExecStart=~/node_exporter/node_exporter [Install] WantedBy=multi-user.target
Reload the systemctl daemon:
sudo systemctl daemon-reload
Start the Prometheus service:
sudo systemctl start node-exporter
Open file ~/prometheus/prometheus.yml add below configuration:
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'] # Add below block for node_exporter - job_name: node_exporter scrape_interval: 1m scrape_timeout: 1m metrics_path: "/metrics" static_configs: - targets: ['localhost:9100']
Save and exit.
~/prometheus/prometheus --storage.tsdb.path=/var/lib/prometheus/data/ --config.file=~/prometheus/prometheus.yml --web.external-url=http://myurl.com:9090
Also, don’t forget to make the same changes in your service file:/etc/systemd/system/prometheus.service
sudo systemctl restart prometheus
Now visit the URL localhost:9090. In the expression field, you can search for “node_filesystem_size_bytes” by clicking on the “Execute” button. You will get the below stats:
After clicking on the graph:
You can explore each metric, such as memory, CPU, etc., similarly.
There are several exporters available on the internet, like Nginx exporter, MongoDB exporter, MySQL server exporter, etc. which you can download and use. You can get more info about such exporters here.
You have now successfully installed Prometheus and node exporters.
This official architecture diagram shows a successful setup of Prometheus, Pushgateway/Exporters, and Prometheus UI.
In the next part, we will explain the setting up of an alert manager and alerts over the metrics.
Note: Prometheus is a storage system for metrics. Therefore, we do not recommend storing any kind of logs in it.
Excited about the latest in Bulk Email Marketing! Check out this insightful blog on Gmail and Yahoo updates in the email marketing landscape.
Explore the Blog - Here
Stay ahead of the game with valuable insights on optimizing your email campaigns! 📬
Netcorecloud's toolkit is the solution to all your email problems.
Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.
Ashish Tiwari
Open source guy | Linux lover | Dev evangelist @ Pepipost