1 개요[ | ]
- Go sendAlert()
Go
Copy
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func sendAlert(alertPayload map[string]interface{}) error {
alertManagerURL := "http://<alertmanager-host>:<alertmanager-port>/api/v2/alerts"
alertJSON, err := json.Marshal(alertPayload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", alertManagerURL, bytes.NewBuffer(alertJSON))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func main() {
alertPayload := map[string]interface{}{
"labels": map[string]string{
"alertname": "High CPU Usage",
"instance": "server1",
"severity": "critical",
},
"annotations": map[string]string{
"summary": "CPU usage is above 90%.",
"details": "Investigate the process causing high CPU usage.",
"runbook": "https://example.com/runbook/cpu-usage",
},
}
err := sendAlert(alertPayload)
if err != nil {
fmt.Printf("Error sending alert: %v\n", err)
return
}
fmt.Println("Alert sent successfully!")
}
2 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.