Home > System Tutorial > LINUX > 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

Christopher Nolan
Release: 2025-03-07 11:12:13
Original
966 people have browsed it

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

Introduction: Proactive Server Monitoring with Prometheus on Ubuntu

In today's demanding digital landscape, ensuring optimal server uptime and performance is paramount. System administrators and DevOps teams rely heavily on robust monitoring solutions to prevent outages, boost efficiency, and proactively address potential issues. Prometheus, a leading open-source monitoring and alerting system, offers a powerful solution. This guide provides a comprehensive walkthrough of setting up Prometheus for monitoring your Ubuntu servers, covering installation, configuration, metric collection, and visualization.

Understanding Prometheus: A Deep Dive

Prometheus is a sophisticated monitoring system that periodically collects metrics from designated targets, evaluates defined rules, presents the results, and triggers alerts based on predefined conditions. Its popularity stems from its key features:

  • Multi-dimensional Data Model: Leverages a time-series data model for efficient high-dimensional data handling and querying.
  • Powerful Query Language: PromQL (Prometheus Query Language) offers flexible and advanced querying capabilities.
  • Standalone Architecture: Operates independently, eliminating reliance on distributed storage systems.
  • Efficient Data Storage: Optimizes storage of time-series data within a local database.
  • Pull-based Data Collection: Retrieves metrics by pulling data from target endpoints.

Key Benefits of Prometheus for Server Monitoring:

  • Scalability: Handles massive datasets, suitable for environments of any scale.
  • Grafana Integration: Seamlessly integrates with Grafana, a popular visualization and dashboarding platform.
  • Extensive Ecosystem: A rich ecosystem of exporters and integrations ensures broad compatibility and extensibility.

Setting Up Prometheus on Ubuntu: A Step-by-Step Guide

System Requirements:

  • Ubuntu 18.04 or later
  • Minimum 2 GB RAM
  • Minimum 2 CPU cores
  • 10 GB free disk space

Installation:

  1. Update System Packages:

    sudo apt-get update
    sudo apt-get upgrade
    Copy after login
    Copy after login
  2. Install Prometheus:

    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
    Copy after login
    Copy after login
  3. Configure Prometheus: (Modify /etc/prometheus/prometheus.yml as needed. The default configuration often suffices for basic setups.)

  4. Start Prometheus Service:

    sudo nano /etc/systemd/system/prometheus.service
    Copy after login
    Copy after login

    Add the following:

    sudo apt-get update
    sudo apt-get upgrade
    Copy after login
    Copy after login

    Then:

    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
    Copy after login
    Copy after login

    Access Prometheus at http://<your_server_ip>:9090</your_server_ip>.

Collecting System Metrics with Node Exporter

Node Exporter is a crucial component for gathering system-level metrics.

  1. Install Node Exporter:

    sudo nano /etc/systemd/system/prometheus.service
    Copy after login
    Copy after login
  2. Create Node Exporter Service: (Similar to the Prometheus service file, create a node_exporter.service file.)

  3. Start and Enable Node Exporter:

    [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
    Copy after login

    Access Node Exporter at http://<your_server_ip>:9100/metrics</your_server_ip>.

  4. Configure Prometheus to Scrape Node Exporter: Add Node Exporter as a target in your prometheus.yml file. Reload Prometheus afterwards.

Visualizing Metrics with Grafana: A Powerful Dashboarding Tool

Grafana provides an intuitive interface for visualizing your collected metrics.

  1. Install Grafana: (Follow Grafana's official installation instructions for Ubuntu.)

  2. Start and Enable Grafana:

  3. Connect Grafana to Prometheus: Configure Grafana to use Prometheus as a data source.

  4. Create Dashboards: Use PromQL queries to create informative dashboards displaying key metrics.

Advanced Monitoring and Alerting: Proactive Issue Management

Prometheus's Alertmanager enables automated alerts based on defined rules. (Installation and configuration steps are similar to Prometheus and Node Exporter.) Configure alert rules and notification channels (email, Slack, etc.) within Alertmanager's configuration file.

Best Practices and Tips for Optimal Performance and Security:

  • Keep all components updated.
  • Regularly review and refine alert rules.
  • Optimize Prometheus configuration parameters.
  • Monitor Prometheus's own health metrics.
  • Secure Prometheus and Grafana with SSL/TLS and proper authentication.

Conclusion: Empowering Server Management with Prometheus

By implementing Prometheus, you gain a powerful tool for proactive server monitoring and management, leading to improved uptime, performance, and overall infrastructure health. Its flexibility and scalability make it an invaluable asset for organizations of all sizes.

The above is the detailed content of How to Monitor an Ubuntu Server With Prometheus: Collecting and Visualizing System Metrics. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template