Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created March 7, 2017 19:25
Show Gist options
  • Select an option

  • Save fuzzy/dada06d5879de6940616372a0a682bff to your computer and use it in GitHub Desktop.

Select an option

Save fuzzy/dada06d5879de6940616372a0a682bff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import os
import sys
import time
import random
import socket
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--metric', help='Specify the metric string to use.', type=str)
parser.add_argument('--interval', help='Specify the interval in seconds.', type=int)
parser.add_argument('--dest', help='Specify the destination host:port.', type=str)
parser.add_argument('--count', help='Specify the numer of metrics to send.', type=int)
parser.add_argument('--value', help='Specify the metric value to send.', type=int)
args = parser.parse_args()
if None in (args.metric, args.interval, args.dest, args.count, args.value):
print 'Error, you screwed up, all arguments are mandatory.'
sys.exit(1)
host = ()
if ':' not in args.dest:
print 'Error, you must specify the destination as host:port'
sys.exit(1)
else:
d = args.dest.split(':')
host = (d[0], int(d[1]))
start = time.time() # - (args.count * args.interval)
count = 0
print 'Sending %d metrics %d seconds apart, to %s:%d with the format:\n%s' % (
args.count, args.interval, host[0], host[1], args.metric)
while count <= args.count:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(host)
sock.sendall('%s %d %d\n' % (args.metric, args.value, start))
sock.close()
count += 1
start += args.interval
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(args.interval)
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment