# /etc/systemd/system/veeam-exporter.service
[Unit]
Description=Veeam Backup Prometheus Exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /opt/veeam_exporter/veeam_exporter.py \
    --port 9199 \
    --vbr 10.115.13.219 \
    --user veeamuser01 \
    --password 'Qazx1234!@#$qwer2'
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target

# 部署
mkdir -p /opt/veeam_exporter
cp veeam_exporter.py /opt/veeam_exporter/
chmod +x /opt/veeam_exporter/veeam_exporter.py

systemctl daemon-reload
systemctl enable --now veeam-exporter

# 验证
curl http://localhost:9199/metrics

# prometheus.yml
scrape_configs:
  - job_name: 'veeam_backup'
    scrape_interval: 5m          # 备份不需要太频繁
    scrape_timeout: 30s
    static_configs:
      - targets: ['10.150.117.190:9199']   # exporter 所在机器
        labels:
          vbr_server: 'ussjcvbr01'

          {
  "annotations": { "list": [] },
  "description": "Veeam Backup & Replication 监控面板",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "links": [],
  "panels": [
    {
      "title": "API 状态",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 0, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_api_up", "refId": "A" }],
      "fieldConfig": {
        "defaults": {
          "mappings": [
            { "options": { "0": { "text": "DOWN", "color": "red" }, "1": { "text": "UP", "color": "green" } }, "type": "value" }
          ],
          "thresholds": { "steps": [{ "color": "red", "value": null }, { "color": "green", "value": 1 }] }
        }
      }
    },
    {
      "title": "备份任务总数",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 3, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_backup_jobs_total", "refId": "A" }],
      "fieldConfig": { "defaults": { "color": { "mode": "fixed", "fixedColor": "blue" } } }
    },
    {
      "title": "24h 成功",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 6, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_sessions_success_24h", "refId": "A" }],
      "fieldConfig": { "defaults": { "color": { "mode": "fixed", "fixedColor": "green" } } }
    },
    {
      "title": "24h 失败",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 9, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_sessions_failed_24h", "refId": "A" }],
      "fieldConfig": {
        "defaults": {
          "thresholds": { "steps": [{ "color": "green", "value": null }, { "color": "red", "value": 1 }] }
        }
      }
    },
    {
      "title": "24h 警告",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 12, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_sessions_warning_24h", "refId": "A" }],
      "fieldConfig": {
        "defaults": {
          "thresholds": { "steps": [{ "color": "green", "value": null }, { "color": "orange", "value": 1 }] }
        }
      }
    },
    {
      "title": "采集耗时",
      "type": "stat",
      "gridPos": { "h": 4, "w": 3, "x": 15, "y": 0 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "veeam_collector_duration_seconds", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "s" } }
    },
    {
      "title": "备份任务状态总览",
      "type": "table",
      "gridPos": { "h": 10, "w": 24, "x": 0, "y": 4 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "veeam_job_status",
          "refId": "A",
          "format": "table",
          "instant": true
        },
        {
          "expr": "veeam_job_duration_seconds",
          "refId": "B",
          "format": "table",
          "instant": true
        },
        {
          "expr": "veeam_job_last_run_timestamp",
          "refId": "C",
          "format": "table",
          "instant": true
        },
        {
          "expr": "veeam_job_processed_bytes",
          "refId": "D",
          "format": "table",
          "instant": true
        }
      ],
      "transformations": [
        {
          "id": "seriesToColumns",
          "options": { "byField": "job_name" }
        },
        {
          "id": "organize",
          "options": {
            "excludeByName": { "Time 1": true, "Time 2": true, "Time 3": true, "Time 4": true, "__name__ 1": true, "__name__ 2": true, "__name__ 3": true, "__name__ 4": true, "instance 1": true, "instance 2": true, "instance 3": true, "instance 4": true, "job_id 1": true, "job_id 2": true, "job_id 3": true, "job_id 4": true, "type 2": true, "type 3": true, "type 4": true, "platform 2": true, "platform 3": true, "platform 4": true, "Value #2": false, "Value #3": false, "Value #4": false },
            "renameByName": {
              "job_name": "任务名称",
              "platform": "平台",
              "type": "类型",
              "Value #1": "状态",
              "Value #2": "耗时(秒)",
              "Value #3": "最后运行",
              "Value #4": "处理数据量"
            }
          }
        }
      ],
      "options": {
        "sortBy": [{ "displayName": "状态", "desc": true }]
      },
      "fieldConfig": {
        "overrides": [
          {
            "matcher": { "id": "byName", "options": "状态" },
            "properties": [
              {
                "id": "mappings",
                "value": [
                  { "options": { "0": { "text": "未知", "color": "gray" }, "1": { "text": "成功", "color": "green" }, "2": { "text": "警告", "color": "orange" }, "3": { "text": "失败", "color": "red" } }, "type": "value" }
                ]
              }
            ]
          },
          {
            "matcher": { "id": "byName", "options": "耗时(秒)" },
            "properties": [{ "id": "unit", "value": "s" }]
          },
          {
            "matcher": { "id": "byName", "options": "最后运行" },
            "properties": [{ "id": "unit", "value": "dateTimeFromNow" }]
          },
          {
            "matcher": { "id": "byName", "options": "处理数据量" },
            "properties": [{ "id": "unit", "value": "bytes" }]
          }
        ]
      }
    },
    {
      "title": "任务状态历史",
      "type": "state-timeline",
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 14 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "veeam_job_status",
          "legendFormat": "{{job_name}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "mappings": [
            { "options": { "0": { "text": "未知", "color": "gray" }, "1": { "text": "成功", "color": "green" }, "2": { "text": "警告", "color": "orange" }, "3": { "text": "失败", "color": "red" } }, "type": "value" }
          ],
          "color": { "mode": "thresholds" },
          "thresholds": { "steps": [{ "color": "gray", "value": null }, { "color": "green", "value": 1 }, { "color": "orange", "value": 2 }, { "color": "red", "value": 3 }] }
        }
      },
      "options": {
        "showValue": "always",
        "mergeValues": true,
        "alignValue": "center"
      }
    },
    {
      "title": "任务耗时趋势",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 22 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "veeam_job_duration_seconds",
          "legendFormat": "{{job_name}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "s",
          "custom": { "lineWidth": 2, "fillOpacity": 10, "pointSize": 5, "showPoints": "auto" }
        }
      }
    },
    {
      "title": "处理数据量",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 22 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "veeam_job_processed_bytes",
          "legendFormat": "{{job_name}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "bytes",
          "custom": { "lineWidth": 2, "fillOpacity": 10 }
        }
      }
    },
    {
      "title": "24h 会话统计趋势",
      "type": "timeseries",
      "gridPos": { "h": 8, "w": 24, "x": 0, "y": 30 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        { "expr": "veeam_sessions_success_24h", "legendFormat": "成功", "refId": "A" },
        { "expr": "veeam_sessions_failed_24h", "legendFormat": "失败", "refId": "B" },
        { "expr": "veeam_sessions_warning_24h", "legendFormat": "警告", "refId": "C" }
      ],
      "fieldConfig": {
        "defaults": {
          "custom": { "lineWidth": 2, "fillOpacity": 20 }
        },
        "overrides": [
          { "matcher": { "id": "byName", "options": "成功" }, "properties": [{ "id": "color", "value": { "fixedColor": "green", "mode": "fixed" } }] },
          { "matcher": { "id": "byName", "options": "失败" }, "properties": [{ "id": "color", "value": { "fixedColor": "red", "mode": "fixed" } }] },
          { "matcher": { "id": "byName", "options": "警告" }, "properties": [{ "id": "color", "value": { "fixedColor": "orange", "mode": "fixed" } }] }
        ]
      }
    }
  ],
  "refresh": "5m",
  "schemaVersion": 39,
  "templating": {
    "list": [
      {
        "name": "DS_PROMETHEUS",
        "type": "datasource",
        "query": "prometheus"
      }
    ]
  },
  "time": { "from": "now-24h", "to": "now" },
  "title": "Veeam Backup Monitor",
  "uid": "veeam-backup-monitor"
}

