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.
119 lines
3.9 KiB
Go
119 lines
3.9 KiB
Go
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"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"`
|
|
Relation string `short:"r" long:"relation" description:"relation uuid to check" required:"no"`
|
|
ServerUUID string `short:"u" long:"uuid" description:"uuid of server to check" required:"yes"`
|
|
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"`
|
|
}
|
|
|
|
var serversStatusCommand ServersStatusCommand
|
|
|
|
func (x *ServersStatusCommand) Execute(args []string) error {
|
|
|
|
config(Checker.ConfigFile)
|
|
ctx := context.Background()
|
|
|
|
if x.Type == "server.status" {
|
|
|
|
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
|
|
if err != nil {
|
|
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
|
}
|
|
|
|
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.Properties.Status))
|
|
}
|
|
|
|
if x.Type == "firewall.template" {
|
|
|
|
publicNet, err := Config.Client.GetNetworkPublic(ctx)
|
|
if err != nil {
|
|
Unknown(fmt.Sprintf("%s: %s", "Error retrieving public network", err))
|
|
}
|
|
|
|
networkRel, err := Config.Client.GetServerNetwork(ctx, 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" {
|
|
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
|
|
|
|
if err != nil {
|
|
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
|
}
|
|
|
|
if x.Max == 0 {
|
|
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 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, server.Properties.Cores, x.Max, server.Properties.Cores))
|
|
}
|
|
|
|
if x.Type == "server.memory" {
|
|
server, err := Config.Client.GetServer(ctx, x.ServerUUID)
|
|
|
|
if err != nil {
|
|
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
|
}
|
|
|
|
if x.Max == 0 {
|
|
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 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, server.Properties.Memory, x.Max, server.Properties.Memory))
|
|
}
|
|
|
|
if x.Type == "ip.assigned" {
|
|
ipRel, err := Config.Client.GetServerIPList(ctx, x.ServerUUID)
|
|
if err != nil {
|
|
Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err))
|
|
}
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
// never reached
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
}
|