forked from jbuehl/solaredge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
se2graphite.py
53 lines (47 loc) · 1.66 KB
/
se2graphite.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
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
# send a SolarEdge performance metrics to Graphite
import json
import getopt
import time
import sys
import socket
# parameter defaults
base = ""
hostName = "localhost"
port = 2003
devices = ["inverters", "optimizers"]
delay = .1
# get program arguments and options
(opts, args) = getopt.getopt(sys.argv[1:], "b:h:p:")
try:
inFile = open(args[0])
except:
inFile = sys.stdin
for opt in opts:
if opt[0] == "-b":
base = opt[1]+"."
if opt[0] == "-h":
hostName = opt[1]
if opt[0] == "-p":
port = int(opt[1])
if __name__ == "__main__":
graphiteSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
graphiteSocket.connect((hostName, port))
jsonStr = inFile.readline()
while jsonStr != "":
inDict = json.loads(jsonStr)
for devType in devices:
for devId in inDict[devType].keys():
devAttrs = inDict[devType][devId]
# convert date and time to unix time
(year, month, day ) = devAttrs["Date"].split("-")
(hour, minute, second) = devAttrs["Time"].split(":")
timeStamp = time.mktime((int(year), int(month), int(day), int(hour), int(minute), int(second), 0, 0, 0))
# every attribute is a metric
for devAttr in devAttrs.keys():
if devAttr != "Date" and devAttr != "Time":
metric = "%s.%s.%s.%s %s %d\n" % (base, devType, devId, devAttr, str(devAttrs[devAttr]), timeStamp)
graphiteSocket.send(metric)
time.sleep(delay)
jsonStr = inFile.readline()
graphiteSocket.close()