-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (129 loc) · 5.65 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
// Config holds the configuration data (API URL, server settings, etc.)
type Config struct {
Server struct {
IP string `json:"ip"`
Port string `json:"port"`
Path string `json:"path"`
} `json:"server"`
PhoneNumbers []string `json:"phone_numbers"`
URL string `json:"url"`
UserName string `json:"username"`
Password string `json:"password"`
}
// GrafanaAlert defines the structure for the incoming Grafana webhook payload
type GrafanaAlert struct {
Alerts []struct {
Title string `json:"title"`
Annotations struct {
Description string `json:"description"`
} `json:"annotations"`
} `json:"alerts"`
}
func main() {
// Read configuration file
configFile := "./config.json"
config, err := readConfig(configFile)
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
// Set up the HTTP handler for Grafana webhooks
http.HandleFunc(config.Server.Path, func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// Parse incoming JSON message (Grafana webhook)
var alertPayload GrafanaAlert
if err := json.NewDecoder(r.Body).Decode(&alertPayload); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Process each alert
for _, alert := range alertPayload.Alerts {
// Create the message by combining title and description
message := fmt.Sprintf("Title: %s\nDescription: %s", alert.Title, alert.Annotations.Description)
// Send the message to the destination API, passing each phone number individually
for _, phoneNumber := range config.PhoneNumbers {
if err := sendMessage(config, message, phoneNumber); err != nil {
http.Error(w, fmt.Sprintf("Error sending message: %v", err), http.StatusInternalServerError)
return
}
}
}
// Respond to the sender
w.WriteHeader(http.StatusOK)
w.Write([]byte("Alert titles and descriptions sent successfully"))
} else {
http.Error(w, "Invalid HTTP method", http.StatusMethodNotAllowed)
}
})
// Start the server with the IP and port from config
serverAddress := fmt.Sprintf("%s:%s", config.Server.IP, config.Server.Port)
log.Printf("Starting server at %s...", serverAddress)
log.Fatal(http.ListenAndServe(serverAddress, nil))
}
// readConfig reads the configuration file
func readConfig(filePath string) (Config, error) {
file, err := os.Open(filePath)
if err != nil {
return Config{}, err
}
defer file.Close()
var config Config
decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return Config{}, err
}
return config, nil
}
// sendMessage sends the title-description message along with a single phone number
func sendMessage(config Config, message, phoneNumber string) error {
// Create the JSON body for the request containing "Message" and "PhoneNumber"
body := map[string]string{
"Message": message,
"PhoneNumber": phoneNumber,
}
// Marshal the body into JSON
jsonBody, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal message: %v", err)
}
// Log the JSON body to the console (before sending it)
log.Printf("Sending request to destination API with body: %s", string(jsonBody))
// Create the HTTP POST request
req, err := http.NewRequest(http.MethodPost, config.URL, bytes.NewBuffer(jsonBody))
if err != nil {
return fmt.Errorf("failed to create HTTP request: %v", err)
}
// Set the content-type header
req.Header.Set("Content-Type", "application/json")
// Add the custom headers (UserName and Password)
req.Header.Set("UserName", config.UserName)
req.Header.Set("Password", config.Password)
// Send the request using http.DefaultClient
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send HTTP request: %v", err)
}
defer resp.Body.Close()
// Read the response (optional)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
// Log the response from the API for debugging purposes
log.Printf("Response from destination API: %s", string(respBody))
// Check for a successful response (200 OK)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received non-OK response: %d %s", resp.StatusCode, string(respBody))
}
return nil
}