forked from mozilla/briar-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpMetrics.py
186 lines (140 loc) · 6.05 KB
/
bpMetrics.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
""" bpMetrics
:copyright: (c) 2012 by Mozilla
:license: MPLv2
Assumes Python v2.6+
Usage
-c --config Configuration file (json format)
default: None
--address IP Address
127.0.0.1 or 127.0.0.1:5555
Required Value
-r --redis Redis server connection string
default: localhost:6379
--redisdb Redis database ID
default: 8
-d --debug Turn on debug logging
default: False
-l --logpath Path where the log file output is written
default: None
-b --background Fork to a daemon process
default: False
--graphite host:port where Graphite's carbon-cache service is running
Sample Configuration file
{ 'redis': 'localhost:6379',
'debug': True,
'logpath': '.'
}
Authors:
bear Mike Taylor <[email protected]>
"""
import os, sys
import json
import time
import socket
import logging
from Queue import Empty
from multiprocessing import Process, Queue, current_process, get_logger, log_to_stderr
import zmq
from releng import initOptions, initLogs, dbRedis
from releng.metrics import Metric
from releng.constants import PORT_METRICS, ID_METRICS_WORKER, \
METRICS_COUNT, METRICS_HASH, METRICS_KEY, METRICS_LIST, METRICS_SET
log = get_logger()
jobQueue = Queue()
def worker(jobQueue, graphite, db):
log.info('starting')
metrics = Metric(graphite, db)
while True:
try:
job = jobQueue.get(False)
except Empty:
job = None
if job is not None:
try:
jobs = json.loads(job)
for item in jobs:
metric, data = item
if metric == METRICS_COUNT:
group = data[0]
key = data[1]
metrics.count('count')
metrics.count(group)
metrics.count('%s.%s' % (group, key))
elif metric == METRICS_LIST:
metrics.count('list')
if len(data) == 2:
key = data[0]
value = data[1]
db.rpush(key, value)
elif metric == METRICS_SET:
metrics.count('set')
if len(data) == 2:
key = data[0]
value = data[1]
db.sadd(key, value)
elif metric == METRICS_HASH:
metrics.count('hash')
if len(data) == 3:
hash = data[0]
key = data[1]
value = data[2]
db.hset(hash, key, value)
db.sadd('metrics.hashes', hash)
except:
log.error('Error converting incoming job', exc_info=True)
metrics.check()
log.info('done')
_defaultOptions = { 'config': ('-c', '--config', None, 'Configuration file'),
'debug': ('-d', '--debug', True, 'Enable Debug', 'b'),
'background': ('-b', '--background', False, 'daemonize ourselves', 'b'),
'logpath': ('-l', '--logpath', None, 'Path where log file is to be written'),
'redis': ('-r', '--redis', 'localhost:6379', 'Redis connection string'),
'redisdb': ('', '--redisdb', '8', 'Redis database'),
'address': ('', '--address' , None, 'IP Address'),
'graphite': ('', '--graphite', None, "host:port where Graphite's carbon-cache service is running")
}
if __name__ == '__main__':
options = initOptions(params=_defaultOptions)
initLogs(options)
log.info('Starting')
if options.address is None:
log.error('Address is a required parameter, exiting')
sys.exit(2)
log.info('Connecting to datastore')
db = dbRedis(options)
if db.ping():
log.info('Creating processes')
Process(name='worker', target=worker, args=(jobQueue, options.graphite, db)).start()
if ':' not in options.address:
options.address = '%s:%s' % (options.address, PORT_METRICS)
log.debug('binding to tcp://%s' % options.address)
context = zmq.Context()
server = context.socket(zmq.ROUTER)
server.identity = '%s:%s' % (ID_METRICS_WORKER, options.address)
server.bind('tcp://%s' % options.address)
log.info('Adding %s to the list of active servers' % server.identity)
db.rpush(ID_METRICS_WORKER, server.identity)
while True:
try:
request = server.recv_multipart()
except:
log.error('error raised during recv_multipart()', exc_info=True)
break
# [ destination, sequence, control, payload ]
address, sequence, control = request[:3]
reply = [address, sequence]
if control == 'ping':
reply.append('pong')
else:
reply.append('ok')
jobQueue.put(request[3])
server.send_multipart(reply)
log.info('Removing ourselves to the list of active servers')
db.lrem(ID_METRICS_WORKER, 0, server.identity)
else:
log.error('Unable to reach the database')
log.info('done')