raidcheck/main.go

50 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-12-21 20:51:12 +01:00
package main
import (
"fmt"
"os"
"time"
2023-07-19 17:35:51 +02:00
"git.milar.in/milarin/adverr"
"git.milar.in/milarin/envvars/v2"
2021-12-21 20:51:12 +01:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
2023-07-19 17:35:51 +02:00
MdstatFile = envvars.String("MDSTAT_FILE", "/proc/mdstat")
PollInterval = envvars.Duration("POLL_INTERVAL", time.Minute)
BotToken = envvars.String("TELEGRAM_API_TOKEN", "")
ChatID = envvars.Int64("TELEGRAM_CHAT_ID", 0)
2021-12-21 20:51:12 +01:00
)
func main() {
2023-07-19 17:35:51 +02:00
if BotToken == "" {
fmt.Fprintln(os.Stderr, "$TELEGRAM_API_TOKEN not set")
os.Exit(1)
2021-12-21 20:51:12 +01:00
}
2023-07-19 17:35:51 +02:00
if ChatID == 0 {
fmt.Fprintln(os.Stderr, "$TELEGRAM_CHAT_ID not set")
os.Exit(1)
2021-12-21 20:51:12 +01:00
}
2023-07-19 17:35:51 +02:00
bot, err := tgbotapi.NewBotAPI(BotToken)
2021-12-21 20:51:12 +01:00
if err != nil {
adverr.Fatalln(err, 1)
}
2023-07-19 17:35:51 +02:00
for state := range filterChanges(mdstat(MdstatFile, PollInterval)) {
2021-12-21 20:51:12 +01:00
str := state.String()
fmt.Println(str)
2023-07-19 17:35:51 +02:00
message := tgbotapi.NewMessage(ChatID, fmt.Sprintf("Der Zustand des RAID Systems %s hat sich verändert:\n\n%s", state.Name, str))
2021-12-21 20:51:12 +01:00
message.Entities = append(message.Entities, tgbotapi.MessageEntity{
Type: "code",
Offset: len("Der Zustand des RAID Systems %s hat sich verändert:\n\n"),
Length: len(str),
})
2023-07-19 17:35:51 +02:00
if _, err := bot.Send(message); err != nil {
2021-12-21 20:51:12 +01:00
adverr.Println(err)
}
}
}