gridscale-check/servers_status.go

119 lines
3.9 KiB
Go
Raw Normal View History

2018-04-24 23:55:15 +02:00
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
2020-03-26 20:01:50 +01:00
"context"
2018-04-24 23:55:15 +02:00
"fmt"
)
type ServersStatusCommand struct {
Comparator string `short:"c" long:"comp" description:"value to compare with" required:"no"`
Max int `short:"n" long:"max" description:"max value" required:"no"`
Min int `short:"m" long:"min" description:"minimal value" required:"no"`
2018-05-14 11:44:09 +02:00
Relation string `short:"r" long:"relation" description:"relation uuid to check" required:"no"`
2018-04-24 23:55:15 +02:00
ServerUUID string `short:"u" long:"uuid" description:"uuid of server to check" required:"yes"`
2018-05-14 11:44:09 +02:00
Status string `short:"s" long:"status" description:"value of status to compare against" choice:"active" choice:"deleted" required:"no"`
Type string `short:"t" long:"type" description:"type to check for state" required:"yes"`
2018-04-24 23:55:15 +02:00
}
var serversStatusCommand ServersStatusCommand
func (x *ServersStatusCommand) Execute(args []string) error {
config(Checker.ConfigFile)
2020-03-26 20:01:50 +01:00
ctx := context.Background()
2018-05-14 11:44:09 +02:00
if x.Type == "server.status" {
2020-03-26 20:01:50 +01:00
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
2018-05-14 11:44:09 +02:00
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
}
2018-04-24 23:55:15 +02:00
2019-09-09 22:41:45 +02:00
if server.Properties.Status != x.Status {
Critical(fmt.Sprintf("Server state critical - State required: '%s' - State found: '%s'", x.Status, server.Properties.Status))
2018-05-14 11:44:09 +02:00
}
2018-04-24 23:55:15 +02:00
2019-09-09 22:41:45 +02:00
Ok(fmt.Sprintf("State required: '%s' - State found: '%s'", x.Status, server.Properties.Status))
2018-04-24 23:55:15 +02:00
}
2018-05-14 11:44:09 +02:00
if x.Type == "firewall.template" {
2019-09-09 22:41:45 +02:00
2020-03-26 20:01:50 +01:00
publicNet, err := Config.Client.GetNetworkPublic(ctx)
2018-05-14 11:44:09 +02:00
if err != nil {
2019-09-09 22:41:45 +02:00
Unknown(fmt.Sprintf("%s: %s", "Error retrieving public network", err))
2018-05-14 11:44:09 +02:00
}
2020-03-26 20:01:50 +01:00
networkRel, err := Config.Client.GetServerNetwork(ctx, x.ServerUUID, publicNet.Properties.ObjectUUID)
2019-09-09 22:41:45 +02:00
if networkRel.FirewallTemplateUUID == x.Relation {
Ok(fmt.Sprintf("Expected firewall template '%s' is active", networkRel.FirewallTemplateUUID))
2018-05-14 11:44:09 +02:00
}
Critical(fmt.Sprintf("Expected firewall template '%s' is NOT active!", x.Relation))
}
2018-04-24 23:55:15 +02:00
if x.Type == "server.cores" {
2020-03-26 20:01:50 +01:00
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
}
if x.Max == 0 {
2019-09-09 22:41:45 +02:00
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 {
2019-09-09 22:41:45 +02:00
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))
}
}
2019-09-09 22:41:45 +02:00
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" {
2020-03-26 20:01:50 +01:00
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
}
if x.Max == 0 {
2019-09-09 22:41:45 +02:00
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 {
2019-09-09 22:41:45 +02:00
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))
}
}
2019-09-09 22:41:45 +02:00
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" {
2020-03-26 20:01:50 +01:00
ipRel, err := Config.Client.GetServerIPList(ctx, x.ServerUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
}
2019-09-09 22:41:45 +02:00
for _, ipRelation := range ipRel {
if ipRelation.IP == x.Comparator || ipRelation.ObjectUUID == x.Relation {
Ok(fmt.Sprintf("Expected ip '%s' is assigned", ipRelation.IP))
}
}
Critical("Expected ip is NOT assigned!")
}
2018-04-24 23:55:15 +02:00
// never reached
return nil
}
func init() {
}