APC UPS fixes.
[monky] / src / apcupsd.c
1 /* apcupsd.c:  conky module for APC UPS daemon monitoring
2  *
3  * Copyright (C) 2009 Jaromir Smrcek <jaromir.smrcek@zoner.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
18  * USA. */
19
20 #include "conky.h"
21 #include "apcupsd.h"
22 #include "logging.h"
23
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <unistd.h>
27
28 //
29 // encapsulated recv()
30 //
31 static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
32 {
33         
34         fd_set  fds;
35         int             res;
36
37         // wait for some data to be read
38         do {
39                 errno = 0;
40                 FD_ZERO(&fds);
41                 FD_SET(sock, &fds);
42                 res = select(sock + 1, &fds, NULL, NULL, tv);
43         } while (res < 0 && errno == EINTR);
44         if (res < 0) return 0;
45         if (res == 0) {
46                 // timeout
47                 errno = ETIMEDOUT;  // select was succesfull, errno is now 0
48                 return 0;
49         }
50
51         // socket ready, read the data
52         do {
53                 errno = 0;
54                 res = recv(sock, (char*)buf, size, 0);
55         } while (res < 0 && errno == EINTR);
56         if (res < 0) return 0;
57         if (res == 0) {
58                 // orderly shutdown
59                 errno = ENOTCONN;
60                 return 0;
61         }
62
63         return res;
64 }
65
66 //
67 // read whole buffer or fail
68 //
69 static int net_recv(int sock, void* buf, int size) {
70
71         int todo = size;
72         int off = 0;
73         int len;
74         struct timeval tv = { 0, 250000 };
75
76         while (todo) {
77                 len = net_recv_ex(sock, (char*)buf + off, todo, &tv);
78                 if (!len) return 0;
79                 todo -= len;
80                 off  += len;
81         }
82         return 1;
83 }
84
85 //
86 // get one response line
87 //
88 static int get_line(int sock, char line[], short linesize) {
89
90         // get the line length
91         short sz;
92         if (!net_recv(sock, &sz, sizeof(sz))) return -1;
93         sz = ntohs(sz);
94         if (!sz) return 0;
95
96         // get the line
97         while (sz > linesize) {
98                 // this is just a hack (being lazy), this should not happen anyway
99                 net_recv(sock, line, linesize);
100                 sz -= sizeof(line);
101         }
102         if (!net_recv(sock, line, sz)) return 0;
103         line[sz] = 0;
104         return sz;
105 }
106
107 #define FILL(NAME,FIELD,FIRST)                                                                                                          \
108         if (!strncmp(NAME, line, sizeof(NAME)-1)) {                                                                             \
109                 strncpy(apc->items[FIELD], line+11, APCUPSD_MAXSTR);                                            \
110                 /* remove trailing newline and assure termination */                                            \
111                 apc->items[FIELD][len-11 > APCUPSD_MAXSTR ? APCUPSD_MAXSTR : len-12] = 0;       \
112                 if (FIRST) {                                                                                                                            \
113                         char* c;                                                                                                                                \
114                         for (c = apc->items[FIELD]; *c; ++c)                                                                    \
115                                 if (*c == ' ' && c > apc->items[FIELD]+2) {                                                     \
116                                         *c = 0;                                                                                                                 \
117                                         break;                                                                                                                  \
118                                 }                                                                                                                                       \
119                 }                                                                                                                                                       \
120         }
121
122 //
123 // fills in the data received from a socket
124 //
125 static int fill_items(int sock, PAPCUPSD_S apc) {
126
127         char line[512];
128         int len;
129         while ((len = get_line(sock, line, sizeof(line)))) {
130                 // fill the right types in
131                 FILL("UPSNAME",         APCUPSD_NAME,           FALSE);
132                 FILL("MODEL",           APCUPSD_MODEL,          FALSE);
133                 FILL("UPSMODE",         APCUPSD_UPSMODE,        FALSE);
134                 FILL("CABLE",           APCUPSD_CABLE,          FALSE);
135                 FILL("STATUS",          APCUPSD_STATUS,         FALSE);
136                 FILL("LINEV",           APCUPSD_LINEV,          TRUE);
137                 FILL("LOADPCT",         APCUPSD_LOAD,           TRUE);
138                 FILL("BCHARGE",         APCUPSD_CHARGE,         TRUE);
139                 FILL("TIMELEFT",        APCUPSD_TIMELEFT,       TRUE);
140                 FILL("ITEMP",           APCUPSD_TEMP,           TRUE);
141                 FILL("LASTXFER",        APCUPSD_LASTXFER,       FALSE);
142         }
143         
144         return len == 0;
145 }
146
147 //
148 // Conky update function for apcupsd data
149 //
150 void update_apcupsd(void) {
151
152         int i;
153         APCUPSD_S apc;
154         int sock;
155
156         for (i = 0; i < _APCUPSD_COUNT; ++i)
157                 memcpy(apc.items[i], "N/A", 4); // including \0
158
159         do {
160                 struct hostent* he = 0;
161                 struct sockaddr_in addr;
162                 short sz = 0;
163 #ifdef HAVE_GETHOSTBYNAME_R
164                 struct hostent he_mem;
165                 int he_errno;
166                 char hostbuff[2048];
167 #endif
168                 //
169                 // connect to apcupsd daemon
170                 //
171                 sock = socket(AF_INET, SOCK_STREAM, 0);
172                 if (sock < 0) {
173                         perror("socket");
174                         break;
175                 }
176 #ifdef HAVE_GETHOSTBYNAME_R
177                 if (gethostbyname_r(info.apcupsd.host, &he_mem, hostbuff, sizeof(hostbuff), &he, &he_errno)) {
178                         ERR("APCUPSD gethostbyname_r: %s", hstrerror(h_errno));
179                         break;
180                 }
181 #else /* HAVE_GETHOSTBYNAME_R */
182                 he = gethostbyname(info.apcupsd.host);
183                 if (!he) {
184                         herror("gethostbyname");
185                         break;
186                 }
187 #endif /* HAVE_GETHOSTBYNAME_R */
188                 
189                 memset(&addr, 0, sizeof(addr));
190                 addr.sin_family = AF_INET;
191                 addr.sin_port = info.apcupsd.port;
192                 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
193                 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) < 0) {
194                         // no error reporting, the daemon is probably not running
195                         break;
196                 }
197         
198                 //
199                 // send status request - "status" - 6B
200                 //
201                 sz = htons(6);
202                 // no waiting to become writeable is really needed
203                 if (send(sock, &sz, sizeof(sz), 0) != sizeof(sz) || send(sock, "status", 6, 0) != 6) {
204                         perror("send");
205                         break;
206                 }
207         
208                 //
209                 // read the lines of output and put them into the info structure
210                 //
211                 if (!fill_items(sock, &apc)) break;
212
213         } while (0);
214
215         close(sock);
216
217         //
218         // "atomically" copy the data into working set
219         //
220         memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));
221         return;
222 }
223
224 //
225 // fills in the N/A strings and default host:port
226 //
227 void init_apcupsd(void) {
228
229         int i;
230         for (i = 0; i < _APCUPSD_COUNT; ++i)
231                 memcpy(info.apcupsd.items[i], "N/A", 4); // including \0
232         memcpy(info.apcupsd.host, "localhost", 10);
233         info.apcupsd.port = htons(3551);
234 }