Skip to content

Commit

Permalink
Merge pull request #2247 from aquette/2243-nut-scanner-generated-conf…
Browse files Browse the repository at this point in the history
…-can-cause-device-name-collisions

nut-scanner: fix device-name collisions in generated conf
  • Loading branch information
jimklimov authored Jan 5, 2024
2 parents e873d5a + e9632f9 commit aeb0c3c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
25 changes: 24 additions & 1 deletion docs/man/nut-scanner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ DESCRIPTION
*nut-scanner* scans available communication buses and displays any
NUT-compatible devices it has found.

*nut-scanner* can also display the detected devices in various formats,
including ups.conf, and ensures that the generated devices name are unique
across buses.

INSTALLATION
------------

Expand Down Expand Up @@ -91,7 +95,6 @@ ports using 'X-Y', where X and Y are characters referring to the port number.
- a single port name.
- a list of ports name, coma separated, like '/dev/ttyS1,/dev/ttyS4'.


NETWORK OPTIONS
---------------

Expand Down Expand Up @@ -220,20 +223,40 @@ EXAMPLES
To scan USB devices only:

*nut-scanner -U*
+
[nutdev-usb1]
driver = "snmp-ups"
port = "192.168.0.42"
+

To scan SNMP v1 device with public community on address range 192.168.0.0
to 192.168.0.255:

*nut-scanner -S -s 192.168.0.0 -e 192.168.0.255*
+
[nutdev-snmp1]
driver = "snmp-ups"
port = "192.168.0.42"
+

The same using CIDR notation:

*nut-scanner -S -m 192.168.0.0/24*
+
[nutdev-snmp1]
driver = "snmp-ups"
port = "192.168.0.42"
+

To scan NUT servers with a timeout of 10 seconds on IP range 192.168.0.0
to 192.168.0.127 using CIDR notation:

*nut-scanner -O -t 10 -m 192.168.0.0/25*
+
[nutdev-nut1]
driver = "dummy-ups"
port = "[email protected]"
+

To scan for power supplies, through IPMI (1.5 mode) over the network,
on address range 192.168.0.0 to 192.168.0.255 using CIDR notation:
Expand Down
9 changes: 6 additions & 3 deletions tests/NIT/nit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1215,9 +1215,12 @@ testcase_sandbox_nutscanner_list() {
# Note: the reported "driver" string is not too helpful as a "nutclient".
# In practice this could be a "dummy-ups" repeater or "clone" driver,
# or some of the config elements needed for upsmon (lacking creds/role)
# Also note that before PR #2247 nut-scanner returned "nutdev<NUM>"
# section names, but now it returns "nutdev-<BUS><NUM>" to differentiate
# the scanned buses (serial, snmp, usb, etc.)
if (
test -n "$CMDOUT" \
&& echo "$CMDOUT" | grep -E '^\[nutdev1\]$' \
&& echo "$CMDOUT" | grep -E '^\[nutdev-nut1\]$' \
&& echo "$CMDOUT" | grep 'port = "dummy@' \
|| return

Expand All @@ -1234,9 +1237,9 @@ testcase_sandbox_nutscanner_list() {
if [ x"${TOP_SRCDIR}" = x ]; then
log_info "[testcase_sandbox_nutscanner_list] Note: only testing one dummy device" >&2
else
echo "$CMDOUT" | grep -E '^\[nutdev2\]$' \
echo "$CMDOUT" | grep -E '^\[nutdev-nut2\]$' \
&& echo "$CMDOUT" | grep 'port = "UPS1@' \
&& echo "$CMDOUT" | grep -E '^\[nutdev3\]$' \
&& echo "$CMDOUT" | grep -E '^\[nutdev-nut3\]$' \
&& echo "$CMDOUT" | grep 'port = "UPS2@' \
|| {
log_error "[testcase_sandbox_nutscanner_list] something about UPS1/UPS2 not found" >&2
Expand Down
16 changes: 15 additions & 1 deletion tools/nut-scanner/nutscan-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#include <string.h>
#include <assert.h>

const char * nutscan_device_type_strings[TYPE_END - 1] = {
const char * nutscan_device_type_strings[TYPE_END] = {
"NONE", /* 0 */
"USB",
"SNMP",
"XML",
Expand All @@ -40,6 +41,19 @@ const char * nutscan_device_type_strings[TYPE_END - 1] = {
"serial",
};

/* lower strings, used for device names */
const char * nutscan_device_type_lstrings[TYPE_END] = {
"none", /* 0 */
"usb",
"snmp",
"xml",
"nut",
"simulation",
"ipmi",
"avahi",
"serial",
};

nutscan_device_t * nutscan_new_device(void)
{
nutscan_device_t * device;
Expand Down
5 changes: 4 additions & 1 deletion tools/nut-scanner/nutscan-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ extern "C" {
*/
#define nutscan_device_type_string(type) \
(assert(0 < (type) && (type) < TYPE_END), nutscan_device_type_strings[type - 1])
#define nutscan_device_type_lstring(type) \
(assert(0 < (type) && (type) < TYPE_END), nutscan_device_type_lstrings[type - 1])

typedef enum nutscan_device_type {
TYPE_NONE = 0,
Expand All @@ -56,7 +58,8 @@ typedef enum nutscan_device_type {
} nutscan_device_type_t;

/** Device type -> string mapping */
extern const char * nutscan_device_type_strings[TYPE_END - 1];
extern const char * nutscan_device_type_strings[TYPE_END];
extern const char * nutscan_device_type_lstrings[TYPE_END];

typedef struct nutscan_options {
char * option;
Expand Down
9 changes: 5 additions & 4 deletions tools/nut-scanner/nutscan-display.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void nutscan_display_ups_conf(nutscan_device_t * device)

/* Display each device */
do {
printf("[nutdev%i]\n\tdriver = \"%s\"",
printf("[nutdev-%s%i]\n\tdriver = \"%s\"",
nutscan_device_type_lstrings[current_dev->type],
nutdev_num, current_dev->driver);

if (current_dev->alt_driver_names) {
Expand Down Expand Up @@ -214,7 +215,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
}

/* Process each device:
* Build a map of "serial"=>"nutdevX[,...,nutdevZ]"
* Build a map of "serial"=>"nutdev-serialX[,...,nutdev-serialZ]"
* and warn if there are bogus "serial" keys or if
* there are several nutdev's (a comma in value).
*/
Expand Down Expand Up @@ -256,7 +257,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
* dynamic allocation, malloc data for larger "val".
*/
snprintfcat(entry->val, sizeof(entry->val),
",nutdev%i", nutdev_num);
",nutdev-serial%i", nutdev_num);
} else {
/* No hit => new key */
upsdebugx(3, "%s: new entry for serial '%s'",
Expand All @@ -280,7 +281,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
snprintf(entry->key, sizeof(entry->key),
"%s", keytmp);
snprintf(entry->val, sizeof(entry->val),
"nutdev%i", nutdev_num);
"nutdev-serial%i", nutdev_num);
}

/* Abort the opt-searching loop for this device */
Expand Down

0 comments on commit aeb0c3c

Please sign in to comment.