|
|
|
@ -4,8 +4,9 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gridscale/gsclient-go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SnapshotsAgeCommand struct {
|
|
|
|
@ -21,21 +22,25 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
|
|
|
|
|
|
|
|
|
|
config(Checker.ConfigFile)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
snapshotList, error := Config.Client.GetStorageSnapshotList(x.StorageUUID)
|
|
|
|
|
|
|
|
|
|
if error != nil {
|
|
|
|
|
Unknown("Error retrieving snapshots")
|
|
|
|
|
}
|
|
|
|
|
if snapshot == nil {
|
|
|
|
|
if snapshotList == nil {
|
|
|
|
|
Unknown("No snapshots found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timestamp := snapshotList[0].Properties.CreateTime
|
|
|
|
|
storageSnapshot := gsclient.StorageSnapshot{}
|
|
|
|
|
|
|
|
|
|
for _,v := range snapshotList {
|
|
|
|
|
if v.Properties.CreateTime > timestamp {
|
|
|
|
|
timestamp = v.Properties.CreateTime
|
|
|
|
|
storageSnapshot = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warningDuration := time.Duration(x.Warning) * time.Hour
|
|
|
|
|
criticalDuration := time.Duration(x.Critical) * time.Hour
|
|
|
|
|
|
|
|
|
@ -44,7 +49,7 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
|
|
|
|
|
criticalDuration = criticalDuration * 24
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkAge(*snapshot, warningDuration, criticalDuration)
|
|
|
|
|
checkAge(storageSnapshot, warningDuration, criticalDuration)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
@ -52,20 +57,26 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
|
|
|
|
|
func init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkAge(snapshot gridscale.Snapshot, warning time.Duration, critical time.Duration) error {
|
|
|
|
|
func checkAge(snapshot gsclient.StorageSnapshot, warning time.Duration, critical time.Duration) error {
|
|
|
|
|
|
|
|
|
|
snapshotCreateTime, err := time.Parse("2019-09-02T12:44:48Z", snapshot.Properties.CreateTime)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
Unknown("Error converting CreateTime of snapshot")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 snapshotCreateTime.Before(nowAndC) {
|
|
|
|
|
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if snapshot.CreateTime.Before(nowAndW) {
|
|
|
|
|
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshot.CreateTime))
|
|
|
|
|
if snapshotCreateTime.Before(nowAndW) {
|
|
|
|
|
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshot.CreateTime))
|
|
|
|
|
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshotCreateTime))
|
|
|
|
|
|
|
|
|
|
// never reached
|
|
|
|
|
return nil
|
|
|
|
|