rewrite to a proper syntax and libary, this isn't php

parent b7e77159
import logging
import subprocess
import time
from calendar import timegm
from influxdb import InfluxDBClient
from .models import UrlRating
# https://docs.influxdata.com/influxdb/v1.3/tools/shell/#influx-arguments
# https://docs.influxdata.com/influxdb/v0.9/write_protocols/write_syntax/#http
# https://docs.influxdata.com/influxdb/v1.3/tools/api/#write
# http://influxdb-python.readthedocs.io/en/latest/examples.html
logger = logging.getLogger(__package__)
# create the smallest, most complete and lowest possible granulation for the data.
def update_stats():
url_ratings = UrlRating.objects.all()
client = InfluxDBClient("influxdb", 8086, "admin", "admin", "elger_test")
url_ratings = UrlRating.objects.all()
logger.info("Creating stats, this can take a while. Get a cup of tea and say hi to the cat.")
for url_rating in url_ratings:
for endpoint in url_rating.calculation['endpoints']:
for rating in endpoint['ratings']:
for organization in url_rating.url.organization.all():
tags = "url_rating,ip_versie=%s,port=%s,protocol=%s,scan_type=%s,url=%s,organization=%s," \
"organization_type=%s,country=%s" % (
endpoint['ip'], endpoint['port'], endpoint['protocol'], poorly_escape(rating['type']),
poorly_escape(url_rating.url.url),
poorly_escape(organization.name),
poorly_escape(organization.type.name),
poorly_escape(organization.country.name))
values = "low=%s,medium=%s,high=%s,points=%s" % (
rating['low'], rating['medium'], rating['high'], rating['points'])
utc_time = time.strptime(rating['last_scan'], "%Y-%m-%dT%H:%M:%S+00:00")
epoch_time = timegm(utc_time)
data = "%s %s %s" % (tags, values, epoch_time)
logger.debug(data)
subprocess.call(
['curl', '-s', '-XPOST', '"influxdb:8086/write?db=elger_test&precision=s"',
'--data-binary', data])
print("Jobs done.")
metrics = [
{
"measurement": "url_rating",
"tags": {
"ip_version": endpoint['ip'],
"port": endpoint['port'],
"protocol": endpoint['protocol'],
"scan_type": poorly_escape(rating['type']),
"url": poorly_escape(url_rating.url.url),
"organization": poorly_escape(organization.name),
"organization_type": poorly_escape(organization.type.name),
"country": poorly_escape(organization.country.name)
},
# epoch time, from isotime in UTC
"time": timegm(time.strptime(rating['last_scan'], "%Y-%m-%dT%H:%M:%S+00:00")),
"fields": {
"low": rating['low'],
"medium": rating['medium'],
"high": rating['high'],
"points": rating['points']
}
}
]
client.write_points(metrics)
logger.info("Done creating stats.")
return
......
......@@ -52,3 +52,4 @@ raven
# allow recursive comparison of dictionaries
deepdiff
influxdb
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment