I am noticing what appears to be something that just doesn’t make sense to me.
By inserting a new entry to the database, it seems to cause all existing data to get wiped?
I have two python scripts. The first writes some historical data from a CSV file. The second polls an API and writes the current measurement.
When I run the script to write the current measurement, it causes all the previous data to vanish.
Here you can see me run the first script to write the measurements. And then I view the measurements to confirm they are in the DB.
[username@testbox scripts]$ python oldportcounts.py
[{'fields': {'ports': '2\r'}, 'time': '2017-03-16T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '50\r'}, 'time': '2017-03-17T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-18T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-19T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-20T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-21T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-22T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '62\r'}, 'time': '2017-03-23T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '23\r'}, 'time': '2017-03-24T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-25T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-26T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-27T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '2\r'}, 'time': '2017-03-28T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '10\r'}, 'time': '2017-03-29T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '4\r'}, 'time': '2017-03-30T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-03-31T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-01T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-02T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-03T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '8\r'}, 'time': '2017-04-04T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '42\r'}, 'time': '2017-04-05T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-06T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '20\r'}, 'time': '2017-04-07T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-08T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-09T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-10T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '8\r'}, 'time': '2017-04-11T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-12T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '6\r'}, 'time': '2017-04-13T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '5\r'}, 'time': '2017-04-14T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-15T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-16T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-17T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '8\r'}, 'time': '2017-04-18T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '16\r'}, 'time': '2017-04-19T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-20T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '2\r'}, 'time': '2017-04-21T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-22T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-23T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-24T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '4\r'}, 'time': '2017-04-25T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-26T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '14\r'}, 'time': '2017-04-27T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-28T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-29T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-04-30T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-01T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '22\r'}, 'time': '2017-05-02T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '23\r'}, 'time': '2017-05-03T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '5\r'}, 'time': '2017-05-04T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '1\r'}, 'time': '2017-05-05T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-06T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-07T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-08T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-09T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '5\r'}, 'time': '2017-05-10T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '2\r'}, 'time': '2017-05-11T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '60\r'}, 'time': '2017-05-12T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '12\r'}, 'time': '2017-05-13T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-14T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '14\r'}, 'time': '2017-05-15T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '60\r'}, 'time': '2017-05-16T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-17T07:00:00Z', 'measurement': 'portcount'}]
[{'fields': {'ports': '0\r'}, 'time': '2017-05-18T07:00:00Z', 'measurement': 'portcount'}]
[username@testbox scripts]$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.2.2
InfluxDB shell version: 1.2.2
> use portcount
Using database portcount
> select * from portcount
name: portcount
time ports
---- -----
2017-03-16T07:00:00Z 2
2017-03-17T07:00:00Z 50
2017-03-18T07:00:00Z 0
2017-03-19T07:00:00Z 0
2017-03-20T07:00:00Z 0
2017-03-21T07:00:00Z 0
2017-03-22T07:00:00Z 0
2017-03-23T07:00:00Z 62
2017-03-24T07:00:00Z 23
2017-03-25T07:00:00Z 0
2017-03-26T07:00:00Z 0
2017-03-27T07:00:00Z 0
2017-03-28T07:00:00Z 2
2017-03-29T07:00:00Z 10
2017-03-30T07:00:00Z 4
2017-03-31T07:00:00Z 0
2017-04-01T07:00:00Z 0
2017-04-02T07:00:00Z 0
2017-04-03T07:00:00Z 0
2017-04-04T07:00:00Z 8
2017-04-05T07:00:00Z 42
2017-04-06T07:00:00Z 0
2017-04-07T07:00:00Z 20
2017-04-08T07:00:00Z 0
2017-04-09T07:00:00Z 0
2017-04-10T07:00:00Z 0
2017-04-11T07:00:00Z 8
2017-04-12T07:00:00Z 0
2017-04-13T07:00:00Z 6
2017-04-14T07:00:00Z 5
2017-04-15T07:00:00Z 0
2017-04-16T07:00:00Z 0
2017-04-17T07:00:00Z 0
2017-04-18T07:00:00Z 8
2017-04-19T07:00:00Z 16
2017-04-20T07:00:00Z 0
2017-04-21T07:00:00Z 2
2017-04-22T07:00:00Z 0
2017-04-23T07:00:00Z 0
2017-04-24T07:00:00Z 0
2017-04-25T07:00:00Z 4
2017-04-26T07:00:00Z 0
2017-04-27T07:00:00Z 14
2017-04-28T07:00:00Z 0
2017-04-29T07:00:00Z 0
2017-04-30T07:00:00Z 0
2017-05-01T07:00:00Z 0
2017-05-02T07:00:00Z 22
2017-05-03T07:00:00Z 23
2017-05-04T07:00:00Z 5
2017-05-05T07:00:00Z 1
2017-05-06T07:00:00Z 0
2017-05-07T07:00:00Z 0
2017-05-08T07:00:00Z 0
2017-05-09T07:00:00Z 0
2017-05-10T07:00:00Z 5
2017-05-11T07:00:00Z 2
2017-05-12T07:00:00Z 60
2017-05-13T07:00:00Z 12
2017-05-14T07:00:00Z 0
2017-05-15T07:00:00Z 14
2017-05-16T07:00:00Z 60
2017-05-17T07:00:00Z 0
2017-05-18T07:00:00Z 0
>
> exit
Then I run the second script and print out the data that is being written so you can see. After it is written I go check, and all the old data is gone, and just the new measurement is there.
[username@testbox scripts]$ python portcount.py
[{'fields': {'ports': 6}, 'time': '2017-05-22T21:51:58Z', 'measurement': 'portcount'}]
[username@testbox scripts]$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.2.2
InfluxDB shell version: 1.2.2
> use portcount
Using database portcount
> select * from portcount
name: portcount
time ports
---- -----
2017-05-22T21:51:58Z 6
The format of the data is the same. The timestamp format appears to be the same also? Yet for some reason it causes all previous data to disappear.
There is nothing in the scripts stating to drop anything.
oldportcounts.py script:
# !/usr/bin/env python
from __future__ import print_function
from functools import wraps
from pprint import pprint
import sys
import requests
import datetime
import json
import influxdb
from influxdb import client as influxdb
db = influxdb.InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='portcount')
portcount = {}
with open(r'portcountlog.csv', 'r') as h:
for line in h:
log = line.strip('\n').split(',')
loglist = []
logentry = {}
logentry['measurement'] = "portcount"
logentry['time'] = log[0]
logentry['fields'] = {}
logentry['fields']['ports'] = log[1]
loglist.append(logentry)
print (loglist)
db.write_points(loglist)
portcount.py:
# !/usr/bin/env python
from __future__ import print_function
from functools import wraps
from pprint import pprint
import sys
import requests
from datetime import datetime
import json
import influxdb
from influxdb import client as influxdb
# Define URL and Params for pulling CIM JSON data
url = 'http://api.website.com/api/ports/getPortActivations'
# Create variable called "resp" containing data from response we get back
resp = requests.get(url)
data = resp.json()
# Create timestamp for InfluxDB
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
# Extract the "count" from API and store as variable.
portcount = [
{
'measurement':"portcount",
'time': current_time,
'fields':{
'ports':data['count']},
}]
# Define InfluxDB Client Information
db = influxdb.InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='portcount')
# Write the port count to InfluxDB
print(portcount)
db.write_points(portcount)
I have to be able to load the old data and start updating the database with new data each night. Just not sure what’s going on.