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.
gridscale-check/snapshots_count.go

56 lines
1.5 KiB
Go

// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"fmt"
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
)
type SnapshotsCountCommand struct {
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"`
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
}