Skip to content

Commit

Permalink
added interval to not have message older than x
Browse files Browse the repository at this point in the history
  • Loading branch information
deltxprt committed Aug 27, 2023
1 parent ce30c99 commit db68883
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ var (
apikey string
ntfyUrl string
ntfyTopic string
interval int64
)

func getAlerts() ([]TruenasAlerts, error) {
//slog.Info("url: " + truenasUrl)
url := truenasUrl + "/api/v2.0/alert/list"
var nasAlerts []TruenasAlerts
//var currentAlerts []TruenasAlerts
token := "Bearer " + apikey
request, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down Expand Up @@ -116,12 +118,18 @@ func getAlerts() ([]TruenasAlerts, error) {
return nasAlerts, err
}

for i := 0; i < len(nasAlerts); i++ {
if nasAlerts[i].Datetime.Date >= interval {
nasAlerts = append(nasAlerts[:i], nasAlerts[i+1:]...)
}
}

return nasAlerts, nil
}

func CreateNtfyMessage(alerts TruenasAlerts) NtfyMessage {
alertTime := time.Unix(alerts.Datetime.Date/1000, 0).Format("2006/01/02 15:04:05")
lastOccTime := time.Unix(alerts.LastOccurrence.Date/1000, 0).Format("2006/01/02 15:04:05")
alertTime := time.UnixMilli(alerts.Datetime.Date).Format("2006/01/02 15:04:05")
lastOccTime := time.UnixMilli(alerts.LastOccurrence.Date).Format("2006/01/02 15:04:05")
message := fmt.Sprintf(`
Level:%s
Time: %s
Expand Down Expand Up @@ -205,10 +213,16 @@ func sendNtfy(content NtfyMessage) error {
}

func main() {
currentTime := time.Now()
truenasUrl = os.Getenv("TRUENASURL")
apikey = os.Getenv("APIKEY")
ntfyUrl = os.Getenv("NTFYURL")
ntfyTopic = os.Getenv("TOPIC")
duration, err := time.ParseDuration(os.Getenv("INTERVAL"))
if err != nil {
slog.Error("unable to parse time", err)
}
interval = currentTime.Add(-duration).UnixMilli()

//slog.Info("sending to " + ntfyUrl + " with topic: " + ntfyTopic)
alerts, err := getAlerts()
Expand Down

0 comments on commit db68883

Please sign in to comment.