1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * apcupsd.c: conky module for APC UPS daemon monitoring
6 * Copyright (C) 2009 Jaromir Smrcek <jaromir.smrcek@zoner.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
30 #include <netinet/in.h>
36 // encapsulated recv()
38 static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
44 // wait for some data to be read
49 res = select(sock + 1, &fds, NULL, NULL, tv);
50 } while (res < 0 && errno == EINTR);
51 if (res < 0) return 0;
54 errno = ETIMEDOUT; // select was succesfull, errno is now 0
58 // socket ready, read the data
61 res = recv(sock, (char*)buf, size, 0);
62 } while (res < 0 && errno == EINTR);
63 if (res < 0) return 0;
74 // read whole buffer or fail
76 static int net_recv(int sock, void* buf, int size) {
81 struct timeval tv = { 0, 250000 };
84 len = net_recv_ex(sock, (char*)buf + off, todo, &tv);
93 // get one response line
95 static int get_line(int sock, char line[], short linesize) {
97 // get the line length
99 if (!net_recv(sock, &sz, sizeof(sz))) return -1;
104 while (sz > linesize) {
105 // this is just a hack (being lazy), this should not happen anyway
106 net_recv(sock, line, linesize);
109 if (!net_recv(sock, line, sz)) return 0;
114 #define FILL(NAME,FIELD,FIRST) \
115 if (!strncmp(NAME, line, sizeof(NAME)-1)) { \
116 strncpy(apc->items[FIELD], line+11, APCUPSD_MAXSTR); \
117 /* remove trailing newline and assure termination */ \
118 apc->items[FIELD][len-11 > APCUPSD_MAXSTR ? APCUPSD_MAXSTR : len-12] = 0; \
121 for (c = apc->items[FIELD]; *c; ++c) \
122 if (*c == ' ' && c > apc->items[FIELD]+2) { \
130 // fills in the data received from a socket
132 static int fill_items(int sock, PAPCUPSD_S apc) {
136 while ((len = get_line(sock, line, sizeof(line)))) {
137 // fill the right types in
138 FILL("UPSNAME", APCUPSD_NAME, FALSE);
139 FILL("MODEL", APCUPSD_MODEL, FALSE);
140 FILL("UPSMODE", APCUPSD_UPSMODE, FALSE);
141 FILL("CABLE", APCUPSD_CABLE, FALSE);
142 FILL("STATUS", APCUPSD_STATUS, FALSE);
143 FILL("LINEV", APCUPSD_LINEV, TRUE);
144 FILL("LOADPCT", APCUPSD_LOAD, TRUE);
145 FILL("BCHARGE", APCUPSD_CHARGE, TRUE);
146 FILL("TIMELEFT", APCUPSD_TIMELEFT, TRUE);
147 FILL("ITEMP", APCUPSD_TEMP, TRUE);
148 FILL("LASTXFER", APCUPSD_LASTXFER, FALSE);
155 // Conky update function for apcupsd data
157 void update_apcupsd(void) {
163 for (i = 0; i < _APCUPSD_COUNT; ++i)
164 memcpy(apc.items[i], "N/A", 4); // including \0
167 struct hostent* he = 0;
168 struct sockaddr_in addr;
170 #ifdef HAVE_GETHOSTBYNAME_R
171 struct hostent he_mem;
176 // connect to apcupsd daemon
178 sock = socket(AF_INET, SOCK_STREAM, 0);
183 #ifdef HAVE_GETHOSTBYNAME_R
184 if (gethostbyname_r(info.apcupsd.host, &he_mem, hostbuff, sizeof(hostbuff), &he, &he_errno) || !he ) {
185 NORM_ERR("APCUPSD gethostbyname_r: %s", hstrerror(h_errno));
188 #else /* HAVE_GETHOSTBYNAME_R */
189 he = gethostbyname(info.apcupsd.host);
191 herror("gethostbyname");
194 #endif /* HAVE_GETHOSTBYNAME_R */
196 memset(&addr, 0, sizeof(addr));
197 addr.sin_family = AF_INET;
198 addr.sin_port = info.apcupsd.port;
199 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
200 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) < 0) {
201 // no error reporting, the daemon is probably not running
206 // send status request - "status" - 6B
209 // no waiting to become writeable is really needed
210 if (send(sock, &sz, sizeof(sz), 0) != sizeof(sz) || send(sock, "status", 6, 0) != 6) {
216 // read the lines of output and put them into the info structure
218 if (!fill_items(sock, &apc)) break;
225 // "atomically" copy the data into working set
227 memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));