Browse Source

Make icon/color maps configurable

master
Silke 4 years ago
parent
commit
5b2a8c3442
  1. 27
      README.md
  2. 26
      alertmanager-matrix.go
  3. 4
      alerts.go

27
README.md

@ -4,7 +4,7 @@ Service for sending alerts from the Alertmanager webhook to a Matrix room.
## Usage
The service is configured either through command line arguments or environment variables.
With the provided systemd service file (`alertmanager-matrix.service`),
the configuration is done in `/etc/default/alertmanager-matrix` as follows:
the configuration is done in `/etc/default/alertmanager-matrix` as follows:
```sh
HOMESERVER=http://localhost:8008
@ -25,3 +25,28 @@ receivers:
```
The service will *not* automatically join configured rooms.
The icons and colors can be configured by providing a JSON file.
The defaults are:
```json
{
"alert": "🔔️",
"warning":"⚠️",
"critical": "😱",
"ok": "😄",
"up": "😄",
"down": "😱"
}
```
```json
{
"alert": "black",
"warning": "orange",
"critical": "red",
"ok": "green",
"up": "green",
"down": "red"
}
```

26
alertmanager-matrix.go

@ -43,22 +43,46 @@ func setStringFromEnv(target *string, env string) {
}
}
func setMapFromJsonFile(m *map[string]string, fileName string) {
file, err := os.Open(fileName)
if err != nil {
log.Fatal("Unable to open JSON file: ", err)
}
err = json.NewDecoder(file).Decode(m)
if err != nil {
log.Fatal("Unable to parse JSON file: ", err)
}
}
func main() {
var addr string
var homeserver, userID, token string
var homeserver, userID, token, iconFile, colorFile string
var err error
flag.StringVar(&addr, "addr", ":4051", "Address to listen on.")
flag.StringVar(&homeserver, "homeserver", "https://matrix.org", "Homeserver to connect to.")
flag.StringVar(&userID, "userID", "", "User ID to connect with.")
flag.StringVar(&token, "token", "", "Token to connect with.")
flag.StringVar(&iconFile, "icon-file", "", "JSON file with icons for message types.")
flag.StringVar(&colorFile, "color-file", "", "JSON file with colors for message types.")
flag.Parse()
// Set variables from the environment
setStringFromEnv(&addr, "ADDR")
setStringFromEnv(&homeserver, "HOMESERVER")
setStringFromEnv(&userID, "USER_ID")
setStringFromEnv(&token, "TOKEN")
// Load mappings from files
if iconFile != "" {
setMapFromJsonFile(&alertIcons, iconFile)
}
if colorFile != "" {
setMapFromJsonFile(&alertColors, colorFile)
}
log.Printf("%+v\n%+v\n", alertIcons, alertColors)
log.Printf("Connecting to Matrix homeserver at %s as %s", homeserver, userID)
client, err = matrix.NewClient(homeserver, userID, token)
if err != nil {

4
alerts.go

@ -6,7 +6,7 @@ import (
"strings"
)
var alertEmoji = map[string]string{
var alertIcons = map[string]string{
"alert": "🔔️",
"warning": "⚠️",
"critical": "😱",
@ -25,7 +25,7 @@ var alertColors = map[string]string{
}
func emoji(t string) string {
if e, ok := alertEmoji[t]; ok {
if e, ok := alertIcons[t]; ok {
return e
}
log.Printf("Unknown status: %s", t)

Loading…
Cancel
Save