raidcheck/main.go
2023-07-19 17:35:51 +02:00

50 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"time"
"git.milar.in/milarin/adverr"
"git.milar.in/milarin/envvars/v2"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
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)
)
func main() {
if BotToken == "" {
fmt.Fprintln(os.Stderr, "$TELEGRAM_API_TOKEN not set")
os.Exit(1)
}
if ChatID == 0 {
fmt.Fprintln(os.Stderr, "$TELEGRAM_CHAT_ID not set")
os.Exit(1)
}
bot, err := tgbotapi.NewBotAPI(BotToken)
if err != nil {
adverr.Fatalln(err, 1)
}
for state := range filterChanges(mdstat(MdstatFile, PollInterval)) {
str := state.String()
fmt.Println(str)
message := tgbotapi.NewMessage(ChatID, fmt.Sprintf("Der Zustand des RAID Systems %s hat sich verändert:\n\n%s", state.Name, str))
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),
})
if _, err := bot.Send(message); err != nil {
adverr.Println(err)
}
}
}