From: octo Date: Fri, 19 May 2006 06:16:07 +0000 (+0000) Subject: Added the apcups plugin to the build system. X-Git-Tag: svn-trunk~27 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=bfec4e6fa12d3ff5f3a4f36cc35b296abe2c04ad;p=collectd.git Added the apcups plugin to the build system. Removed the av?snprintf-auxiliary-functions from the apcups-plugin. `fixed' indentation in the apcups-plugin (not done yet). --- diff --git a/configure.in b/configure.in index d13689ef..8d3bad41 100644 --- a/configure.in +++ b/configure.in @@ -819,6 +819,7 @@ AC_COLLECTD([daemon], [disable], [feature], [daemon mode]) m4_divert_once([HELP_ENABLE], [ collectd modules:]) AC_COLLECTD([apache], [disable], [module], [Apache httpd statistics]) +AC_COLLECTD([apcups], [disable], [module], [Statistics of UPSes by APC]) AC_COLLECTD([apple_sensors], [disable], [module], [Apple's hardware sensors]) AC_COLLECTD([battery], [disable], [module], [battery statistics]) AC_COLLECTD([cpu], [disable], [module], [cpu usage statistics]) @@ -1080,6 +1081,7 @@ Configuration: Modules: apache . . . . . . $enable_apache + apcups . . . . . . $enable_apcups apple_sensors . . . $enable_apple_sensors battery . . . . . . $enable_battery cpu . . . . . . . . $enable_cpu diff --git a/src/Makefile.am b/src/Makefile.am index b281232c..5646a062 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -75,6 +75,17 @@ collectd_LDADD += "-dlopen" apache.la collectd_DEPENDENCIES += apache.la endif +if BUILD_MODULE_APCUPS +pkglib_LTLIBRARIES += apcups.la +apcups_la_SOURCES = apcups.c +apcups_la_LDFLAGS = -module -avoid-version +if BUILD_WITH_LIBSOCKET +apcups_la_LDFLAGS += -lsocket +endif +collectd_LDADD += "-dlopen" apcups.la +collectd_DEPENDENCIES += apcups.la +endif + if BUILD_MODULE_APPLE_SENSORS pkglib_LTLIBRARIES += apple_sensors.la apple_sensors_la_SOURCES = apple_sensors.c diff --git a/src/apcups.c b/src/apcups.c index 50e6fe83..f481d0eb 100644 --- a/src/apcups.c +++ b/src/apcups.c @@ -1,6 +1,8 @@ - /* +/* + * collectd - src/apcups.c + * Copyright (C) 2006 Anthony Gialluca * Copyright (C) 2000-2004 Kern Sibbald - * Copyright (C) 1996-99 Andre M. Hedrick + * Copyright (C) 1996-99 Andre M. Hedrick * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General @@ -15,12 +17,19 @@ * License along with this program; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. - */ + * + * Authors: + * Anthony Gialluca + **/ -# include -# include -# include -# include +#include "collectd.h" +#include "common.h" /* rrd_update_file */ +#include "plugin.h" /* plugin_register, plugin_submit */ +#include "configfile.h" /* cf_register */ + +/* FIXME: Check defines before including anything! */ +#include +#include #include #include #include @@ -35,51 +44,41 @@ #include #include #include -# include -# include -# include -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include "collectd.h" -#include "common.h" /* rrd_update_file */ -#include "plugin.h" /* plugin_register, plugin_submit */ -#include "configfile.h" /* cf_register */ - #define NISPORT 3551 #define _(String) (String) #define N_(String) (String) #define MAXSTRING 256 -#define Error_abort0(fmd) error_out(__FILE__, __LINE__, fmd) +#define Error_abort0(fmd) generic_error_out(__FILE__, __LINE__, fmd) #define MODULE_NAME "apcups" /* Prototypes */ -void generic_error_out(const char *, int , const char *, ...); +static void generic_error_out(const char *, int , const char *, ...); /* Default values for contacting daemon */ static char *host = "localhost"; static int port = NISPORT; -char argvalue[MAXSTRING]; - -struct sockaddr_in tcp_serv_addr; /* socket information */ -int net_errno = 0; /* error number -- not yet implemented */ -char *net_errmsg = NULL; /* pointer to error message */ -char net_errbuf[256]; /* error message buffer for messages */ - -void (*error_out) (const char *file, int line, const char *fmt, ...) = generic_error_out; -void (*error_cleanup) (void) = NULL; +static struct sockaddr_in tcp_serv_addr; /* socket information */ +static int net_errno = 0; /* error number -- not yet implemented */ +static char *net_errmsg = NULL; /* pointer to error message */ +static char net_errbuf[256]; /* error message buffer for messages */ /* - * The following are only if not compiled to test the module with - * its own main. + * The following are only if not compiled to test the module with its own main. */ +/* FIXME: Rename DSes to be more generic and follow established conventions. */ #ifndef APCMAIN static char *volt_file_template = "apcups_volt-%s.rrd"; static char *volt_ds_def[] = @@ -142,116 +141,38 @@ static int config_keys_num = 2; #endif /* ifndef APCMAIN */ -struct apc_detail_s { - float linev; - float loadpct; - float bcharge; - float timeleft; - float outputv; - float itemp; - float battv; - float linefreq; -}; - -/* Guarantee that the string is properly terminated */ -char *astrncpy(char *dest, const char *src, int maxlen) -{ - strncpy(dest, src, maxlen - 1); - dest[maxlen - 1] = 0; - return dest; -} - - -#define BIG_BUF 5000 - -/* Implement snprintf */ -int asnprintf(char *str, size_t size, const char *fmt, ...) -{ -#ifdef HAVE_VSNPRINTF - va_list arg_ptr; - int len; - - va_start(arg_ptr, fmt); - len = vsnprintf(str, size, fmt, arg_ptr); - va_end(arg_ptr); - - str[size - 1] = 0; - return len; - -#else - - va_list arg_ptr; - int len; - char *buf; - - buf = (char *)malloc(BIG_BUF); - - va_start(arg_ptr, fmt); - len = vsprintf(buf, fmt, arg_ptr); - va_end(arg_ptr); - - if (len >= BIG_BUF) - Error_abort0(_("Buffer overflow.\n")); - - memcpy(str, buf, size); - str[size - 1] = 0; - - free(buf); - return len; -#endif -} - -/* Implement vsnprintf() */ -int avsnprintf(char *str, size_t size, const char *format, va_list ap) +struct apc_detail_s { -#ifdef HAVE_VSNPRINTF - int len; - - len = vsnprintf(str, size, format, ap); - str[size - 1] = 0; - - return len; - -#else - - int len; - char *buf; - - buf = (char *)malloc(BIG_BUF); - - len = vsprintf(buf, format, ap); - if (len >= BIG_BUF) - Error_abort0(_("Buffer overflow.\n")); - - memcpy(str, buf, size); - str[size - 1] = 0; + float linev; + float loadpct; + float bcharge; + float timeleft; + float outputv; + float itemp; + float battv; + float linefreq; +}; - free(buf); - return len; -#endif -} +#define BIG_BUF 4096 /* * Subroutine normally called by macro error_abort() to print * FATAL ERROR message and supplied error message */ -void generic_error_out(const char *file, int line, const char *fmt, ...) +static void generic_error_out(const char *file, int line, const char *fmt, ...) { - char buf[256]; - va_list arg_ptr; - int i; + char buf[256]; + va_list arg_ptr; + int i; - asnprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line); - i = strlen(buf); - va_start(arg_ptr, fmt); - avsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr); - va_end(arg_ptr); - fprintf(stdout, "%s", buf); + snprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line); + i = strlen(buf); + va_start(arg_ptr, fmt); + vsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr); + va_end(arg_ptr); + fprintf(stdout, "%s", buf); - if (error_cleanup) - error_cleanup(); - - exit(1); + exit(1); } /* @@ -261,62 +182,59 @@ void generic_error_out(const char *file, int line, const char *fmt, ...) */ static int read_nbytes(int fd, char *ptr, int nbytes) { - int nleft, nread; + int nleft, nread; + nleft = nbytes; - nleft = nbytes; + while (nleft > 0) { + do { + nread = read(fd, ptr, nleft); + } while (nread == -1 && (errno == EINTR || errno == EAGAIN)); - while (nleft > 0) { - do { - nread = read(fd, ptr, nleft); - } while (nread == -1 && (errno == EINTR || errno == EAGAIN)); + if (nread <= 0) { + net_errno = errno; + return (nread); /* error, or EOF */ + } - if (nread <= 0) { - net_errno = errno; - return (nread); /* error, or EOF */ - } + nleft -= nread; + ptr += nread; + } - nleft -= nread; - ptr += nread; - } - - return (nbytes - nleft); /* return >= 0 */ + return (nbytes - nleft); /* return >= 0 */ } - /* * Write nbytes to the network. * It may require several writes. */ static int write_nbytes(int fd, char *ptr, int nbytes) { - int nleft, nwritten; + int nleft, nwritten; - nleft = nbytes; - while (nleft > 0) { - nwritten = write(fd, ptr, nleft); + nleft = nbytes; + while (nleft > 0) { + nwritten = write(fd, ptr, nleft); - if (nwritten <= 0) { - net_errno = errno; - return (nwritten); /* error */ - } + if (nwritten <= 0) { + net_errno = errno; + return (nwritten); /* error */ + } - nleft -= nwritten; - ptr += nwritten; - } + nleft -= nwritten; + ptr += nwritten; + } - return (nbytes - nleft); + return (nbytes - nleft); } - /* Close the network connection */ -void net_close(int sockfd) +void net_close (int sockfd) { - short pktsiz = 0; + short pktsiz = 0; - /* send EOF sentinel */ - write_nbytes(sockfd, (char *)&pktsiz, sizeof(short)); - close(sockfd); + /* send EOF sentinel */ + write_nbytes(sockfd, (char *) &pktsiz, sizeof(short)); + close (sockfd); } @@ -327,64 +245,61 @@ void net_close(int sockfd) */ int net_open(char *host, char *service, int port) { - int sockfd; - struct hostent *hp; - unsigned int inaddr; /* Careful here to use unsigned int for */ - /* compatibility with Alpha */ - - /* - * Fill in the structure serv_addr with the address of - * the server that we want to connect with. - */ - memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr)); - tcp_serv_addr.sin_family = AF_INET; - tcp_serv_addr.sin_port = htons(port); - - if ((inaddr = inet_addr(host)) != INADDR_NONE) { - tcp_serv_addr.sin_addr.s_addr = inaddr; - } else { - if ((hp = gethostbyname(host)) == NULL) { - net_errmsg = "tcp_open: hostname error\n"; - return -1; - } - - if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) { - net_errmsg = "tcp_open: funny gethostbyname value\n"; - return -1; - } - - tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr; - } - - - /* Open a TCP socket */ - if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - net_errmsg = "tcp_open: cannot open stream socket\n"; - return -1; - } - - /* connect to server */ + int sockfd; + struct hostent *hp; + unsigned int inaddr; /* Careful here to use unsigned int for */ + /* compatibility with Alpha */ + + /* + * Fill in the structure serv_addr with the address of the server that + * we want to connect with. + */ + memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr)); + tcp_serv_addr.sin_family = AF_INET; + tcp_serv_addr.sin_port = htons(port); + + if ((inaddr = inet_addr(host)) != INADDR_NONE) { + tcp_serv_addr.sin_addr.s_addr = inaddr; + } else { + if ((hp = gethostbyname(host)) == NULL) { + net_errmsg = "tcp_open: hostname error\n"; + return -1; + } + + if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) { + net_errmsg = "tcp_open: funny gethostbyname value\n"; + return -1; + } + + tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr; + } + + /* Open a TCP socket */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + net_errmsg = "tcp_open: cannot open stream socket\n"; + return -1; + } + + /* connect to server */ #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS - /* - * Work around a bug in OpenBSD & FreeBSD userspace pthreads - * implementations. Rationale is the same as described above. - */ - fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL)); + /* + * Work around a bug in OpenBSD & FreeBSD userspace pthreads + * implementations. Rationale is the same as described above. + */ + fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL)); #endif - if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) { - asnprintf(net_errbuf, sizeof(net_errbuf), - _("tcp_open: cannot connect to server %s on port %d.\n" - "ERR=%s\n"), host, port, strerror(errno)); - net_errmsg = net_errbuf; - close(sockfd); - return -1; - } - - return sockfd; -} - - + if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) { + snprintf(net_errbuf, sizeof(net_errbuf), + _("tcp_open: cannot connect to server %s on port %d.\n" + "ERR=%s\n"), host, port, strerror(errno)); + net_errmsg = net_errbuf; + close(sockfd); + return -1; + } + + return sockfd; +} /* int net_open(char *host, char *service, int port) */ /* * Receive a message from the other end. Each message consists of