send data to graphite
import argparse
import json
+import socket
import dateutil
from django.conf import settings
@@ -14,21 +15,26 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('statsfile', type=argparse.FileType('r'), help=_('stats file to convert'))
- parser.add_argument('--graphite', action='store_const', const=True, default=False,
- help=_('graphite format'))
+ parser.add_argument('--graphite', type=str, help=_('graphite address'), default=None)
+ parser.add_argument('--graphite-port', type=int, default=2003, help=_('graphite port (default 2003)'))
- def _output_graphite(self, prefix, result, timestamp):
+ def _output_graphite(self, s, prefix, result, timestamp):
for name, value in result.items():
if isinstance(value, dict):
- self._output_graphite(prefix+name+'.', value, timestamp)
+ self._output_graphite(s, prefix+name+'.', value, timestamp)
continue
- print('%s%s %s %s' % (prefix, name, value, timestamp))
+ s.sendall(('%s%s %s %s\n' % (prefix, name, value, timestamp)).encode())
def handle(self, *args, **options):
data = json.load(options['statsfile'])
end_time = int(dateutil.parser.parse(data['end_date']).timestamp())
result = convert_stats(data)
if options['graphite']:
- self._output_graphite('c3nav.%s.' % settings.INSTANCE_NAME, result, end_time)
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ s.connect((options['graphite'], options['graphite_port']))
+ self._output_graphite(s, 'c3nav.%s.' % settings.INSTANCE_NAME, result, end_time)
+ finally:
+ s.close()
else:
print(json.dumps(result, indent=4))
