meichthys / uptime_kuma

Uptime Kuma HACS integration
110 stars 23 forks source link

`Unexpected error` when setting up the integration #19

Closed Disane87 closed 1 year ago

Disane87 commented 1 year ago

Hi, thank for this project! But I have encountered a pretty weird bug. When I set up the integration, the settings dialog provides me this error:

image

I already checked the credentials, the url (with/without protocol and port etc.) but it won't work. I even disabled the 2FA in Uptime Kuma and changed my password, as mentioned in #10.

I saw an error in the homeassistant logs:

Logger: custom_components.uptime_kuma
Source: custom_components/uptime_kuma/config_flow.py:58
Integration: uptime_kuma
First occurred: 09:53:29 (6 occurrences)
Last logged: 19:38:32

could not convert string to float: 'html>'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 114, in _parse_sample
    label_start, label_end = text.index("{"), text.rindex("}")
ValueError: substring not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/uptime_kuma/config_flow.py", line 58, in _validate_input
    await uptime_robot_api.async_get_monitors()
  File "/usr/local/lib/python3.10/site-packages/pyuptimekuma/decorator.py", line 68, in wrapper
    response = UptimeKumaApiResponse.from_prometheus(
  File "/usr/local/lib/python3.10/site-packages/pyuptimekuma/models.py", line 76, in from_prometheus
    for family in parsed:
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 13, in text_string_to_metric_families
    yield from text_fd_to_metric_families(StringIO.StringIO(text))
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 209, in text_fd_to_metric_families
    sample = _parse_sample(line)
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 132, in _parse_sample
    value, timestamp = _parse_value_and_timestamp(text[name_end:])
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 106, in _parse_value_and_timestamp
    value = float(values[0])
ValueError: could not convert string to float: 'html>'

I guess this could be a problem by my dns, because I redirect every call to my subdomains '*.disane.dev' to my local reverse proxy (Nginx proxy manager) which provides the SSL.

All right, I thought to myself. I just take the IP address. and that works. So I guess that problems could result from my dns. But when I browse to my subdomain for uptime kuma, it works flawlessless.

As a DNS I use AdGuard home and some DNS redirects. Do you have any idead how I could solve this?

meichthys commented 1 year ago

Can you confirm what version you are installing from HACS?

meichthys commented 1 year ago

V2.0.0 should work fine. I'm also using a very similar setup as you. I'm also redirecting all subdomains to Nginxproxymanager which provides a let's encrypt ssl and then redirects the traffic to a docker instance running uptime kuma (with 2fa enabled). I also use Adguard for my DNS with rewrites (sounds like we have a VERY similar setup!) Make sure you are entering https:// in the url and just use your normal username and password (no special app password needed). It seems like the uptime kuma api doesn't require the 2fa code or an app password currently. Port should be 443 if you're using ssl.

If you're following the above, then I suppose DNS would be the next culprit.

meichthys commented 1 year ago

Maybe double check your https:// url. The error you copied seems to indicate that you're trying to use http> or something similar?

Disane87 commented 1 year ago

Can you confirm what version you are installing from HACS?

I added your repo to HACS as custom repo, so I guess this will be v2.

Maybe double check your https:// url. The error you copied seems to indicate that you're trying to use http> or something similar?

This is not http>in the error, it's:

could not convert string to float: 'html>'

It seems like the uptime kuma api doesn't require the 2fa code or an app password currently.

Yeah thats right @meichthys, looks like the /metrics endpoint of uptime kuma only uses the basic auth without 2FA, which is totally fine because you can't change anything.

Furthermore, I did a cURL to the metrics endpint of my uptime kuma subdomain from my HA docker with host network which resulted in this response:

<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

I had expected that there is this kind of response:

# HELP monitor_cert_days_remaining The number of days remaining until the certificate expires
# TYPE monitor_cert_days_remaining gauge 
[ ... ]

I guess your code doesn't handle the 302 HTTP code not properly? That would explain why it's working with the IP.

The strange thing is, I had to use the --insecure flag for my cURL request because without it, the request failed with curl: (60) SSL certificate problem: self signed certificate even when it's a SSL cert from LE.

Did you added your subdomains to the DNS rewrites in AdGuard like this? image

traceroute resolved properly to my unraid server, so I guess this isn't really a problem of my DNS.

But the config of my reverse proxy looks good imho: image

So this issue is pretty weird. Especially while it's working with the IP and port I used for my uptime kuma docker with bridged network:

image

So the problem seems to be in the DNS and/or Proxy but the configs seem to ok.

meichthys commented 1 year ago

I'm not sure what else to try, but yes, I do have my Adguard rewrites setup the same 👍

meichthys commented 1 year ago

I have my uptime kuma accessible publicly, so that is a difference. My guess is still something with dns.

Are you running traceroute from your homeassistant box? That's the machine that needs to be able to resolve uptime kuma.

Disane87 commented 1 year ago

I have my uptime kuma accessible publicly, so that is a difference. My guess is still something with dns.

Mine is public too 🙂

Are you running traceroute from your homeassistant box? That's the machine that needs to be able to resolve uptime kuma.

Yeah I tried this from the HA machine.

What I can check is: You use the external dep UptimeKuma to connect. I could write something similar only to and check the connection from different machines, so I know if it's the machine or something else. Since I know python this shouldn't be a problem.

meichthys commented 1 year ago

Maybe you have your internal network setup differently, but you may want your Access List in Nginxproxymanager to be set to public?

Disane87 commented 1 year ago

Maybe you have your internal network setup differently, but you may want your Access List in Nginxproxymanager to be set to public?

Already tried this, nothing changed

meichthys commented 1 year ago

Are you using vlans in your setup? Or does homeassistant have multiple network adapters?

Disane87 commented 1 year ago

No, none of this. I don't have any vlan nor does HA has more than one network

hans99 commented 1 year ago

Same fault for me. Uptime Kuma in Kubernetes behind proxy (Traefik). Works fine in a browser and with curl using http://host:80/metrics Let me know if I can help with more info!

meichthys commented 1 year ago

@jayakornk any ideas here? Seems to be related to the new pyuptimekuma dependency.

Disane87 commented 1 year ago

Same fault for me. Uptime Kuma in Kubernetes behind proxy (Traefik). Works fine in a browser and with curl using http://host:80/metrics

Let me know if I can help with more info!

Thank you for reporting this! Actually I guess the problem is the reverse proxy and the redirect.

I hope I can test the dependency for uptime kuma in a blank new project to test how this can be solved.

Disane87 commented 1 year ago

Had the chance to test this with the example script of https://github.com/jayakornk/pyuptimekuma

This even crashes with the same error: image

could not convert string to float: 'html>'

During handling of the above exception, another exception occurred:

  File "C:\Users\Disane\OneDrive\Desktop\uptime-kuma-proxy.py", line 17, in main
    response = await uptime_robot_api.async_get_monitors()
  File "C:\Users\Disane\OneDrive\Desktop\uptime-kuma-proxy.py", line 22, in <module>
    loop.run_until_complete(main())

I guess this is a problem of the package pyuptimekuma

@meichthys I've setup a test instance of uptime kuma in my infrastructure (with the same proxy and such things lile my prod instance) so you can test this behavior:

Host: https://uptime-kuma-proxy.disane.dev/
User: TestProxy
PW:  TestProxy

Thie exception is the same in that instance: image

Code:

import asyncio

import aiohttp
from pyuptimekuma import UptimeKuma

URL = "https://uptime-kuma-proxy.disane.dev/"
USERNAME = "TestProxy"
PASSWORD = "TestProxy"
VERIFY_SSL = False

async def main():

    async with aiohttp.ClientSession() as session:
        uptime_robot_api = UptimeKuma(
            session, URL, USERNAME, PASSWORD, VERIFY_SSL)
        response = await uptime_robot_api.async_get_monitors()
        print(response.data)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Feel free to reach out to me if you need something.

meichthys commented 1 year ago

@Disane87 Thanks for setting up the test instance. It probably doesn't help you find out what's happening on your end, but your test instance works fine in my HomeAssistant: image image 🤷‍♂️

Disane87 commented 1 year ago

Thank you for the test! In my networks this fails, so the only thing which is differing from internal and external call is the AdGuard DNS.

meichthys commented 1 year ago

Yes it does seem like it is dns related. Either in Adguard or in homeassistant.

dougmaitelli commented 1 year ago

I am having the exact same error too:

could not convert string to float: 'html>'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/prometheus_client/parser.py", line 114, in _parse_sample
    label_start, label_end = text.index("{"), text.rindex("}")
ValueError: substring not found
dannytsang commented 1 year ago

I also get the same error:

2022-11-26 22:39:42.487 ERROR (MainThread) [custom_components.uptime_kuma] Request exception for '192.168.1.4:3001/metrics' with - 192.168.1.4:3001/metrics

Unt-icked verify SSL and it was using http locally.

When I use external URL with SSL, it went through fine.

meichthys commented 1 year ago

@dougmaitelli Please check your dns configuration.

Disane87 commented 1 year ago

Since others have this problem too:

I've fixed my issue. It was a problem of DNS and my infrastructure.

All my incoming traffic of port 443 is forwarded to my NPM which routes per domain binding to the appropiate services. This worked perfectly but I saw, the pings from the internal network to my domains (which are set in NPM) went out and than over my public IP back in and that was totally crap.

I thought the dns rewrites would fix this, but they made it much worse because they pointed to my internal unraid which listend to port 80/443 and on this was the self signed cert and not the /metrics endpoint. Instead this the response was my unraid ui.

NPM runs on that unraid as bridge with an own IP on port 4443 and 8080. To forward all my internal 80/443 on the domains I've created a stream host which maps port 443 to 4443 which is now handled by the proxy itself.

I this helps others :)

meichthys commented 1 year ago

Thanks for following up!