Go sendAlert()

Jmnote (토론 | 기여)님의 2024년 3월 28일 (목) 15:18 판 (새 문서: ==개요== ;Go sendAlert() <syntaxhighlight lang='go'> package main import ( "bytes" "encoding/json" "fmt" "net/http" ) func sendAlert(alertPayload map[string]interface{}) erro...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

Go sendAlert()
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 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}