You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gridscale-check/snapshots_age.go

70 lines
2.0 KiB
Go

// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"fmt"
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
"time"
)
type SnapshotsAgeCommand struct {
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"`
Unit string `short:"n" long:"unit" description:"unit: hours or days. default: hours" choice:"hours" choice:"days"`
}
var snapshotsAgeCommand SnapshotsAgeCommand
func (x *SnapshotsAgeCommand) Execute(args []string) error {
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving storage", err))
}
snapshot, error := storage.GetYoungestSnapshot(Config.UserId, Config.Token)
if error != nil {
Unknown("Error retrieving snapshots")
}
if snapshot == nil {
Unknown("No snapshots found")
}
warningDuration := time.Duration(x.Warning) * time.Hour
criticalDuration := time.Duration(x.Critical) * time.Hour
if x.Unit == "days" {
warningDuration = warningDuration * 24
criticalDuration = criticalDuration * 24
}
checkAge(*snapshot, warningDuration, criticalDuration)
return nil
}
func init() {
}
func checkAge(snapshot gridscale.Snapshot, warning time.Duration, critical time.Duration) error {
nowAndW := time.Now().Local().Add(-warning)
nowAndC := time.Now().Local().Add(-critical)
if snapshot.CreateTime.Before(nowAndC) {
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshot.CreateTime))
}
if snapshot.CreateTime.Before(nowAndW) {
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshot.CreateTime))
}
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshot.CreateTime))
// never reached
return nil
}