hardoverflow / check-unifi

Icinga 2 check plugin for UniFi Network Application
The Unlicense
4 stars 1 forks source link

Plugin crashes when no clients are connected/no wifi experience is available #6

Closed TiZon closed 9 months ago

TiZon commented 9 months ago

Hey

First, thanks for the excellent plugin. We have a few sites that are not getting a lot of traffic, this causes an issue for the plugin:

File "/usr/lib/nagios/plugins/check_unifi.py", line 297, in main() File "/usr/lib/nagios/plugins/check_unifi.py", line 293, in main fmt_output(check_site_stats(args)) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/nagios/plugins/check_unifi.py", line 228, in check_site_stats stats_wifi_exp = mean([item for item in ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/statistics.py", line 432, in mean raise StatisticsError('mean requires at least one data point') statistics.StatisticsError: mean requires at least one data point

If there are no data points, the mean calculation fails.

I've amended this with a try/catch, but maybe you see a more elegant solution:

Starting at line 227:

# Calculate WiFi Experiance
try:
    stats_wifi_exp = mean([item for item in
                       [item.get('satisfaction')
                        for item in blob[1]['data']]
                      if item is not None])
except:
    stats_wifi_exp = 100

Thanks!

hardoverflow commented 9 months ago

Thank u @TiZon for reporting the bug. I will fix this asap.