Imported Upstream version 2.6.3

This commit is contained in:
Arnaud Quette 2012-01-24 11:22:33 +01:00
parent 45043b58d0
commit fad6ced6f6
255 changed files with 11081 additions and 4629 deletions

View file

@ -21,6 +21,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include "common.h"
#include "nut_version.h"
#include <unistd.h>
#include <string.h>
#ifdef HAVE_PTHREAD
@ -31,7 +32,9 @@
#define DEFAULT_TIMEOUT 5
const char optstring[] = "?ht:s:e:c:l:u:W:X:w:x:p:CUSMOAm:NPqI";
#define ERR_BAD_OPTION (-1)
const char optstring[] = "?ht:s:e:c:l:u:W:X:w:x:p:CUSMOAm:NPqIVa";
#ifdef HAVE_GETOPT_LONG
const struct option longopts[] =
@ -58,13 +61,14 @@ const struct option longopts[] =
{ "disp_parsable",no_argument,NULL,'P' },
{ "quiet",no_argument,NULL,'q' },
{ "help",no_argument,NULL,'h' },
{ "version",no_argument,NULL,'V' },
{ "available",no_argument,NULL,'a' },
{NULL,0,NULL,0}};
#else
#define getopt_long(a,b,c,d,e) getopt(a,b,c)
#endif /* HAVE_GETOPT_LONG */
static nutscan_device_t *dev[TYPE_END];
static pthread_t thread[TYPE_END];
static long timeout = DEFAULT_TIMEOUT*1000*1000; /* in usec */
static char * start_ip = NULL;
@ -72,14 +76,13 @@ static char * end_ip = NULL;
static char * port = NULL;
#ifdef HAVE_PTHREAD
#ifdef HAVE_USB_H
static pthread_t thread[TYPE_END];
static void * run_usb(void * arg)
{
dev[TYPE_USB] = nutscan_scan_usb();
return NULL;
}
#endif
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
static void * run_snmp(void * arg)
{
nutscan_snmp_t * sec = (nutscan_snmp_t *)arg;
@ -87,14 +90,11 @@ static void * run_snmp(void * arg)
dev[TYPE_SNMP] = nutscan_scan_snmp(start_ip,end_ip,timeout,sec);
return NULL;
}
#endif
#ifdef WITH_NEON
static void * run_xml(void * arg)
{
dev[TYPE_XML] = nutscan_scan_xml_http(timeout);
return NULL;
}
#endif
static void * run_nut_old(void * arg)
{
@ -102,20 +102,16 @@ static void * run_nut_old(void * arg)
return NULL;
}
#ifdef HAVE_AVAHI_CLIENT_CLIENT_H
static void * run_avahi(void * arg)
{
dev[TYPE_AVAHI] = nutscan_scan_avahi(timeout);
return NULL;
}
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
static void * run_ipmi(void * arg)
{
dev[TYPE_IPMI] = nutscan_scan_ipmi();
return NULL;
}
#endif
#endif /* HAVE_PTHREAD */
static int printq(int quiet,const char *fmt, ...)
{
@ -147,9 +143,12 @@ int main(int argc, char *argv[])
int allow_ipmi = 0;
int quiet = 0;
void (*display_func)(nutscan_device_t * device);
int ret_code = EXIT_SUCCESS;
memset(&sec,0,sizeof(sec));
nutscan_init();
display_func = nutscan_display_ups_conf;
while((opt_ret = getopt_long(argc, argv, optstring, longopts, NULL))!=-1) {
@ -172,63 +171,87 @@ int main(int argc, char *argv[])
case 'm':
cidr = strdup(optarg);
break;
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
case 'c':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.community = strdup(optarg);
break;
case 'l':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.secLevel = strdup(optarg);
break;
case 'u':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.secName = strdup(optarg);
break;
case 'W':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.authPassword = strdup(optarg);
break;
case 'X':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.privPassword = strdup(optarg);
break;
case 'w':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.authProtocol = strdup(optarg);
break;
case 'x':
if(!nutscan_avail_snmp) {
goto display_help;
}
sec.privProtocol = strdup(optarg);
break;
#endif
case 'S':
if(!nutscan_avail_snmp) {
goto display_help;
}
allow_snmp = 1;
break;
case 'p':
port = strdup(optarg);
break;
case 'C':
allow_all = 1;
break;
#ifdef HAVE_USB_H
case 'U':
if(!nutscan_avail_usb) {
goto display_help;
}
allow_usb = 1;
break;
#endif
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
case 'S':
allow_snmp = 1;
break;
#endif
#ifdef WITH_NEON
case 'M':
if(!nutscan_avail_xml_http) {
goto display_help;
}
allow_xml = 1;
break;
#endif
case 'O':
allow_oldnut = 1;
break;
#ifdef WITH_AVAHI
case 'A':
if(!nutscan_avail_avahi) {
goto display_help;
}
allow_avahi = 1;
break;
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
case 'I':
if(!nutscan_avail_ipmi) {
goto display_help;
}
allow_ipmi = 1;
break;
#endif
case 'N':
display_func = nutscan_display_ups_conf;
break;
@ -238,51 +261,79 @@ int main(int argc, char *argv[])
case 'q':
quiet = 1;
break;
case 'h':
case 'V':
printf("Network UPS Tools - %s\n", NUT_VERSION_MACRO);
exit(EXIT_SUCCESS);
case 'a':
printf("OLDNUT\n");
if(nutscan_avail_usb) {
printf("USB\n");
}
if(nutscan_avail_snmp) {
printf("SNMP\n");
}
if(nutscan_avail_xml_http) {
printf("XML\n");
}
if(nutscan_avail_avahi) {
printf("AVAHI\n");
}
if(nutscan_avail_ipmi) {
printf("IPMI\n");
}
exit(EXIT_SUCCESS);
case '?':
ret_code = ERR_BAD_OPTION;
case 'h':
default:
puts("nut-scanner : detecting available UPS.\n");
display_help:
puts("nut-scanner : detecting available power devices.\n");
puts("OPTIONS:");
printf(" -C, --complete_scan : Scan all available devices (default).\n");
#ifdef HAVE_USB_H
printf(" -U, --usb_scan : Scan USB devices.\n");
#endif
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
printf(" -S, --snmp_scan : Scan SNMP devices.\n");
#endif
#ifdef WITH_NEON
printf(" -M, --xml_scan : Scan XML/HTTP devices.\n");
#endif
printf(" -O, --oldnut_scan : Scan NUT devices (old method).\n");
#ifdef WITH_AVAHI
printf(" -A, --avahi_scan : Scan NUT devices (avahi method).\n");
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
printf(" -I, --ipmi_scan : Scan IPMI devices.\n");
#endif
printf(" -C, --complete_scan: Scan all available devices (default).\n");
if( nutscan_avail_usb ) {
printf(" -U, --usb_scan: Scan USB devices.\n");
}
if( nutscan_avail_snmp ) {
printf(" -S, --snmp_scan: Scan SNMP devices.\n");
}
if( nutscan_avail_xml_http ) {
printf(" -M, --xml_scan: Scan XML/HTTP devices.\n");
}
printf(" -O, --oldnut_scan: Scan NUT devices (old method).\n");
if( nutscan_avail_avahi ) {
printf(" -A, --avahi_scan: Scan NUT devices (avahi method).\n");
}
if( nutscan_avail_ipmi ) {
printf(" -I, --ipmi_scan: Scan IPMI devices.\n");
}
printf(" -t, --timeout <timeout in seconds>: network operation timeout (default %d).\n",DEFAULT_TIMEOUT);
printf(" -s, --start_ip <IP address>: First IP address to scan.\n");
printf(" -e, --end_ip <IP address>: Last IP address to scan.\n");
printf(" -m, --mask_cidr <IP address/mask>: Give a range of IP using CIDR notation.\n");
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
printf("\nSNMP v1 specific options:\n");
printf(" -c, --community <community name>: Set SNMP v1 community name (default = public)\n");
if( nutscan_avail_snmp ) {
printf("\nSNMP v1 specific options:\n");
printf(" -c, --community <community name>: Set SNMP v1 community name (default = public)\n");
printf("\nSNMP v3 specific options:\n");
printf(" -l, --secLevel <security level>: Set the securityLevel used for SNMPv3 messages (allowed values: noAuthNoPriv,authNoPriv,authPriv)\n");
printf(" -u, --secName <security name>: Set the securityName used for authenticated SNMPv3 messages (mandatory if you set secLevel. No default)\n");
printf(" -a, --authProtocol <authentication protocol>: Set the authentication protocol (MD5 or SHA) used for authenticated SNMPv3 messages (default=MD5)\n");
printf(" -A, --authPassword <authentication pass phrase>: Set the authentication pass phrase used for authenticated SNMPv3 messages (mandatory if you set secLevel to authNoPriv or authPriv)\n");
printf(" -x, --privProtocol <privacy protocol>: Set the privacy protocol (DES or AES) used for encrypted SNMPv3 messages (default=DES)\n");
printf(" -X, --privPassword <privacy pass phrase>: Set the privacy pass phrase used for encrypted SNMPv3 messages (mandatory if you set secLevel to authPriv)\n");
#endif
printf("\nSNMP v3 specific options:\n");
printf(" -l, --secLevel <security level>: Set the securityLevel used for SNMPv3 messages (allowed values: noAuthNoPriv,authNoPriv,authPriv)\n");
printf(" -u, --secName <security name>: Set the securityName used for authenticated SNMPv3 messages (mandatory if you set secLevel. No default)\n");
printf(" -w, --authProtocol <authentication protocol>: Set the authentication protocol (MD5 or SHA) used for authenticated SNMPv3 messages (default=MD5)\n");
printf(" -W, --authPassword <authentication pass phrase>: Set the authentication pass phrase used for authenticated SNMPv3 messages (mandatory if you set secLevel to authNoPriv or authPriv)\n");
printf(" -x, --privProtocol <privacy protocol>: Set the privacy protocol (DES or AES) used for encrypted SNMPv3 messages (default=DES)\n");
printf(" -X, --privPassword <privacy pass phrase>: Set the privacy pass phrase used for encrypted SNMPv3 messages (mandatory if you set secLevel to authPriv)\n");
}
printf("\nNUT device specific options:\n");
printf(" -p, --port <port number>: Port number of remote NUT devices\n");
printf("\nNUT specific options:\n");
printf(" -p, --port <port number>: Port number of remote NUT upsd\n");
printf("\ndisplay specific options:\n");
printf(" -N, --disp_nut_conf : Display result in the ups.conf format\n");
printf(" -P, --disp_parsable : Display result in a parsable format\n");
return 0;
printf(" -N, --disp_nut_conf: Display result in the ups.conf format\n");
printf(" -P, --disp_parsable: Display result in a parsable format\n");
printf("\nMiscellaneous options:\n");
printf(" -V, --version: Display NUT version\n");
printf(" -a, --available: Display available bus that can be scanned\n");
printf(" -q, --quiet: Display only scan result. No information on currently scanned bus is displayed.\n");
return ret_code;
}
}
@ -296,120 +347,128 @@ int main(int argc, char *argv[])
allow_all = 1;
}
#ifdef HAVE_USB_H
if( allow_all || allow_usb) {
if( allow_all ) {
allow_usb = 1;
allow_snmp = 1;
allow_xml = 1;
allow_oldnut = 1;
allow_avahi = 1;
allow_ipmi = 1;
}
if( allow_usb && nutscan_avail_usb ) {
printq(quiet,"Scanning USB bus.\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_USB],NULL,run_usb,NULL);
if(pthread_create(&thread[TYPE_USB],NULL,run_usb,NULL)) {
nutscan_avail_usb = 0;
}
#else
dev[TYPE_USB] = nutscan_scan_usb();
#endif
#endif /* HAVE_PTHREAD */
}
#endif /* HAVE_USB_H */
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
if( allow_all || allow_snmp) {
if( allow_snmp && nutscan_avail_snmp ) {
if( start_ip == NULL ) {
printq(quiet,"No start IP, skipping SNMP\n");
}
else {
printq(quiet,"Scanning SNMP bus.\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_SNMP],NULL,run_snmp,&sec);
if( pthread_create(&thread[TYPE_SNMP],NULL,run_snmp,&sec)) {
nutscan_avail_snmp = 0;
}
#else
dev[TYPE_SNMP] = nutscan_scan_snmp(start_ip,end_ip,timeout,&sec);
#endif
#endif /* HAVE_PTHREAD */
}
}
#endif /* HAVE_NET_SNMP_NET_SNMP_CONFIG_H */
#ifdef WITH_NEON
if( allow_all || allow_xml) {
if( allow_xml && nutscan_avail_xml_http) {
printq(quiet,"Scanning XML/HTTP bus.\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_XML],NULL,run_xml,NULL);
if(pthread_create(&thread[TYPE_XML],NULL,run_xml,NULL)) {
nutscan_avail_xml_http = 0;
}
#else
dev[TYPE_XML] = nutscan_scan_xml_http(timeout);
#endif
#endif /* HAVE_PTHREAD */
}
#endif
if( allow_all || allow_oldnut) {
if( allow_oldnut && nutscan_avail_nut) {
if( start_ip == NULL ) {
printq(quiet,"No start IP, skipping NUT bus (old connect method)\n");
}
else {
printq(quiet,"Scanning NUT bus (old connect method).\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_NUT],NULL,run_nut_old,NULL);
if(pthread_create(&thread[TYPE_NUT],NULL,run_nut_old,NULL)) {
nutscan_avail_nut = 0;
}
#else
dev[TYPE_NUT] = nutscan_scan_nut(start_ip,end_ip,port,timeout);
#endif
#endif /* HAVE_PTHREAD */
}
}
#ifdef HAVE_AVAHI_CLIENT_CLIENT_H
if( allow_all || allow_avahi) {
if( allow_avahi && nutscan_avail_avahi) {
printq(quiet,"Scanning NUT bus (avahi method).\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_AVAHI],NULL,run_avahi,NULL);
if(pthread_create(&thread[TYPE_AVAHI],NULL,run_avahi,NULL)) {
nutscan_avail_avahi = 0;
}
#else
dev[TYPE_AVAHI] = nutscan_scan_avahi();
#endif
dev[TYPE_AVAHI] = nutscan_scan_avahi(timeout);
#endif /* HAVE_PTHREAD */
}
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
if( allow_all || allow_ipmi) {
if( allow_ipmi && nutscan_avail_ipmi) {
printq(quiet,"Scanning IPMI bus.\n");
#ifdef HAVE_PTHREAD
pthread_create(&thread[TYPE_IPMI],NULL,run_ipmi,NULL);
if(pthread_create(&thread[TYPE_IPMI],NULL,run_ipmi,NULL)) {
nutscan_avail_ipmi = 0;
}
#else
dev[TYPE_IPMI] = nutscan_scan_ipmi();
#endif
#endif /* HAVE_PTHREAD */
}
#endif
#ifdef HAVE_PTHREAD
#ifdef HAVE_USB_H
pthread_join(thread[TYPE_USB],NULL);
#endif
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
pthread_join(thread[TYPE_SNMP],NULL);
#endif
#ifdef WITH_NEON
pthread_join(thread[TYPE_XML],NULL);
#endif
pthread_join(thread[TYPE_NUT],NULL);
#ifdef HAVE_AVAHI_CLIENT_CLIENT_H
pthread_join(thread[TYPE_AVAHI],NULL);
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
pthread_join(thread[TYPE_IPMI],NULL);
#endif
if( allow_usb && nutscan_avail_usb ) {
pthread_join(thread[TYPE_USB],NULL);
}
if( allow_snmp && nutscan_avail_snmp ) {
pthread_join(thread[TYPE_SNMP],NULL);
}
if( allow_xml && nutscan_avail_xml_http ) {
pthread_join(thread[TYPE_XML],NULL);
}
if( allow_oldnut && nutscan_avail_nut ) {
pthread_join(thread[TYPE_NUT],NULL);
}
if( allow_avahi && nutscan_avail_avahi ) {
pthread_join(thread[TYPE_AVAHI],NULL);
}
if( allow_ipmi && nutscan_avail_ipmi ) {
pthread_join(thread[TYPE_IPMI],NULL);
}
#endif /* HAVE_PTHREAD */
#ifdef HAVE_USB_H
display_func(dev[TYPE_USB]);
nutscan_free_device(dev[TYPE_USB]);
#endif
#ifdef HAVE_NET_SNMP_NET_SNMP_CONFIG_H
display_func(dev[TYPE_SNMP]);
nutscan_free_device(dev[TYPE_SNMP]);
#endif
#ifdef WITH_NEON
display_func(dev[TYPE_XML]);
nutscan_free_device(dev[TYPE_XML]);
#endif
display_func(dev[TYPE_NUT]);
nutscan_free_device(dev[TYPE_NUT]);
#ifdef HAVE_AVAHI_CLIENT_CLIENT_H
display_func(dev[TYPE_AVAHI]);
nutscan_free_device(dev[TYPE_AVAHI]);
#endif
#ifdef HAVE_FREEIPMI_FREEIPMI_H
display_func(dev[TYPE_IPMI]);
nutscan_free_device(dev[TYPE_IPMI]);
#endif
return EXIT_SUCCESS;
}