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.
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gridscale/gsclient-go/v2"
|
|
)
|
|
|
|
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 {
|
|
|
|
config(Checker.ConfigFile)
|
|
|
|
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{}
|
|
|
|
for _, v := range snapshotList {
|
|
if v.Properties.CreateTime.After(timestamp.Time) {
|
|
timestamp = v.Properties.CreateTime
|
|
storageSnapshot = v
|
|
}
|
|
}
|
|
|
|
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(storageSnapshot, warningDuration, criticalDuration)
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
}
|
|
|
|
func checkAge(snapshot gsclient.StorageSnapshot, warning time.Duration, critical time.Duration) error {
|
|
|
|
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)
|
|
|
|
if snapshotCreateTime.Before(nowAndC) {
|
|
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
|
|
}
|
|
|
|
if snapshotCreateTime.Before(nowAndW) {
|
|
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
|
|
}
|
|
|
|
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshotCreateTime))
|
|
|
|
// never reached
|
|
return nil
|
|
}
|