1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

Changed to upercase const values for command and events, and added choosing of prometheus port

This commit is contained in:
postmannen 2021-02-22 09:33:37 +01:00
parent 25de8982b2
commit e19d76a5fc
13 changed files with 55 additions and 70 deletions

View file

@ -81,20 +81,20 @@ Subject naming are case sensitive, and can not contain the space are the tab cha
Nodename: Are the hostname of the device. This do not have to be resolvable via DNS, it is just a unique name for the host to receive the message.
Command/Event: Are type of message sent. `command` or `event`. Description of the differences are mentioned earlier.\
Command/Event: Are type of message sent. `CommandACK`/`EventACK`/`CommandNACK`/`EventNACK`. Description of the differences are mentioned earlier.\
Info: The command/event which is called a MessageType are present in both the Subject structure and the Message structure. The reason for this is that it is used both in the naming of a subject, and in the message for knowing what kind of message it is and how to handle it.
Method: Are the functionality the message provide. Example could be `shellcommand` or `syslogforwarding`
Method: Are the functionality the message provide. Example could be `Shellcommand` or `Syslogforwarding`
##### Complete subject example
For syslog of type event to a host named "ship1"
`ship1.event.syslogforwarding`
`ship1.EventACK.Syslogforwarding`
and for a shell command of type command to a host named "ship2"
`ship2.command.shellcommand`
`ship2.CommandACK.Shellcommand`
## TODO
@ -146,6 +146,8 @@ One the nodes out there
`./steward --node="ship1"` & `./steward --node="ship1"` and so on.
Use the `--help` flag to get all possibilities.
### Sending messages with commands or events
Right now there are to types of messages.
@ -169,8 +171,8 @@ Example JSON for pasting a message of type command into the `inmsg.txt` file
"toNode": "ship1",
"data": ["bash","-c","ls -l ../"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
}
]
@ -184,16 +186,16 @@ To send specify more messages at once do
"toNode": "ship1",
"data": ["bash","-c","ls -l ../"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
},
{
"toNode": "ship2",
"data": ["bash","-c","ls -l ../"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
}
]
@ -212,8 +214,8 @@ The content of `inmsg.txt` will be erased as messages a processed.
{
"toNode": "central",
"data": ["some message sent from a ship for testing\n"],
"commandOrEvent":"eventACK",
"method":"textLogging"
"commandOrEvent":"EventACK",
"method":"TextLogging"
}
]
```

View file

@ -15,6 +15,7 @@ func main() {
nodeName := flag.String("node", "0", "some unique string to identify this Edge unit")
brokerAddress := flag.String("brokerAddress", "0", "the address of the message broker")
profilingPort := flag.String("profilingPort", "", "The number of the profiling port")
promHostAndPort := flag.String("promHostAndPort", ":2112", "host and port for prometheus listener, e.g. localhost:2112")
//isCentral := flag.Bool("isCentral", false, "used to indicate that this is the central master that will subscribe to error message subjects")
flag.Parse()
@ -27,7 +28,7 @@ func main() {
}
s, err := steward.NewServer(*brokerAddress, *nodeName)
s, err := steward.NewServer(*brokerAddress, *nodeName, *promHostAndPort)
if err != nil {
log.Printf("error: failed to connect to broker: %v\n", err)
os.Exit(1)

View file

@ -41,17 +41,17 @@ const (
// delivered back in the reply ack message.
// The message should contain the unique ID of the
// command.
CommandACK CommandOrEvent = "commandACK"
CommandACK CommandOrEvent = "CommandACK"
// Same as above, but No ACK.
CommandNACK CommandOrEvent = "commandNACK"
CommandNACK CommandOrEvent = "CommandNACK"
// Same as above, but No ACK
// Event, wait for and return the ACK message. This means
// that the command should be executed immediately
// and that we should get the confirmation if it
// was successful or not.
EventACK CommandOrEvent = "eventACK"
EventACK CommandOrEvent = "EventACK"
// Same as above, but No ACK.
EventNACK CommandOrEvent = "eventNACK"
EventNACK CommandOrEvent = "EventNACK"
// eventCommand, just wait for the ACK that the
// message is received. What action happens on the
// receiving side is up to the received to decide.

View file

@ -1,30 +0,0 @@
[
{
"subject":
{
"node":"ship1",
"messageType":"command",
"method":"shellcommand",
"domain":"shell"
},
"message":
{
"data": ["bash","-c","uname -a"],
"messageType":"Command"
}
},
{
"subject":
{
"node":"ship2",
"messageType":"command",
"method":"shellcommand",
"domain":"shell"
},
"message":
{
"data": ["bash","-c","uname -a"],
"messageType":"Command"
}
}
]

View file

@ -2,7 +2,7 @@
{
"toNode": "central",
"data": [""],
"commandOrEvent":"eventNACK",
"method":"sayHello"
"commandOrEvent":"EventNACK",
"method":"SayHello"
}
]

View file

@ -2,7 +2,7 @@
{
"toNode": "central",
"data": ["some message sent from a ship for testing\n"],
"commandOrEvent":"eventACK",
"method":"textLogging"
"commandOrEvent":"EventACK",
"method":"TextLogging"
}
]

View file

@ -3,8 +3,8 @@
"toNode": "ship1",
"data": ["bash","-c","netstat -an|grep -i listen"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
}
]

View file

@ -2,8 +2,8 @@
{
"toNode": "*",
"data": ["bash","-c","tree ../"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
}
]

View file

@ -2,8 +2,8 @@
{
"toNode": "ship2",
"data": ["bash","-c","tree ../"],
"commandOrEvent":"commandACK",
"method":"shellCommand"
"commandOrEvent":"CommandACK",
"method":"ShellCommand"
}
]

Binary file not shown.

View file

@ -2,6 +2,8 @@ package steward
import (
"fmt"
"log"
"net"
"net/http"
"github.com/prometheus/client_golang/prometheus"
@ -19,14 +21,17 @@ type metrics struct {
sayHelloNodes map[node]struct{}
// The channel to pass metrics that should be processed
metricsCh chan metricType
// host and port where prometheus metrics will be exported
hostAndPort string
}
// HERE:
func newMetrics() *metrics {
func newMetrics(hostAndPort string) *metrics {
m := metrics{
sayHelloNodes: make(map[node]struct{}),
}
m.metricsCh = make(chan metricType)
m.hostAndPort = hostAndPort
return &m
}
@ -100,6 +105,13 @@ func (s *server) startMetrics() {
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
//http.Handle("/metrics", promhttp.Handler())
//http.ListenAndServe(":2112", nil)
n, err := net.Listen("tcp", s.metrics.hostAndPort)
if err != nil {
log.Printf("error: failed to open prometheus listen port: %v\n", err)
}
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
http.Serve(n, m)
}

View file

@ -64,7 +64,7 @@ type server struct {
}
// newServer will prepare and return a server type
func NewServer(brokerAddress string, nodeName string) (*server, error) {
func NewServer(brokerAddress string, nodeName string, promHostAndPort string) (*server, error) {
conn, err := nats.Connect(brokerAddress, nil)
if err != nil {
log.Printf("error: nats.Connect failed: %v\n", err)
@ -82,7 +82,7 @@ func NewServer(brokerAddress string, nodeName string) (*server, error) {
logCh: make(chan []byte),
methodsAvailable: m.GetMethodsAvailable(),
commandOrEventAvailable: co.GetCommandOrEventAvailable(),
metrics: newMetrics(),
metrics: newMetrics(promHostAndPort),
}
return s, nil
@ -112,7 +112,7 @@ func (s *server) Start() {
// Start a subscriber for shellCommand messages
{
fmt.Printf("Starting shellCommand subscriber: %#v\n", s.nodeName)
sub := newSubject("shellCommand", CommandACK, s.nodeName)
sub := newSubject(ShellCommand, CommandACK, s.nodeName)
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
// fmt.Printf("*** %#v\n", proc)
go s.processSpawnWorker(proc)
@ -121,16 +121,16 @@ func (s *server) Start() {
// Start a subscriber for textLogging messages
{
fmt.Printf("Starting textlogging subscriber: %#v\n", s.nodeName)
sub := newSubject("textLogging", EventACK, s.nodeName)
sub := newSubject(TextLogging, EventACK, s.nodeName)
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
// fmt.Printf("*** %#v\n", proc)
go s.processSpawnWorker(proc)
}
// Start a subscriber for sayHello messages
// Start a subscriber for SayHello messages
{
fmt.Printf("Starting sayHello subscriber: %#v\n", s.nodeName)
sub := newSubject("sayHello", EventNACK, s.nodeName)
fmt.Printf("Starting SayHello subscriber: %#v\n", s.nodeName)
sub := newSubject(SayHello, EventNACK, s.nodeName)
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
// fmt.Printf("*** %#v\n", proc)
go s.processSpawnWorker(proc)

View file

@ -34,11 +34,11 @@ func (m Method) GetMethodsAvailable() MethodsAvailable {
const (
// Shell command to be executed via f.ex. bash
ShellCommand Method = "shellCommand"
ShellCommand Method = "ShellCommand"
// Send text logging to some host
TextLogging Method = "textLogging"
TextLogging Method = "TextLogging"
// Send Hello I'm here message
SayHello Method = "sayHello"
SayHello Method = "SayHello"
)
type MethodsAvailable struct {