Last active
April 15, 2022 06:54
-
-
Save mistymagich/1220771c434b18dd778afe2af0d8bea8 to your computer and use it in GitHub Desktop.
Grafana, Alertmanagerのアラートをwebhookを使って、Google Apps Scriptを経由してChatworkに送信 https://mistymagich.wordpress.com/2022/04/15/grafana-alertmanager%e3%81%ae%e3%82%a2%e3%83%a9%e3%83%bc%e3%83%88%e3%82%92webhook%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%80%81google-apps-script%e3%82%92%e7%b5%8c%e7%94%b1%e3%81%97%e3%81%a6chatwork/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doPost(e: GoogleAppsScript.Events.DoPost): void { | |
// Chatwork APIトークン | |
const token = "<Chatwork APIトークン>"; | |
// 通知先ルームID | |
const room_id = "<Chatwork ルームID>"; | |
// チャットワークAPI | |
const client = ChatWorkClient.factory({ token: token }); | |
// 通知内容データ | |
const grafanaAlert = JSON.parse(e.postData.contents); | |
// 通知本文 | |
let body = "[toall]"; | |
grafanaAlert.alerts.forEach((alert) => { | |
body += `[info][title][${alert.status}] ${alert.labels.alertname}[/title]\n`; | |
body += "Labels:\n"; | |
for (const property in alert.labels) { | |
body += ` ${property}: ${alert.labels[property]}\n`; | |
} | |
if (alert.annotations) { | |
body += "Annotations:\n"; | |
for (const property in alert.annotations) { | |
body += ` ${property}: ${alert.annotations[property]}\n`; | |
} | |
} | |
if (alert.silenceURL) { | |
body += `Silence alert: ${alert.silenceURL}\n`; | |
} | |
if (alert.dashboardURL) { | |
body += `Go to dashboard: ${alert.dashboardURL}\n`; | |
} | |
body += "[/info]\n"; | |
}); | |
// Chatworkへ送信 | |
client.sendMessage({ | |
room_id: room_id, | |
body: body, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment