How to Monitor an Ubuntu Server With Prometheus: Collecting and Visualizing System Metrics

How to Monitor an Ubuntu Server With Prometheus: Collecting and Visualizing System Metrics

Introduction

In today's fast-paced digital world, server uptime and performance are critical. Monitoring servers to ensure they are functioning optimally is a top priority for system administrators and DevOps teams. Effective server monitoring can prevent downtime, improve performance, and help troubleshoot issues before they escalate. One of the most powerful tools for this purpose is Prometheus, an open source monitoring and alerting toolkit originally developed at SoundCloud.

This article aims to walk you through the process of setting up Prometheus for monitoring Ubuntu servers. We will cover everything from installation and configuration to collecting and visualizing system metrics. By the end of this guide, you will have a fully functional monitoring setup that can provide valuable insights into your server's performance.

Understanding Prometheus

What is Prometheus?

Prometheus is a robust monitoring system that collects metrics from configured targets at specified intervals, evaluates rule expressions, displays results, and triggers alerts if certain conditions are observed. It has become the de facto standard for monitoring in cloud-native environments. Key features of Prometheus include:

  • Multi-dimensional data model: Prometheus uses a time series data model, allowing for high-dimensional data collection and querying.
  • Flexible query language: PromQL, the Prometheus Query Language, is highly flexible and powerful.
  • Autonomous server: Prometheus can run independently without relying on distributed storage.
  • Efficient storage: Stores time series data efficiently in a local time series database.
  • Pull-based model: Prometheus collects metrics by pulling them from target endpoints.
Benefits of Using Prometheus for Server Monitoring
  • Scalability: Prometheus can handle a large number of metrics, making it suitable for environments of any size.
  • Integration with Grafana: Prometheus integrates seamlessly with Grafana, a leading open source platform for monitoring and observability, for advanced visualization.
  • Rich ecosystem: A wide range of exporters and integrations are available, making it easy to extend Prometheus to meet specific needs.

Setting Up Prometheus on Ubuntu

System Requirements

Before installing Prometheus, ensure your Ubuntu server meets the following minimum requirements:

  • Ubuntu 18.04 or later.
  • At least 2 GB of RAM.
  • At least 2 CPU cores.
  • 10 GB of free disk space.
Installation Steps

Updating Ubuntu Packages

First, update the package lists and install any available upgrades:

sudo apt-get update sudo apt-get upgrade

Installing Prometheus

Download and install Prometheus from the official repository:

sudo useradd --no-create-home --shell /bin/false prometheus sudo mkdir /etc/prometheus sudo mkdir /var/lib/prometheus cd /tmp wget https://github.com/prometheus/prometheus/releases/download/v2.31.1/prometheus-2.31.1.linux-amd64.tar.gz tar -xvzf prometheus-2.31.1.linux-amd64.tar.gz cd prometheus-2.31.1.linux-amd64 sudo cp prometheus /usr/local/bin/ sudo cp promtool /usr/local/bin/ sudo cp -r consoles /etc/prometheus sudo cp -r console_libraries /etc/prometheus sudo cp prometheus.yml /etc/prometheus

Configuring Prometheus

Edit the Prometheus configuration file (/etc/prometheus/prometheus.yml) to suit your needs. For a basic setup, the default configuration is often sufficient:

global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']

Starting Prometheus Service

Create a systemd service file for Prometheus:

sudo nano /etc/systemd/system/prometheus.service

Add the following content:

[Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target

Reload systemd, enable, and start Prometheus:

sudo systemctl daemon-reload sudo systemctl enable prometheus sudo systemctl start prometheus

Verify that Prometheus is running by visiting http://<your_server_ip>:9090 in a web browser.

Collecting System Metrics

Introduction to Exporters

Exporters are lightweight software components that expose system metrics to Prometheus. They are crucial for collecting data from various sources.

Installing and Configuring Node Exporter

Node Exporter is a popular exporter that collects hardware and OS metrics. Install Node Exporter using the following steps:

Download and Install Node Exporter

cd /tmp wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz tar -xvzf node_exporter-1.2.2.linux-amd64.tar.gz cd node_exporter-1.2.2.linux-amd64 sudo cp node_exporter /usr/local/bin/

Create a Systemd Service for Node Exporter

sudo nano /etc/systemd/system/node_exporter.service

Add the following content:

[Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target

Start and enable Node Exporter:

sudo systemctl daemon-reload sudo systemctl enable node_exporter sudo systemctl start node_exporter

Verify that Node Exporter is running by visiting http://<your_server_ip>:9100/metrics in a web browser.

Configuring Prometheus to Collect Metrics from Node Exporter

Edit the Prometheus configuration file to include Node Exporter:

scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100']

Reload Prometheus configuration:

sudo systemctl reload prometheus

Check the Prometheus web interface to ensure that metrics from Node Exporter are being collected.

Visualizing Metrics with Grafana

Introduction to Grafana

Grafana is an open source platform for monitoring and observability. It provides rich visualizations and a user-friendly interface to interact with metrics collected by Prometheus.

Installing Grafana on Ubuntu

To install Grafana, follow these steps:

Add the Grafana APT Repository

sudo apt-get install -y software-properties-common wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"

Install Grafana

sudo apt-get update sudo apt-get install grafana

Start and Enable Grafana

sudo systemctl daemon-reload sudo systemctl enable grafana-server sudo systemctl start grafana-server

Verify that Grafana is running by visiting http://<your_server_ip>:3000 in a web browser. The default login credentials are admin/admin.

Connecting Grafana to Prometheus

To connect Grafana to Prometheus:

  1. Add Prometheus as a Data Source:
    • Log in to Grafana.
    • Go to Configuration > Data Sources.
    • Click Add data source and select Prometheus.
    • Enter the URL of your Prometheus server (http://localhost:9090).
    • Click Save & Test to verify the connection.
Creating Dashboards and Visualizations
  1. Create a New Dashboard:

    • Click the + icon on the left sidebar and select Dashboard.
    • Click Add new panel to create a new visualization.
  2. Add Panels to Visualize Metrics:

    • Select the type of visualization (e.g., graph, gauge, table).
    • Use PromQL to query metrics from Prometheus. For example, to visualize CPU usage, you might use:
      rate(node_cpu_seconds_total{mode="idle"}[5m])
      
    • Customize the panel with titles, labels, and colors.
  3. Save and Share Dashboards:

    • Save your dashboard by clicking the Save icon at the top.
    • Share dashboards by clicking the Share icon and copying the link.

Advanced Monitoring and Alerting

Setting Up Alerts in Prometheus

Prometheus's Alertmanager handles alerts, sending notifications based on pre-defined rules.

Configure Alertmanager

Install Alertmanager:

cd /tmp wget https://github.com/prometheus/alertmanager/releases/download/v0.22.2/alertmanager-0.22.2.linux-amd64.tar.gz tar -xvzf alertmanager-0.22.2.linux-amd64.tar.gz cd alertmanager-0.22.2.linux-amd64 sudo cp alertmanager /usr/local/bin/

Create a systemd service for Alertmanager:

sudo nano /etc/systemd/system/alertmanager.service

Add the following content:

[Unit] Description=Prometheus Alertmanager Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/alertmanager --config.file=/etc/prometheus/alertmanager.yml [Install] WantedBy=multi-user.target

Start and enable Alertmanager:

sudo systemctl daemon-reload sudo systemctl enable alertmanager sudo systemctl start alertmanager

Configuring Alerts in Prometheus

Edit prometheus.yml to include Alertmanager configuration:

alerting: alertmanagers: - static_configs: - targets: ['localhost:9093'] rule_files: - "alert.rules"

Create alert.rules with sample alert rules:

groups: - name: example rules: - alert: HighCPUUsage expr: rate(node_cpu_seconds_total{mode="idle"}[5m]) < 20 for: 2m labels: severity: critical annotations: summary: "High CPU usage detected" description: "CPU usage has exceeded 80% for more than 2 minutes."

Reload Prometheus configuration:

sudo systemctl reload prometheus

Creating and Managing Alerts

Prometheus can send notifications via various channels such as email, Slack, or PagerDuty. Configure notification channels in alertmanager.yml:

global: smtp_smarthost: 'smtp.example.com:587' smtp_from: 'alertmanager@example.com' smtp_auth_username: 'user@example.com' smtp_auth_password: 'password' route: receiver: 'email-alert' receivers: - name: 'email-alert' email_configs: - to: 'admin@example.com'

Test your alerts to ensure they are triggered and notifications are sent correctly.

Best Practices and Tips

  • Keep Prometheus, Node Exporter, and Grafana updated to benefit from the latest features and security patches.
  • Regularly review and update alert rules to match your current monitoring needs.
  • Fine-tune Prometheus configuration parameters such as scrape_interval and storage.tsdb.retention to balance performance and resource usage.
  • Monitor Prometheus's own metrics to ensure it is running efficiently.
  • Secure Prometheus and Grafana interfaces using SSL/TLS.
  • Implement authentication and authorization to restrict access to your monitoring setup.
  • Regularly audit and review security settings to protect against unauthorized access.

Conclusion

Implementing Prometheus for server monitoring can significantly enhance your ability to manage and optimize your server infrastructure. Its flexibility, scalability, and integration capabilities make it a powerful tool for any organization.

George Whittaker is the editor of Linux Journal, and also a regular contributor. George has been writing about technology for two decades, and has been a Linux user for over 15 years. In his free time he enjoys programming, reading, and gaming.

Load Disqus comments