049383da6fe3bc9c80efc68c807d07526fb18796
1 /*
2 * collectd - src/apcups.c
3 * Copyright (C) 2006 Anthony Gialluca <tonyabg at charter.net>
4 * Copyright (C) 2000-2004 Kern Sibbald
5 * Copyright (C) 1996-99 Andre M. Hedrick <andre at suse.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General
9 * Public License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Authors:
22 * Anthony Gialluca <tonyabg at charter.net>
23 **/
25 #include "collectd.h"
26 #include "common.h" /* rrd_update_file */
27 #include "plugin.h" /* plugin_register, plugin_submit */
28 #include "configfile.h" /* cf_register */
30 /* FIXME: Check defines before including anything! */
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <signal.h>
36 #include <ctype.h>
37 #include <syslog.h>
38 #include <limits.h>
39 #include <pwd.h>
40 #include <time.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <setjmp.h>
44 #include <termios.h>
45 #include <netdb.h>
46 #include <sys/ioctl.h>
47 #include <sys/ipc.h>
48 #include <sys/sem.h>
49 #include <sys/shm.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 #include <sys/time.h>
53 #include <time.h>
54 #include <sys/wait.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
58 #define NISPORT 3551
59 #define _(String) (String)
60 #define N_(String) (String)
61 #define MAXSTRING 256
62 #define MODULE_NAME "apcups"
65 /* Prototypes */
66 static void generic_error_out(const char *, int , const char *, ...);
68 /* Default values for contacting daemon */
69 static char *host = "localhost";
70 static int port = NISPORT;
72 static struct sockaddr_in tcp_serv_addr; /* socket information */
73 static char *net_errmsg = NULL; /* pointer to error message */
74 static char net_errbuf[256]; /* error message buffer for messages */
76 /*
77 * The following are only if not compiled to test the module with its own main.
78 */
79 /* FIXME: Rename DSes to be more generic and follow established conventions. */
80 #ifndef APCMAIN
81 static char *volt_file_template = "apcups_volt-%s.rrd";
82 static char *volt_ds_def[] =
83 {
84 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
85 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
86 NULL
87 };
88 static int volt_ds_num = 2;
90 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
91 static char *bvolt_ds_def[] =
92 {
93 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
94 };
95 static int bvolt_ds_num = 1;
97 static char *load_file_template = "apcups_load-%s.rrd";
98 static char *load_ds_def[] =
99 {
100 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
101 };
102 static int load_ds_num = 1;
104 static char *charge_file_template = "apcups_charge-%s.rrd";
105 static char *charge_ds_def[] =
106 {
107 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
108 };
109 static int charge_ds_num = 1;
111 static char *time_file_template = "apcups_time-%s.rrd";
112 static char *time_ds_def[] =
113 {
114 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
115 };
116 static int time_ds_num = 1;
118 static char *temp_file_template = "apcups_temp-%s.rrd";
119 static char *temp_ds_def[] =
120 {
121 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
122 };
123 static int temp_ds_num = 1;
125 static char *freq_file_template = "apcups_freq-%s.rrd";
126 static char *freq_ds_def[] =
127 {
128 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
129 };
130 static int freq_ds_num = 1;
132 static char *config_keys[] =
133 {
134 "Host",
135 "Port",
136 NULL
137 };
138 static int config_keys_num = 2;
140 #endif /* ifndef APCMAIN */
142 struct apc_detail_s
143 {
144 float linev;
145 float loadpct;
146 float bcharge;
147 float timeleft;
148 float outputv;
149 float itemp;
150 float battv;
151 float linefreq;
152 };
154 #define BIG_BUF 4096
156 /*
157 * Subroutine normally called by macro error_abort() to print
158 * FATAL ERROR message and supplied error message
159 */
160 static void generic_error_out(const char *file, int line, const char *fmt, ...)
161 {
162 char buf[256];
163 va_list arg_ptr;
164 int i;
166 snprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
167 i = strlen(buf);
168 va_start(arg_ptr, fmt);
169 vsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
170 va_end(arg_ptr);
171 fprintf(stdout, "%s", buf);
173 exit(1);
174 }
176 /*
177 * Read nbytes from the network.
178 * It is possible that the total bytes require in several
179 * read requests
180 */
181 static int read_nbytes(int fd, char *ptr, int nbytes)
182 {
183 int nleft, nread;
185 nleft = nbytes;
187 while (nleft > 0) {
188 do {
189 nread = read(fd, ptr, nleft);
190 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
192 if (nread <= 0) {
193 return (nread); /* error, or EOF */
194 }
196 nleft -= nread;
197 ptr += nread;
198 }
200 return (nbytes - nleft); /* return >= 0 */
201 }
203 /*
204 * Write nbytes to the network.
205 * It may require several writes.
206 */
207 static int write_nbytes(int fd, void *buf, int buflen)
208 {
209 int nleft;
210 int nwritten;
211 char *ptr;
213 ptr = (char *) buf;
215 nleft = buflen;
216 while (nleft > 0)
217 {
218 nwritten = write(fd, ptr, nleft);
220 if (nwritten <= 0)
221 {
222 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
223 return (nwritten);
224 }
226 nleft -= nwritten;
227 ptr += nwritten;
228 }
230 /* If we get here, (nleft <= 0) is true */
231 return (buflen);
232 }
234 /* Close the network connection */
235 static void net_close (int sockfd)
236 {
237 short pktsiz = 0;
239 /* send EOF sentinel */
240 write_nbytes (sockfd, &pktsiz, sizeof(short));
241 close (sockfd);
242 }
245 /*
246 * Open a TCP connection to the UPS network server
247 * Returns -1 on error
248 * Returns socket file descriptor otherwise
249 */
250 static int net_open(char *host, char *service, int port)
251 {
252 int sockfd;
253 struct hostent *hp;
254 unsigned int inaddr; /* Careful here to use unsigned int for */
255 /* compatibility with Alpha */
257 /*
258 * Fill in the structure serv_addr with the address of the server that
259 * we want to connect with.
260 */
261 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
262 tcp_serv_addr.sin_family = AF_INET;
263 tcp_serv_addr.sin_port = htons(port);
265 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
266 tcp_serv_addr.sin_addr.s_addr = inaddr;
267 } else {
268 if ((hp = gethostbyname(host)) == NULL) {
269 net_errmsg = "tcp_open: hostname error\n";
270 return -1;
271 }
273 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
274 net_errmsg = "tcp_open: funny gethostbyname value\n";
275 return -1;
276 }
278 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
279 }
281 /* Open a TCP socket */
282 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
283 net_errmsg = "tcp_open: cannot open stream socket\n";
284 return -1;
285 }
287 /* connect to server */
288 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
289 /*
290 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
291 * implementations. Rationale is the same as described above.
292 */
293 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
294 #endif
296 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
297 snprintf(net_errbuf, sizeof(net_errbuf),
298 _("tcp_open: cannot connect to server %s on port %d.\n"
299 "ERR=%s\n"), host, port, strerror(errno));
300 net_errmsg = net_errbuf;
301 close(sockfd);
302 return -1;
303 }
305 return sockfd;
306 } /* int net_open(char *host, char *service, int port) */
308 /*
309 * Receive a message from the other end. Each message consists of
310 * two packets. The first is a header that contains the size
311 * of the data that follows in the second packet.
312 * Returns number of bytes read
313 * Returns 0 on end of file
314 * Returns -1 on hard end of file (i.e. network connection close)
315 * Returns -2 on error
316 */
317 static int net_recv(int sockfd, char *buff, int maxlen)
318 {
319 int nbytes;
320 short pktsiz;
322 /* get data size -- in short */
323 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
324 /* probably pipe broken because client died */
325 return -1; /* assume hard EOF received */
326 }
327 if (nbytes != sizeof(short))
328 return -2;
330 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
331 if (pktsiz > maxlen) {
332 net_errmsg = "net_recv: record length too large\n";
333 return -2;
334 }
335 if (pktsiz == 0)
336 return 0; /* soft EOF */
338 /* now read the actual data */
339 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
340 net_errmsg = "net_recv: read_nbytes error\n";
341 return -2;
342 }
343 if (nbytes != pktsiz) {
344 net_errmsg = "net_recv: error in read_nbytes\n";
345 return -2;
346 }
348 return (nbytes); /* return actual length of message */
349 }
351 /*
352 * Send a message over the network. The send consists of
353 * two network packets. The first is sends a short containing
354 * the length of the data packet which follows.
355 * Returns zero on success
356 * Returns non-zero on error
357 */
358 static int net_send(int sockfd, char *buff, int len)
359 {
360 int rc;
361 short packet_size;
363 /* send short containing size of data packet */
364 packet_size = htons ((short) len);
366 rc = write_nbytes(sockfd, &packet_size, sizeof (packet_size));
367 if (rc != sizeof(packet_size))
368 return (-1);
370 /* send data packet */
371 rc = write_nbytes (sockfd, buff, len);
372 if (rc != len)
373 return (-1);
375 return (0);
376 }
378 /* Get and print status from apcupsd NIS server */
379 static int do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
380 {
381 int sockfd;
382 int n;
383 char recvline[MAXSTRING + 1];
384 char *tokptr;
385 char *key;
386 double value;
387 #if APCMAIN
388 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
389 #else
390 # define PRINT_VALUE(name, val) /**/
391 #endif
393 /* TODO: Keep the socket open, if possible */
394 if ((sockfd = net_open (host, NULL, port)) < 0)
395 {
396 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
397 return (-1);
398 }
400 net_send (sockfd, "status", 6);
402 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
403 {
404 recvline[n] = '\0';
405 #if APCMAIN
406 printf ("net_recv = `%s';\n", recvline);
407 #endif /* if APCMAIN */
409 tokptr = strtok (recvline, ":");
410 while (tokptr != NULL)
411 {
412 key = tokptr;
413 if ((tokptr = strtok (NULL, " \t")) == NULL)
414 continue;
415 value = atof (tokptr);
416 PRINT_VALUE (key, value);
418 if (strcmp ("LINEV", key) == 0)
419 apcups_detail->linev = value;
420 else if (strcmp ("BATTV", tokptr) == 0)
421 apcups_detail->battv = value;
422 else if (strcmp ("ITEMP", tokptr) == 0)
423 apcups_detail->itemp = value;
424 else if (strcmp ("LOADPCT", tokptr) == 0)
425 apcups_detail->loadpct = value;
426 else if (strcmp ("BCHARGE", tokptr) == 0)
427 apcups_detail->bcharge = value;
428 else if (strcmp ("OUTPUTV", tokptr) == 0)
429 apcups_detail->outputv = value;
430 else if (strcmp ("LINEFREQ", tokptr) == 0)
431 apcups_detail->linefreq = value;
432 else if (strcmp ("TIMELEFT", tokptr) == 0)
433 apcups_detail->timeleft = value;
434 else
435 {
436 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
437 key, value);
438 }
440 tokptr = strtok (NULL, ":");
441 } /* while (tokptr != NULL) */
442 }
444 net_close (sockfd);
446 if (n < 0)
447 {
448 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
449 return (-1);
450 }
452 return (0);
453 }
455 #ifdef APCMAIN
456 int main(int argc, char **argv)
457 {
458 /* we are not really going to use this */
459 struct apc_detail_s apcups_detail;
461 if (!*host || strcmp(host, "0.0.0.0") == 0)
462 host = "localhost";
464 do_pthreads_status(host, port, &apcups_detail);
466 return 0;
467 }
468 #else
469 static void apcups_init (void)
470 {
471 return;
472 }
474 static int apcups_config (char *key, char *value)
475 {
476 static char lhost[126];
478 if (strcasecmp (key, "host") == 0)
479 {
480 lhost[0] = '\0';
481 strcpy(lhost,key);
482 host = lhost;
483 }
484 else if (strcasecmp (key, "Port") == 0)
485 {
486 int port_tmp = atoi (value);
487 if(port_tmp < 1 || port_tmp > 65535) {
488 syslog (LOG_WARNING, "apcups: `port' failed: %s",
489 value);
490 return (1);
491 } else {
492 port = port_tmp;
493 }
494 }
495 else
496 {
497 return (-1);
498 }
499 return(0);
500 }
502 #define BUFSIZE 256
503 static void apcups_submit (char *host,
504 struct apc_detail_s *apcups_detail)
505 {
506 char buf[BUFSIZE];
508 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
509 (unsigned int) curtime,
510 apcups_detail->linev,
511 apcups_detail->outputv) >= BUFSIZE)
512 return;
514 plugin_submit (MODULE_NAME, host, buf);
515 }
517 static void apc_bvolt_submit (char *host,
518 struct apc_detail_s *apcups_detail)
519 {
520 char buf[BUFSIZE];
522 if (snprintf (buf, BUFSIZE, "%u:%f",
523 (unsigned int) curtime,
524 apcups_detail->battv) >= BUFSIZE)
525 return;
527 plugin_submit ("apcups_bvolt", host, buf);
528 }
530 static void apc_load_submit (char *host,
531 struct apc_detail_s *apcups_detail)
532 {
533 char buf[BUFSIZE];
535 if (snprintf (buf, BUFSIZE, "%u:%f",
536 (unsigned int) curtime,
537 apcups_detail->loadpct) >= BUFSIZE)
538 return;
540 plugin_submit ("apcups_load", host, buf);
541 }
543 static void apc_charge_submit (char *host,
544 struct apc_detail_s *apcups_detail)
545 {
546 char buf[BUFSIZE];
548 if (snprintf (buf, BUFSIZE, "%u:%f",
549 (unsigned int) curtime,
550 apcups_detail->bcharge) >= BUFSIZE)
551 return;
553 plugin_submit ("apcups_charge", host, buf);
554 }
556 static void apc_temp_submit (char *host,
557 struct apc_detail_s *apcups_detail)
558 {
559 char buf[BUFSIZE];
561 if (snprintf (buf, BUFSIZE, "%u:%f",
562 (unsigned int) curtime,
563 apcups_detail->itemp) >= BUFSIZE)
564 return;
566 plugin_submit ("apcups_temp", host, buf);
567 }
569 static void apc_time_submit (char *host,
570 struct apc_detail_s *apcups_detail)
571 {
572 char buf[BUFSIZE];
574 if (snprintf (buf, BUFSIZE, "%u:%f",
575 (unsigned int) curtime,
576 apcups_detail->timeleft) >= BUFSIZE)
577 return;
579 plugin_submit ("apcups_time", host, buf);
580 }
582 static void apc_freq_submit (char *host,
583 struct apc_detail_s *apcups_detail)
584 {
585 char buf[BUFSIZE];
587 if (snprintf (buf, BUFSIZE, "%u:%f",
588 (unsigned int) curtime,
589 apcups_detail->linefreq) >= BUFSIZE)
590 return;
592 plugin_submit ("apcups_freq", host, buf);
593 }
594 #undef BUFSIZE
596 static void apcups_read (void)
597 {
598 struct apc_detail_s apcups_detail;
600 apcups_detail.linev = 0.0;
601 apcups_detail.loadpct = 0.0;
602 apcups_detail.bcharge = 0.0;
603 apcups_detail.timeleft = 0.0;
604 apcups_detail.outputv = 0.0;
605 apcups_detail.itemp = 0.0;
606 apcups_detail.battv = 0.0;
607 apcups_detail.linefreq = 0.0;
610 if (!*host || strcmp(host, "0.0.0.0") == 0)
611 host = "localhost";
613 do_pthreads_status(host, port, &apcups_detail);
615 apcups_submit (host, &apcups_detail);
616 apc_bvolt_submit (host, &apcups_detail);
617 apc_load_submit (host, &apcups_detail);
618 apc_charge_submit (host, &apcups_detail);
619 apc_temp_submit (host, &apcups_detail);
620 apc_time_submit (host, &apcups_detail);
621 apc_freq_submit (host, &apcups_detail);
622 }
625 static void apcups_write (char *host, char *inst, char *val)
626 {
627 char file[512];
628 int status;
630 status = snprintf (file, 512, volt_file_template, inst);
631 if (status < 1)
632 return;
633 else if (status >= 512)
634 return;
636 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
637 }
639 static void apc_bvolt_write (char *host, char *inst, char *val)
640 {
641 char file[512];
642 int status;
644 status = snprintf (file, 512, bvolt_file_template, inst);
645 if (status < 1)
646 return;
647 else if (status >= 512)
648 return;
650 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
651 }
653 static void apc_load_write (char *host, char *inst, char *val)
654 {
655 char file[512];
656 int status;
658 status = snprintf (file, 512, load_file_template, inst);
659 if (status < 1)
660 return;
661 else if (status >= 512)
662 return;
664 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
665 }
667 static void apc_charge_write (char *host, char *inst, char *val)
668 {
669 char file[512];
670 int status;
672 status = snprintf (file, 512, charge_file_template, inst);
673 if (status < 1)
674 return;
675 else if (status >= 512)
676 return;
678 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
679 }
681 static void apc_temp_write (char *host, char *inst, char *val)
682 {
683 char file[512];
684 int status;
686 status = snprintf (file, 512, temp_file_template, inst);
687 if (status < 1)
688 return;
689 else if (status >= 512)
690 return;
692 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
693 }
695 static void apc_time_write (char *host, char *inst, char *val)
696 {
697 char file[512];
698 int status;
700 status = snprintf (file, 512, time_file_template, inst);
701 if (status < 1)
702 return;
703 else if (status >= 512)
704 return;
706 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
707 }
709 static void apc_freq_write (char *host, char *inst, char *val)
710 {
711 char file[512];
712 int status;
714 status = snprintf (file, 512, freq_file_template, inst);
715 if (status < 1)
716 return;
717 else if (status >= 512)
718 return;
720 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
721 }
723 void module_register (void)
724 {
725 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
726 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
727 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
728 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
729 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
730 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
731 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
732 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
733 }
735 #endif /* ifdef APCMAIN */
736 #undef MODULE_NAME