2018-04-24 23:55:15 +02:00
|
|
|
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-25 00:13:51 +02:00
|
|
|
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
|
2018-04-24 23:55:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SnapshotsCountCommand struct {
|
2018-04-25 00:21:15 +02:00
|
|
|
Critical int `short:"c" description:"Critical value" required:"yes" description:"how few snapshots must only exist to count as critical"`
|
|
|
|
Warning int `short:"w" description:"Warning value" required:"yes" description:"how few snapshots must only exist to count as warning"`
|
2018-04-24 23:55:15 +02:00
|
|
|
StorageUUID string `short:"u" long:"uuid" description:"uuid of storage to check" optional:"no"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var snapshotsCountCommand SnapshotsCountCommand
|
|
|
|
|
|
|
|
func (x *SnapshotsCountCommand) 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshots, error := storage.GetSnapshots(Config.UserId, Config.Token)
|
|
|
|
|
|
|
|
if error != nil {
|
|
|
|
Unknown("Error retrieving snapshots")
|
|
|
|
}
|
|
|
|
|
|
|
|
count(snapshots, x.Warning, x.Critical)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func count(snapshots map[string]*gridscale.Snapshot, warning int, critical int) error {
|
|
|
|
|
|
|
|
snapshotCount := len(snapshots)
|
|
|
|
|
|
|
|
if snapshotCount < critical {
|
|
|
|
Critical(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotCount))
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapshotCount < warning {
|
|
|
|
Warning(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotCount))
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(fmt.Sprintf("%d snapshots exist", len(snapshots)))
|
|
|
|
|
|
|
|
// never reached
|
|
|
|
return nil
|
|
|
|
}
|