Browse Source

Add sensu support

master
Chris Smith 5 years ago
parent
commit
9d7a615775
5 changed files with 29 additions and 2 deletions
  1. 4
    0
      README.md
  2. 6
    0
      lineandsinker/services/__init__.py
  3. 2
    2
      lineandsinker/services/irccat.py
  4. 9
    0
      lineandsinker/services/sensu.py
  5. 8
    0
      main.py

+ 4
- 0
README.md View File

@@ -67,6 +67,10 @@ Environment variable | Description
67 67
 
68 68
 LaS can send messages to a ReportBot instance.
69 69
 
70
+### Sensu
71
+
72
+LaS accepts JSON events from Sensu. There are no configuration options.
73
+
70 74
 ### Slack
71 75
 
72 76
 LaS accepts Slack-style webhooks. There are no configuration options.

+ 6
- 0
lineandsinker/services/__init__.py View File

@@ -5,6 +5,7 @@ from .gitea import Gitea
5 5
 from .irccat import IrcCat
6 6
 from .jenkins import Jenkins
7 7
 from .reportbot import ReportBot
8
+from .sensu import Sensu
8 9
 from .slack import Slack
9 10
 
10 11
 services = {}
@@ -50,6 +51,11 @@ try:
50 51
 except:
51 52
     pass
52 53
 
54
+try:
55
+    services["sensu"] = Sensu()
56
+except:
57
+    pass
58
+
53 59
 try:
54 60
     services["slack"] = Slack()
55 61
 except:

+ 2
- 2
lineandsinker/services/irccat.py View File

@@ -10,9 +10,9 @@ class IrcCat(Service):
10 10
         self._port = port
11 11
         self._channel = channel
12 12
 
13
-    def announce(self, message):
13
+    def announce(self, message, channel=None):
14 14
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
15 15
         s.connect((self._host, self._port))
16 16
         for line in message.split("\n"):
17
-            s.sendall(f"{self._channel} {line}\n".encode())
17
+            s.sendall(f"{channel or self._channel} {line}\n".encode())
18 18
         s.close()

+ 9
- 0
lineandsinker/services/sensu.py View File

@@ -0,0 +1,9 @@
1
+from .service import Service
2
+
3
+
4
+class Sensu(Service):
5
+    def __init__(self):
6
+        super().__init__("sensu")
7
+
8
+    def accept_hook(self, identifier, request):
9
+        yield {"type": "sensu", **request.json}

+ 8
- 0
main.py View File

@@ -37,6 +37,14 @@ def handle_events(events):
37 37
             )
38 38
         elif event["type"] == "slack":
39 39
             services["irccat"].announce(f"\002[{event['source']}]\002 {event['text']}")
40
+        elif event["type"] == "sensu":
41
+            state = event["check"]["state"].upper()
42
+            if state == "FAILING":
43
+                state = f"\0034{state}\003"
44
+            services["irccat"].announce(
45
+                f"{event['check']['metadata']['name']} is now \002{state}\002\n{event['check']['output']}",
46
+                "#sensu",
47
+            )
40 48
 
41 49
 
42 50
 @app.route("/")

Loading…
Cancel
Save