Skip to content

Commit 6942b32

Browse files
committed
Added carbon input prototype
fixes #1
1 parent 76030a4 commit 6942b32

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

carbon.py

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

0 commit comments

Comments
 (0)