-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 2 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
func main() {
botToken := flag.String("bot_token", "", "Telegram Bot Token")
chatID := flag.String("chat_id", "", "Telegram Chat ID")
healthURL := flag.String("health_url", "", "Health Check URL")
message := flag.String("message", "⚠️ *Attention!* The server is not responding properly.", "Error message to send")
expectedStatus := flag.String("expected_status", "OK", "Expected status response")
timeout := flag.Int("timeout", 5, "HTTP client timeout in seconds")
flag.Parse()
client := &http.Client{
Timeout: time.Duration(*timeout) * time.Second,
}
resp, err := client.Get(*healthURL)
if err != nil {
sendTelegramMessage(*botToken, *chatID, *message)
log.Fatalf("Error fetching health URL: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
sendTelegramMessage(*botToken, *chatID, *message)
log.Fatalf("Error reading response body: %v", err)
}
status := strings.TrimSpace(string(body))
if status != *expectedStatus {
sendTelegramMessage(*botToken, *chatID, *message)
log.Fatalf("Unexpected status: got %s, expected %s", status, *expectedStatus)
}
fmt.Println("Server is healthy.")
}
func sendTelegramMessage(botToken, chatID, message string) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
payload := fmt.Sprintf(`{"chat_id":"%s","text":"%s","parse_mode":"Markdown"}`, chatID, message)
req, err := http.NewRequest("POST", url, strings.NewReader(payload))
if err != nil {
log.Printf("Error creating Telegram request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error sending Telegram message: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("Non-OK response from Telegram: %s", string(body))
}
}