# veeam_alerts.yml
groups:
  - name: veeam_backup
    rules:
      # API 不可达
      - alert: VeeamAPIUnreachable
        expr: veeam_api_up == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Veeam API 不可达"
          description: "无法连接 VBR REST API，请检查 VBR 服务状态"

      # 备份任务失败
      - alert: VeeamBackupFailed
        expr: veam_job_status == 3
        for: 0m
        labels:
          severity: critical
        annotations:
          summary: "备份任务失败: {{ $labels.job_name }}"
          description: "任务 {{ $labels.job_name }} ({{ $labels.platform }}) 最近一次执行失败"

      # 备份任务警告
      - alert: VeeamBackupWarning
        expr: veeam_job_status == 2
        for: 0m
        labels:
          severity: warning
        annotations:
          summary: "备份任务警告: {{ $labels.job_name }}"

      # 备份任务超过24小时未运行
      - alert: VeeamBackupStale
        expr: (time() - veeam_job_last_run_timestamp) > 86400
        for: 30m
        labels:
          severity: warning
        annotations:
          summary: "备份任务超过24小时未运行: {{ $labels.job_name }}"
          description: "任务 {{ $labels.job_name }} 最后运行于 {{ $value | humanizeDuration }} 前"

      # 24小时内有失败
      - alert: VeeamFailedSessionsExist
        expr: veeam_sessions_failed_24h > 0
        for: 0m
        labels:
          severity: warning
        annotations:
          summary: "过去24小时有 {{ $value }} 个备份任务失败"

          # 1. 安装 exporter
mkdir -p /opt/veeam_exporter
cp veeam_exporter.py /opt/veeam_exporter/

# 2. 测试运行
python3 /opt/veeam_exporter/veeam_exporter.py \
  --port 9199 \
  --vbr 10.115.13.219 \
  --user veeamuser01 \
  --password 'Qazx1234!@#$qwer2'

# 3. 验证指标
curl http://localhost:9199/metrics

# 4. 配置 systemd
cp veeam-exporter.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now veeam-exporter

# 5. Prometheus 添加 target
# 6. Grafana 导入 dashboard JSON
# 7. 配置告警规则