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 #if HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #if HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #if HAVE_NETDB_H
37 # include <netdb.h>
38 #endif
39 #if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h> /* inet_addr */
44 #endif
46 #if 0
47 #include <pwd.h>
48 #include <setjmp.h> /* FIXME: Is this really neccessary? */
49 #include <termios.h> /* FIXME: Is this really neccessary? */
50 #include <sys/ioctl.h> /* FIXME: Is this really neccessary? */
51 #include <sys/ipc.h> /* FIXME: Is this really neccessary? */
52 #include <sys/sem.h> /* FIXME: Is this really neccessary? */
53 #include <sys/shm.h> /* FIXME: Is this really neccessary? */
54 #endif
56 #define NISPORT 3551
57 #define _(String) (String)
58 #define N_(String) (String)
59 #define MAXSTRING 256
60 #define MODULE_NAME "apcups"
62 /* Default values for contacting daemon */
63 static char *host = "localhost";
64 static int port = NISPORT;
66 static struct sockaddr_in tcp_serv_addr; /* socket information */
67 static char *net_errmsg = NULL; /* pointer to error message */
68 static char net_errbuf[256]; /* error message buffer for messages */
70 /*
71 * The following are only if not compiled to test the module with its own main.
72 */
73 /* FIXME: Rename DSes to be more generic and follow established conventions. */
74 #ifndef APCMAIN
75 static char *volt_file_template = "apcups_volt-%s.rrd";
76 static char *volt_ds_def[] =
77 {
78 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
79 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
80 NULL
81 };
82 static int volt_ds_num = 2;
84 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
85 static char *bvolt_ds_def[] =
86 {
87 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
88 };
89 static int bvolt_ds_num = 1;
91 static char *load_file_template = "apcups_load-%s.rrd";
92 static char *load_ds_def[] =
93 {
94 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
95 };
96 static int load_ds_num = 1;
98 static char *charge_file_template = "apcups_charge-%s.rrd";
99 static char *charge_ds_def[] =
100 {
101 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
102 };
103 static int charge_ds_num = 1;
105 static char *time_file_template = "apcups_time-%s.rrd";
106 static char *time_ds_def[] =
107 {
108 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
109 };
110 static int time_ds_num = 1;
112 static char *temp_file_template = "apcups_temp-%s.rrd";
113 static char *temp_ds_def[] =
114 {
115 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
116 };
117 static int temp_ds_num = 1;
119 static char *freq_file_template = "apcups_freq-%s.rrd";
120 static char *freq_ds_def[] =
121 {
122 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
123 };
124 static int freq_ds_num = 1;
126 static char *config_keys[] =
127 {
128 "Host",
129 "Port",
130 NULL
131 };
132 static int config_keys_num = 2;
134 #endif /* ifndef APCMAIN */
136 struct apc_detail_s
137 {
138 float linev;
139 float loadpct;
140 float bcharge;
141 float timeleft;
142 float outputv;
143 float itemp;
144 float battv;
145 float linefreq;
146 };
148 #define BIG_BUF 4096
150 /*
151 * Read nbytes from the network.
152 * It is possible that the total bytes require in several
153 * read requests
154 */
155 static int read_nbytes(int fd, char *ptr, int nbytes)
156 {
157 int nleft, nread;
159 nleft = nbytes;
161 while (nleft > 0) {
162 do {
163 nread = read(fd, ptr, nleft);
164 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
166 if (nread <= 0) {
167 return (nread); /* error, or EOF */
168 }
170 nleft -= nread;
171 ptr += nread;
172 }
174 return (nbytes - nleft); /* return >= 0 */
175 }
177 /*
178 * Write nbytes to the network.
179 * It may require several writes.
180 */
181 static int write_nbytes(int fd, void *buf, int buflen)
182 {
183 int nleft;
184 int nwritten;
185 char *ptr;
187 ptr = (char *) buf;
189 nleft = buflen;
190 while (nleft > 0)
191 {
192 nwritten = write(fd, ptr, nleft);
194 if (nwritten <= 0)
195 {
196 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
197 return (nwritten);
198 }
200 nleft -= nwritten;
201 ptr += nwritten;
202 }
204 /* If we get here, (nleft <= 0) is true */
205 return (buflen);
206 }
208 /* Close the network connection */
209 static void net_close (int sockfd)
210 {
211 short pktsiz = 0;
213 /* send EOF sentinel */
214 write_nbytes (sockfd, &pktsiz, sizeof (short));
215 close (sockfd);
216 }
219 /*
220 * Open a TCP connection to the UPS network server
221 * Returns -1 on error
222 * Returns socket file descriptor otherwise
223 */
224 static int net_open(char *host, char *service, int port)
225 {
226 int sockfd;
227 struct hostent *hp;
228 unsigned int inaddr; /* Careful here to use unsigned int for */
229 /* compatibility with Alpha */
231 /*
232 * Fill in the structure serv_addr with the address of the server that
233 * we want to connect with.
234 */
235 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
236 tcp_serv_addr.sin_family = AF_INET;
237 tcp_serv_addr.sin_port = htons(port);
239 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
240 tcp_serv_addr.sin_addr.s_addr = inaddr;
241 } else {
242 if ((hp = gethostbyname(host)) == NULL) {
243 net_errmsg = "tcp_open: hostname error\n";
244 return -1;
245 }
247 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
248 net_errmsg = "tcp_open: funny gethostbyname value\n";
249 return -1;
250 }
252 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
253 }
255 /* Open a TCP socket */
256 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
257 net_errmsg = "tcp_open: cannot open stream socket\n";
258 return -1;
259 }
261 /* connect to server */
262 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
263 /*
264 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
265 * implementations. Rationale is the same as described above.
266 */
267 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
268 #endif
270 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
271 snprintf(net_errbuf, sizeof(net_errbuf),
272 _("tcp_open: cannot connect to server %s on port %d.\n"
273 "ERR=%s\n"), host, port, strerror(errno));
274 net_errmsg = net_errbuf;
275 close(sockfd);
276 return -1;
277 }
279 return sockfd;
280 } /* int net_open(char *host, char *service, int port) */
282 /*
283 * Receive a message from the other end. Each message consists of
284 * two packets. The first is a header that contains the size
285 * of the data that follows in the second packet.
286 * Returns number of bytes read
287 * Returns 0 on end of file
288 * Returns -1 on hard end of file (i.e. network connection close)
289 * Returns -2 on error
290 */
291 static int net_recv(int sockfd, char *buff, int maxlen)
292 {
293 int nbytes;
294 short pktsiz;
296 /* get data size -- in short */
297 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
298 /* probably pipe broken because client died */
299 return -1; /* assume hard EOF received */
300 }
301 if (nbytes != sizeof(short))
302 return -2;
304 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
305 if (pktsiz > maxlen) {
306 net_errmsg = "net_recv: record length too large\n";
307 return -2;
308 }
309 if (pktsiz == 0)
310 return 0; /* soft EOF */
312 /* now read the actual data */
313 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
314 net_errmsg = "net_recv: read_nbytes error\n";
315 return -2;
316 }
317 if (nbytes != pktsiz) {
318 net_errmsg = "net_recv: error in read_nbytes\n";
319 return -2;
320 }
322 return (nbytes); /* return actual length of message */
323 }
325 /*
326 * Send a message over the network. The send consists of
327 * two network packets. The first is sends a short containing
328 * the length of the data packet which follows.
329 * Returns zero on success
330 * Returns non-zero on error
331 */
332 static int net_send (int sockfd, char *buff, int len)
333 {
334 int rc;
335 short packet_size;
337 /* send short containing size of data packet */
338 packet_size = htons ((short) len);
340 rc = write_nbytes(sockfd, &packet_size, sizeof (packet_size));
341 if (rc != sizeof(packet_size))
342 return (-1);
344 /* send data packet */
345 rc = write_nbytes (sockfd, buff, len);
346 if (rc != len)
347 return (-1);
349 return (0);
350 }
352 /* Get and print status from apcupsd NIS server */
353 static int do_pthreads_status (char *host, int port,
354 struct apc_detail_s *apcups_detail)
355 {
356 int sockfd;
357 int n;
358 char recvline[MAXSTRING + 1];
359 char *tokptr;
360 char *key;
361 double value;
362 #if APCMAIN
363 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
364 #else
365 # define PRINT_VALUE(name, val) /**/
366 #endif
368 /* TODO: Keep the socket open, if possible */
369 if ((sockfd = net_open (host, NULL, port)) < 0)
370 {
371 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
372 return (-1);
373 }
375 net_send (sockfd, "status", 6);
377 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
378 {
379 recvline[n] = '\0';
380 #if APCMAIN
381 printf ("net_recv = `%s';\n", recvline);
382 #endif /* if APCMAIN */
384 tokptr = strtok (recvline, ":");
385 while (tokptr != NULL)
386 {
387 key = tokptr;
388 if ((tokptr = strtok (NULL, " \t")) == NULL)
389 continue;
390 value = atof (tokptr);
391 PRINT_VALUE (key, value);
393 if (strcmp ("LINEV", key) == 0)
394 apcups_detail->linev = value;
395 else if (strcmp ("BATTV", tokptr) == 0)
396 apcups_detail->battv = value;
397 else if (strcmp ("ITEMP", tokptr) == 0)
398 apcups_detail->itemp = value;
399 else if (strcmp ("LOADPCT", tokptr) == 0)
400 apcups_detail->loadpct = value;
401 else if (strcmp ("BCHARGE", tokptr) == 0)
402 apcups_detail->bcharge = value;
403 else if (strcmp ("OUTPUTV", tokptr) == 0)
404 apcups_detail->outputv = value;
405 else if (strcmp ("LINEFREQ", tokptr) == 0)
406 apcups_detail->linefreq = value;
407 else if (strcmp ("TIMELEFT", tokptr) == 0)
408 apcups_detail->timeleft = value;
409 else
410 {
411 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
412 key, value);
413 }
415 tokptr = strtok (NULL, ":");
416 } /* while (tokptr != NULL) */
417 }
419 net_close (sockfd);
421 if (n < 0)
422 {
423 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
424 return (-1);
425 }
427 return (0);
428 }
430 #ifdef APCMAIN
431 int main(int argc, char **argv)
432 {
433 /* we are not really going to use this */
434 struct apc_detail_s apcups_detail;
436 if (!*host || strcmp(host, "0.0.0.0") == 0)
437 host = "localhost";
439 do_pthreads_status(host, port, &apcups_detail);
441 return 0;
442 }
443 #else
444 static void apcups_init (void)
445 {
446 return;
447 }
449 static int apcups_config (char *key, char *value)
450 {
451 static char lhost[126];
453 if (strcasecmp (key, "host") == 0)
454 {
455 lhost[0] = '\0';
456 strcpy(lhost,key);
457 host = lhost;
458 }
459 else if (strcasecmp (key, "Port") == 0)
460 {
461 int port_tmp = atoi (value);
462 if(port_tmp < 1 || port_tmp > 65535) {
463 syslog (LOG_WARNING, "apcups: `port' failed: %s",
464 value);
465 return (1);
466 } else {
467 port = port_tmp;
468 }
469 }
470 else
471 {
472 return (-1);
473 }
474 return(0);
475 }
477 #define BUFSIZE 256
478 static void apcups_submit (char *host,
479 struct apc_detail_s *apcups_detail)
480 {
481 char buf[BUFSIZE];
483 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
484 (unsigned int) curtime,
485 apcups_detail->linev,
486 apcups_detail->outputv) >= BUFSIZE)
487 return;
489 plugin_submit (MODULE_NAME, host, buf);
490 }
492 static void apc_bvolt_submit (char *host,
493 struct apc_detail_s *apcups_detail)
494 {
495 char buf[BUFSIZE];
497 if (snprintf (buf, BUFSIZE, "%u:%f",
498 (unsigned int) curtime,
499 apcups_detail->battv) >= BUFSIZE)
500 return;
502 plugin_submit ("apcups_bvolt", host, buf);
503 }
505 static void apc_load_submit (char *host,
506 struct apc_detail_s *apcups_detail)
507 {
508 char buf[BUFSIZE];
510 if (snprintf (buf, BUFSIZE, "%u:%f",
511 (unsigned int) curtime,
512 apcups_detail->loadpct) >= BUFSIZE)
513 return;
515 plugin_submit ("apcups_load", host, buf);
516 }
518 static void apc_charge_submit (char *host,
519 struct apc_detail_s *apcups_detail)
520 {
521 char buf[BUFSIZE];
523 if (snprintf (buf, BUFSIZE, "%u:%f",
524 (unsigned int) curtime,
525 apcups_detail->bcharge) >= BUFSIZE)
526 return;
528 plugin_submit ("apcups_charge", host, buf);
529 }
531 static void apc_temp_submit (char *host,
532 struct apc_detail_s *apcups_detail)
533 {
534 char buf[BUFSIZE];
536 if (snprintf (buf, BUFSIZE, "%u:%f",
537 (unsigned int) curtime,
538 apcups_detail->itemp) >= BUFSIZE)
539 return;
541 plugin_submit ("apcups_temp", host, buf);
542 }
544 static void apc_time_submit (char *host,
545 struct apc_detail_s *apcups_detail)
546 {
547 char buf[BUFSIZE];
549 if (snprintf (buf, BUFSIZE, "%u:%f",
550 (unsigned int) curtime,
551 apcups_detail->timeleft) >= BUFSIZE)
552 return;
554 plugin_submit ("apcups_time", host, buf);
555 }
557 static void apc_freq_submit (char *host,
558 struct apc_detail_s *apcups_detail)
559 {
560 char buf[BUFSIZE];
562 if (snprintf (buf, BUFSIZE, "%u:%f",
563 (unsigned int) curtime,
564 apcups_detail->linefreq) >= BUFSIZE)
565 return;
567 plugin_submit ("apcups_freq", host, buf);
568 }
569 #undef BUFSIZE
571 static void apcups_read (void)
572 {
573 struct apc_detail_s apcups_detail;
574 int status;
576 apcups_detail.linev = 0.0;
577 apcups_detail.loadpct = 0.0;
578 apcups_detail.bcharge = 0.0;
579 apcups_detail.timeleft = 0.0;
580 apcups_detail.outputv = 0.0;
581 apcups_detail.itemp = 0.0;
582 apcups_detail.battv = 0.0;
583 apcups_detail.linefreq = 0.0;
586 if (!*host || strcmp(host, "0.0.0.0") == 0)
587 host = "localhost";
589 status = do_pthreads_status(host, port, &apcups_detail);
591 /*
592 * if we did not connect then do not bother submitting
593 * zeros. We want rrd files to have NAN.
594 */
595 if (status != 0)
596 return;
598 apcups_submit (host, &apcups_detail);
599 apc_bvolt_submit (host, &apcups_detail);
600 apc_load_submit (host, &apcups_detail);
601 apc_charge_submit (host, &apcups_detail);
602 apc_temp_submit (host, &apcups_detail);
603 apc_time_submit (host, &apcups_detail);
604 apc_freq_submit (host, &apcups_detail);
605 }
608 static void apcups_write (char *host, char *inst, char *val)
609 {
610 char file[512];
611 int status;
613 status = snprintf (file, 512, volt_file_template, inst);
614 if (status < 1)
615 return;
616 else if (status >= 512)
617 return;
619 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
620 }
622 static void apc_bvolt_write (char *host, char *inst, char *val)
623 {
624 char file[512];
625 int status;
627 status = snprintf (file, 512, bvolt_file_template, inst);
628 if (status < 1)
629 return;
630 else if (status >= 512)
631 return;
633 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
634 }
636 static void apc_load_write (char *host, char *inst, char *val)
637 {
638 char file[512];
639 int status;
641 status = snprintf (file, 512, load_file_template, inst);
642 if (status < 1)
643 return;
644 else if (status >= 512)
645 return;
647 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
648 }
650 static void apc_charge_write (char *host, char *inst, char *val)
651 {
652 char file[512];
653 int status;
655 status = snprintf (file, 512, charge_file_template, inst);
656 if (status < 1)
657 return;
658 else if (status >= 512)
659 return;
661 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
662 }
664 static void apc_temp_write (char *host, char *inst, char *val)
665 {
666 char file[512];
667 int status;
669 status = snprintf (file, 512, temp_file_template, inst);
670 if (status < 1)
671 return;
672 else if (status >= 512)
673 return;
675 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
676 }
678 static void apc_time_write (char *host, char *inst, char *val)
679 {
680 char file[512];
681 int status;
683 status = snprintf (file, 512, time_file_template, inst);
684 if (status < 1)
685 return;
686 else if (status >= 512)
687 return;
689 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
690 }
692 static void apc_freq_write (char *host, char *inst, char *val)
693 {
694 char file[512];
695 int status;
697 status = snprintf (file, 512, freq_file_template, inst);
698 if (status < 1)
699 return;
700 else if (status >= 512)
701 return;
703 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
704 }
706 void module_register (void)
707 {
708 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
709 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
710 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
711 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
712 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
713 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
714 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
715 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
716 }
718 #endif /* ifdef APCMAIN */
719 #undef MODULE_NAME