leiweibau / Pi.Alert

Scan the devices connected to your WIFI / LAN and alert you the connection of unknown devices. It also warns if a "always connected" device disconnects. In addition, it is possible to check web services for availability. For this purpose HTTP status codes and the response time of the service are evaluated.
https://leiweibau.net
GNU General Public License v3.0
406 stars 28 forks source link

[Bug] Unify method does not work #185

Closed danielo515 closed 11 months ago

danielo515 commented 11 months ago

Hello again. I had setup some test configs in my instance, then I decided that I didn't wanted them because they were failing, so I disabled them again. However, when I run a scan I see the same errors happening. For example, I first enabled the pihole integration, tried a scan and it failed with a file not found (the DB was not there). No matter how many times I tried disabling it, I was getting the same error. Even after rebooting.

I then added the pihole file, just to check if that was the problem, and indeed the issue was gone, now I'm getting the following error:

Scanning...
    arp-scan Method...
    Pi-hole Method...
    DHCP Leases Method...
    Fritzbox Method...
    Mikrotik Method...
    UniFi Method...
Traceback (most recent call last):
File "/opt/pialert/back/pialert.py", line 2755, in
    sys.exit(main())    
             ^^^^^^
File "/opt/pialert/back/pialert.py", line 108, in main
    res = scan_network()
         ^^^^^^^^^^^^^^
File "/opt/pialert/back/pialert.py", line 482, in scan_network
    read_unifi_clients()
File "/opt/pialert/back/pialert.py", line 758, in read_unifi_clients
    from pyunifi.controller import Controller
ModuleNotFoundError: No module named 'pyunifi'

It does not matter if I disable the unify method, this problem keeps happening. How can I fix this? Do I need to remove the complet configuration?

danielo515 commented 11 months ago

My relevant config:

# Pi-hole Configuration
# ----------------------
PIHOLE_ACTIVE     = False
PIHOLE_DB         = '/media/pihole_share/pihole-FTL.db'
DHCP_ACTIVE       = False
DHCP_LEASES       = ''

# UniFi Configuration
# -------------------
UNIFI_ACTIVE = False
UNIFI_IP     = '192.168.0.7:8443'
UNIFI_USER   = '******'
UNIFI_PASS   = '*****'
danielo515 commented 11 months ago

Commenting the relevant values makes it work again. It seems you are only checking the existence of the values, and not the active flag

leiweibau commented 11 months ago

Since I don't have a Unifi system myself and the integration came into the project through a pull request, I can only help in a limited way right now. However, one thing stands out to me.

Does the error disappear if you make the following change in line 758 of the file "pialert/back/pialert.py"?

Change from pyunifi.controller import Controller to from unifi.controller import Controller

leiweibau commented 11 months ago

After you have changed this, it is also necessary to remove the port specification from the config.

UNIFI_IP = '192.168.0.7'

The module already uses port 8443 internally

leiweibau commented 11 months ago

Upon further analysis, I have now found that there is a mismatch between the installed python unifi library and the function used in the script. I will install pyunifi instead of the python3 lib unifi with the next update. Unfortunately, however, I still can not test the functionality.

leiweibau commented 11 months ago

Update Released

modem7 commented 11 months ago

Hey guys,

Just putting in my 2 cents on this for how I've gotten this working as I've got a UDMP (dream machine pro), and this is how I've installed:

(Looking at https://github.com/finish06/pyunifi#__init__self-host-username-password there are a few options depending on your unifi OS: the base version of the controller API [v4|v5|unifiOS|UDMP-unifiOS])

Config (multiple VLANs):

# Arp-scan Options & Samples
# ----------------------
ARPSCAN_ACTIVE  = True
SCAN_SUBNETS    = [ '192.168.0.0/24 --interface=eth0', '192.168.20.0/24 --interface=eth0', '192.168.30.0/24 --interface=eth0', '192.168.40.0/24 --interface=eth0', '192.168.50.0/24 --interface=eth0' ]
# MAC_IGNORE_LIST = ['11:22:33:aa:bb:cc']
# SCAN_SUBNETS    = '--localnet'
# SCAN_SUBNETS    = '--localnet --interface=eth0'
# SCAN_SUBNETS    = [ '192.168.1.0/24 --interface=eth0', '192.168.2.0/24 --interface=eth1' ]
# UniFi Configuration
# -------------------
UNIFI_ACTIVE = True
UNIFI_IP     = '192.168.0.200'
UNIFI_USER   = 'user'
UNIFI_PASS   = 'password'

Results:

Scanning...
    arp-scan Method...
    arp-scan: Multiple interfaces
    Pi-hole Method...
    DHCP Leases Method...
    Fritzbox Method...
    Mikrotik Method...
    UniFi Method...
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:1048: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.0.200'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(

Processing scan results...
    Processing ignore list...
        No ignore list defined
    Devices Detected.......: 26
        arp-scan Method....: 8
        Pi-hole Method.....: +0
        Fritzbox Method....: +0
        Mikrotik Method....: +0
        UniFi Method.......: +17
        New Devices........: 0

    Devices in this cycle..: 26
        Down Alerts........: 0
        New Down Alerts....: 0
        New Connections....: 0
        Disconnections.....: 0
        IP Changes.........: 0

Updating DB Info...
    Sessions Events (connect / discconnect) ...
    Creating new devices...
    Updating Devices Info...
        Trying to resolve devices without name...
        Names updated:   0
    Voiding false (ghost) disconnections...
    Pairing session events (connection / disconnection) ...
    Creating sessions snapshot...
    Skipping repeated notifications...
    Calculate Activity History...

Reporting...
    Formating report...
    No changes to report...
    Notifications: 0

DONE!!!
leiweibau commented 11 months ago

Awesome. I add this as an option to the config file as well. And I will also try to fix that warning

modem7 commented 11 months ago

I will also try to fix that warning

The warning at most, could be silenced.

It's unfortunately a "feature" as the SSL cert that is originally setup with things like the UDMP is self signed out the box, so there's very little one can do unless they deploy their own proper certificate with an authority/domain, which is obviously outside the scope of this project, and down to the user to implement if they so wish.

leiweibau commented 11 months ago

But it should be possible to suppress this warning. Such a log file should also somehow look a bit pretty 😉

leiweibau commented 11 months ago

Update released with https://github.com/leiweibau/Pi.Alert/commit/37b6b1e7a051dec1668b86bea410f37ef4feb055

modem7 commented 11 months ago

Can confirm that the new method works as expected, no issues found so far.

leiweibau commented 11 months ago

Thank you for your help