Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raw = 'raw-%s.json' % time.strftime('%Y-%m-%d')
self.update_query(config)
self.do_query(config['query'], raw)
self.copy(raw, os.path.join(config['metric'], raw))
consumer_error = False
for consumer in [self.jq_upload, self.influx_upload]:
try:
consumer(config, raw)
except (
ValueError,
KeyError,
IOError,
requests.exceptions.ConnectionError,
influxdb.client.InfluxDBClientError,
influxdb.client.InfluxDBServerError,
):
print(traceback.format_exc(), file=sys.stderr)
consumer_error = True
if consumer_error:
raise ValueError('Error(s) were thrown by query result consumers.')
# Correct issue #1530
if self.protocol is not None and (self.protocol.lower() == 'https'):
ssl = True
else:
ssl = False
try:
db = InfluxDBClient(host=self.host,
port=self.port,
ssl=ssl,
verify_ssl=False,
username=self.user,
password=self.password,
database=self.db)
get_all_db = [i['name'] for i in db.get_list_database()]
except InfluxDBClientError as e:
logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e))
sys.exit(2)
if self.db in get_all_db:
logger.info(
"Stats will be exported to InfluxDB server: {}".format(db._baseurl))
else:
logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db)
sys.exit(2)
return db
global influx_insert_buffer
influx_insert_buffer.append(self.data)
if len(influx_insert_buffer) >= settings.INFLUX_INSERT_BUFFER_COUNT:
client = influxdb.InfluxDBClient(
host=settings.INFLUX_DB_HOST,
port=settings.INFLUX_DB_PORT,
username=settings.INFLUX_DB_USERNAME,
password=settings.INFLUX_DB_PASSWORD
)
client.switch_database(settings.INFLUX_DB_UI_EVENTS_DB_NAME)
try:
client.write_points(influx_insert_buffer)
except influxdb.client.InfluxDBClientError:
client.create_database(settings.INFLUX_DB_UI_EVENTS_DB_NAME)
client.switch_database(settings.INFLUX_DB_UI_EVENTS_DB_NAME)
client.write_points(influx_insert_buffer)
influx_insert_buffer = []
client.close()
"value": value,
},
"tags": {
"hostName": hostName,
},
}
series.append(pointValues)
print(series)
client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)
print("Create database: " + DBNAME)
try:
client.create_database(DBNAME)
except InfluxDBClientError:
# Drop and create
client.drop_database(DBNAME)
client.create_database(DBNAME)
print("Create a retention policy")
retention_policy = 'server_data'
client.create_retention_policy(retention_policy, '3d', 3, default=True)
print("Write points #: {0}".format(total_records))
client.write_points(series, retention_policy=retention_policy)
time.sleep(2)
query = "SELECT MEAN(value) FROM {} WHERE \
time > now() - 10d GROUP BY time(500m)".format(metric)
result = client.query(query, database=DBNAME)
def __init__(self):
self.client = InfluxDBClient(
"influxsrv.hyperpilot", 8086, "root", "root", "be_controller")
try:
self.client.create_database("be_controller")
except InfluxDBClientError:
pass #Ignore
def action(self, event):
try:
data = self._data
data[0]['tags'] = event.dict
data[0]['fields'] = {"value": event.timing}
self.db.write_points(data)
except InfluxDBClientError as e:
self.logger.critical("Error InfluxDB '%s'" % str(e))
except Exception as e:
self.logger.critical("Error Unknow on InfluxDB '%s'" % str(e))
def metric_details(request, pk, format=None):
"""
Get or write metric values
"""
metric = get_object_or_404(Metric, pk=pk)
# get
if request.method == 'GET':
try:
results = metric.select(q=request.query_params.get('q', metric.query))
except InfluxDBClientError as e:
return Response({'detail': e.content}, status=e.code)
return Response(list(results.get_points(metric.name)))
# post
else:
if not request.data:
return Response({'detail': 'expected values in POST data or JSON payload'},
status=400)
data = request.data.copy()
# try converting strings to floats when sending form-data
if request.content_type != 'application/json':
for key, value in data.items():
try:
data[key] = float(value) if '.' in value else int(value)
except ValueError:
pass
# write
def get_result(q, conn=None, **kwargs):
if not conn:
conn = InfluxDBClient(**kwargs)
try:
result = conn.query(q.query())
except InfluxDBClientError:
raise
return parse_influxdb_response(result)