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 int update_apcupsd(void) {
163 for (i = 0; i < _APCUPSD_COUNT; ++i)
164 memcpy(apc.items[i], "N/A", 4); // including \0
167 struct addrinfo hints;
168 struct addrinfo *ai, *rp;
173 // connect to apcupsd daemon
175 memset(&hints, 0, sizeof(struct addrinfo));
176 hints.ai_family = AF_UNSPEC;
177 hints.ai_socktype = SOCK_STREAM;
179 hints.ai_protocol = 0;
180 snprintf(portbuf, 8, "%d", info.apcupsd.port);
181 res = getaddrinfo(info.apcupsd.host, portbuf, &hints, &ai);
183 NORM_ERR("APCUPSD getaddrinfo: %s", gai_strerror(res));
186 for (rp = ai; rp != NULL; rp = rp->ai_next) {
187 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
191 if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
198 // no error reporting, the daemon is probably not running
203 // send status request - "status" - 6B
206 // no waiting to become writeable is really needed
207 if (send(sock, &sz, sizeof(sz), 0) != sizeof(sz) || send(sock, "status", 6, 0) != 6) {
213 // read the lines of output and put them into the info structure
215 if (!fill_items(sock, &apc)) break;
222 // "atomically" copy the data into working set
224 memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));