first real working version

release/release-0.2.0
parent 3cada8ac1f
commit 9d978adbc9

@ -0,0 +1,23 @@
Copyright 2018 - Felix Kronlage <fkr@hazardous.org>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

@ -1,7 +1,39 @@
#
# gridscale-check
gridscale-check snapshots count -w 2 -c 1
A variety of commands to check various aspects of your IaaS-assetts at [gridscale](https://gridscale.io).
This check makes use of [libretto](https://github.com/apcera/libretto). However, since the
gridscale-support has not been merged yet, it imports libretto from here:
gridscale-check snapshots age -w 1 -c 2
[https://g.hazardous.org/fkr/libretto](https://g.hazardous.org/fkr/libretto)
## commands
### snapshots
```
$ gridscale-check snapshots count -w 2 -c 1 -u <uuid-of-storage>
```
```
$ gridscale-check snapshots age -w 1 -c 2 -u <uuid-of-storage>
```
### servers
```
$ gridscale-check servers state -s active -u <uuid-of-server>
```
### helper commands
There are some helper commands, such as listing servers and storages:
```
$ gridscale-check servers list
```
```
$ gridscale-check storages list
```

@ -1,51 +0,0 @@
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 {
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
}

@ -1,3 +1,5 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
@ -9,6 +11,7 @@ import (
)
type CheckCommand struct {
Servers ServersCommand `command:"servers" description:"checks on servers"`
Snapshots SnapshotsCommand `command:"snapshots" description:"checks on snapshots"`
Storages StoragesCommand `command:"storages" description:"checks on storages"`
}
@ -28,18 +31,23 @@ func main() {
var parser = flags.NewParser(&Checker, flags.Default)
// look in check.toml for the config
viper.SetConfigName("check")
viper.AddConfigPath("config")
if (os.Getenv("GRIDSCALE_USER") != "") && (os.Getenv("GRIDSCALE_TOKEN") != "") {
Config.UserId = os.Getenv("GRIDSCALE_USER")
Config.Token = os.Getenv("GRIDSCALE_TOKEN")
} else {
// 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)
}
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")
}
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)

@ -0,0 +1,15 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
type ServersCommand struct {
List ListServersCommand `command:"list"`
Status ServersStatusCommand `command:"status"`
}
var serversCommand StoragesCommand
func (x *ServersCommand) Execute(args []string) error {
return nil
}

@ -0,0 +1,36 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"github.com/apcera/libretto/virtualmachine/gridscale"
"fmt"
)
type ListServersCommand struct {
}
var listServersCommand ListServersCommand
func (x *ListServersCommand) Execute(args []string) error {
serversResponse, error := gridscale.GetServers(Config.UserId, Config.Token)
if error != nil {
Unknown(fmt.Sprintf("Error while retrieving storages: %s", error))
}
for key, server := range serversResponse.Servers {
fmt.Printf("%s: %v\n\n", key, server)
}
Ok((""))
// never reached
return nil
}
func init() {
}

@ -0,0 +1,38 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"github.com/apcera/libretto/virtualmachine/gridscale"
"fmt"
)
type ServersStatusCommand struct {
Status string `short:"s" long:"status" description:"value of status to compare against" choice:"active" choice:"deleted" required:"yes"`
ServerUUID string `short:"u" long:"uuid" description:"uuid of server to check" required:"yes"`
}
var serversStatusCommand ServersStatusCommand
func (x *ServersStatusCommand) Execute(args []string) error {
server, err := gridscale.GetServer(Config.UserId, Config.Token, x.ServerUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
}
if server.Status != x.Status {
Critical(fmt.Sprintf("Server state critical - State required: '%s' - State found: '%s'", x.Status, server.Status))
}
Ok(fmt.Sprintf("State required: '%s' - State found: '%s'", x.Status, server.Status))
// never reached
return nil
}
func init() {
}

@ -1,18 +1,15 @@
package main
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
import (
"fmt"
)
package main
type SnapshotsCommand struct {
Age AgeCommand `command:"age"`
Count CountCommand `command:"count"`
Age SnapshotsAgeCommand `command:"age" description:"commands relating to the age of snapshots"`
Count SnapshotsCountCommand `command:"count" description:"commands relating to the amount of snapshots"`
}
var snapshotsCommand SnapshotsCommand
func (x *SnapshotsCommand) Execute(args []string) error {
fmt.Println("snapshots")
return nil
}

@ -1,3 +1,5 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
@ -6,16 +8,16 @@ import (
"time"
)
type AgeCommand struct {
Critical int `short:"c" description:"Critical value" required:"yes"`
Warning int `short:"w" description:"Warning value" required:"yes"`
type SnapshotsAgeCommand struct {
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"`
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
var snapshotsAgeCommand SnapshotsAgeCommand
func (x *AgeCommand) Execute(args []string) error {
func (x *SnapshotsAgeCommand) Execute(args []string) error {
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {

@ -0,0 +1,55 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"github.com/apcera/libretto/virtualmachine/gridscale"
"fmt"
)
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
}

@ -1,3 +1,5 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
@ -6,7 +8,7 @@ import (
type StoragesCommand struct {
//Age struct {} `command:"age"`
List ListCommand `command:"list"`
List StorageListCommand `command:"list" description:"list the available storages"`
}
var storagesCommand StoragesCommand

@ -1,3 +1,5 @@
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
@ -5,12 +7,12 @@ import (
"fmt"
)
type ListCommand struct {
type StorageListCommand struct {
}
var listCommand ListCommand
var storageListCommand StorageListCommand
func (x *ListCommand) Execute(args []string) error {
func (x *StorageListCommand) Execute(args []string) error {
storages, error := gridscale.GetStorages(Config.UserId, Config.Token)
@ -23,6 +25,9 @@ func (x *ListCommand) Execute(args []string) error {
}
Ok((""))
// never reached
return nil
}
Loading…
Cancel
Save