first initial two checks working now: age and count for snapshots

main
parent 31bd1d5805
commit 3cada8ac1f
  1. 43
      age.go
  2. 1
      count.go
  3. 2
      snapshots.go

@ -3,28 +3,61 @@ package main
import (
"fmt"
"github.com/apcera/libretto/virtualmachine/gridscale"
"time"
)
type AgeCommand struct {
Critical int `short:"c" description:"Critical value" optional:"no"`
Warning int `short:"w" description:"Warning value" optional:"no"`
Critical int `short:"c" description:"Critical value" required:"yes"`
Warning int `short:"w" description:"Warning value" required:"yes"`
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 ageCommand AgeCommand
func (x *AgeCommand) Execute(args []string) error {
fmt.Println("age")
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")
}
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 int, critical int) error {
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("")
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshot.CreateTime))
// never reached
return nil

@ -14,7 +14,6 @@ type CountCommand struct {
var countCommand CountCommand
func (x *CountCommand) Execute(args []string) error {
fmt.Printf("%s", countCommand.StorageUUID)
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {

@ -5,7 +5,7 @@ import (
)
type SnapshotsCommand struct {
Age struct {} `command:"age"`
Age AgeCommand `command:"age"`
Count CountCommand `command:"count"`
}

Loading…
Cancel
Save