forked from dschutterop/Graphite-graylog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graylog_notifications.py
100 lines (79 loc) · 2.51 KB
/
graylog_notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
#
# == Synopsis
#
# This script returns notifications from Graylog
# And pushes them into Graphite
#
# === Workflow
# This script grabs JSON from a Graylog host,
# transforms data into valid Carbon metrics
# and delivers it into carbon
#
# Carbon only needs three things:
# <metric> <value> <timestamp>
#
# So what we do is grab the Graylog
# key as the metric, grab the Graylog value
# as the value and make up our own timestamp. Well,
# actually, that's done through time()
#
# Author : D. Schutterop
# Email : daniel@schutterop.nl
# Version : v0.1
#
# === HowTo
#
# Please replace the value of grayHost with the hostname
# or IP address of one of your Graylog hosts
# (or DNS RR, load balanced addresses or whatever)
#
# Replace the value of grpHost (and port) of the Carbon
# server and, if you want, change the grpDatabase to
# something that makes sense to you.
#
# Fire the script and see the data appear in Graphite
# (Creation of the database files may take some time...
#
#
import json,requests,time,socket,os,sys
runInterval = 15
grayHost = 'graylog-cluster.localdomain'
grayPort = 12900
grayUser = 'graphite' # Read only user!
grayPass = 'graphite'
grpHost = 'graphite.localdomain'
grpPort = 2003
grpDatabase = 'graylog'
#Suppress SSL warnings generated when contacting Foreman
requests.packages.urllib3.disable_warnings()
def grayGetData(grayHost,grayPort):
grayUrl = "http://%s:%s/system/notifications" % (grayHost,grayPort)
grayHeaders = {'Content-type': 'application/json'}
grayRequest = requests.get(grayUrl, verify=False, auth=(grayUser,grayPass), headers=grayHeaders)
return json.loads(grayRequest.text)
def grpPutMessage(grpMetricKey,grpMetricValue):
metricPrepend = grpDatabase
metricAppend = grpMetricKey
metricKey = "%s.%s" % (metricPrepend,grpMetricKey)
metricTime = int(time.time())
metricValue = grpMetricValue
return "%s %s %s" % (metricKey,metricValue,metricTime)
def run(runInterval):
while True:
grpSocket = socket.socket()
grpSocket.connect((grpHost,grpPort))
grayData = grayGetData(grayHost,grayPort)
message = ' '
for listItem in grayData:
message = "\n %s %s" % (grpPutMessage(listItem,grayData[listItem]),message)
message = "%s \n" % (message)
grpSocket.sendall(message)
grpSocket.close()
time.sleep(runInterval)
if __name__ == "__main__":
procPid = os.fork()
if procPid != 0:
sys.exit(0)
print ("Running %s every %s seconds in the background." % (__file__,runInterval))
run(runInterval)