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