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"
2019-09-10 22:12:56 +02:00
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" `
2018-04-22 12:14:10 +02:00
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 {
2018-07-04 01:12:17 +02:00
config ( Checker . ConfigFile )
2020-03-26 20:01:50 +01:00
ctx := context . Background ( )
snapshotList , error := Config . Client . GetStorageSnapshotList ( ctx , x . StorageUUID )
2018-04-22 12:14:10 +02:00
if error != nil {
Unknown ( "Error retrieving snapshots" )
}
2019-09-10 22:12:56 +02:00
if snapshotList == nil {
2018-04-25 13:49:48 +02:00
Unknown ( "No snapshots found" )
}
2018-04-22 12:14:10 +02:00
2019-09-10 22:12:56 +02:00
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 ) {
2019-09-10 22:12:56 +02:00
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
2018-04-22 12:14:10 +02:00
if x . Unit == "days" {
2018-04-25 00:21:15 +02:00
warningDuration = warningDuration * 24
criticalDuration = criticalDuration * 24
2018-04-22 12:14:10 +02:00
}
2019-09-10 22:12:56 +02:00
checkAge ( storageSnapshot , warningDuration , criticalDuration )
2018-04-22 12:14:10 +02:00
2018-04-22 00:07:06 +02:00
return nil
}
func init ( ) {
}
2019-09-10 22:12:56 +02:00
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 ( ) )
2019-09-10 22:12:56 +02:00
if err != nil {
Unknown ( "Error converting CreateTime of snapshot" )
}
2018-04-22 12:14:10 +02:00
nowAndW := time . Now ( ) . Local ( ) . Add ( - warning )
nowAndC := time . Now ( ) . Local ( ) . Add ( - critical )
2018-04-22 00:07:06 +02:00
2019-09-10 22:12:56 +02:00
if snapshotCreateTime . Before ( nowAndC ) {
Critical ( fmt . Sprintf ( "Snapshot is too old: %s" , snapshotCreateTime ) )
2018-04-22 12:14:10 +02:00
}
2018-04-22 00:07:06 +02:00
2019-09-10 22:12:56 +02:00
if snapshotCreateTime . Before ( nowAndW ) {
Warning ( fmt . Sprintf ( "Snapshot is too old: %s" , snapshotCreateTime ) )
2018-04-22 12:14:10 +02:00
}
2018-04-22 00:07:06 +02:00
2019-09-10 22:12:56 +02:00
Ok ( fmt . Sprintf ( "Snapshot is new enough: %s" , snapshotCreateTime ) )
2018-04-22 00:07:06 +02:00
// never reached
return nil
}