Why is VDS Monitoring Important?
If you are using a VDS (Virtual Dedicated Server), continuously monitoring your server's performance and health is critical. You need the right tools to respond quickly to situations like sudden traffic spikes, resource exhaustion, or attacks. The Prometheus and Grafana duo is one of the most powerful combinations in the open-source world to meet this need. In this guide, I will walk you through step by step how to collect metrics with Prometheus and visualize them with Grafana on a VDS.
What is Prometheus?
Prometheus is a time-series-based monitoring and alerting system developed by SoundCloud. With its pull-based architecture, it periodically scrapes metrics from target systems. It stands out with its scalability and flexible query language, PromQL. For a VDS, it typically collects metrics such as CPU, RAM, disk usage, and network traffic.
Installing Prometheus
First, connect to your VDS via SSH. You can download and install Prometheus from the official website. Example commands for Ubuntu/Debian:
- wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
- tar xvf prometheus-2.53.0.linux-amd64.tar.gz
- sudo mv prometheus-2.53.0.linux-amd64 /opt/prometheus
Then create a systemd service. Edit the prometheus.yml file to add the targets you want to monitor. For example, to collect hardware metrics with node_exporter:
scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']Note: Remember that you also need to install Node Exporter separately.
Visualization with Grafana
Grafana is an open-source analytics platform that presents data in stunning dashboards. You can add Prometheus as a data source and create dashboards in minutes.
Installing Grafana
Add Grafana's official repository and install it:
- sudo apt-get install -y software-properties-common
- sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
- sudo apt-get update && sudo apt-get install grafana
Start the service and access the web interface at http://server_ip:3000. The default username/password is admin/admin.
Creating a Dashboard
To create a dashboard in Grafana, use the "Create" > "Dashboard" option. Then select the Prometheus data source and enter a metric. For example, to see CPU usage:
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
This query averages the idle CPU time over the last 5 minutes and gives the usage as a percentage. You can customize these queries to add RAM, disk I/O, and network metrics.
Setting Up Alerts
One of Prometheus's strongest features is its integrated alerting system with Alertmanager. For example, you can write a rule to send an email notification when CPU usage exceeds 90%.
Add the following rule to the Prometheus configuration file:
groups: - name: cpu_alerts rules: - alert: HighCPUUsage expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90 for: 5m labels: severity: critical annotations: summary: "CPU usage above 90%"With Alertmanager, you can route these alerts to channels like email, Slack, or Telegram.
A Real-Life Example
After setting up Prometheus-Grafana on one of our client's VDS, an automatic alert was triggered when disk space reached 85%, and we resolved the issue by cleaning up unnecessary log files. Additionally, we spotted a memory leak that would normally go unnoticed on the Grafana dashboard and updated the application. This increased uptime.
“You cannot improve without monitoring. Prometheus and Grafana are the eyes and ears of VDS administrators.”
In conclusion, with these two tools, you can take control of your VDS's status step by step and solve problems proactively before they grow. Remember, monitoring is just the beginning; regularly update your dashboards and add new metrics according to your needs.