start moving towards gsclient-go
parent
e8107215d3
commit
1169b00074
23
main.go
23
main.go
|
@ -4,6 +4,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gridscale/gsclient-go"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
|
@ -24,6 +25,7 @@ var Config CheckConfig
|
|||
type CheckConfig struct {
|
||||
UserId string
|
||||
Token string
|
||||
Client *gsclient.Client
|
||||
}
|
||||
|
||||
func config(configFile string) {
|
||||
|
@ -41,6 +43,16 @@ func config(configFile string) {
|
|||
|
||||
Config.UserId = viper.GetString("gridscale.userid")
|
||||
Config.Token = viper.GetString("gridscale.token")
|
||||
|
||||
gsConfig := gsclient.NewConfiguration(
|
||||
"https://api.gridscale.io",
|
||||
Config.UserId,
|
||||
Config.Token,
|
||||
true,
|
||||
)
|
||||
|
||||
Config.Client = gsclient.NewClient(gsConfig)
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -50,12 +62,23 @@ func main() {
|
|||
if (os.Getenv("GRIDSCALE_USER") != "") && (os.Getenv("GRIDSCALE_TOKEN") != "") {
|
||||
Config.UserId = os.Getenv("GRIDSCALE_USER")
|
||||
Config.Token = os.Getenv("GRIDSCALE_TOKEN")
|
||||
|
||||
gsConfig := gsclient.NewConfiguration(
|
||||
"https://api.gridscale.io",
|
||||
Config.UserId,
|
||||
Config.Token,
|
||||
true,
|
||||
)
|
||||
|
||||
Config.Client = gsclient.NewClient(gsConfig)
|
||||
|
||||
} else {
|
||||
if checkFileExistance("config/check.toml") {
|
||||
config("config/check.toml")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
|
|
|
@ -4,7 +4,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
|
||||
"github.com/gridscale/gsclient-go"
|
||||
)
|
||||
|
||||
type ListServersCommand struct {
|
||||
|
@ -16,15 +16,14 @@ func (x *ListServersCommand) Execute(args []string) error {
|
|||
|
||||
config(Checker.ConfigFile)
|
||||
|
||||
serversResponse, error := gridscale.GetServers(Config.UserId, Config.Token)
|
||||
servers, error := Config.Client.GetServerList()
|
||||
|
||||
if error != nil {
|
||||
Unknown(fmt.Sprintf("Error while retrieving storages: %s", error))
|
||||
Unknown(fmt.Sprintf("Error while retrieving servers: %s", error))
|
||||
}
|
||||
|
||||
for key, server := range serversResponse.Servers {
|
||||
fmt.Printf("%s: %+v\n\n", key, server)
|
||||
|
||||
for key, server := range servers {
|
||||
fmt.Printf("%s: %+v\n\n", key, server.Properties)
|
||||
}
|
||||
|
||||
Ok((""))
|
||||
|
|
|
@ -5,7 +5,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
|
||||
"github.com/gridscale/gsclient-go"
|
||||
)
|
||||
|
||||
type ServersStatusCommand struct {
|
||||
|
@ -25,81 +25,81 @@ func (x *ServersStatusCommand) Execute(args []string) error {
|
|||
config(Checker.ConfigFile)
|
||||
|
||||
if x.Type == "server.status" {
|
||||
server, err := gridscale.GetServer(Config.UserId, Config.Token, x.ServerUUID)
|
||||
server, err := Config.Client.GetServer(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))
|
||||
if server.Properties.Status != x.Status {
|
||||
Critical(fmt.Sprintf("Server state critical - State required: '%s' - State found: '%s'", x.Status, server.Properties.Status))
|
||||
}
|
||||
|
||||
Ok(fmt.Sprintf("State required: '%s' - State found: '%s'", x.Status, server.Status))
|
||||
Ok(fmt.Sprintf("State required: '%s' - State found: '%s'", x.Status, server.Properties.Status))
|
||||
}
|
||||
|
||||
if x.Type == "firewall.template" {
|
||||
vm, err := gridscale.GetVM(Config.UserId, Config.Token, x.ServerUUID)
|
||||
|
||||
publicNet, err := Config.Client.GetNetworkPublic()
|
||||
if err != nil {
|
||||
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
||||
Unknown(fmt.Sprintf("%s: %s", "Error retrieving public network", err))
|
||||
}
|
||||
|
||||
for _, networkRelation := range vm.NetworksRelations {
|
||||
if networkRelation.FirewallTemplateUUID == x.Relation {
|
||||
Ok(fmt.Sprintf("Expected firewall template '%s' is active", networkRelation.FirewallTemplateUUID))
|
||||
}
|
||||
networkRel, err := Config.Client.GetServerNetwork(x.ServerUUID, publicNet.Properties.ObjectUUID)
|
||||
|
||||
if networkRel.FirewallTemplateUUID == x.Relation {
|
||||
Ok(fmt.Sprintf("Expected firewall template '%s' is active", networkRel.FirewallTemplateUUID))
|
||||
}
|
||||
|
||||
Critical(fmt.Sprintf("Expected firewall template '%s' is NOT active!", x.Relation))
|
||||
|
||||
}
|
||||
|
||||
if x.Type == "server.cores" {
|
||||
vm, err := gridscale.GetVM(Config.UserId, Config.Token, x.ServerUUID)
|
||||
server, err := Config.Client.GetServer(x.ServerUUID)
|
||||
|
||||
if err != nil {
|
||||
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
||||
}
|
||||
|
||||
if x.Max == 0 {
|
||||
if vm.Server.Cores >= x.Min {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d) of cores assigned: %d", x.Min, vm.Server.Cores, vm.Server.Cores))
|
||||
if server.Properties.Cores >= x.Min {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d) of cores assigned: %d", x.Min, server.Properties.Cores, server.Properties.Cores))
|
||||
}
|
||||
} else {
|
||||
if vm.Server.Cores >= x.Min && vm.Server.Cores <= x.Max {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d <= %d) of cores assigned: %d", x.Min, vm.Server.Cores, x.Max, vm.Server.Cores))
|
||||
if server.Properties.Cores >= x.Min && server.Properties.Cores <= x.Max {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d <= %d) of cores assigned: %d", x.Min, server.Properties.Cores, x.Max, server.Properties.Cores))
|
||||
}
|
||||
}
|
||||
|
||||
Critical(fmt.Sprintf("Expect amount (%d <= %d <= %d) of cores NOT assigned: %d", x.Min, vm.Server.Cores, x.Max, vm.Server.Cores))
|
||||
Critical(fmt.Sprintf("Expect amount (%d <= %d <= %d) of cores NOT assigned: %d", x.Min, server.Properties.Cores, x.Max, server.Properties.Cores))
|
||||
}
|
||||
|
||||
if x.Type == "server.memory" {
|
||||
vm, err := gridscale.GetVM(Config.UserId, Config.Token, x.ServerUUID)
|
||||
server, err := Config.Client.GetServer(x.ServerUUID)
|
||||
|
||||
if err != nil {
|
||||
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
||||
}
|
||||
|
||||
if x.Max == 0 {
|
||||
if vm.Server.Memory >= x.Min {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d) of memory assigned: %d", x.Min, vm.Server.Memory, vm.Server.Memory))
|
||||
if server.Properties.Memory >= x.Min {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d) of memory assigned: %d", x.Min, server.Properties.Memory, server.Properties.Memory))
|
||||
}
|
||||
} else {
|
||||
if vm.Server.Memory >= x.Min && vm.Server.Memory <= x.Max {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d <= %d) of memory assigned: %d", x.Min, vm.Server.Memory, x.Max, vm.Server.Memory))
|
||||
if server.Properties.Memory >= x.Min && server.Properties.Memory <= x.Max {
|
||||
Ok(fmt.Sprintf("Expected amount (%d <= %d <= %d) of memory assigned: %d", x.Min, server.Properties.Memory, x.Max, server.Properties.Memory))
|
||||
}
|
||||
}
|
||||
|
||||
Critical(fmt.Sprintf("Expect amount (%d <= %d <= %d) of memory NOT assigned: %d", x.Min, vm.Server.Memory, x.Max, vm.Server.Memory))
|
||||
Critical(fmt.Sprintf("Expect amount (%d <= %d <= %d) of memory NOT assigned: %d", x.Min, server.Properties.Memory, x.Max, server.Properties.Memory))
|
||||
}
|
||||
|
||||
if x.Type == "ip.assigned" {
|
||||
vm, err := gridscale.GetVM(Config.UserId, Config.Token, x.ServerUUID)
|
||||
ipRel, err := Config.Client.GetServerIPList(x.ServerUUID)
|
||||
if err != nil {
|
||||
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
||||
}
|
||||
|
||||
for _, ipRelation := range vm.IPsRelations {
|
||||
for _, ipRelation := range ipRel {
|
||||
if ipRelation.IP == x.Comparator || ipRelation.ObjectUUID == x.Relation {
|
||||
Ok(fmt.Sprintf("Expected ip '%s' is assigned", ipRelation.IP))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue