Collecting System Metrics in Prometheus with Node Exporter
Node Exporter is a powerful tool that collects system-level metrics from Linux and Unix servers and exports them in a format that Prometheus can understand. By default, Node Exporter collects a wide range of metrics, including CPU usage, memory usage, disk I/O statistics, network usage, and much more. In this article, we will explore how to collect system metrics with Node Exporter, configure Prometheus to scrape those metrics, and create a service in Systemd to manage the Node Exporter process.
Step 1: Download and Install Node Exporter
The first step is to download the Node Exporter binary for your operating system from the official Prometheus website. After downloading, extract the binary to a directory on your server. Then, create a new user to run the Node Exporter service:
sudo adduser node_exporter
Assign permissions to the Node Exporter binary and its parent directory to ensure that the node_exporter
user can access the files:
sudo chown node_exporter:node_exporter /path/to/node_exporter
sudo chmod +x /path/to/node_exporter
Step 2: Explore the Metrics
Node Exporter provides a wide range of metrics that can help you understand the health and performance of your system. You can view the available metrics by running the following command:
./node_exporter --web.listen-address=":9100" --collector.cpu.info --collector.meminfo --collector.diskstats --collector.netdev
This command starts the Node Exporter service and enables the collection of CPU information, memory information, disk I/O statistics, and network device statistics. Open a web browser and navigate to http://your_server_ip:9100/metrics
to view the metrics collected by Node Exporter. You should see a list of metrics in a text format.
Step 3: Create a Systemd Service File
Creating a Systemd service file is the best way to manage the Node Exporter process on your server. Create a new file named node_exporter.service
in the /etc/systemd/system/
directory and add the following lines to it:
[Unit]
Description=Node Exporter
[Service]
User=node_exporter
ExecStart=/path/to/node_exporter --web.listen-address=":9100" --collector.cpu.info --collector.meminfo --collector.diskstats --collector.netdev
[Install]
WantedBy=default.target
Replace /path/to/node_exporter
with the actual path to the Node Exporter binary on your server.
Step 4: Reload and Start the Service
Reload the Systemd configuration and start the Node Exporter service with the following commands:
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
Step 5: Verify the Service
Verify that the Node Exporter service is running correctly by checking its status:
sudo systemctl status node_exporter
You should see a status message indicating that the service is active and running:
● node_exporter.service - Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-01 15:03:24 UTC; 3s ago
Main PID: 12345 (node_exporter)
Tasks: 8 (limit: 4915)
Memory: 23.1M
CGroup: /system.slice/node_exporter.service
└─12345 /path/to/node_exporter --web.listen-address=":9100" --collector.cpu.info --collector.meminfo --collector.diskstats --collector.netdev
Step 6: Configure Prometheus
Now that you have Node Exporter collecting system metrics, you need to configure Prometheus to scrape those metrics. Open the Prometheus configuration file located at /etc/prometheus/prometheus.yml
and add the following lines:
scrape_configs:
- job_name: 'node'
scrape_interval: 5s
static_configs:
- targets: ['your_server_ip:9100']
These lines configure Prometheus to scrape metrics from Node Exporter running on your_server_ip
every 5 seconds.
Step 7: Restart Prometheus
Restart Prometheus to apply the changes to the prometheus.yml
configuration file.
sudo systemctl restart prometheus
Step 8: Verify the Metrics
Open the Prometheus web interface at http://your_server_ip:9090/graph
and enter a query to display the metrics collected by Node Exporter. For example, you can enter node_cpu_seconds_total
to display CPU usage metrics. You should see a graph displaying the CPU usage of your server over time.
conclusion
Node Exporter is a powerful tool for collecting system-level metrics from Linux and Unix servers. By configuring a service in Systemd and configuring Prometheus to scrape those metrics, you can gain valuable insights into the health and performance of your system. With these simple steps, you can collect system metrics with Node Exporter, manage the Node Exporter process with Systemd, and configure Prometheus to display and analyze those metrics.