هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
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
checkconfigFILE- Validate prometheus.yml (and the rule files it loads).
checkrulesFILE...- Validate rule files.
checkmetrics- Lint metrics text from stdin (naming rules).
testrulesFILE- Unit tests for alerts and recording rules.
queryinstantURLEXPR- Run a PromQL query now.
queryrangeURLEXPR--start--end--step- Query over time.
querylabelsURLNAME- Values of a label.
tsdbanalyzeDIR- Show which series use the most space.
tsdbcreate-blocks-fromopenmetrics- Backfill old data.
الخيارات 6
الأكثر استخدامًا
prometheus server flags
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 10
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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"]2Check the config and reload
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.yml4Query 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]))'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) < 06Ask the HTTP API with curl
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.yml8Install the full stack on Kubernetes
9Find series that use the most memory
promtool tsdb analyze /var/lib/prometheus/data | head -3010Open 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 checkin the CI of the repo that holds your rules. - Follow the USE (utilization, saturation, errors) and RED (rate, errors, duration) methods for dashboards.
رموز الخروج
0 | Success (valid). |
1 | Error or invalid file. |
3 | Lint problems found (check rules/metrics). |