Skip to content

cdsl-research/tickets-grouping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

tickets-grouping

概要

Prometheu・Alertmanagerからのアラート通知を受信し、Redmineにチケットを作成・集約するプログラムです。

主な機能

・Alertmanagerからのアラートを受信し、Redmineにチケットを作成
・同一アラート名(alertname)で複数ホスト (instance) の場合、親チケット [Root] を作成し階層化
・再発時は既存チケットにコメント追記
・対処されなかったチケットは「持ち越し」ステータスに自動更新

環境

・Ubuntu 24.04.2
・Python(3.12.3)
・Redmine(6.0.4)
・Prometheus(2.53.1)
・Alertmanager(0.27.0)

Pythonで使用したパッケージ・ライブラリ

標準ライブラリ

・OS
・json
・datetime
・typing ・time / timezone

外部ライブラリ

・Fast API
・Uvicorn
・requests

ディレクトリ構成

~/tickets_grouping/
├── tickets_grouping.py
└── .env

準備

パッケージインストール

先にapt updateをします

hoge@test:~/tickets_grouping$ sudo apt update
[sudo] password for hoge:
Get:1 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Hit:2 http://jp.archive.ubuntu.com/ubuntu noble InRelease
Get:3 http://jp.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:4 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [1,298 kB]
Get:5 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [213 kB]
Get:6 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB]
Get:7 http://security.ubuntu.com/ubuntu noble-security/main amd64 c-n-f Metadata [9,012 B]
Get:8 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [2,131 kB]
Get:9 http://jp.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:10 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [483 kB]
Get:11 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:12 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [906 kB]
Get:13 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [203 kB]
Get:14 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [52.2 kB]
Get:15 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [208 B]
Get:16 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1,578 kB]
Get:17 http://jp.archive.ubuntu.com/ubuntu noble-updates/main Translation-en [297 kB]
Get:18 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [175 kB]
Get:19 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 c-n-f Metadata [15.4 kB]
Get:20 http://jp.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [2,235 kB]
Get:21 http://jp.archive.ubuntu.com/ubuntu noble-updates/restricted Translation-en [506 kB]
Get:22 http://jp.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:23 http://jp.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [378 kB]
Get:24 http://jp.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:25 http://jp.archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7,140 B]
Get:26 http://jp.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:27 http://jp.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [11.0 kB]
Get:28 http://jp.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Fetched 10.9 MB in 4s (2,560 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
102 packages can be upgraded. Run 'apt list --upgradable' to see them.
hoge@test:~/tickets_grouping$

FastAPIuvicornrequestsをインストールします

hoge@test:~/tickets_grouping$ sudo apt install -y python3-fastapi python3-uvicorn python3-requests
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3-fastapi is already the newest version (0.101.0-3).
python3-uvicorn is already the newest version (0.27.1-1).
python3-requests is already the newest version (2.31.0+dfsg-1ubuntu1.1).
0 upgraded, 0 newly installed, 0 to remove and 102 not upgraded.
hoge@test:~/tickets_grouping$

この環境では既にインストール済みなので、出力結果が違うと思います

環境変数ファイルを設定

環境変数ファイルに、Redmineの各種情報を設定します

hoge@test:~/tickets_grouping$ sudo tee .env <<'EOF'
REDMINE_URL=https://redmine.example.com
REDMINE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
REDMINE_PROJECT_ID=123
REDMINE_TRACKER_ID=1
EOF
hoge@test:~/tickets_grouping$

REDMINE_URL:Redmineの「ホーム」のURL
REDMINE_API_KEY:APIキー(個人設定から確認可能)
REDMINE_PROJECT_ID:チケットを登録するプロジェクトのID
REDMINE_TRACKER_ID:チケットを登録するトラッカーのID

systemdサービス設定

今回はsystemdで動かします
/etc/systemd/system/alert-webhook.service を作成します

hoge@test:~/tickets-grouping$sudo nano /etc/systemd/system/alert-webhook.service

↓alert-webhook.serviceの内容

[Unit]
Description=Alert Webhook FastAPI Service
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/hoge/tickets_grouping
EnvironmentFile/home/hoge/tickets_grouping/.env
ExecStart=/usr/bin/uvicorn app:app --host 0.0.0.0 --port 5005
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

lsをするとalert-webhook.serviceが追加されています

hoge@test:/etc/systemd/system$ ls
alert-webhook.service                       graphical.target.wants          sockets.target.wants
cloud-config.target.wants                   hibernate.target.wants          sshd.service
cloud-final.service.wants                   hybrid-sleep.target.wants       ssh.service.requires
cloud-init.target.wants                     iscsi.service                   suspend.target.wants
dbus-org.freedesktop.ModemManager1.service  mdmonitor.service.wants         suspend-then-hibernate.target.wants
dbus-org.freedesktop.resolve1.service       multi-user.target.wants         sysinit.target.wants
dbus-org.freedesktop.thermald.service       network-online.target.wants     syslog.service
dbus-org.freedesktop.timesync1.service      oem-config.service.wants        sysstat.service.wants
display-manager.service.wants               open-vm-tools.service.requires  timers.target.wants
emergency.target.wants                      paths.target.wants              vmtoolsd.service
final.target.wants                          rescue.target.wants
getty.target.wants                          sleep.target.wants
hoge@test:/etc/systemd/system$

systemdサービスの起動

systemdを起動します

以下のように表示され、Active: active (running)となっていれば動いています

hoge@test:~/tickets_grouping$ sudo systemctl daemon-reload
sudo systemctl enable alert-webhook
sudo systemctl start alert-webhook
sudo systemctl status alert-webhook
● alert-webhook.service - Alertmanager Webhook Receiver (FastAPI)
     Loaded: loaded (/etc/systemd/system/alert-webhook.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-11-03 07:08:36 UTC; 1h 1min ago
   Main PID: 1265 (uvicorn)
      Tasks: 1 (limit: 4605)
     Memory: 47.2M (peak: 47.7M)
        CPU: 5.054s
     CGroup: /system.slice/alert-webhook.service
             └─1265 /home/hoge/tickets-grouping/venv/bin/python3 /home/hoge/tickets-grouping/venv/bin/uv>

Nov 03 07:08:36 test systemd[1]: Started alert-webhook.service - Alertmanager Webhook Receiver (FastAPI).
Nov 03 07:08:37 test uvicorn[1265]: INFO:     Started server process [1265]
Nov 03 07:08:37 test uvicorn[1265]: INFO:     Waiting for application startup.
Nov 03 07:08:37 test uvicorn[1265]: INFO:     Application startup complete.
Nov 03 07:08:37 test uvicorn[1265]: INFO:     Uvicorn running on http://0.0.0.0:5005 (Press CTRL+C to quit)
Nov 03 07:08:40 test uvicorn[1265]: INFO:     192.168.100.61:65370 - "POST /webhook HTTP/1.1" 200 OK
Nov 03 07:09:20 test uvicorn[1265]: INFO:     192.168.100.61:9695 - "POST /webhook HTTP/1.1" 200 OK
Nov 03 07:13:12 test uvicorn[1265]: INFO:     192.168.100.61:4784 - "POST /webhook HTTP/1.1" 200 OK
Nov 03 07:13:12 test uvicorn[1265]: INFO:     192.168.100.61:45367 - "POST /webhook HTTP/1.1" 200 OK
Nov 03 07:26:01 test uvicorn[1265]: INFO:     192.168.100.61:39234 - "POST /webhook HTTP/1.1" 200 OK
lines 1-20/20 (END)

Alertmanagerの設定

alertmanager.ymlに以下を追加し、チケット作成ソフトに通知が飛ぶようにします

 receivers:
      - name: "redmine"
        webhook_configs:
          - url: "http://<マシンのIPアドレス OR DNS名:<port番号>/webhook"
            send_resolved: <false OR ture>

動作確認

ローカルマシンからcurlでRedmineにチケットがきちんと作成されるか確認します

うまくいけば、{"status":"ok"}と表示され、Redmine に [Alert] test (server01) チケットが作成されます

hoge@test:~/tickets_grouping$ curl -X POST http://localhost:5005/webhook -H "Content-Type: application/json" -d '{
  "alerts": [
    {
      "labels": {
        "alertname": "test",
        "instance": "server01"
      },
      "annotations": {
        "description": "test: server01"
      }
    }
  ]
}'
{"status":"ok"}hoge@test:~/tickets_grouping$

7

うまくいかない場合は、.envファイルとAlertmanagerのyamlファイルを確認してください。

チケットグルーピング

Alertmanagerから通知を受け取るとalertnameinstance(ホスト名)に基づいてチケットを登録していきます。

初回アラート

[Alert] <alertname> (<instance>) 形式でチケットを作成

同一ホスト・同一アラート再発

既存チケットに「再発」コメントを追加

異なるホストで同一アラートが発生

[Root] [Alert] <alertname> の親チケットを自動生成し、各ホストのチケットを子チケットとして、関連付ける。


これにより、同じ種類のアラートが複数ホストで発生しても、Redmine上では1つの「Root」チケットを中心に整理できます。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages