Guest mode. You can read posts without signing up. Comments, ratings, job contacts and your own posts open after you sign up and accept the rules. What is Kommit →

Редакция КоммитРедакция портала
commit 7266f01 · 18 days ago

Monitoring, part 4: Prometheus, node_exporter, site checks, and Alertmanager with alerts in Telegram

🌐 Machine-translated from Russian. Show original

In this part:

How Prometheus works#

  1. The object outputs metrics over HTTP in text format (/metrics) — this is done by the exporter or the application itself.
  2. Prometheus schedules (usually every 15–30 seconds) to poll all targets and saves data.
  3. Rules check conditions and send triggered alerts to Alertmanager.
  4. Alertmanager groups, suppresses duplicates, and sends notifications.

Downloads: prometheus.io/download, GitHub releases: node_exporter, blackbox_exporter, alertmanager, windows_exporter, snmp_exporter.

💡 Ubuntu has a prometheus package via apt, but its version is noticeably older. Below are the official binaries — this way you will have the current version.

Step 1. Installing Prometheus#

PV=3.16.0
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v$PV/prometheus-$PV.linux-amd64.tar.gz
tar xzf prometheus-$PV.linux-amd64.tar.gz
sudo useradd --system --no-create-home --shell /usr/sbin/nologin prometheus
sudo install -m 755 prometheus-$PV.linux-amd64/{prometheus,promtool} /usr/local/bin/
sudo mkdir -p /etc/prometheus /var/lib/prometheus
sudo chown -R prometheus:prometheus /var/lib/prometheus

Config /etc/prometheus/prometheus.yml:

global:
  scrape_interval: 30s
  evaluation_interval: 30s

alerting:
  alertmanagers:
    - static_configs:
        - targets: ["127.0.0.1:9093"]

rule_files:
  - /etc/prometheus/rules/*.yml

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["127.0.0.1:9090"]

  - job_name: node
    static_configs:
      - targets: ["192.168.1.10:9100", "192.168.1.21:9100"]

Service /etc/systemd/system/prometheus.service:

[Unit]
Description=Prometheus
After=network-online.target

[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus \
  --storage.tsdb.retention.time=30d \
  --web.listen-address=127.0.0.1:9090
ExecReload=/bin/kill -HUP $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target
sudo mkdir -p /etc/prometheus/rules
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl daemon-reload && sudo systemctl enable --now prometheus

Prometheus only listens to localhost: it does not have built-in default authorization. View the interface — via SSH tunnel ssh -L 9090:127.0.0.1:9090 ivan@monitor → http://localhost:9090, and charts — in Grafana (part 5).

Step 2. node_exporter on each Linux server#

NV=1.12.1
cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v$NV/node_exporter-$NV.linux-amd64.tar.gz
tar xzf node_exporter-$NV.linux-amd64.tar.gz
sudo install -m 755 node_exporter-$NV.linux-amd64/node_exporter /usr/local/bin/
sudo useradd --system --no-create-home --shell /usr/sbin/nologin node_exporter
sudo tee /etc/systemd/system/node_exporter.service >/dev/null <<'EOF'
[Unit]
Description=Node Exporter
After=network-online.target
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload && sudo systemctl enable --now node_exporter
sudo ufw allow from 192.168.1.11 to any port 9100 proto tcp

Check: curl -s localhost:9100/metrics | head. In the Prometheus → Status → Targets interface, the target must be UP.

For Windows — windows_exporter (MSI, port 9182).

Step 3. Site checks: blackbox_exporter#

Checks HTTP(S), TCP ports, ICMP, and DNS "externally," plus SSL certificate expiration.

BV=0.29.1
wget https://github.com/prometheus/blackbox_exporter/releases/download/v$BV/blackbox_exporter-$BV.linux-amd64.tar.gz
tar xzf blackbox_exporter-$BV.linux-amd64.tar.gz
sudo install -m 755 blackbox_exporter-$BV.linux-amd64/blackbox_exporter /usr/local/bin/
sudo install -m 644 blackbox_exporter-$BV.linux-amd64/blackbox.yml /etc/prometheus/blackbox.yml

The service is created the same way as for node_exporter, with ExecStart=/usr/local/bin/blackbox_exporter --config.file=/etc/prometheus/blackbox.yml --web.listen-address=127.0.0.1:9115.

Task in prometheus.yml:

  - job_name: sites
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: ["https://company.kz", "https://shop.company.kz"]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

Step 4. PromQL Basics#

QueryWhat shows
up1 — target answers, 0 — no
100 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100CPU load, %
`node_filesystem_avail_bytes{fstype!~"tmpfsoverlay"} / node_filesystem_size_bytes * 100`
probe_successis the
(probe_ssl_earliest_cert_expiry - time()) / 86400days before the expiration of the

Reference book: prometheus.io/docs/.../querying/basics.

Step 5. Alert rules#

/etc/prometheus/rules/basic.yml:

groups:
  - name: basic
    rules:
      - alert: TargetDown
        expr: up == 0
        for: 2m
        labels: { severity: critical }
        annotations: { summary: "{{ $labels.instance }} не отвечает" }

      - alert: DiskAlmostFull
        expr: node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes < 0.10
        for: 10m
        labels: { severity: warning }
        annotations: { summary: "{{ $labels.instance }}: меньше 10 % места на {{ $labels.mountpoint }}" }

      - alert: SiteDown
        expr: probe_success == 0
        for: 2m
        labels: { severity: critical }
        annotations: { summary: "Сайт {{ $labels.instance }} недоступен" }

      - alert: CertExpiresSoon
        expr: (probe_ssl_earliest_cert_expiry - time()) / 86400 < 14
        labels: { severity: warning }
        annotations: { summary: "Сертификат {{ $labels.instance }} истекает через 14 дней или раньше" }
promtool check rules /etc/prometheus/rules/basic.yml && sudo systemctl reload prometheus

Step 6. Alertmanager and Telegram#

AV=0.34.2
wget https://github.com/prometheus/alertmanager/releases/download/v$AV/alertmanager-$AV.linux-amd64.tar.gz
tar xzf alertmanager-$AV.linux-amd64.tar.gz
sudo install -m 755 alertmanager-$AV.linux-amd64/{alertmanager,amtool} /usr/local/bin/
sudo mkdir -p /etc/alertmanager /var/lib/alertmanager

Place the bot token in a separate file with 600 rights — not in the main config:

sudo install -m 600 /dev/null /etc/alertmanager/telegram_token   # затем вставьте токен через sudo nano

/etc/alertmanager/alertmanager.yml:

route:
  receiver: telegram
  group_by: [alertname, instance]
  group_wait: 30s
  repeat_interval: 4h

receivers:
  - name: telegram
    telegram_configs:
      - bot_token_file: /etc/alertmanager/telegram_token
        chat_id: -1001234567890
        send_resolved: true

The service is modeled after Prometheus, ExecStart=/usr/local/bin/alertmanager --config.file=/etc/alertmanager/alertmanager.yml --storage.path=/var/lib/alertmanager --web.listen-address=127.0.0.1:9093. Config verification: amtool check-config /etc/alertmanager/alertmanager.yml. Grant the rights to the directories and token file to the service user.

Data is collected, alerts are sent. Now let's make it beautiful and convenient: Monitoring, part 5: Grafana — installation, Prometheus and Zabbix sources, ready-made dashboards and alerts.

Comments 00 участников · 1 гость🔒 Rate and discuss after signing up

Comments · 0

No comments yet.Sign in to comment, rate and see all comments.