Browse Source

Initial commit

tags/v0.1.0
Russ Garrett 7 years ago
parent
commit
aa6b4a9ef4
No account linked to committer's email address
5 changed files with 82 additions and 4 deletions
  1. 3
    0
      README.md
  2. 67
    0
      httplistener/httplistener.go
  3. 7
    2
      irccat.json
  4. 4
    1
      main.go
  5. 1
    1
      tcplistener/tcplistener.go

+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+A hasty re-implementation of [irccat](https://github.com/RJ/irccat) in Go.
2
+
3
+Supports HTTP POST endpoints as well as the traditional TCP protocol.

+ 67
- 0
httplistener/httplistener.go View File

@@ -0,0 +1,67 @@
1
+package httplistener
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"github.com/juju/loggo"
8
+	"github.com/spf13/viper"
9
+	"github.com/thoj/go-ircevent"
10
+	"net/http"
11
+)
12
+
13
+var log = loggo.GetLogger("HTTPListener")
14
+
15
+type HTTPListener struct {
16
+	http http.Server
17
+	irc  *irc.Connection
18
+}
19
+
20
+func New(irc *irc.Connection) (*HTTPListener, error) {
21
+	hl := HTTPListener{}
22
+	hl.irc = irc
23
+	hl.http = http.Server{Addr: viper.GetString("http.listen")}
24
+
25
+	mux := http.NewServeMux()
26
+	if viper.IsSet("http.listeners.grafana") {
27
+		mux.HandleFunc("/grafana", hl.grafanaAlertHandler)
28
+	}
29
+
30
+	hl.http.Handler = mux
31
+	log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
32
+	go hl.http.ListenAndServe()
33
+	return &hl, nil
34
+}
35
+
36
+type grafanaMatch struct {
37
+	Metric string
38
+	Value  float32
39
+}
40
+
41
+type grafanaAlert struct {
42
+	Title       string
43
+	RuleName    string
44
+	RuleUrl     string
45
+	State       string
46
+	ImageUrl    string
47
+	Message     string
48
+	EvalMatches []grafanaMatch
49
+}
50
+
51
+func (hl *HTTPListener) grafanaAlertHandler(w http.ResponseWriter, request *http.Request) {
52
+	if request.Method != "POST" {
53
+		http.NotFound(w, request)
54
+		return
55
+	}
56
+
57
+	var alert grafanaAlert
58
+	buf := new(bytes.Buffer)
59
+	buf.ReadFrom(request.Body)
60
+	json.Unmarshal(buf.Bytes(), &alert)
61
+	msg := fmt.Sprintf("[Grafana] [%s] %s: %s.", alert.State, alert.RuleName, alert.Message)
62
+	for _, match := range alert.EvalMatches {
63
+		msg += fmt.Sprintf(" %s:%f", match.Metric, match.Value)
64
+	}
65
+	msg += " " + alert.RuleUrl
66
+	hl.irc.Privmsgf(viper.GetString("http.listeners.grafana"), msg)
67
+}

+ 7
- 2
irccat.json View File

@@ -1,11 +1,16 @@
1 1
 {
2 2
   "tcp_listen": ":12345",
3
-  "http_listen": ":8045",
3
+  "http": {
4
+    "listen": ":8045",
5
+    "listeners": {
6
+      "grafana": "#russtest"
7
+    }
8
+  },
4 9
   "irc": {
5 10
     "server": "irc.irccloud.com:6667",
6 11
     "tls": false,
7 12
     "tls_skip_verify": true,
8
-    "nick": "gocat",
13
+    "nick": "irccat2",
9 14
     "realname": "IRCCat",
10 15
     "channels": ["#russtest"]
11 16
   }

+ 4
- 1
main.go View File

@@ -2,6 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"crypto/tls"
5
+	"github.com/irccloud/irccat/httplistener"
5 6
 	"github.com/irccloud/irccat/tcplistener"
6 7
 	"github.com/juju/loggo"
7 8
 	"github.com/spf13/viper"
@@ -29,7 +30,7 @@ func main() {
29 30
 
30 31
 	irccat := IRCCat{}
31 32
 
32
-	irccat.tcp, err = tcplistener.Bind()
33
+	irccat.tcp, err = tcplistener.New()
33 34
 	if err != nil {
34 35
 		return
35 36
 	}
@@ -41,6 +42,8 @@ func main() {
41 42
 		return
42 43
 	}
43 44
 
45
+	httplistener.New(irccat.irc)
46
+
44 47
 	irccat.tcp.Run(irccat.irc)
45 48
 	irccat.irc.Loop()
46 49
 }

+ 1
- 1
tcplistener/tcplistener.go View File

@@ -16,7 +16,7 @@ type TCPListener struct {
16 16
 	irc    *irc.Connection
17 17
 }
18 18
 
19
-func Bind() (*TCPListener, error) {
19
+func New() (*TCPListener, error) {
20 20
 	var err error
21 21
 
22 22
 	listener := TCPListener{}

Loading…
Cancel
Save