Skip to content

Commit

Permalink
Merge pull request #2449 from jimklimov/issue-2246
Browse files Browse the repository at this point in the history
`nut-scanner`: Fix "Old NUT" detection and clarify some log messages
  • Loading branch information
jimklimov committed May 18, 2024
2 parents c8d7e3c + de157b4 commit 9f8dc40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
3 changes: 3 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ https://github.com/networkupstools/nut/milestone/11
* The `nut-driver-enumerator.sh` improvements misfired in v2.8.2 release
with an overlooked bit of shell syntax, and caused `nut-driver@upsname`
instances to not auto-restart when `ups.conf` is edited. [#682, #2410]
* Addition of "NUT Simulated devices" support to `nut-scanner` in v2.8.2
broke detection of (in-)ability to find and query "Old NUT" servers via
`libupsclient.so` (internal flag got always enabled). [#2246]
- drivers, upsd, upsmon: reduce "scary noise" about failure to `fopen()`
the PID file (which most of the time means that no previous instance of
Expand Down
30 changes: 15 additions & 15 deletions tools/nut-scanner/nut-scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ static const struct option longopts[] = {
{ "usb_scan", no_argument, NULL, 'U' },
{ "snmp_scan", no_argument, NULL, 'S' },
{ "xml_scan", no_argument, NULL, 'M' },
{ "oldnut_scan", no_argument, NULL, 'O' },
{ "avahi_scan", no_argument, NULL, 'A' },
{ "oldnut_scan", no_argument, NULL, 'O' }, /* "old" NUT libupsclient.so scan */
{ "avahi_scan", no_argument, NULL, 'A' }, /* "new" NUT scan where deployed */
{ "nut_simulation_scan", no_argument, NULL, 'n' },
{ "ipmi_scan", no_argument, NULL, 'I' },
{ "disp_nut_conf_with_sanity_check", no_argument, NULL, 'Q' },
Expand Down Expand Up @@ -256,11 +256,11 @@ static void show_usage(void)
} else {
printf("* Options for XML/HTTP devices scan not enabled: library not detected.\n");
}
printf(" -O, --oldnut_scan: Scan NUT devices (old method).\n");
printf(" -O, --oldnut_scan: Scan NUT devices (old method via libupsclient).\n");
if (nutscan_avail_avahi) {
printf(" -A, --avahi_scan: Scan NUT devices (avahi method).\n");
printf(" -A, --avahi_scan: Scan NUT devices (new avahi method).\n");
} else {
printf("* Options for NUT devices (avahi method) scan not enabled: library not detected.\n");
printf("* Options for NUT devices (new avahi method) scan not enabled: library not detected.\n");
}
printf(" -n, --nut_simulation_scan: Scan for NUT simulated devices (.dev files in $CONFPATH).\n");
if (nutscan_avail_ipmi) {
Expand Down Expand Up @@ -842,7 +842,7 @@ int main(int argc, char *argv[])
dev[TYPE_USB] = nutscan_scan_usb(&cli_link_detail_level);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "USB SCAN: not requested, SKIPPED");
upsdebugx(1, "USB SCAN: not requested or supported, SKIPPED");
}

if (allow_snmp && nutscan_avail_snmp) {
Expand All @@ -864,7 +864,7 @@ int main(int argc, char *argv[])
#endif /* HAVE_PTHREAD */
}
} else {
upsdebugx(1, "SNMP SCAN: not requested, SKIPPED");
upsdebugx(1, "SNMP SCAN: not requested or supported, SKIPPED");
}

if (allow_xml && nutscan_avail_xml_http) {
Expand All @@ -881,16 +881,16 @@ int main(int argc, char *argv[])
dev[TYPE_XML] = nutscan_scan_xml_http_range(start_ip, end_ip, timeout, &xml_sec);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "XML/HTTP SCAN: not requested, SKIPPED");
upsdebugx(1, "XML/HTTP SCAN: not requested or supported, SKIPPED");
}

if (allow_oldnut && nutscan_avail_nut) {
if (start_ip == NULL) {
upsdebugx(quiet, "No start IP, skipping NUT bus (old connect method)");
upsdebugx(quiet, "No start IP, skipping NUT bus (old libupsclient connect method)");
nutscan_avail_nut = 0;
}
else {
upsdebugx(quiet, "Scanning NUT bus (old connect method).");
upsdebugx(quiet, "Scanning NUT bus (old libupsclient connect method).");
#ifdef HAVE_PTHREAD
upsdebugx(1, "NUT bus (old) SCAN: starting pthread_create with run_nut_old...");
if (pthread_create(&thread[TYPE_NUT], NULL, run_nut_old, NULL)) {
Expand All @@ -903,7 +903,7 @@ int main(int argc, char *argv[])
#endif /* HAVE_PTHREAD */
}
} else {
upsdebugx(1, "NUT bus (old) SCAN: not requested, SKIPPED");
upsdebugx(1, "NUT bus (old) SCAN: not requested or supported, SKIPPED");
}

if (allow_nut_simulation && nutscan_avail_nut_simulation) {
Expand All @@ -919,7 +919,7 @@ int main(int argc, char *argv[])
dev[TYPE_NUT_SIMULATION] = nutscan_scan_nut_simulation(timeout);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "NUT simulation devices SCAN: not requested, SKIPPED");
upsdebugx(1, "NUT simulation devices SCAN: not requested or supported, SKIPPED");
}

