1
1
''' Amlen exporter for prometheus '''
2
- import json
3
- import sys
4
- import time
5
- import requests
2
+ from json import loads
3
+ from time import sleep
4
+ from argparse import ArgumentParser
5
+ from requests import get
6
6
from prometheus_client import start_http_server , Metric , REGISTRY
7
7
8
8
class JsonServerCollector ():
@@ -12,9 +12,9 @@ def __init__(self, endpoint):
12
12
def collect (self ):
13
13
''' Collect metrics'''
14
14
try :
15
- response = json . loads (requests . get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
16
- except Exception as ex :
17
- print (f'Cannot make a request to { self ._endpoint } : { type (ex ).__name__ } ' )
15
+ response = loads (get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
16
+ except Exception as unknown_exception :
17
+ print (f'Error: Cannot request { self ._endpoint } : { type (unknown_exception ).__name__ } ' )
18
18
return None
19
19
20
20
metric = Metric ('amlen_server_connections' ,
@@ -79,9 +79,9 @@ def __init__(self, endpoint):
79
79
def collect (self ):
80
80
''' Collect metrics'''
81
81
try :
82
- response = json . loads (requests . get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
83
- except Exception as ex :
84
- print (f'Cannot make a request to { self ._endpoint } : { type (ex ).__name__ } ' )
82
+ response = loads (get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
83
+ except Exception as unknown_exception :
84
+ print (f'Error: Cannot request { self ._endpoint } : { type (unknown_exception ).__name__ } ' )
85
85
return None
86
86
memory = response ['Memory' ]
87
87
metric = Metric ('amlen_memory' , 'Memory metrics' , 'gauge' )
@@ -126,10 +126,10 @@ def collect(self):
126
126
metric = Metric ('amlen_subscription_message' ,
127
127
'Messages in subscriptions' , 'gauge' )
128
128
try :
129
- response = json . loads (requests . get (self ._endpoint , timeout = 10 , params = {})
129
+ response = loads (get (self ._endpoint , timeout = 10 , params = {})
130
130
.content .decode ('UTF-8' ))
131
- except Exception as ex :
132
- print (f'Cannot make a request to { self ._endpoint } : { type (ex ).__name__ } ' )
131
+ except Exception as unknown_exception :
132
+ print (f'Error: Cannot request { self ._endpoint } : { type (unknown_exception ).__name__ } ' )
133
133
return None
134
134
# Response example: { "Version":"v1", "Subscription":
135
135
# [{"SubName":"DemoSubscription","TopicString":"DemoTopic",
@@ -172,8 +172,8 @@ def collect(self):
172
172
yield metric
173
173
except KeyError :
174
174
print ('Error collecting Subscription data: No Subscription key' )
175
- except Exception as ex :
176
- print (f'Cannot create subscription metrics: { type (ex ).__name__ } ' )
175
+ except Exception as unknown_exception :
176
+ print (f'Error: Cannot create subscription metrics: { type (unknown_exception ).__name__ } ' )
177
177
return None
178
178
179
179
class JsonEndpointCollector ():
@@ -185,7 +185,7 @@ def collect(self):
185
185
186
186
try :
187
187
params = {'StatType' :'ReadMsgs' , 'SubType' :'History' , 'Duration' :6 }
188
- response = json . loads (requests . get (self ._endpoint , timeout = 10 , params = params )
188
+ response = loads (get (self ._endpoint , timeout = 10 , params = params )
189
189
.content .decode ('UTF-8' ))
190
190
metric = Metric ('amlen_endpoint_message_rate' ,
191
191
'Messages per second' , 'gauge' )
@@ -195,7 +195,7 @@ def collect(self):
195
195
metric .add_sample ('amlen_endpoint_message_rate_incoming' , {}, msg_rate )
196
196
yield metric
197
197
198
- response = json . loads (requests . get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
198
+ response = loads (get (self ._endpoint , timeout = 10 ).content .decode ('UTF-8' ))
199
199
endpoints = response ['Endpoint' ]
200
200
201
201
metric = Metric ('amlen_endpoint' , 'Endpoint counters' , 'counter' )
@@ -236,9 +236,9 @@ def collect(self):
236
236
#yield metric
237
237
238
238
except KeyError as keyerr :
239
- print (f'Error collecting Endpoint data: No Endpoint key { keyerr } ' )
240
- except Exception as ex :
241
- print (f'Cannot make a request to { self ._endpoint } : { type (ex ).__name__ } ' )
239
+ print (f'Error: collecting Endpoint data: No Endpoint key { keyerr } ' )
240
+ except Exception as unknown_exception :
241
+ print (f'Error: Cannot request { self ._endpoint } : { type (unknown_exception ).__name__ } ' )
242
242
243
243
244
244
class JsonInfoCollector ():
@@ -248,10 +248,10 @@ def __init__(self, endpoint):
248
248
def collect (self ):
249
249
''' Collect metrics'''
250
250
try :
251
- response = json . loads (requests . get (self ._endpoint , timeout = 10 )
251
+ response = loads (get (self ._endpoint , timeout = 10 )
252
252
.content .decode ('UTF-8' ))
253
- except Exception as ex :
254
- print (f'Cannot make a request to { self ._endpoint } : { type (ex ).__name__ } ' )
253
+ except Exception as unknown_exception :
254
+ print (f'Error: Cannot request { self ._endpoint } : { type (unknown_exception ).__name__ } ' )
255
255
return None
256
256
metric = Metric ('amlen_info' , 'Status metrics counters' , 'info' )
257
257
try :
@@ -275,14 +275,32 @@ def collect(self):
275
275
yield metric
276
276
return None
277
277
278
-
279
278
if __name__ == '__main__' :
280
279
# Usage: json_exporter.py port endpoint
281
- start_http_server (int (sys .argv [1 ]))
282
- REGISTRY .register (JsonServerCollector (sys .argv [2 ]))
283
- REGISTRY .register (JsonMemoryCollector (sys .argv [2 ]))
284
- REGISTRY .register (JsonEndpointCollector (sys .argv [2 ]))
285
- REGISTRY .register (JsonSubscriptionCollector (sys .argv [2 ]))
286
- REGISTRY .register (JsonInfoCollector (sys .argv [2 ]))
287
- while True :
288
- time .sleep (1 )
280
+ parser = ArgumentParser (description = 'Amlen Prometheus exporter' )
281
+ parser .add_argument ('port' , type = int , nargs = '?' ,
282
+ default = 9672 ,
283
+ help = 'This exporters\' port. Default: 9672' )
284
+ parser .add_argument ('amlen_address' , type = str , nargs = '?' ,
285
+ default = 'localhost:9089' ,
286
+ help = 'Address of Amlen server. Default: localhost:9089' )
287
+ parser .add_argument ('--once' , nargs = '?' , const = True , default = False ,
288
+ help = 'Run once instead of running server' )
289
+ args = parser .parse_args ()
290
+ start_http_server (args .port )
291
+ REGISTRY .register (JsonServerCollector (args .amlen_address ))
292
+ REGISTRY .register (JsonMemoryCollector (args .amlen_address ))
293
+ REGISTRY .register (JsonEndpointCollector (args .amlen_address ))
294
+ REGISTRY .register (JsonSubscriptionCollector (args .amlen_address ))
295
+ REGISTRY .register (JsonInfoCollector (args .amlen_address ))
296
+ try :
297
+ selfrequest = get (f'http://localhost:{ args .port } '
298
+ , timeout = 10 ).content .decode ('UTF-8' )
299
+ except Exception as ex :
300
+ print (f'Error: Cannot request localhost:{ args .port } : { type (ex ).__name__ } ' )
301
+
302
+ if args .once :
303
+ print (selfrequest )
304
+
305
+ while not args .once :
306
+ sleep (1 )
0 commit comments