Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds /metrics endpoint with prometheus format #55

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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