الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

promtool

Prometheus collects metrics by scraping targets and runs alert rules; promtool checks its config and rules and runs queries.

المستوى
خدمة DevSecOps
المجموعة
المراقبة والسجلات
يحتاج root
للتغييرات
الحزمة
prometheus
يُسمّى أيضًا
prometheus

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

Prometheus (CNCF) scrapes HTTP /metrics endpoints every few seconds and stores time series (name + labels + values). You query with PromQL in the web UI (port 9090), in Grafana, or with promtool query. Recording rules pre-compute queries; alerting rules fire alerts that Alertmanager sends out. Targets come from static lists or service discovery (Kubernetes, Azure, file).

promtool ships with Prometheus: always run promtool check config and check rules (in CI, too) before a reload.

طريقة الكتابة

promtool check config|rules|metrics FILE
promtool query instant|range URL 'PROMQL'
promtool test rules TEST.yml

الأجزاء الرئيسية

promtool commands

check config FILE
Validate prometheus.yml (and the rule files it loads).
check rules FILE...
Validate rule files.
check metrics
Lint metrics text from stdin (naming rules).
test rules FILE
Unit tests for alerts and recording rules.
query instant URL EXPR
Run a PromQL query now.
query range URL EXPR --start --end --step
Query over time.
query labels URL NAME
Values of a label.
tsdb analyze DIR
Show which series use the most space.
tsdb create-blocks-from openmetrics
Backfill old data.

الخيارات 6

الأكثر استخدامًا

prometheus server flags

--config.file=FILE
Configuration file.
--storage.tsdb.path=DIR
Data folder.
--storage.tsdb.retention.time=30d
How long to keep data.
--web.enable-lifecycle
Allow reload with HTTP POST /-/reload.
--web.listen-address=:9090
Listen address.
--web.config.file=FILE
TLS and basic auth for the UI/API.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1A small prometheus.yml

    cat /etc/prometheus/prometheus.yml
    global:
      scrape_interval: 15s
    rule_files: [/etc/prometheus/rules/*.yml]
    alerting:
      alertmanagers: [{ static_configs: [{ targets: ["localhost:9093"] }] }]
    scrape_configs:
      - job_name: node
        static_configs:
          - targets: ["web1:9100", "web2:9100", "db1:9100"]
  2. 2Check the config and reload

    promtool check config /etc/prometheus/prometheus.yml
    systemctl reload prometheus
    curl -X POST http://localhost:9090/-/reload
  3. 3Alert when a server is down or a disk is almost full

    cat /etc/prometheus/rules/node.yml
    groups:
      - name: node
        rules:
          - alert: InstanceDown
            expr: up == 0
            for: 2m
            labels: { severity: critical }
            annotations: { summary: "{{ $labels.instance }} is down" }
          - alert: DiskAlmostFull
            expr: (node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes) < 0.10
            for: 10m
            labels: { severity: warning }
    promtool check rules /etc/prometheus/rules/node.yml
  4. 4Query from the terminal

    promtool query instant http://localhost:9090 'up'
    promtool query instant http://localhost:9090 'sum by (instance) (rate(node_cpu_seconds_total{mode!="idle"}[5m]))'
  5. 5Useful PromQL queries

    cat promql-cheatsheet.txt
    100 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100
    (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
    sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m]))
    histogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))
    predict_linear(node_filesystem_avail_bytes[6h], 24*3600) < 0
  6. 6Ask the HTTP API with curl

    curl -s 'http://localhost:9090/api/v1/query?query=up' | jq '.data.result[] | [.metric.instance, .value[1]]'
    curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | [.labels.job, .health]'
  7. 7Unit-test an alert

    cat node-test.yml
    rule_files: [node.yml]
    tests:
      - interval: 1m
        input_series:
          - series: 'up{job="node",instance="web1:9100"}'
            values: '0x5'
        alert_rule_test:
          - eval_time: 5m
            alertname: InstanceDown
            exp_alerts: [{ exp_labels: { severity: critical, job: node, instance: "web1:9100" } }]
    promtool test rules node-test.yml
  8. 8Install the full stack on Kubernetes

    helm install monitoring prometheus-community/kube-prometheus-stack -n monitoring --create-namespace
    kubectl -n monitoring port-forward svc/monitoring-kube-prometheus-prometheus 9090
  9. 9Find series that use the most memory

    promtool tsdb analyze /var/lib/prometheus/data | head -30
  10. 10Open the firewall for the UI (lab only)

    firewall-cmd --permanent --add-port=9090/tcp && firewall-cmd --reload

انتبه

  • High-cardinality labels (user IDs, URLs with IDs) can kill Prometheus memory.
  • The UI and API have no login by default; use --web.config.file (TLS + basic auth) or keep them internal.
  • {{ $labels.x }} in alert annotations is Go template syntax — quote it in YAML.

الملفات

/etc/prometheus/prometheus.yml
Main config.
/etc/prometheus/rules/
Rule files.
/var/lib/prometheus/
Time-series data.

نصائح للحفظ

  • Run promtool check in the CI of the repo that holds your rules.
  • Follow the USE (utilization, saturation, errors) and RED (rate, errors, duration) methods for dashboards.

رموز الخروج

0Success (valid).
1Error or invalid file.
3Lint problems found (check rules/metrics).