-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon.py
40 lines (32 loc) · 1.16 KB
/
carbon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python3
import configparser
import logging
import time, datetime
import socketserver
from pprint import pprint,pformat
from datapoint import DataPoint
from database import DbConnection
CONFIG_FILE = "ingress.conf"
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
LOGFILE = config['logging']['file']
LISTEN = config['carbon']['listen']
PORT = config['carbon'].getint('port')
logging.basicConfig(filename=LOGFILE,
level=logging.DEBUG,
format="%(asctime)s %(levelname)s %(message)s")
db = DbConnection()
class RequestHandler(socketserver.StreamRequestHandler):
def handle(self):
line = str(self.rfile.readline().strip(),"utf8")
logging.debug(line)
fields = line.split()
logging.debug(fields)
sensorid,channel = fields[0].split(".")
value = float(fields[1])
timestamp = int(fields[2])
dp = DataPoint(sensorid=sensorid,channel=channel,value=value,timestamp=datetime.datetime.fromtimestamp(timestamp,tz=datetime.timezone.utc))
logging.debug(dp)
addr = (LISTEN,PORT)
server = socketserver.TCPServer(addr, RequestHandler)
server.serve_forever()