-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbixmon_utils.go
62 lines (53 loc) · 1.35 KB
/
zabbixmon_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"os/exec"
"runtime"
"github.com/gen2brain/beeep"
"github.com/samber/lo"
)
// dump items as json on stdout
func dumpJsonIfRedirect(items []zabbixmonItem) {
o, _ := os.Stdout.Stat()
if (o.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
if data, err := json.Marshal(items); err != nil {
slog.Error(err.Error(), slog.String("scope", "dumping json"))
os.Exit(1)
} else {
fmt.Println(string(data))
os.Exit(0)
}
}
}
// open url in the default web browser
func openUrl(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default:
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
// send notification for all items
func notify(items, prevItems []zabbixmonItem) {
if config.Notify && len(prevItems) > 0 {
newItems, _ := lo.Difference(items, prevItems)
for _, item := range newItems {
slog.Debug("", slog.String("type", "new_item"), slog.String("item", fmt.Sprintf("%#v", item)))
if err := beeep.Notify(fmt.Sprintf("%s - %s", item.Status, item.Host), item.Description, "assets/information.png"); err != nil {
slog.Error(err.Error(), slog.String("scope", "sending notification"))
os.Exit(1)
}
}
}
}