Skip to content

Commit

Permalink
Merge pull request #55 from michbeck100/feature/prometheus-export
Browse files Browse the repository at this point in the history
adds /metrics endpoint with prometheus format
  • Loading branch information
bohdan-s committed Jul 7, 2022
2 parents 97b1121 + bca665a commit 8258ed5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Has multiple export locations out of the box:
* MQTT - Load into MQTT, and optionally Home Assistance including Discovery
* PVOutput - Load into PVOutput.org
* InfluxDB - Load data directly into InfluxDB (v1.8 or v2.x)
* Prometheus - Scrape from /metrics endpoint
* Simple webserver showing collected data
* Rasberry Pi Docker support
* and more coming....
Expand Down
32 changes: 22 additions & 10 deletions SunGather/exports/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class export_webserver(object):
html_body = "Pending Data Retrieval"
metrics = ""
def __init__(self):
False

Expand All @@ -25,28 +26,39 @@ def configure(self, config, inverter):

def publish(self, inverter):
body = "<h3>Sungather v" + __version__ + "</h3></p><table><th>Address</th><tr><th>Register</th><th>Value</th></tr>"
metrics = ""
for register, value in inverter.latest_scrape.items():
body = body + f"<tr><td>{str(inverter.getRegisterAddress(register))}</td><td>{str(register)}</td><td>{str(value)} {str(inverter.getRegisterUnit(register))}</td></tr>"
body += f"<tr><td>{str(inverter.getRegisterAddress(register))}</td><td>{str(register)}</td><td>{str(value)} {str(inverter.getRegisterUnit(register))}</td></tr>"
metrics += f"{str(register)}{{address=\"{str(inverter.getRegisterAddress(register))}\", unit=\"{str(inverter.getRegisterUnit(register))}\"}} {str(value)}\n"
export_webserver.html_body = body + f"</table><p>Total {len(inverter.latest_scrape)} registers"
export_webserver.metrics = metrics

body = "</p></p><table><tr><th>Configuration</th><th>Value</th></tr>"
for setting, value in inverter.client_config.items():
body = body + f"<tr><td>{str(setting)}</td><td>{str(value)}</td></tr>"
for setting, value in inverter.inverter_config.items():
body = body + f"<tr><td>{str(setting)}</td><td>{str(value)}</td></tr>"
export_webserver.html_body = export_webserver.html_body + body + f"</table></p>"

return True

class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>SunGather</title><meta charset='UTF-8'><meta http-equiv='refresh' content='15'></head>", "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes(export_webserver.html_body, "utf-8"))
self.wfile.write(bytes("</table>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
if self.path == '/metrics':
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(bytes(export_webserver.metrics, "utf-8"))
else:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>SunGather</title><meta charset='UTF-8'><meta http-equiv='refresh' content='15'></head>", "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes(export_webserver.html_body, "utf-8"))
self.wfile.write(bytes("</table>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))


def log_message(self, format, *args):
pass

0 comments on commit 8258ed5

Please sign in to comment.