diff --git a/README.md b/README.md index c340d5b..82c108c 100644 --- a/README.md +++ b/README.md @@ -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" } ] ``` diff --git a/cmd/main.go b/cmd/main.go index 08be11a..71c177a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) diff --git a/commandOrEventType.go b/commandOrEventType.go index fc63bb2..1de68d2 100644 --- a/commandOrEventType.go +++ b/commandOrEventType.go @@ -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. diff --git a/example/orig.json b/example/orig.json deleted file mode 100644 index 0d1cd75..0000000 --- a/example/orig.json +++ /dev/null @@ -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" - } - } -] \ No newline at end of file diff --git a/example/orig-toCentral-sayHello.json b/example/toCentral-sayHello.json similarity index 50% rename from example/orig-toCentral-sayHello.json rename to example/toCentral-sayHello.json index d15457c..1959404 100644 --- a/example/orig-toCentral-sayHello.json +++ b/example/toCentral-sayHello.json @@ -2,7 +2,7 @@ { "toNode": "central", "data": [""], - "commandOrEvent":"eventNACK", - "method":"sayHello" + "commandOrEvent":"EventNACK", + "method":"SayHello" } ] \ No newline at end of file diff --git a/example/orig-toCentral.json b/example/toCentral.json similarity index 61% rename from example/orig-toCentral.json rename to example/toCentral.json index 243ed29..c0daf32 100644 --- a/example/orig-toCentral.json +++ b/example/toCentral.json @@ -2,7 +2,7 @@ { "toNode": "central", "data": ["some message sent from a ship for testing\n"], - "commandOrEvent":"eventACK", - "method":"textLogging" + "commandOrEvent":"EventACK", + "method":"TextLogging" } ] \ No newline at end of file diff --git a/example/orig-toShip1.json b/example/toShip1.json similarity index 63% rename from example/orig-toShip1.json rename to example/toShip1.json index 84ec9c6..abce93c 100644 --- a/example/orig-toShip1.json +++ b/example/toShip1.json @@ -3,8 +3,8 @@ "toNode": "ship1", "data": ["bash","-c","netstat -an|grep -i listen"], - "commandOrEvent":"commandACK", - "method":"shellCommand" + "commandOrEvent":"CommandACK", + "method":"ShellCommand" } ] \ No newline at end of file diff --git a/example/orig-toShip1and2.json b/example/toShip1and2.json similarity index 53% rename from example/orig-toShip1and2.json rename to example/toShip1and2.json index da3370e..f2d5d28 100644 --- a/example/orig-toShip1and2.json +++ b/example/toShip1and2.json @@ -2,8 +2,8 @@ { "toNode": "*", "data": ["bash","-c","tree ../"], - "commandOrEvent":"commandACK", - "method":"shellCommand" + "commandOrEvent":"CommandACK", + "method":"ShellCommand" } ] \ No newline at end of file diff --git a/example/orig-toShip2.json b/example/toShip2.json similarity index 54% rename from example/orig-toShip2.json rename to example/toShip2.json index a1929de..84651df 100644 --- a/example/orig-toShip2.json +++ b/example/toShip2.json @@ -2,8 +2,8 @@ { "toNode": "ship2", "data": ["bash","-c","tree ../"], - "commandOrEvent":"commandACK", - "method":"shellCommand" + "commandOrEvent":"CommandACK", + "method":"ShellCommand" } ] \ No newline at end of file diff --git a/incommmingBuffer.db b/incommmingBuffer.db index 6e0e7e4..3899734 100644 Binary files a/incommmingBuffer.db and b/incommmingBuffer.db differ diff --git a/prometheus.go b/prometheus.go index 2e26641..c79d82f 100644 --- a/prometheus.go +++ b/prometheus.go @@ -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) } diff --git a/publisher.go b/publisher.go index 70a689f..5fcb308 100644 --- a/publisher.go +++ b/publisher.go @@ -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) diff --git a/subscriberMethodTypes.go b/subscriberMethodTypes.go index 3592b74..15e9ba3 100644 --- a/subscriberMethodTypes.go +++ b/subscriberMethodTypes.go @@ -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 {