Skip to content

إعداد Prometheus وNode Exporter وGrafana على دبيان أو أوبنتو

دليل خطوة بخطوة لبناء منظومة مراقبة على خادم دبيان/أوبنتو. الإصدارات المستخدمة أدناه هي أحدث الإصدارات المستقرة وقت كتابة هذا الدليل (أغسطس 2026): Prometheus 3.13.1، وNode Exporter 1.12.1، وGrafana 13.1.1. راجع صفحات الإصدارات قبل التنفيذ تحسبًا لصدور نسخ أحدث.

البنية

+----------------+   scrapes :9100   +----------------+
|  Node Exporter | <---------------- |   Prometheus   |
|   :9100        |                   |   :9090        |
+----------------+                   +--------+-------+
                                              |
                                              | PromQL queries :9090
                                              v
                                     +----------------+
                                     |    Grafana     |
                                     |   :3000        |
                                     +----------------+
                                              |
                                              v
                                        Browser / user

المتطلبات المسبقة

  • خادم دبيان أو أوبنتو مع صلاحية sudo
  • تثبيت curl وtar
  • فتح المنافذ 9090 (Prometheus) و9100 (Node Exporter) و3000 (Grafana) في جدارك الناري إن كنت تصل إليها عن بُعد

الجزء الأول: تثبيت Prometheus

الخطوة 1: إنشاء مستخدم النظام prometheus

bash
sudo useradd --no-create-home --shell /bin/false prometheus

الخطوة 2: إنشاء المجلدات

bash
sudo mkdir -p /etc/prometheus /var/lib/prometheus

الخطوة 3: تنزيل Prometheus

bash
cd /tmp
curl -LO https://github.com/prometheus/prometheus/releases/download/v3.13.1/prometheus-3.13.1.linux-amd64.tar.gz
tar -xvf prometheus-3.13.1.linux-amd64.tar.gz

الخطوة 4: تثبيت الملفات التنفيذية

bash
sudo cp prometheus-3.13.1.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-3.13.1.linux-amd64/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

الخطوة 5: تثبيت قوالب الكونسول وضبط الملكية

bash
sudo cp -r prometheus-3.13.1.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-3.13.1.linux-amd64/console_libraries /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

الخطوة 6: إنشاء ملف الإعدادات

bash
sudo tee /etc/prometheus/prometheus.yml > /dev/null <<EOF
global:
  scrape_interval: 10s

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

تُضاف مهمة الجمع الخاصة بـ Node Exporter في الجزء الثالث، بعد تثبيته.

الخطوة 7: إنشاء خدمة systemd

bash
sudo tee /etc/systemd/system/prometheus.service > /dev/null <<EOF
[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 \\
  --storage.tsdb.retention.time=365d
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

ExecReload هو ما يجعل systemctl reload prometheus يعمل في الجزء الثالث. بدونه يرفض systemd إعادة التحميل برسالة "Job type reload is not applicable"، ولا يبقى أمامك سوى إعادة تشغيل كاملة. لاحظ الشرطة المائلة العكسية في \$MAINPID: هذا الـ heredoc بلا علامات اقتباس، وإلا لوسّعت الصدفة المتغير إلى لا شيء أثناء كتابة الملف.

الخطوة 8: تشغيل Prometheus وتفعيله

bash
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus

الخطوة 9: التحقق

bash
sudo systemctl status prometheus
curl -s http://localhost:9090/-/healthy

افتح http://<server-ip>:9090 في المتصفح للتأكد من تحميل الواجهة.

تنظيف ملفات تنزيل Prometheus

bash
rm -rf /tmp/prometheus-3.13.1.linux-amd64*

الجزء الثاني: تثبيت Node Exporter

الخطوة 1: إنشاء مستخدم النظام node_exporter

bash
sudo useradd --no-create-home --shell /bin/false node_exporter

الخطوة 2: تنزيل Node Exporter

bash
cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.12.1/node_exporter-1.12.1.linux-amd64.tar.gz
tar -xvf node_exporter-1.12.1.linux-amd64.tar.gz

الخطوة 3: تثبيت الملف التنفيذي

bash
sudo cp node_exporter-1.12.1.linux-amd64/node_exporter /usr/local/bin/
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

الخطوة 4: إنشاء خدمة systemd

bash
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOF

الخطوة 5: تشغيل Node Exporter وتفعيله

bash
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter

الخطوة 6: التحقق

bash
curl -s http://localhost:9100/metrics | head

تنظيف ملفات تنزيل Node Exporter

bash
rm -rf /tmp/node_exporter-1.12.1.linux-amd64*

الجزء الثالث: توجيه Prometheus إلى Node Exporter

الخطوة 1: إضافة مهمة الجمع

bash
sudo vim /etc/prometheus/prometheus.yml

أضف تحت scrape_configs:

yaml
  - job_name: "node_exporter"
    static_configs:
      - targets: ['localhost:9100']

الخطوة 2: التحقق من الإعدادات

bash
promtool check config /etc/prometheus/prometheus.yml

الخطوة 3: إعادة تحميل Prometheus

bash
sudo systemctl reload prometheus

الخطوة 4: التأكد من أن الهدف يعمل

افتح http://<server-ip>:9090/targets، ويفترض أن تظهر المهمتان prometheus وnode_exporter بحالة State = UP.

الجزء الرابع: تثبيت Grafana

الطريقة الرسمية: مستودع APT، بحيث يبقي apt-get update نسخة Grafana محدثة. المصدر: https://grafana.com/docs/grafana/latest/setup-grafana/installation/debian/

الخطوة 1: تثبيت المتطلبات

bash
sudo apt-get install -y apt-transport-https wget gnupg

الخطوة 2: استيراد مفتاح GPG

bash
sudo mkdir -p /etc/apt/keyrings
sudo wget -O /etc/apt/keyrings/grafana.asc https://apt.grafana.com/gpg-full.key
sudo chmod 644 /etc/apt/keyrings/grafana.asc

الخطوة 3: إضافة المستودع المستقر

bash
echo "deb [signed-by=/etc/apt/keyrings/grafana.asc] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

الخطوة 4: تحديث قائمة الحزم

bash
sudo apt-get update

الخطوة 5: تثبيت Grafana OSS

bash
sudo apt-get install -y grafana

الخطوة 6: تشغيل Grafana وتفعيله

bash
sudo systemctl daemon-reload
sudo systemctl enable --now grafana-server

الخطوة 7: التحقق

bash
sudo systemctl status grafana-server

افتح http://<server-ip>:3000. بيانات الدخول الافتراضية هي admin / admin، وسيُطلب منك تغييرها عند أول تسجيل دخول.

الجزء الخامس: إعدادات Grafana الاختيارية (نطاق مخصص / HTTPS)

لا تحتاجه إلا إذا كنت ستضع Grafana خلف اسم نطاق مع TLS.

الخطوة 1: تعديل grafana.ini

bash
sudo vim /etc/grafana/grafana.ini
ini
[server]
http_addr =
http_port = 3000
domain = mysite.com
root_url = https://subdomain.mysite.com:3000
cert_key = /etc/grafana/grafana.key
cert_file = /etc/grafana/grafana.crt
enforce_domain = False
protocol = https

الخطوة 2: إعادة تشغيل Grafana

bash
sudo systemctl restart grafana-server

الخطوة 3 (اختيارية): وكيل عكسي عبر Nginx

nginx
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    listen [::]:80;

    server_name your_domain www.your_domain;

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://localhost:3000;
    }

    location /api/live {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $http_host;
        proxy_pass http://localhost:3000;
    }
}

الجزء السادس: ربط Grafana بـ Prometheus

الخطوة 1: إضافة مصدر البيانات

في Grafana: Connections > Data sources > Add data source > Prometheus.

اضبط الـ URL على:

http://localhost:9090

اضغط Save & test، ويفترض أن ترى "Successfully queried the Prometheus API."

الخطوة 2: استيراد لوحة Node Exporter

Dashboards > New > Import، ثم أدخل معرّف اللوحة:

1860

اختر مصدر بيانات Prometheus عندما يُطلب منك، ثم اضغط Import. هذه لوحة المجتمع "Node Exporter Full"، وهي تغطي المعالج والذاكرة والقرص والشبكة منذ البداية.

ورقة مرجعية لإدارة الخدمات

service          start                 status                enable
---------------  --------------------  --------------------  -------------------------------
prometheus       systemctl start       systemctl status       systemctl enable prometheus
node_exporter    systemctl start       systemctl status       systemctl enable node_exporter
grafana-server   systemctl start       systemctl status       systemctl enable grafana-server

إعادة تحميل الإعدادات دون توقف:

bash
sudo systemctl reload prometheus
sudo systemctl restart grafana-server

إلى أين بعد ذلك

خادم واحد هو نقطة البداية لا النهاية. الخطوتان التاليتان عادةً هما التنبيهات (Alertmanager، ليوقظك القرص الممتلئ في الثالثة فجرًا بدل أن يفاجئك في التاسعة) وإضافة مُصدِّر ثانٍ لما يشغّله الخادم فعليًا. وإن كان ذلك الخادم موقعًا إلكترونيًا، فإن استضافة ووردبريس ذاتيًا مع Docker وRedis هو النصف الآخر من الإعداد نفسه.

المراقبة على نطاق أوسع جزء كبير من عملي: المراقبة عبر Grafana وPrometheus، بما في ذلك منصة قياس أداء أرسلت عشرة ملايين طلب في خمس عشرة دقيقة. يسعدني أن نتحدث في الأمر.

إزالة التثبيت

إزالة Prometheus

bash
sudo systemctl disable --now prometheus
sudo rm -f /usr/local/bin/prometheus /usr/local/bin/promtool
sudo rm -rf /etc/prometheus /var/lib/prometheus /etc/systemd/system/prometheus.service
sudo userdel prometheus
sudo systemctl daemon-reload

إزالة Node Exporter

bash
sudo systemctl disable --now node_exporter
sudo rm -f /usr/local/bin/node_exporter /etc/systemd/system/node_exporter.service
sudo userdel node_exporter
sudo systemctl daemon-reload

إزالة Grafana

bash
sudo systemctl stop grafana-server
sudo apt-get remove -y grafana
sudo rm -i /etc/apt/sources.list.d/grafana.list