-
Notifications
You must be signed in to change notification settings - Fork 1
/
alerts.go
127 lines (108 loc) · 2.53 KB
/
alerts.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"golang.org/x/sys/windows/registry"
)
var ApiKey = "your_api_key"
var BaseURL string
type AlertData struct {
AlertType string `json:"alert_type"`
Severity string `json:"severity"`
Agent int `json:"agent"`
Message string `json:"message"`
Hostname string `json:"hostname"`
AgentID string `json:"agent_id"`
Client string `json:"client"`
Site string `json:"site"`
}
func GetSeverity() string {
switch Urgency {
case "Low":
return "info"
case "Medium":
return "warning"
case "High":
return "error"
default:
return "error" // This is a fallback in case Urgency has an unexpected value
}
}
func SendAlert() {
// Fetch registry values
reg, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.QUERY_VALUE)
if err != nil {
fmt.Println(err)
return
}
defer reg.Close()
agentID, _, err := reg.GetStringValue("agentID")
if err != nil {
fmt.Println(err)
return
}
agentPKStr, _, err := reg.GetStringValue("AgentPK")
if err != nil {
fmt.Println(err)
return
}
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
return
}
baseURL, _, err := reg.GetStringValue("BaseURL")
if err != nil {
fmt.Println(err)
return
}
BaseURL = baseURL
// Convert agentPKStr to int
agentPK, err := strconv.Atoi(agentPKStr)
if err != nil {
fmt.Println("Failed to convert AgentPK to int:", err)
return
}
// Prepare data
data := AlertData{
AlertType: "custom",
Severity: GetSeverity(),
Agent: agentPK,
Message: fmt.Sprintf("%s has requested assistance. Their issue: "+Problem, hostname),
Hostname: hostname,
AgentID: agentID,
Client: "Demo",
Site: "Demo",
}
url := BaseURL + "/alerts/"
// Send the POST request
payload, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
return
}
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("X-API-KEY", ApiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Check the response
if resp.StatusCode == http.StatusOK {
fmt.Println("Alert created successfully:", resp)
} else {
fmt.Printf("Failed to create alert: %d %s\n", resp.StatusCode, resp.Status)
}
}