if (allow_avahi && nutscan_avail_avahi) {
Expand All @@ -935,7 +935,7 @@ int main(int argc, char *argv[])
dev[TYPE_AVAHI] = nutscan_scan_avahi(timeout);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "NUT bus (avahi) SCAN: not requested, SKIPPED");
upsdebugx(1, "NUT bus (avahi) SCAN: not requested or supported, SKIPPED");
}

if (allow_ipmi && nutscan_avail_ipmi) {
Expand All @@ -951,7 +951,7 @@ int main(int argc, char *argv[])
dev[TYPE_IPMI] = nutscan_scan_ipmi(start_ip, end_ip, &ipmi_sec);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "IPMI SCAN: not requested, SKIPPED");
upsdebugx(1, "IPMI SCAN: not requested or supported, SKIPPED");
}

/* Eaton serial scan */
Expand All @@ -968,7 +968,7 @@ int main(int argc, char *argv[])
dev[TYPE_EATON_SERIAL] = nutscan_scan_eaton_serial (serial_ports);
#endif /* HAVE_PTHREAD */
} else {
upsdebugx(1, "SERIAL SCAN: not requested, SKIPPED");
upsdebugx(1, "SERIAL SCAN: not requested or supported, SKIPPED");
}

#ifdef HAVE_PTHREAD
Expand Down
40 changes: 27 additions & 13 deletions tools/nut-scanner/nutscan-init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
* Copyright (C) 2011 - 2024 Arnaud Quette (Design and part of implementation)
* Copyright (C) 2011-2021 - EATON
* Copyright (C) 2011 - 2021 EATON
* Copyright (C) 2016 - 2021 Jim Klimov <[email protected]>
* Copyright (C) 2022 - 2024 Jim Klimov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +23,8 @@
\brief init functions for nut scanner library
\author Frederic Bohe <[email protected]>
\author Arnaud Quette <[email protected]>
\author Arnaud Quette <[email protected]>
\author Arnaud Quette <[email protected]>
\author Jim Klimov <[email protected]>
*/

#include "common.h"
Expand All @@ -38,15 +41,14 @@
#define SOEXT ".dll"
#endif

/* Flags for code paths we can support in this run (libs available or not
* needed). For consistency, only set non-zero values via nutscan_init() call.
*/
int nutscan_avail_avahi = 0;
int nutscan_avail_ipmi = 0;
int nutscan_avail_nut = 0;
int nutscan_avail_nut_simulation = 1;
#ifdef WITH_SNMP_STATIC
int nutscan_avail_snmp = 1;
#else
int nutscan_avail_nut = 0; /* aka oldnut detection via libupsclient compared to avahi as newnut */
int nutscan_avail_nut_simulation = 0;
int nutscan_avail_snmp = 0;
#endif
int nutscan_avail_usb = 0;
int nutscan_avail_xml_http = 0;

Expand Down Expand Up @@ -209,12 +211,23 @@ void nutscan_init(void)
#endif /* WITH_USB */

#ifdef WITH_SNMP
#ifdef WITH_SNMP_STATIC
/* This is a rare situation, reserved for platforms where libnetsnmp or
* equivalent (some other ucd-snmp descendants) was not packaged, and
* thus was custom-built for NUT (so linked statically to avoid potential
* conflicts with whatever else people may have practically deployed
* nearby).
*/
upsdebugx(1, "%s: skipped loading the library for %s: was linked statically during NUT build",
__func__, "LibSNMP");
nutscan_avail_snmp = 1;
#else
libname = get_libname("libnetsnmp" SOEXT);
#ifdef WIN32
#ifdef WIN32
if (!libname) {
libname = get_libname("libnetsnmp-40" SOEXT);
}
#endif
#endif
if (libname) {
upsdebugx(1, "%s: get_libname() resolved '%s' for %s, loading it",
__func__, libname, "LibSNMP");
Expand All @@ -226,14 +239,15 @@ void nutscan_init(void)
"trying to load it with libtool default resolver",
__func__, "LibSNMP");
nutscan_avail_snmp = nutscan_load_snmp_library("libnetsnmp" SOEXT);
#ifdef WIN32
#ifdef WIN32
if (!nutscan_avail_snmp) {
nutscan_avail_snmp = nutscan_load_snmp_library("libnetsnmp-40" SOEXT);
}
#endif
#endif
}
upsdebugx(1, "%s: %s to load the library for %s",
__func__, nutscan_avail_snmp ? "succeeded" : "failed", "LibSNMP");
#endif /* WITH_SNMP_STATIC */
#else
upsdebugx(1, "%s: skipped loading the library for %s: was absent during NUT build",
__func__, "LibSNMP");
Expand Down Expand Up @@ -363,7 +377,7 @@ void nutscan_init(void)

/* start of "NUT Simulation" - unconditional */
/* no need for additional library */
nutscan_avail_nut = 1;
nutscan_avail_nut_simulation = 1;

}

Expand Down

0 comments on commit 9f8dc40

Please sign in to comment.