initial works on a gridscale command

main
commit 60daed247a

@ -0,0 +1,7 @@
#
gridscale-check snapshots count -w 2 -c 1
gridscale-check snapshots age -w 1 -c 2

@ -0,0 +1,31 @@
package main
import (
"fmt"
"github.com/apcera/libretto/virtualmachine/gridscale"
)
type AgeCommand struct {
Critical int `short:"c" description:"Critical value" optional:"no"`
Warning int `short:"w" description:"Warning value" optional:"no"`
}
var ageCommand AgeCommand
func (x *AgeCommand) Execute(args []string) error {
fmt.Println("age")
return nil
}
func init() {
}
func checkAge(Snapshot gridscale.Snapshot, warning int, critical int) error {
Ok("")
// never reached
return nil
}

@ -0,0 +1,52 @@
package main
import (
"github.com/apcera/libretto/virtualmachine/gridscale"
"fmt"
)
type CountCommand struct {
Critical int `short:"c" description:"Critical value" optional:"no"`
Warning int `short:"w" description:"Warning value" optional:"no"`
StorageUUID string `short:"u" long:"uuid" description:"uuid of storage to check" optional:"no"`
}
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 {
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, countCommand.Warning, countCommand.Critical)
return nil
}
func init() {
}
func count(snapshots map[string]*gridscale.Snapshot, warning int, critical int) error {
if len(snapshots) < critical {
Critical("")
}
if len(snapshots) < warning {
Warning("");
}
Ok(fmt.Sprintf("%d snapshots exist", len(snapshots)))
// never reached
return nil
}

@ -0,0 +1,31 @@
package main
import (
"github.com/apcera/libretto/virtualmachine/gridscale"
"fmt"
)
type ListCommand struct {
}
var listCommand ListCommand
func (x *ListCommand) Execute(args []string) error {
storages, error := gridscale.GetStorages(Config.UserId, Config.Token)
if error != nil {
Unknown(fmt.Sprintf("Error while retrieving storages: %s", error))
}
for key, storage := range storages {
fmt.Printf("%s: %v\n\n", key, storage)
}
return nil
}
func init() {
}

@ -0,0 +1,73 @@
package main
import (
"os"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/spf13/viper"
"log"
)
type CheckCommand struct {
Snapshots SnapshotsCommand `command:"snapshots" description:"checks on snapshots"`
Storages StoragesCommand `command:"storages" description:"checks on storages"`
}
var Checker CheckCommand
var Config CheckConfig
type CheckConfig struct {
UserId string
Token string
}
func main() {
var parser = flags.NewParser(&Checker, flags.Default)
// look in check.toml for the config
viper.SetConfigName("check")
viper.AddConfigPath("config")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Config file not found or error parsing: %v", err)
}
Config.UserId = viper.GetString("gridscale.userid")
Config.Token = viper.GetString("gridscale.token")
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
Unknown("no subcommands specificed.")
}
func Ok(msg string) {
fmt.Println("OK - " + msg)
os.Exit(0)
}
func Warning(msg string) {
fmt.Println("WARNING - " + msg)
os.Exit(1)
}
func Critical(msg string) {
fmt.Println("CRITICAL - " + msg)
os.Exit(2)
}
func Unknown(msg string) {
fmt.Println("UNKNOWN - " + msg)
os.Exit(3)
}

@ -0,0 +1,18 @@
package main
import (
"fmt"
)
type SnapshotsCommand struct {
Age struct {} `command:"age"`
Count CountCommand `command:"count"`
}
var snapshotsCommand SnapshotsCommand
func (x *SnapshotsCommand) Execute(args []string) error {
fmt.Println("snapshots")
return nil
}

@ -0,0 +1,18 @@
package main
import (
"fmt"
)
type StoragesCommand struct {
//Age struct {} `command:"age"`
List ListCommand `command:"list"`
}
var storagesCommand StoragesCommand
func (x *StoragesCommand) Execute(args []string) error {
fmt.Println("storages")
return nil
}
Loading…
Cancel
Save