diff --git a/servers_status.go b/servers_status.go index 3ebbc7f..6115c75 100644 --- a/servers_status.go +++ b/servers_status.go @@ -4,29 +4,49 @@ package main import ( "fmt" + "g.hazardous.org/fkr/libretto/virtualmachine/gridscale" ) type ServersStatusCommand struct { - Status string `short:"s" long:"status" description:"value of status to compare against" choice:"active" choice:"deleted" required:"yes"` + 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 { - server, err := gridscale.GetServer(Config.UserId, Config.Token, x.ServerUUID) + if x.Type == "server.status" { + server, err := gridscale.GetServer(Config.UserId, Config.Token, x.ServerUUID) + if err != nil { + Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err)) + } - 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.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)) } - Ok(fmt.Sprintf("State required: '%s' - State found: '%s'", x.Status, server.Status)) + if x.Type == "firewall.template" { + vm, err := gridscale.GetVM(Config.UserId, Config.Token, x.ServerUUID) + if err != nil { + Unknown(fmt.Sprintf("%s: %s", "Error retrieving server", err)) + } + + for _, networkRelation := range vm.NetworksRelations { + if networkRelation.FirewallTemplateUUID == x.Relation { + Ok(fmt.Sprintf("Expected firewall template '%s' is active", networkRelation.FirewallTemplateUUID)) + } + } + + Critical(fmt.Sprintf("Expected firewall template '%s' is NOT active!", x.Relation)) + + } // never reached return nil