Skip to content

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

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

يحسن بك أن تُبقي ثلاث مراجع رسمية مفتوحة إلى جانب هذا الدليل: مرجع إعدادات Prometheus، وقائمة مجمّعات Node Exporter وما يكلّفك كل واحد منها، ودليل تثبيت Grafana على دبيان من Grafana نفسها.

البنية ​

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

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

  • خادم دبيان أو أوبنتو مع صلاحية sudo
  • تثبيت curl وtar
  • وصول عبر SSH لإنشاء نفق. أبقِ المنافذ 9090 و9100 و3000 مغلقة أمام الإنترنت؛ ستستمع الخدمات الثلاث على عنوان الاسترجاع المحلي فقط.

الجزء الأول: تثبيت 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.listen-address=127.0.0.1:9090 \\
  --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

من حاسوبك، افتح نفق SSH واتركه يعمل:

bash
ssh -N -L 9090:127.0.0.1:9090 -L 3000:127.0.0.1:3000 user@server

استبدل user@server بوجهة SSH الخاصة بك، ثم افتح http://localhost: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 --web.listen-address=127.0.0.1:9100

[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: التأكد من أن الهدف يعمل ​

عبر نفق SSH، افتح http://localhost:9090/targets، ويفترض أن تظهر المهمتان prometheus وnode_exporter بحالة State = UP.

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

الطريقة الرسمية هي مستودع APT. يحدّث apt-get update قوائم الحزم فقط؛ لتثبيت ترقية متاحة شغّل apt-get install --only-upgrade 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 mkdir -p /etc/systemd/system/grafana-server.service.d
printf '[Service]\nEnvironment=GF_SERVER_HTTP_ADDR=127.0.0.1\n' | sudo tee /etc/systemd/system/grafana-server.service.d/listen.conf
sudo systemctl daemon-reload
sudo systemctl enable grafana-server
sudo systemctl restart grafana-server

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

bash
sudo systemctl status grafana-server

عبر نفق SSH، افتح http://localhost:3000. بيانات الدخول الافتراضية هي admin / admin، وسيُطلب منك تغييرها عند أول تسجيل دخول.

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

يكفي نفق SSH للوصول الخاص. لنشر لوحة عبر نطاق، وجّه grafana.example.com إلى الخادم واجعل nginx يتولى TLS. يبقى Grafana على HTTP محلي؛ لا تفعّل HTTPS على منفذ الخدمة الخلفية.

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

bash
sudo vim /etc/grafana/grafana.ini
ini
[server]
http_addr = 127.0.0.1
http_port = 3000
domain = grafana.example.com
root_url = https://grafana.example.com/
enforce_domain = true
protocol = http

الخطوة 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 grafana.example.com;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1: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 $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:3000;
    }
}

ثبّت nginx وCertbot، ثم احفظ الإعداد أعلاه في /etc/nginx/sites-available/grafana.example.com. استبدل نطاق المثال بنطاقك في كل المواضع:

bash
sudo apt-get install -y nginx certbot python3-certbot-nginx
sudo ln -s /etc/nginx/sites-available/grafana.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d grafana.example.com
sudo certbot renew --dry-run

افتح المنفذين 80 و443 لـ nginx، لا المنفذ 3000. يضيف Certbot إعداد TLS ومؤقت التجديد. أبقِ مصادقة Grafana مفعّلة وغيّر كلمة المرور الأولية عبر نفق SSH قبل إتاحة النطاق.

الجزء السادس: ربط 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