f3353482b7ad01305c2d8314838c8d10b48aa837
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 /*
26 * FIXME: Don't know why but without this here atof() was not returning
27 * correct values for me. This is behavior that I don't understand and
28 * should be examined in closer detail.
29 */
30 #include <stdlib.h>
32 #include "collectd.h"
33 #include "common.h" /* rrd_update_file */
34 #include "plugin.h" /* plugin_register, plugin_submit */
35 #include "configfile.h" /* cf_register */
36 #include "utils_debug.h"
38 <<<<<<< .mine
39 #include <string.h>
40 #include <errno.h>
41 #include <netdb.h>
42 #include <sys/socket.h> /* Used for socket connections */
43 #include <netinet/in.h> /* Used for socket connections */
44 #include <arpa/inet.h> /* Used for socket connections */
45 =======
46 #if HAVE_SYS_TYPES_H
47 # include <sys/types.h>
48 #endif
49 #if HAVE_SYS_SOCKET_H
50 # include <sys/socket.h>
51 #endif
52 #if HAVE_NETDB_H
53 # include <netdb.h>
54 #endif
55 #if HAVE_NETINET_IN_H
56 # include <netinet/in.h>
57 #endif
59 #if 0
60 #if HAVE_ARPA_INET_H
61 # include <arpa/inet.h> /* inet_addr */
62 #endif
63 #include <pwd.h>
64 #include <setjmp.h> /* FIXME: Is this really neccessary? */
65 #include <termios.h> /* FIXME: Is this really neccessary? */
66 #include <sys/ioctl.h> /* FIXME: Is this really neccessary? */
67 #include <sys/ipc.h> /* FIXME: Is this really neccessary? */
68 #include <sys/sem.h> /* FIXME: Is this really neccessary? */
69 #include <sys/shm.h> /* FIXME: Is this really neccessary? */
70 #endif
71 >>>>>>> .r743
75 #define NISPORT 3551
76 #define _(String) (String)
77 #define N_(String) (String)
78 #define MAXSTRING 256
79 #define MODULE_NAME "apcups"
81 <<<<<<< .mine
83 =======
84 >>>>>>> .r743
85 /* Default values for contacting daemon */
86 <<<<<<< .mine
87 static char *host = "localhost"; /* the default host to connect to */
88 static int port = NISPORT; /* the default port to connect to */
89 =======
90 static char *global_host = NULL;
91 static int global_port = NISPORT;
92 >>>>>>> .r743
94 <<<<<<< .mine
95 /*
96 * This is used in do_apc_status() to track the last connection state.
97 * We do not want the read function spitting out an error every "step"
98 * seconds (usually 10 secs).
99 */
100 static int apcConnStatus = 0;
102 static struct sockaddr_in tcp_serv_addr; /* socket information */
103 static char *net_errmsg = NULL; /* pointer to error message */
104 static char net_errbuf[256]; /* error message buffer for messages */
106 =======
107 >>>>>>> .r743
108 /*
109 * The following are normally compiled, when the module is compiled with its
110 * own main for testing these are ifdef'd out.
111 */
112 /*
113 * FIXME: Rename DSes to be more generic and follow established conventions.
114 * However, they currently match the values put out by apcupsd.
115 */
116 #ifndef APCMAIN
117 static char *bvolt_file_template = "apcups/voltage-%s.rrd";
118 static char *bvolt_ds_def[] =
119 {
120 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":0:U",
121 };
122 static int bvolt_ds_num = 1;
124 static char *load_file_template = "apcups/charge_percent.rrd";
125 static char *load_ds_def[] =
126 {
127 "DS:percent:GAUGE:"COLLECTD_HEARTBEAT":0:100",
128 };
129 static int load_ds_num = 1;
131 static char *charge_file_template = "apcups/charge.rrd";
132 static char *charge_ds_def[] =
133 {
134 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
135 };
136 static int charge_ds_num = 1;
138 static char *time_file_template = "apcups/time.rrd";
139 static char *time_ds_def[] =
140 {
141 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
142 };
143 static int time_ds_num = 1;
145 static char *temp_file_template = "apcups/temperature.rrd";
146 static char *temp_ds_def[] =
147 {
148 /* -273.15 is absolute zero */
149 "DS:temperature:GAUGE:"COLLECTD_HEARTBEAT":-274:U",
150 };
151 static int temp_ds_num = 1;
153 static char *freq_file_template = "apcups/frequency-%s.rrd";
154 static char *freq_ds_def[] =
155 {
156 "DS:frequency:GAUGE:"COLLECTD_HEARTBEAT":0:U",
157 };
158 static int freq_ds_num = 1;
160 static char *config_keys[] =
161 {
162 "Host",
163 "Port",
164 NULL
165 };
166 static int config_keys_num = 2;
168 #endif /* ifndef APCMAIN */
170 struct apc_detail_s
171 {
172 <<<<<<< .mine
173 int connected;
174 float linev;
175 float loadpct;
176 float bcharge;
177 float timeleft;
178 float outputv;
179 float itemp;
180 float battv;
181 float linefreq;
182 =======
183 double linev;
184 double loadpct;
185 double bcharge;
186 double timeleft;
187 double outputv;
188 double itemp;
189 double battv;
190 double linefreq;
191 >>>>>>> .r743
192 };
194 <<<<<<< .mine
195 =======
196 #define BIG_BUF 4096
198 >>>>>>> .r743
199 /*
200 * Read nbytes from the network.
201 * It is possible that the total bytes require in several
202 * read requests
203 */
204 static int read_nbytes (int fd, char *ptr, int nbytes)
205 {
206 int nleft, nread;
208 nleft = nbytes;
210 while (nleft > 0) {
211 do {
212 nread = read (fd, ptr, nleft);
213 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
215 if (nread <= 0) {
216 return (nread); /* error, or EOF */
217 }
219 nleft -= nread;
220 ptr += nread;
221 }
223 return (nbytes - nleft); /* return >= 0 */
224 }
226 /*
227 * Write nbytes to the network.
228 * It may require several writes.
229 */
230 static int write_nbytes (int fd, void *buf, int buflen)
231 {
232 int nleft;
233 int nwritten;
234 char *ptr;
236 ptr = (char *) buf;
238 <<<<<<< .mine
239 if (nwritten <= 0) {
240 return (nwritten); /* error */
241 =======
242 nleft = buflen;
243 while (nleft > 0)
244 {
245 nwritten = write (fd, ptr, nleft);
247 if (nwritten <= 0)
248 {
249 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
250 return (nwritten);
251 >>>>>>> .r743
252 }
254 nleft -= nwritten;
255 ptr += nwritten;
256 }
258 /* If we get here, (nleft <= 0) is true */
259 return (buflen);
260 }
262 /* Close the network connection */
263 static void net_close (int sockfd)
264 {
265 short pktsiz = 0;
267 /* send EOF sentinel */
268 write_nbytes (sockfd, &pktsiz, sizeof (short));
269 close (sockfd);
270 }
273 /*
274 * Open a TCP connection to the UPS network server
275 * Returns -1 on error
276 * Returns socket file descriptor otherwise
277 */
278 static int net_open (char *host, char *service, int port)
279 {
280 int sd;
281 int status;
282 char port_str[8];
283 struct addrinfo ai_hints;
284 struct addrinfo *ai_return;
285 struct addrinfo *ai_list;
287 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
289 /* Convert the port to a string */
290 snprintf (port_str, 8, "%i", port);
291 port_str[7] = '\0';
293 /* Resolve name */
294 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
295 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
296 ai_hints.ai_socktype = SOCK_STREAM;
298 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
299 if (status != 0)
300 {
301 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
302 return (-1);
303 }
305 /* Create socket */
306 sd = -1;
307 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
308 {
309 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
310 if (sd >= 0)
311 break;
312 }
313 /* `ai_list' still holds the current description of the socket.. */
315 if (sd < 0)
316 {
317 DBG ("Unable to open a socket");
318 freeaddrinfo (ai_return);
319 return (-1);
320 }
322 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
324 freeaddrinfo (ai_return);
326 if (status != 0) /* `connect(2)' failed */
327 {
328 DBG ("connect failed: %s", strerror (errno));
329 return (-1);
330 }
332 return (sd);
333 } /* int net_open (char *host, char *service, int port) */
335 /*
336 * Receive a message from the other end. Each message consists of
337 * two packets. The first is a header that contains the size
338 * of the data that follows in the second packet.
339 * Returns number of bytes read
340 * Returns 0 on end of file
341 * Returns -1 on hard end of file (i.e. network connection close)
342 * Returns -2 on error
343 */
344 static int net_recv (int sockfd, char *buf, int buflen)
345 {
346 int nbytes;
347 short pktsiz;
349 /* get data size -- in short */
350 if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
351 return (-1);
353 if (nbytes != sizeof (short))
354 return (-2);
356 pktsiz = ntohs (pktsiz);
357 if (pktsiz > buflen)
358 {
359 DBG ("record length too large");
360 return (-2);
361 }
363 if (pktsiz == 0)
364 return (0);
366 /* now read the actual data */
367 if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
368 return (-2);
370 if (nbytes != pktsiz)
371 return (-2);
373 return (nbytes);
374 } /* static int net_recv (int sockfd, char *buf, int buflen) */
376 /*
377 * Send a message over the network. The send consists of
378 * two network packets. The first is sends a short containing
379 * the length of the data packet which follows.
380 * Returns zero on success
381 * Returns non-zero on error
382 */
383 static int net_send (int sockfd, char *buff, int len)
384 {
385 int rc;
386 short packet_size;
388 /* send short containing size of data packet */
389 packet_size = htons ((short) len);
391 rc = write_nbytes (sockfd, &packet_size, sizeof (packet_size));
392 if (rc != sizeof (packet_size))
393 return (-1);
395 /* send data packet */
396 rc = write_nbytes (sockfd, buff, len);
397 if (rc != len)
398 return (-1);
400 return (0);
401 }
403 <<<<<<< .mine
404 /*
405 * Get and print status from apcupsd NIS server if APCMAIN is defined.
406 * Poplate apcups_detail structure for plugin submit().
407 */
408 static int do_apc_status(char *host, int port, struct apc_detail_s *apcups_detail)
409 =======
410 /* Get and print status from apcupsd NIS server */
411 static int apc_query_server (char *host, int port,
412 struct apc_detail_s *apcups_detail)
413 >>>>>>> .r743
414 {
415 int sockfd;
416 int n;
417 char recvline[MAXSTRING + 1];
418 char *tokptr;
419 char *key;
420 float value;
421 #if APCMAIN
422 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
423 #else
424 # define PRINT_VALUE(name, val) /**/
425 #endif
427 <<<<<<< .mine
428 /*
429 * TODO: Keep the socket open, if possible.
430 * Can open socket in module init, but without a corresponding module
431 * uninit there is no place to gracefully close the socket.
432 */
433 =======
434 /* TODO: Keep the socket open, if possible */
435 >>>>>>> .r743
436 if ((sockfd = net_open (host, NULL, port)) < 0)
437 {
438 /*
439 * When the integer apcConnStatus rolls over it will print out
440 * again, if we haven't connected by then.
441 */
442 if (apcConnStatus++ == 0)
443 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed: %s",
444 net_errmsg);
445 return (-1);
446 } else apcConnStatus = 0;
448 if (net_send (sockfd, "status", 6) < 0)
449 {
450 syslog (LOG_ERR, "apcups plugin: sending to the apcupsd failed: %s",
451 net_errmsg);
452 return (-1);
453 }
455 <<<<<<< .mine
456 =======
457 net_send (sockfd, "status", 6);
459 >>>>>>> .r743
460 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
461 {
462 recvline[n-1] = '\0';
463 #if APCMAIN
464 printf ("net_recv = \"%s\"\n", recvline);
465 #endif /* if APCMAIN */
467 tokptr = strtok (recvline, ":");
468 while (tokptr != NULL)
469 {
470 key = tokptr;
471 if ((tokptr = strtok (NULL, " \t")) == NULL)
472 continue;
474 if (strncmp ("LINEV", key,5) == 0) {
475 value = atof (tokptr);
476 PRINT_VALUE (key, value);
477 apcups_detail->linev = value;
478 } else if (strncmp ("BATTV", key,5) == 0) {
479 value = atof (tokptr);
480 PRINT_VALUE (key, value);
481 apcups_detail->battv = value;
482 } else if (strncmp ("ITEMP", key,5) == 0) {
483 value = atof (tokptr);
484 PRINT_VALUE (key, value);
485 apcups_detail->itemp = value;
486 } else if (strncmp ("LOADPCT", key,7) == 0) {
487 value = atof (tokptr);
488 PRINT_VALUE (key, value);
489 apcups_detail->loadpct = value;
490 } else if (strncmp ("BCHARGE", key,7) == 0) {
491 value = atof (tokptr);
492 PRINT_VALUE (key, value);
493 apcups_detail->bcharge = value;
494 } else if (strncmp ("OUTPUTV", key,7) == 0) {
495 value = atof (tokptr);
496 PRINT_VALUE (key, value);
497 apcups_detail->outputv = value;
498 } else if (strncmp ("LINEFREQ", key,8) == 0) {
499 value = atof (tokptr);
500 PRINT_VALUE (key, value);
501 apcups_detail->linefreq = value;
502 } else if (strncmp ("TIMELEFT", key,8) == 0) {
503 value = atof (tokptr);
504 PRINT_VALUE (key, value);
505 apcups_detail->timeleft = value;
506 }
508 tokptr = strtok (NULL, ":");
509 } /* while (tokptr != NULL) */
510 }
512 <<<<<<< .mine
513 if (n < 0) {
514 syslog(LOG_ERR, "apcups plugin: Error recieving data: %s",
515 net_errmsg);
516 net_errmsg = NULL;
517 return(-1);
518 }
519 =======
520 net_close (sockfd);
522 if (n < 0)
523 {
524 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
525 return (-1);
526 }
527 >>>>>>> .r743
529 <<<<<<< .mine
530 /* signal that we did in fact connect. */
531 apcups_detail->connected = 1;
533 net_close(sockfd);
535 =======
536 >>>>>>> .r743
537 return (0);
538 }
540 #ifdef APCMAIN
541 <<<<<<< .mine
542 /*
543 * This is used for testing apcups in a standalone mode.
544 * Usefull for debugging.
545 */
546 int main(int argc, char **argv)
547 =======
548 int main (int argc, char **argv)
549 >>>>>>> .r743
550 {
551 /* we are not really going to use this */
552 struct apc_detail_s apcups_detail;
554 <<<<<<< .mine
555 openlog("apcups",LOG_PID | LOG_NDELAY | LOG_LOCAL1);
557 if (!*host || strcmp(host, "0.0.0.0") == 0)
558 =======
559 if (!*host || strcmp (host, "0.0.0.0") == 0)
560 >>>>>>> .r743
561 host = "localhost";
563 <<<<<<< .mine
564 if(do_apc_status(host, port, &apcups_detail) < 0) {
565 printf("apcups: Failed...\n");
566 return(-1);
567 }
568 =======
569 apc_query_server (global_host, global_port, &apcups_detail);
570 >>>>>>> .r743
572 return 0;
573 }
574 #else
575 static int apcups_config (char *key, char *value)
576 {
577 <<<<<<< .mine
578 static char lhost[126];
580 if (strcasecmp (key, "host") == 0)
581 {
582 lhost[0] = '\0';
583 strcpy(lhost,key);
584 host = lhost;
585 }
586 else if (strcasecmp (key, "Port") == 0)
587 {
588 int port_tmp = atoi (value);
589 if(port_tmp < 1 || port_tmp > 65535) {
590 syslog (LOG_WARNING, "apcups: `port' failed: %s",
591 value);
592 return (1);
593 } else {
594 port = port_tmp;
595 }
596 }
597 else
598 {
599 return (-1);
600 }
602 if (strcmp(host, "0.0.0.0") == 0)
603 host = "localhost";
605 return(0);
606 =======
607 if (strcasecmp (key, "host") == 0)
608 {
609 if (global_host != NULL)
610 {
611 free (global_host);
612 global_host = NULL;
613 }
614 if ((global_host = strdup (value)) == NULL)
615 return (1);
616 }
617 else if (strcasecmp (key, "Port") == 0)
618 {
619 int port_tmp = atoi (value);
620 if (port_tmp < 1 || port_tmp > 65535)
621 {
622 syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
623 return (1);
624 }
625 global_port = port_tmp;
626 }
627 else
628 {
629 return (-1);
630 }
631 return (0);
632 >>>>>>> .r743
633 }
635 static void apcups_init (void)
636 {
637 return;
638 }
640 static void apc_write_voltage (char *host, char *inst, char *val)
641 {
642 char file[512];
643 int status;
645 status = snprintf (file, 512, bvolt_file_template, inst);
646 if ((status < 1) || (status >= 512))
647 return;
649 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
650 }
652 static void apc_write_charge (char *host, char *inst, char *val)
653 {
654 rrd_update_file (host, charge_file_template, val, charge_ds_def, charge_ds_num);
655 }
657 static void apc_write_percent (char *host, char *inst, char *val)
658 {
659 rrd_update_file (host, load_file_template, val, load_ds_def, load_ds_num);
660 }
662 static void apc_write_timeleft (char *host, char *inst, char *val)
663 {
664 rrd_update_file (host, time_file_template, val, time_ds_def, time_ds_num);
665 }
667 static void apc_write_temperature (char *host, char *inst, char *val)
668 {
669 rrd_update_file (host, temp_file_template, val, temp_ds_def, temp_ds_num);
670 }
672 static void apc_write_frequency (char *host, char *inst, char *val)
673 {
674 <<<<<<< .mine
675 struct apc_detail_s apcups_detail;
677 apcups_detail.linev = 0.0;
678 apcups_detail.loadpct = 0.0;
679 apcups_detail.bcharge = 0.0;
680 apcups_detail.timeleft = 0.0;
681 apcups_detail.outputv = 0.0;
682 apcups_detail.itemp = 0.0;
683 apcups_detail.battv = 0.0;
684 apcups_detail.linefreq = 0.0;
685 apcups_detail.connected = 0;
686 =======
687 char file[512];
688 int status;
689 >>>>>>> .r743
691 <<<<<<< .mine
693 if (!*host || strcmp(host, "0.0.0.0") == 0)
694 host = "localhost";
696 if(do_apc_status(host, port, &apcups_detail) < 0) return;
698 /*
699 * if we did not connect then do not bother submitting
700 * zeros. We want rrd files to have NAN.
701 */
702 if(!apcups_detail.connected) return;
703 =======
704 status = snprintf (file, 512, freq_file_template, inst);
705 if ((status < 1) || (status >= 512))
706 return;
707 >>>>>>> .r743
709 <<<<<<< .mine
710 apcups_submit (host, &apcups_detail);
711 apc_bvolt_submit (host, &apcups_detail);
712 apc_load_submit (host, &apcups_detail);
713 apc_charge_submit (host, &apcups_detail);
714 apc_temp_submit (host, &apcups_detail);
715 apc_time_submit (host, &apcups_detail);
716 apc_freq_submit (host, &apcups_detail);
717 =======
718 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
719 >>>>>>> .r743
720 }
722 static void apc_submit_generic (char *type, char *inst,
723 double value)
724 {
725 <<<<<<< .mine
726 char file[512];
727 int status;
729 status = snprintf (file, 512, volt_file_template, inst);
730 if (status < 1)
731 return;
732 else if (status >= 512)
733 return;
735 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
736 }
737 =======
738 char buf[512];
739 int status;
740 >>>>>>> .r743
742 <<<<<<< .mine
743 static void apc_bvolt_write (char *host, char *inst, char *val)
744 {
745 char file[512];
746 int status;
748 status = snprintf (file, 512, bvolt_file_template, inst);
749 if (status < 1)
750 return;
751 else if (status >= 512)
752 return;
754 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
755 }
756 =======
757 status = snprintf (buf, 512, "%u:%f",
758 (unsigned int) curtime, value);
759 if ((status < 1) || (status >= 512))
760 return;
761 >>>>>>> .r743
763 <<<<<<< .mine
764 static void apc_load_write (char *host, char *inst, char *val)
765 {
766 char file[512];
767 int status;
769 status = snprintf (file, 512, load_file_template, inst);
770 if (status < 1)
771 return;
772 else if (status >= 512)
773 return;
775 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
776 =======
777 plugin_submit (type, inst, buf);
778 >>>>>>> .r743
779 }
781 static void apc_submit (struct apc_detail_s *apcups_detail)
782 {
783 <<<<<<< .mine
784 char file[512];
785 int status;
787 status = snprintf (file, 512, charge_file_template, inst);
788 if (status < 1)
789 return;
790 else if (status >= 512)
791 return;
793 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
794 =======
795 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
796 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
797 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
798 apc_submit_generic ("apcups_charge", "-", apcups_detail->bcharge);
799 apc_submit_generic ("apcups_charge_pct", "-", apcups_detail->loadpct);
800 apc_submit_generic ("apcups_timeleft", "-", apcups_detail->timeleft);
801 apc_submit_generic ("apcups_temp", "-", apcups_detail->itemp);
802 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
803 >>>>>>> .r743
804 }
806 static void apcups_read (void)
807 {
808 <<<<<<< .mine
809 char file[512];
810 int status;
812 status = snprintf (file, 512, temp_file_template, inst);
813 if (status < 1)
814 return;
815 else if (status >= 512)
816 return;
818 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
819 }
820 =======
821 struct apc_detail_s apcups_detail;
822 int status;
823 >>>>>>> .r743
825 <<<<<<< .mine
826 static void apc_time_write (char *host, char *inst, char *val)
827 {
828 char file[512];
829 int status;
830 =======
831 if (global_host == NULL)
832 return;
834 apcups_detail.linev = -1.0;
835 apcups_detail.outputv = -1.0;
836 apcups_detail.battv = -1.0;
837 apcups_detail.loadpct = -1.0;
838 apcups_detail.bcharge = -1.0;
839 apcups_detail.timeleft = -1.0;
840 apcups_detail.itemp = -300.0;
841 apcups_detail.linefreq = -1.0;
842 >>>>>>> .r743
844 <<<<<<< .mine
845 status = snprintf (file, 512, time_file_template, inst);
846 if (status < 1)
847 return;
848 else if (status >= 512)
849 return;
851 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
852 }
853 =======
854 status = apc_query_server (global_host, global_port, &apcups_detail);
856 /*
857 * if we did not connect then do not bother submitting
858 * zeros. We want rrd files to have NAN.
859 */
860 if (status != 0)
861 return;
862 >>>>>>> .r743
864 <<<<<<< .mine
865 static void apc_freq_write (char *host, char *inst, char *val)
866 {
867 char file[512];
868 int status;
870 status = snprintf (file, 512, freq_file_template, inst);
871 if (status < 1)
872 return;
873 else if (status >= 512)
874 return;
876 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
877 }
878 =======
879 apc_submit (&apcups_detail);
880 } /* apcups_read */
881 >>>>>>> .r743
883 void module_register (void)
884 {
885 plugin_register (MODULE_NAME, apcups_init, apcups_read, NULL);
886 plugin_register ("apcups_voltage", NULL, NULL, apc_write_voltage);
887 plugin_register ("apcups_charge", NULL, NULL, apc_write_charge);
888 plugin_register ("apcups_charge_pct", NULL, NULL, apc_write_percent);
889 plugin_register ("apcups_timeleft", NULL, NULL, apc_write_timeleft);
890 plugin_register ("apcups_temp", NULL, NULL, apc_write_temperature);
891 plugin_register ("apcups_frequency", NULL, NULL, apc_write_frequency);
892 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
893 }
895 #endif /* ifdef APCMAIN */
896 #undef MODULE_NAME
897 #undef MAXSTRING
898 #undef NISPORT