PLG: ICMP v2 better excception handling #1331

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2026-01-04 11:56:11 +11:00
parent 059612185e
commit 1dd5512265
2 changed files with 58 additions and 44 deletions

View File

@@ -155,7 +155,7 @@
"description": [ "description": [
{ {
"language_code": "en_us", "language_code": "en_us",
"string": "Arguments passed to the underlying <code>ping</code> or <code>fping</code> command. The default values are compatible with both modes and work well in most environments. Modify with care, and consult the relevant manual pages if advanced tuning is required." "string": "Arguments passed to the underlying <code>ping</code> or <code>fping</code> command. The default values (<code>-i 0.5 -c 3 -w 5</code>) are compatible with both modes and work well in most environments. Modify with care, and consult the relevant manual pages if advanced tuning is required."
} }
] ]
}, },

View File

@@ -87,7 +87,7 @@ def main():
plugin_objects.write_result_file() plugin_objects.write_result_file()
mylog('verbose', [f'[{pluginName}] Script finished']) mylog('verbose', [f'[{pluginName}] Script finished - {len(plugin_objects)} added or updated'])
return 0 return 0
@@ -174,59 +174,69 @@ def execute_ping(timeout, args, all_devices, regex_pattern, plugin_objects):
def execute_fping(timeout, args, all_devices, plugin_objects, subnets, interfaces, fakeMac): def execute_fping(timeout, args, all_devices, plugin_objects, subnets, interfaces, fakeMac):
""" """
Run fping command and return alive IPs Run fping command and return alive IPs.
Handles:
- fping exit code 1 (some hosts unreachable)
- Mixed subnets and known IPs
- Synology quirks
""" """
cmd = ["fping", "-a"]
if interfaces:
cmd += ["-I", ",".join(interfaces)]
# Build a lookup dict once
device_map = {d["devLastIP"]: d for d in all_devices if d.get("devLastIP")} device_map = {d["devLastIP"]: d for d in all_devices if d.get("devLastIP")}
known_ips = list(device_map.keys()) known_ips = list(device_map.keys())
online_ips = [] online_ips = []
cmd += args.split() # Function to run fping for a list of targets
cmd += subnets def run_fping(targets):
cmd += known_ips if not targets:
return []
mylog("verbose", [f"[{pluginName}] fping cmd: {' '.join(cmd)}"]) cmd = ["fping", "-a"] + args.split() + targets
if interfaces:
cmd += ["-I", ",".join(interfaces)]
try: mylog("verbose", [f"[{pluginName}] fping cmd: {' '.join(cmd)}"])
output = subprocess.check_output(
cmd,
stderr=subprocess.DEVNULL,
timeout=timeout,
text=True
)
online_ips = [line.strip() for line in output.splitlines() if line.strip()]
except subprocess.CalledProcessError: try:
online_ips = [] output = subprocess.check_output(
cmd,
stderr=subprocess.DEVNULL,
timeout=timeout,
text=True
)
except subprocess.CalledProcessError as e:
# fping returns 1 if some hosts are down alive hosts are still in e.output
output = e.output
mylog("verbose", [f"[{pluginName}] fping returned non-zero exit code, reading alive hosts anyway"])
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
mylog("verbose", [f"[{pluginName}] fping timeout"]) mylog("verbose", [f"[{pluginName}] fping timeout"])
online_ips = [] return []
# process all online IPs return [line.strip() for line in output.splitlines() if line.strip()]
# First scan subnets
online_ips += run_fping(subnets)
# Then scan known IPs
online_ips += run_fping(known_ips)
# Remove duplicates
online_ips = list(set(online_ips))
# Process all online IPs
for onlineIp in online_ips: for onlineIp in online_ips:
if onlineIp in known_ips: if onlineIp in device_map:
# use lookup dict instead of looping device = device_map[onlineIp]
device = device_map.get(onlineIp) plugin_objects.add_object(
if device: primaryId = device['devMac'],
plugin_objects.add_object( secondaryId = device['devLastIP'],
primaryId = device['devMac'], watched1 = device['devName'],
secondaryId = device['devLastIP'], watched2 = 'mode:fping',
watched1 = device['devName'], watched3 = '',
watched2 = 'mode:fping', watched4 = '',
watched3 = '', extra = '',
watched4 = '', foreignKey = device['devMac']
extra = '', )
foreignKey = device['devMac']
)
else:
mylog("none", [f"[{pluginName}] ERROR reverse device lookup failed unexpectedly for {onlineIp}"])
elif fakeMac: elif fakeMac:
fakeMacFromIp = string_to_fake_mac(onlineIp) fakeMacFromIp = string_to_fake_mac(onlineIp)
plugin_objects.add_object( plugin_objects.add_object(
@@ -240,7 +250,11 @@ def execute_fping(timeout, args, all_devices, plugin_objects, subnets, interface
foreignKey = fakeMacFromIp foreignKey = fakeMacFromIp
) )
else: else:
mylog('verbose', [f"[{pluginName}] Skipping: {onlineIp}, as new IP and ICMP_FAKE_MAC setting not enabled"]) mylog('verbose', [f"[{pluginName}] Skipping: {onlineIp}, as new IP and ICMP_FAKE_MAC not enabled"])
mylog('verbose', [f"[{pluginName}] online_ips: {online_ips}"])
return plugin_objects
# =============================================================================== # ===============================================================================