Skip to content

Instantly share code, notes, and snippets.

@alex-leonhardt
Created June 16, 2016 22:00
Show Gist options
  • Save alex-leonhardt/643e40c7793d617a0fbdd91654ac4810 to your computer and use it in GitHub Desktop.
Save alex-leonhardt/643e40c7793d617a0fbdd91654ac4810 to your computer and use it in GitHub Desktop.
send random data to riemann for testing
import socket
import time
from functools import wraps
import bernhard
import sys
def wrap_riemann(metric, _host, client=bernhard.Client(), tags=['python']):
def riemann_decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
# host = socket.gethostname()
if not _host:
host = socket.gethostname()
else:
host = _host
started = time.time()
try:
response = f(*args, **kwargs)
except Exception as e:
client.send({'host': host,
'service': metric + ".exceptions",
'description': str(e),
'tags': tags + ['exception'],
'state': 'critical',
'metric': 1})
raise
duration = (time.time() - started)
duration = random.randrange(0, 1000)
client.send({'host': host,
'service': metric + ".time",
'description': 'time metric',
'tags': tags + ['duration'],
'state': 'ok',
'metric': duration})
return response
return decorated_function
return riemann_decorator
####### dummy data
import random
riemann = bernhard.Client(host='192.168.99.101')
def stuff(host='host1'):
@wrap_riemann('dummy', _host=host, client=riemann)
def send_metric():
time.sleep(1)
send_metric()
if __name__ == '__main__':
hostname = sys.argv[1]
while True:
try:
if len(sys.argv) > 1:
stuff(host='{0}'.format(hostname))
except bernhard.TransportError:
print ("Exception caught...")
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment