Added `apcups.c' by Anthony Gialluca <tonyabg at charter.net>
[collectd.git] / src / apcups.c
1  /*
2  * Copyright (C) 2000-2004 Kern Sibbald
3  * Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General
7  * Public License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17  * MA 02111-1307, USA.
18  */
19
20 # include <stdio.h>
21 # include <stdlib.h>
22 # include <stdarg.h>
23 # include <unistd.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <signal.h>
27 #include <ctype.h>
28 #include <syslog.h>
29 #include <limits.h>
30 #include <pwd.h>
31 #include <time.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <setjmp.h>
35 #include <termios.h>
36 #include <netdb.h>
37 #include <sys/ioctl.h>
38 #  include <sys/ipc.h>
39 #  include <sys/sem.h>
40 #  include <sys/shm.h>
41 # include <sys/socket.h>
42 # include <sys/types.h>
43 # include <sys/time.h>
44 # include <time.h>
45 # include <sys/wait.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48
49 #include "collectd.h"
50 #include "common.h" /* rrd_update_file */
51 #include "plugin.h" /* plugin_register, plugin_submit */
52 #include "configfile.h" /* cf_register */
53
54 #define NISPORT 3551
55 #define _(String) (String)
56 #define N_(String) (String)
57 #define MAXSTRING               256
58 #define Error_abort0(fmd) error_out(__FILE__, __LINE__, fmd)
59 #define MODULE_NAME "apcups"
60
61
62 /* Prototypes */
63 void generic_error_out(const char *, int , const char *, ...);
64
65 /* Default values for contacting daemon */
66 static char *host = "localhost";
67 static int port = NISPORT;
68
69 char argvalue[MAXSTRING];
70
71 struct sockaddr_in tcp_serv_addr;  /* socket information */
72 int net_errno = 0;                 /* error number -- not yet implemented */
73 char *net_errmsg = NULL;           /* pointer to error message */
74 char net_errbuf[256];              /* error message buffer for messages */
75
76 void (*error_out) (const char *file, int line, const char *fmt, ...) = generic_error_out;
77 void (*error_cleanup) (void) = NULL;
78
79 /* 
80  * The following are only if not compiled to test the module with
81  * its own main.
82 */
83 #ifndef APCMAIN
84 static char *volt_file_template = "apcups_volt-%s.rrd";
85 static char *volt_ds_def[] = 
86 {
87         "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
88         "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
89         NULL
90 };
91 static int volt_ds_num = 2;
92
93 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
94 static char *bvolt_ds_def[] = 
95 {
96         "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
97 };
98 static int bvolt_ds_num = 1;
99
100 static char *load_file_template = "apcups_load-%s.rrd";
101 static char *load_ds_def[] = 
102 {
103         "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
104 };
105 static int load_ds_num = 1;
106
107 static char *charge_file_template = "apcups_charge-%s.rrd";
108 static char *charge_ds_def[] = 
109 {
110         "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
111 };
112 static int charge_ds_num = 1;
113
114 static char *time_file_template = "apcups_time-%s.rrd";
115 static char *time_ds_def[] = 
116 {
117         "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
118 };
119 static int time_ds_num = 1;
120
121 static char *temp_file_template = "apcups_temp-%s.rrd";
122 static char *temp_ds_def[] = 
123 {
124         "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
125 };
126 static int temp_ds_num = 1;
127
128 static char *freq_file_template = "apcups_freq-%s.rrd";
129 static char *freq_ds_def[] = 
130 {
131         "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
132 };
133 static int freq_ds_num = 1;
134
135 static char *config_keys[] =
136 {
137         "Host",
138         "Port",
139         NULL
140 };
141 static int config_keys_num = 2;
142
143 #endif /* ifndef APCMAIN */
144
145 struct apc_detail_s {
146   float linev;
147   float loadpct;
148   float bcharge;
149   float timeleft;
150   float outputv;
151   float itemp;
152   float battv;
153   float linefreq;
154 };
155
156 /* Guarantee that the string is properly terminated */
157 char *astrncpy(char *dest, const char *src, int maxlen)
158 {
159    strncpy(dest, src, maxlen - 1);
160    dest[maxlen - 1] = 0;
161    return dest;
162 }
163
164
165 #define BIG_BUF 5000
166
167 /* Implement snprintf */
168 int asnprintf(char *str, size_t size, const char *fmt, ...)
169 {
170 #ifdef HAVE_VSNPRINTF
171    va_list arg_ptr;
172    int len;
173
174    va_start(arg_ptr, fmt);
175    len = vsnprintf(str, size, fmt, arg_ptr);
176    va_end(arg_ptr);
177
178    str[size - 1] = 0;
179    return len;
180
181 #else
182
183    va_list arg_ptr;
184    int len;
185    char *buf;
186
187    buf = (char *)malloc(BIG_BUF);
188
189    va_start(arg_ptr, fmt);
190    len = vsprintf(buf, fmt, arg_ptr);
191    va_end(arg_ptr);
192
193    if (len >= BIG_BUF)
194       Error_abort0(_("Buffer overflow.\n"));
195
196    memcpy(str, buf, size);
197    str[size - 1] = 0;
198
199    free(buf);
200    return len;
201 #endif
202 }
203
204 /* Implement vsnprintf() */
205 int avsnprintf(char *str, size_t size, const char *format, va_list ap)
206 {
207 #ifdef HAVE_VSNPRINTF
208    int len;
209
210    len = vsnprintf(str, size, format, ap);
211    str[size - 1] = 0;
212
213    return len;
214
215 #else
216
217    int len;
218    char *buf;
219
220    buf = (char *)malloc(BIG_BUF);
221
222    len = vsprintf(buf, format, ap);
223    if (len >= BIG_BUF)
224       Error_abort0(_("Buffer overflow.\n"));
225
226    memcpy(str, buf, size);
227    str[size - 1] = 0;
228
229    free(buf);
230    return len;
231 #endif
232 }
233
234 /*
235  * Subroutine normally called by macro error_abort() to print
236  * FATAL ERROR message and supplied error message
237  */
238 void generic_error_out(const char *file, int line, const char *fmt, ...)
239 {
240    char buf[256];
241    va_list arg_ptr;
242    int i;
243
244    asnprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
245    i = strlen(buf);
246    va_start(arg_ptr, fmt);
247    avsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
248    va_end(arg_ptr);
249    fprintf(stdout, "%s", buf);
250
251    if (error_cleanup)
252       error_cleanup();
253
254    exit(1);
255 }
256
257 /*
258  * Read nbytes from the network.
259  * It is possible that the total bytes require in several
260  * read requests
261  */
262 static int read_nbytes(int fd, char *ptr, int nbytes)
263 {
264    int nleft, nread;
265
266
267    nleft = nbytes;
268
269    while (nleft > 0) {
270       do {
271          nread = read(fd, ptr, nleft);
272       } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
273
274       if (nread <= 0) {
275          net_errno = errno;
276          return (nread);           /* error, or EOF */
277       }
278
279       nleft -= nread;
280       ptr += nread;
281    }
282
283    return (nbytes - nleft);        /* return >= 0 */
284 }
285
286
287 /*
288  * Write nbytes to the network.
289  * It may require several writes.
290  */
291 static int write_nbytes(int fd, char *ptr, int nbytes)
292 {
293    int nleft, nwritten;
294
295    nleft = nbytes;
296    while (nleft > 0) {
297       nwritten = write(fd, ptr, nleft);
298
299       if (nwritten <= 0) {
300          net_errno = errno;
301          return (nwritten);        /* error */
302       }
303
304       nleft -= nwritten;
305       ptr += nwritten;
306    }
307
308    return (nbytes - nleft);
309 }
310
311
312 /* Close the network connection */
313 void net_close(int sockfd)
314 {
315    short pktsiz = 0;
316
317    /* send EOF sentinel */
318    write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
319    close(sockfd);
320 }
321
322
323 /*     
324  * Open a TCP connection to the UPS network server
325  * Returns -1 on error
326  * Returns socket file descriptor otherwise
327  */
328 int net_open(char *host, char *service, int port)
329 {
330    int sockfd;
331    struct hostent *hp;
332    unsigned int inaddr;            /* Careful here to use unsigned int for */
333                                    /* compatibility with Alpha */
334
335    /* 
336     * Fill in the structure serv_addr with the address of
337     * the server that we want to connect with.
338     */
339    memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
340    tcp_serv_addr.sin_family = AF_INET;
341    tcp_serv_addr.sin_port = htons(port);
342
343    if ((inaddr = inet_addr(host)) != INADDR_NONE) {
344       tcp_serv_addr.sin_addr.s_addr = inaddr;
345    } else {
346       if ((hp = gethostbyname(host)) == NULL) {
347          net_errmsg = "tcp_open: hostname error\n";
348          return -1;
349       }
350
351       if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
352          net_errmsg = "tcp_open: funny gethostbyname value\n";
353          return -1;
354       }
355
356       tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
357    }
358
359
360    /* Open a TCP socket */
361    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
362       net_errmsg = "tcp_open: cannot open stream socket\n";
363       return -1;
364    }
365
366    /* connect to server */
367 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
368    /* 
369     * Work around a bug in OpenBSD & FreeBSD userspace pthreads
370     * implementations. Rationale is the same as described above.
371     */
372    fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
373 #endif
374
375    if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
376       asnprintf(net_errbuf, sizeof(net_errbuf),
377          _("tcp_open: cannot connect to server %s on port %d.\n"
378         "ERR=%s\n"), host, port, strerror(errno));
379       net_errmsg = net_errbuf;
380       close(sockfd);
381       return -1;
382    }
383
384    return sockfd;
385 }
386
387
388
389 /* 
390  * Receive a message from the other end. Each message consists of
391  * two packets. The first is a header that contains the size
392  * of the data that follows in the second packet.
393  * Returns number of bytes read
394  * Returns 0 on end of file
395  * Returns -1 on hard end of file (i.e. network connection close)
396  * Returns -2 on error
397  */
398 int net_recv(int sockfd, char *buff, int maxlen)
399 {
400    int nbytes;
401    short pktsiz;
402
403    /* get data size -- in short */
404    if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
405       /* probably pipe broken because client died */
406       return -1;                   /* assume hard EOF received */
407    }
408    if (nbytes != sizeof(short))
409       return -2;
410
411    pktsiz = ntohs(pktsiz);         /* decode no. of bytes that follow */
412    if (pktsiz > maxlen) {
413       net_errmsg = "net_recv: record length too large\n";
414       return -2;
415    }
416    if (pktsiz == 0)
417       return 0;                    /* soft EOF */
418
419    /* now read the actual data */
420    if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
421       net_errmsg = "net_recv: read_nbytes error\n";
422       return -2;
423    }
424    if (nbytes != pktsiz) {
425       net_errmsg = "net_recv: error in read_nbytes\n";
426       return -2;
427    }
428
429    return (nbytes);                /* return actual length of message */
430 }
431
432 /*
433  * Send a message over the network. The send consists of
434  * two network packets. The first is sends a short containing
435  * the length of the data packet which follows.
436  * Returns number of bytes sent
437  * Returns -1 on error
438  */
439 int net_send(int sockfd, char *buff, int len)
440 {
441    int rc;
442    short pktsiz;
443
444    /* send short containing size of data packet */
445    pktsiz = htons((short)len);
446    rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
447    if (rc != sizeof(short)) {
448       net_errmsg = "net_send: write_nbytes error of length prefix\n";
449       return -1;
450    }
451
452    /* send data packet */
453    rc = write_nbytes(sockfd, buff, len);
454    if (rc != len) {
455       net_errmsg = "net_send: write_nbytes error\n";
456       return -1;
457    }
458
459    return rc;
460 }
461
462
463 /* Get and print status from apcupsd NIS server */
464 static void do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
465 {
466   int sockfd, n;
467   char recvline[MAXSTRING + 1];
468   char *tokptr;
469
470   if ((sockfd = net_open(host, NULL, port)) < 0)
471     Error_abort0(net_errmsg);
472   
473   net_send(sockfd, "status", 6);
474   
475   while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
476     recvline[n] = 0;
477 #ifdef APCMAIN
478     fputs(recvline, stdout);
479     int printit = 1;
480 #else
481     int printit = 0;
482 #endif /* ifdef APCMAIN */
483
484     tokptr = strtok(recvline,":");
485     while(tokptr != NULL) {
486       /* Look for Limit_Add */
487       if(strncmp("LINEV",tokptr,5) == 0) { 
488         if(printit) fprintf(stdout,"  Found LINEV.\n");
489         tokptr = strtok(NULL," \t");
490         if(tokptr == NULL) continue;
491         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
492         apcups_detail->linev = atof (tokptr);
493       }else if(strncmp("BATTV",tokptr,5) == 0) { 
494         if(printit) fprintf(stdout,"  Found BATTV.\n");
495         tokptr = strtok(NULL," \t");
496         if(tokptr == NULL) continue;
497         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
498         apcups_detail->battv = atof (tokptr);
499       }else if(strncmp("ITEMP",tokptr,5) == 0) { 
500         if(printit) fprintf(stdout,"  Found ITEMP.\n");
501         tokptr = strtok(NULL," \t");
502         if(tokptr == NULL) continue;
503         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
504         apcups_detail->itemp = atof (tokptr);
505       }else if(strncmp("LOADPCT",tokptr,7) == 0) { 
506         if(printit) fprintf(stdout,"  Found LOADPCT.\n");
507         tokptr = strtok(NULL," \t");
508         if(tokptr == NULL) continue;
509         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
510         apcups_detail->loadpct = atof (tokptr);
511       }else if(strncmp("BCHARGE",tokptr,7) == 0) { 
512         if(printit) fprintf(stdout,"  Found BCHARGE.\n");
513         tokptr = strtok(NULL," \t");
514         if(tokptr == NULL) continue;
515         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
516         apcups_detail->bcharge = atof (tokptr);
517       }else if(strncmp("OUTPUTV",tokptr,7) == 0) { 
518         if(printit) fprintf(stdout,"  Found OUTPUTV.\n");
519         tokptr = strtok(NULL," \t");
520         if(tokptr == NULL) continue;
521         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
522         apcups_detail->outputv = atof (tokptr);
523       }else if(strncmp("LINEFREQ",tokptr,8) == 0) { 
524         if(printit) fprintf(stdout,"  Found LINEFREQ.\n");
525         tokptr = strtok(NULL," \t");
526         if(tokptr == NULL) continue;
527         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
528         apcups_detail->linefreq = atof (tokptr);
529       }else if(strncmp("TIMELEFT",tokptr,8) == 0) { 
530         if(printit) fprintf(stdout,"  Found TIMELEFT.\n");
531         tokptr = strtok(NULL," \t");
532         if(tokptr == NULL) continue;
533         if(printit) fprintf(stdout,"  Value %s.\n",tokptr);
534         apcups_detail->timeleft = atof (tokptr);
535       } /* */
536       tokptr = strtok(NULL,":");
537     }
538   }
539
540   if (n < 0)
541     Error_abort0(net_errmsg);
542   
543   net_close(sockfd);
544 }
545
546 #ifdef APCMAIN
547 int main(int argc, char **argv)
548 {
549   /* we are not really going to use this */
550   struct apc_detail_s apcups_detail;
551
552   if (!*host || strcmp(host, "0.0.0.0") == 0)
553     host = "localhost";
554   
555   do_pthreads_status(host, port, &apcups_detail);
556   
557   return 0;
558 }
559 #else
560 static void apcups_init (void)
561 {
562         return;
563 }
564
565 static int apcups_config (char *key, char *value)
566 {
567   static char lhost[126];
568   
569   if (strcasecmp (key, "host") == 0)
570     {
571       lhost[0] = '\0';
572       strcpy(lhost,key);
573       host = lhost;
574     }
575   else if (strcasecmp (key, "Port") == 0)
576     {
577       int port_tmp = atoi (value);
578       if(port_tmp < 1 || port_tmp > 65535) {
579         syslog (LOG_WARNING, "apcups: `port' failed: %s",
580                 value);
581         return (1);
582       } else {
583         port = port_tmp;
584       }
585     }
586   else
587     {
588       return (-1);
589     }
590   return(0);
591 }
592
593 #define BUFSIZE 256
594 static void apcups_submit (char *host,
595                            struct apc_detail_s *apcups_detail)
596 {
597         char buf[BUFSIZE];
598
599         if (snprintf (buf, BUFSIZE, "%u:%f:%f",
600                       (unsigned int) curtime,
601                       apcups_detail->linev,
602                       apcups_detail->outputv) >= BUFSIZE)
603           return;
604         
605         plugin_submit (MODULE_NAME, host, buf);
606 }
607
608 static void apc_bvolt_submit (char *host,
609                            struct apc_detail_s *apcups_detail)
610 {
611         char buf[BUFSIZE];
612
613         if (snprintf (buf, BUFSIZE, "%u:%f",
614                       (unsigned int) curtime,
615                       apcups_detail->battv) >= BUFSIZE)
616           return;
617         
618         plugin_submit ("apcups_bvolt", host, buf);
619 }
620
621 static void apc_load_submit (char *host,
622                            struct apc_detail_s *apcups_detail)
623 {
624         char buf[BUFSIZE];
625
626         if (snprintf (buf, BUFSIZE, "%u:%f",
627                       (unsigned int) curtime,
628                       apcups_detail->loadpct) >= BUFSIZE)
629           return;
630         
631         plugin_submit ("apcups_load", host, buf);
632 }
633
634 static void apc_charge_submit (char *host,
635                            struct apc_detail_s *apcups_detail)
636 {
637         char buf[BUFSIZE];
638
639         if (snprintf (buf, BUFSIZE, "%u:%f",
640                       (unsigned int) curtime,
641                       apcups_detail->bcharge) >= BUFSIZE)
642           return;
643         
644         plugin_submit ("apcups_charge", host, buf);
645 }
646
647 static void apc_temp_submit (char *host,
648                            struct apc_detail_s *apcups_detail)
649 {
650         char buf[BUFSIZE];
651
652         if (snprintf (buf, BUFSIZE, "%u:%f",
653                       (unsigned int) curtime,
654                       apcups_detail->itemp) >= BUFSIZE)
655           return;
656         
657         plugin_submit ("apcups_temp", host, buf);
658 }
659
660 static void apc_time_submit (char *host,
661                            struct apc_detail_s *apcups_detail)
662 {
663         char buf[BUFSIZE];
664
665         if (snprintf (buf, BUFSIZE, "%u:%f",
666                       (unsigned int) curtime,
667                       apcups_detail->timeleft) >= BUFSIZE)
668           return;
669         
670         plugin_submit ("apcups_time", host, buf);
671 }
672
673 static void apc_freq_submit (char *host,
674                            struct apc_detail_s *apcups_detail)
675 {
676         char buf[BUFSIZE];
677
678         if (snprintf (buf, BUFSIZE, "%u:%f",
679                       (unsigned int) curtime,
680                       apcups_detail->linefreq) >= BUFSIZE)
681           return;
682         
683         plugin_submit ("apcups_freq", host, buf);
684 }
685 #undef BUFSIZE
686
687 static void apcups_read (void)
688 {
689   struct apc_detail_s apcups_detail;
690         
691   apcups_detail.linev = 0.0;
692   apcups_detail.loadpct = 0.0;
693   apcups_detail.bcharge = 0.0;
694   apcups_detail.timeleft = 0.0;
695   apcups_detail.outputv = 0.0;
696   apcups_detail.itemp = 0.0;
697   apcups_detail.battv = 0.0;
698   apcups_detail.linefreq = 0.0;
699
700   
701   if (!*host || strcmp(host, "0.0.0.0") == 0)
702     host = "localhost";
703   
704   do_pthreads_status(host, port, &apcups_detail);
705  
706   apcups_submit (host, &apcups_detail);
707   apc_bvolt_submit (host, &apcups_detail);
708   apc_load_submit (host, &apcups_detail);
709   apc_charge_submit (host, &apcups_detail);
710   apc_temp_submit (host, &apcups_detail);
711   apc_time_submit (host, &apcups_detail);
712   apc_freq_submit (host, &apcups_detail);
713 }
714
715
716 static void apcups_write (char *host, char *inst, char *val)
717 {
718   char file[512];
719   int status;
720   
721   status = snprintf (file, 512, volt_file_template, inst);
722   if (status < 1)
723     return;
724   else if (status >= 512)
725     return;
726   
727   rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
728 }
729
730 static void apc_bvolt_write (char *host, char *inst, char *val)
731 {
732   char file[512];
733   int status;
734   
735   status = snprintf (file, 512, bvolt_file_template, inst);
736   if (status < 1)
737     return;
738   else if (status >= 512)
739     return;
740   
741   rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
742 }
743
744 static void apc_load_write (char *host, char *inst, char *val)
745 {
746   char file[512];
747   int status;
748   
749   status = snprintf (file, 512, load_file_template, inst);
750   if (status < 1)
751     return;
752   else if (status >= 512)
753     return;
754   
755   rrd_update_file (host, file, val, load_ds_def, load_ds_num);
756 }
757
758 static void apc_charge_write (char *host, char *inst, char *val)
759 {
760   char file[512];
761   int status;
762   
763   status = snprintf (file, 512, charge_file_template, inst);
764   if (status < 1)
765     return;
766   else if (status >= 512)
767     return;
768   
769   rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
770 }
771
772 static void apc_temp_write (char *host, char *inst, char *val)
773 {
774   char file[512];
775   int status;
776   
777   status = snprintf (file, 512, temp_file_template, inst);
778   if (status < 1)
779     return;
780   else if (status >= 512)
781     return;
782   
783   rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
784 }
785
786 static void apc_time_write (char *host, char *inst, char *val)
787 {
788   char file[512];
789   int status;
790   
791   status = snprintf (file, 512, time_file_template, inst);
792   if (status < 1)
793     return;
794   else if (status >= 512)
795     return;
796   
797   rrd_update_file (host, file, val, time_ds_def, time_ds_num);
798 }
799
800 static void apc_freq_write (char *host, char *inst, char *val)
801 {
802   char file[512];
803   int status;
804   
805   status = snprintf (file, 512, freq_file_template, inst);
806   if (status < 1)
807     return;
808   else if (status >= 512)
809     return;
810   
811   rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
812 }
813
814 void module_register (void)
815 {
816         plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
817         plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
818         plugin_register ("apcups_load", NULL, NULL, apc_load_write);
819         plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
820         plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
821         plugin_register ("apcups_time", NULL, NULL, apc_time_write);
822         plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
823         cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
824 }
825
826 #endif /* ifdef APCMAIN */
827 #undef MODULE_NAME