gridscale-check/snapshots_age.go

86 lines
2.3 KiB
Go
Raw Normal View History

2018-04-24 23:55:15 +02:00
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
2018-04-22 00:07:06 +02:00
package main
import (
2020-03-26 20:01:50 +01:00
"context"
2018-04-22 00:07:06 +02:00
"fmt"
2018-04-25 00:21:15 +02:00
"time"
2020-03-26 20:01:50 +01:00
"github.com/gridscale/gsclient-go/v2"
2018-04-22 00:07:06 +02:00
)
2018-04-24 23:55:15 +02:00
type SnapshotsAgeCommand struct {
2018-04-25 00:21:15 +02:00
Critical int `short:"c" description:"Critical value" required:"yes" description:"Number of <unit> time the youngest snapshot is allowed to be before critical"`
Warning int `short:"w" description:"Warning value" required:"yes" description:"Number of <unit> time the youngest snapshot is allowed to be before warning"`
StorageUUID string `short:"u" long:"uuid" description:"uuid of storage to check" required:"yes"`
2018-04-25 00:21:15 +02:00
Unit string `short:"n" long:"unit" description:"unit: hours or days. default: hours" choice:"hours" choice:"days"`
2018-04-22 00:07:06 +02:00
}
2018-04-24 23:55:15 +02:00
var snapshotsAgeCommand SnapshotsAgeCommand
2018-04-22 00:07:06 +02:00
2018-04-24 23:55:15 +02:00
func (x *SnapshotsAgeCommand) Execute(args []string) error {
config(Checker.ConfigFile)
2020-03-26 20:01:50 +01:00
ctx := context.Background()
snapshotList, error := Config.Client.GetStorageSnapshotList(ctx, x.StorageUUID)
if error != nil {
Unknown("Error retrieving snapshots")
}
if snapshotList == nil {
Unknown("No snapshots found")
}
timestamp := snapshotList[0].Properties.CreateTime
storageSnapshot := gsclient.StorageSnapshot{}
2019-09-10 22:21:54 +02:00
for _, v := range snapshotList {
2020-03-26 20:01:50 +01:00
if v.Properties.CreateTime.After(timestamp.Time) {
timestamp = v.Properties.CreateTime
storageSnapshot = v
}
}
2018-04-25 00:21:15 +02:00
warningDuration := time.Duration(x.Warning) * time.Hour
criticalDuration := time.Duration(x.Critical) * time.Hour
if x.Unit == "days" {
2018-04-25 00:21:15 +02:00
warningDuration = warningDuration * 24
criticalDuration = criticalDuration * 24
}
checkAge(storageSnapshot, warningDuration, criticalDuration)
2018-04-22 00:07:06 +02:00
return nil
}
func init() {
}
func checkAge(snapshot gsclient.StorageSnapshot, warning time.Duration, critical time.Duration) error {
2020-03-26 20:01:50 +01:00
snapshotCreateTime, err := time.Parse("2019-09-02T12:44:48Z", snapshot.Properties.CreateTime.String())
if err != nil {
Unknown("Error converting CreateTime of snapshot")
}
nowAndW := time.Now().Local().Add(-warning)
nowAndC := time.Now().Local().Add(-critical)
2018-04-22 00:07:06 +02:00
if snapshotCreateTime.Before(nowAndC) {
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
}
2018-04-22 00:07:06 +02:00
if snapshotCreateTime.Before(nowAndW) {
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
}
2018-04-22 00:07:06 +02:00
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshotCreateTime))
2018-04-22 00:07:06 +02:00
// never reached
return nil
}