1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
|
/* netxml-ups.c Driver routines for network XML UPS units
Copyright (C)
2008-2009 Arjen de Korte <adkorte-guest@alioth.debian.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "main.h"
#include "netxml-ups.h"
#include "mge-xml.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ne_request.h>
#include <ne_basic.h>
#include <ne_props.h>
#include <ne_uri.h>
#include <ne_xml.h>
#include <ne_xmlreq.h>
#include <ne_ssl.h>
#include <ne_auth.h>
#include <ne_socket.h>
#define DRIVER_NAME "network XML UPS"
#define DRIVER_VERSION "0.30"
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Arjen de Korte <adkorte-guest@alioth.debian.org>",
DRV_EXPERIMENTAL,
{ NULL }
};
/* FIXME:
* "built with neon library %s" LIBNEON_VERSION
* subdrivers (limited to MGE only ATM) */
/* Global vars */
uint32_t ups_status = 0;
static int timeout = 5;
static time_t lastheard = 0;
static subdriver_t *subdriver = &mge_xml_subdriver;
static ne_session *session = NULL;
static ne_socket *sock = NULL;
static ne_uri uri;
/* Support functions */
static void netxml_alarm_set(void);
static void netxml_status_set(void);
static int netxml_authenticate(void *userdata, const char *realm, int attempt, char *username, char *password);
static int netxml_dispatch_request(ne_request *request, ne_xml_parser *parser);
static int netxml_get_page(const char *page);
static int netxml_alarm_subscribe(const char *page);
#if HAVE_NE_SET_CONNECT_TIMEOUT && HAVE_NE_SOCK_CONNECT_TIMEOUT
/* we don't need to use alarm() */
#else
static void netxml_alarm_handler(int sig)
{
/* don't do anything here, just return */
}
#endif
void upsdrv_initinfo(void)
{
char *page, *last = NULL;
char buf[SMALLBUF];
snprintf(buf, sizeof(buf), "%s", subdriver->initinfo);
for (page = strtok_r(buf, " ", &last); page != NULL; page = strtok_r(NULL, " ", &last)) {
if (netxml_get_page(page) != NE_OK) {
continue;
}
dstate_setinfo("driver.version.internal", "%s", subdriver->version);
if (testvar("subscribe") && (netxml_alarm_subscribe(subdriver->subscribe) == NE_OK)) {
extrafd = ne_sock_fd(sock);
time(&lastheard);
}
return;
}
fatalx(EXIT_FAILURE, "%s: communication failure [%s]", __func__, ne_get_error(session));
}
void upsdrv_updateinfo(void)
{
int ret, errors = 0;
/* We really should be dealing with alarms through a separate callback, so that we can keep the
* processing of alarms and polling for data separated. Currently, this isn't supported by the
* driver main body, so we'll have to revert to polling each time we're called, unless the
* socket indicates we're no longer connected.
*/
if (testvar("subscribe")) {
char buf[LARGEBUF];
ret = ne_sock_read(sock, buf, sizeof(buf));
if (ret > 0) {
/* alarm message received */
ne_xml_parser *parser = ne_xml_create();
upsdebugx(2, "%s: ne_sock_read(%d bytes) => %s", __func__, ret, buf);
ne_xml_push_handler(parser, subdriver->startelm_cb, subdriver->cdata_cb, subdriver->endelm_cb, NULL);
ne_xml_parse(parser, buf, strlen(buf));
ne_xml_destroy(parser);
time(&lastheard);
} else if ((ret == NE_SOCK_TIMEOUT) && (difftime(time(NULL), lastheard) < 180)) {
/* timed out */
upsdebugx(2, "%s: ne_sock_read(timeout)", __func__);
} else {
/* connection closed or unknown error */
upslogx(LOG_ERR, "NSM connection with '%s' lost", uri.host);
upsdebugx(2, "%s: ne_sock_read(%d) => %s", __func__, ret, ne_sock_error(sock));
ne_sock_close(sock);
if (netxml_alarm_subscribe(subdriver->subscribe) == NE_OK) {
extrafd = ne_sock_fd(sock);
time(&lastheard);
return;
}
dstate_datastale();
extrafd = -1;
return;
}
}
/* get additional data */
ret = netxml_get_page(subdriver->getobject);
if (ret != NE_OK) {
errors++;
}
ret = netxml_get_page(subdriver->summary);
if (ret != NE_OK) {
errors++;
}
if (errors > 1) {
dstate_datastale();
return;
}
status_init();
alarm_init();
netxml_alarm_set();
alarm_commit();
netxml_status_set();
status_commit();
dstate_dataok();
}
void upsdrv_shutdown(void)
{
/* tell the UPS to shut down, then return - DO NOT SLEEP HERE */
/* maybe try to detect the UPS here, but try a shutdown even if
it doesn't respond at first if possible */
/* replace with a proper shutdown function */
fatalx(EXIT_FAILURE, "shutdown not supported");
/* you may have to check the line status since the commands
for toggling power are frequently different for OL vs. OB */
/* OL: this must power cycle the load if possible */
/* OB: the load must remain off until the power returns */
}
/*
static int instcmd(const char *cmdname, const char *extra)
{
if (!strcasecmp(cmdname, "test.battery.stop")) {
ser_send_buf(upsfd, ...);
return STAT_INSTCMD_HANDLED;
}
upslogx(LOG_NOTICE, "%s: unknown command [%s]", __func__, cmdname);
return STAT_INSTCMD_UNKNOWN;
}
*/
/*
static int setvar(const char *varname, const char *val)
{
if (!strcasecmp(varname, "ups.test.interval")) {
ser_send_buf(upsfd, ...);
return STAT_SET_HANDLED;
}
upslogx(LOG_NOTICE, "%s: unknown variable [%s]", __func__, varname);
return STAT_SET_UNKNOWN;
}
*/
void upsdrv_help(void)
{
}
/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
char buf[SMALLBUF];
snprintf(buf, sizeof(buf), "network timeout (default: %d seconds)", timeout);
addvar(VAR_VALUE, "timeout", buf);
addvar(VAR_FLAG, "subscribe", "NSM subscribe to NMC");
addvar(VAR_VALUE | VAR_SENSITIVE, "login", "login value for authenticated mode");
addvar(VAR_VALUE | VAR_SENSITIVE, "password", "password value for authenticated mode");
}
void upsdrv_initups(void)
{
int ret;
char *val;
FILE *fp;
#if HAVE_NE_SET_CONNECT_TIMEOUT && HAVE_NE_SOCK_CONNECT_TIMEOUT
/* we don't need to use alarm() */
#else
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = netxml_alarm_handler;
sigaction(SIGALRM, &sa, NULL);
#endif
/* allow override of default network timeout value */
val = getval("timeout");
if (val) {
timeout = atoi(val);
if (timeout < 1) {
fatalx(EXIT_FAILURE, "timeout must be greater than 0");
}
}
if (nut_debug_level > 5) {
ne_debug_init(stderr, NE_DBG_HTTP | NE_DBG_HTTPBODY);
}
if (ne_sock_init()) {
fatalx(EXIT_FAILURE, "%s: failed to initialize socket libraries", progname);
}
if (ne_uri_parse(device_path, &uri) || uri.host == NULL) {
fatalx(EXIT_FAILURE, "%s: invalid hostname '%s'", progname, device_path);
}
/*
if (uri.scheme == NULL) {
uri.scheme = strdup("http");
}
if (uri.host == NULL) {
uri.host = strdup(device_path);
}
*/
if (uri.port == 0) {
uri.port = ne_uri_defaultport(uri.scheme);
}
upsdebugx(1, "using %s://%s port %d", uri.scheme, uri.host, uri.port);
session = ne_session_create(uri.scheme, uri.host, uri.port);
/* timeout if we can't (re)connect to the UPS */
#ifdef HAVE_NE_SET_CONNECT_TIMEOUT
ne_set_connect_timeout(session, timeout);
#endif
/* just wait for a couple of seconds */
ne_set_read_timeout(session, timeout);
ne_set_useragent(session, subdriver->version);
if (strcasecmp(uri.scheme, "https") == 0) {
ne_ssl_trust_default_ca(session);
}
ne_set_server_auth(session, netxml_authenticate, NULL);
/* if debug level is set, direct output to stderr */
if (!nut_debug_level) {
fp = fopen("/dev/null", "w");
} else {
fp = stderr;
}
if (!fp) {
fatal_with_errno(EXIT_FAILURE, "Connectivity test failed");
}
/* see if we have a connection */
ret = ne_get(session, subdriver->initups, fileno(fp));
if (!nut_debug_level) {
fclose(fp);
} else {
fprintf(fp, "\n");
}
if (ret != NE_OK) {
fatalx(EXIT_FAILURE, "Connectivity test: %s", ne_get_error(session));
}
upslogx(LOG_INFO, "Connectivity test: %s", ne_get_error(session));
}
void upsdrv_cleanup(void)
{
free(subdriver->configure);
free(subdriver->subscribe);
free(subdriver->summary);
free(subdriver->getobject);
free(subdriver->setobject);
if (sock) {
ne_sock_close(sock);
}
if (session) {
ne_session_destroy(session);
}
ne_uri_free(&uri);
}
/**********************************************************************
* Support functions
*********************************************************************/
static int netxml_get_page(const char *page)
{
int ret;
ne_request *request;
ne_xml_parser *parser;
upsdebugx(2, "%s: %s", __func__, page);
request = ne_request_create(session, "GET", page);
parser = ne_xml_create();
ne_xml_push_handler(parser, subdriver->startelm_cb, subdriver->cdata_cb, subdriver->endelm_cb, NULL);
ret = netxml_dispatch_request(request, parser);
if (ret) {
upsdebugx(2, "%s: %s", __func__, ne_get_error(session));
}
ne_xml_destroy(parser);
ne_request_destroy(request);
return ret;
}
static int netxml_alarm_subscribe(const char *page)
{
int ret, port = -1, secret = -1;
char buf[LARGEBUF], *s;
ne_request *request;
ne_sock_addr *addr;
const ne_inet_addr *ai;
upsdebugx(2, "%s: %s", __func__, page);
sock = ne_sock_create();
if (gethostname(buf, sizeof(buf)) == 0) {
dstate_setinfo("driver.hostname", "%s", buf);
} else {
dstate_setinfo("driver.hostname", "<unknown>");
}
#ifdef HAVE_NE_SOCK_CONNECT_TIMEOUT
ne_sock_connect_timeout(sock, timeout);
#endif
ne_sock_read_timeout(sock, 1);
netxml_get_page(subdriver->configure);
snprintf(buf, sizeof(buf), "<?xml version=\"1.0\">\n");
snprintfcat(buf, sizeof(buf), "<Subscribe>\n");
snprintfcat(buf, sizeof(buf), "<Class>%s v%s</Class>\n", progname, DRIVER_VERSION);
snprintfcat(buf, sizeof(buf), "<Type>connected socket</Type>\n");
snprintfcat(buf, sizeof(buf), "<HostName>%s</HostName>\n", dstate_getinfo("driver.hostname"));
snprintfcat(buf, sizeof(buf), "<XMLClientParameters>\n");
snprintfcat(buf, sizeof(buf), "<ShutdownDuration>%s</ShutdownDuration>\n", dstate_getinfo("driver.delay.shutdown"));
snprintfcat(buf, sizeof(buf), "<ShutdownTimer>%s</ShutdownTimer>\n", dstate_getinfo("driver.timer.shutdown"));
snprintfcat(buf, sizeof(buf), "<AutoConfig>CENTRALIZED</AutoConfig>\n");
snprintfcat(buf, sizeof(buf), "<OutletGroup>1</OutletGroup>\n");
snprintfcat(buf, sizeof(buf), "</XMLClientParameters>\n");
/* snprintfcat(buf, sizeof(buf), "<Warning>NUT driver</Warning>\n"); */
snprintfcat(buf, sizeof(buf), "</Subscribe>\n");
/* now send subscription message setting all the proper flags */
request = ne_request_create(session, "POST", page);
ne_set_request_body_buffer(request, buf, strlen(buf));
/* as the NMC reply is not xml standard compliant let's parse it this way */
do {
#ifndef HAVE_NE_SOCK_CONNECT_TIMEOUT
alarm(timeout+1);
#endif
ret = ne_begin_request(request);
#ifndef HAVE_NE_SOCK_CONNECT_TIMEOUT
alarm(0);
#endif
if (ret != NE_OK) {
break;
}
ret = ne_read_response_block(request, buf, sizeof buf);
if (ret == NE_OK) {
ret = ne_end_request(request);
}
} while (ret == NE_RETRY);
ne_request_destroy(request);
/* due to different formats used by the various NMCs, we need to\
break up the reply in lines and parse each one separately */
for (s = strtok(buf, "\r\n"); s != NULL; s = strtok(NULL, "\r\n")) {
upsdebugx(2, "%s: parsing %s", __func__, s);
if (!strncasecmp(s, "<Port>", 6) && (sscanf(s+6, "%u", &port) != 1)) {
return NE_RETRY;
}
if (!strncasecmp(s, "<Secret>", 8) && (sscanf(s+8, "%u", &secret) != 1)) {
return NE_RETRY;
}
}
if ((port == -1) || (secret == -1)) {
upsdebugx(2, "%s: parsing initial subcription failed", __func__);
return NE_RETRY;
}
/* Resolve the given hostname. 'flags' must be zero. Hex
* string IPv6 addresses (e.g. `::1') may be enclosed in brackets
* (e.g. `[::1]'). */
addr = ne_addr_resolve(uri.host, 0);
/* Returns zero if name resolution was successful, non-zero on
* error. */
if (ne_addr_result(addr) != 0) {
upsdebugx(2, "%s: name resolution failure on %s: %s", __func__, uri.host, ne_addr_error(addr, buf, sizeof(buf)));
ne_addr_destroy(addr);
return NE_RETRY;
}
for (ai = ne_addr_first(addr); ai != NULL; ai = ne_addr_next(addr)) {
upsdebugx(2, "%s: connecting to host %s port %d", __func__, ne_iaddr_print(ai, buf, sizeof(buf)), port);
#ifndef HAVE_NE_SOCK_CONNECT_TIMEOUT
alarm(timeout+1);
#endif
ret = ne_sock_connect(sock, ai, port);
#ifndef HAVE_NE_SOCK_CONNECT_TIMEOUT
alarm(0);
#endif
if (ret == NE_OK) {
upsdebugx(2, "%s: connection to %s open on fd %d", __func__, uri.host, ne_sock_fd(sock));
break;
}
}
ne_addr_destroy(addr);
if (ai == NULL) {
upsdebugx(2, "%s: failed to create listening socket", __func__);
return NE_RETRY;
}
snprintf(buf, sizeof(buf), "<Subscription Identification=\"%u\"></Subscription>\n", secret);
ret = ne_sock_fullwrite(sock, buf, strlen(buf));
if (ret != NE_OK) {
upsdebugx(2, "%s: send failed: %s", __func__, ne_sock_error(sock));
return NE_RETRY;
}
ret = ne_sock_read(sock, buf, sizeof(buf));
if (ret < 1) {
upsdebugx(2, "%s: read failed: %s", __func__, ne_sock_error(sock));
return NE_RETRY;
}
if (strcasecmp(buf, "<Subscription Answer=\"ok\"></Subscription>")) {
upsdebugx(2, "%s: subscription rejected", __func__);
return NE_RETRY;
}
upslogx(LOG_INFO, "NSM connection to '%s' established", uri.host);
return NE_OK;
}
static int netxml_dispatch_request(ne_request *request, ne_xml_parser *parser)
{
int ret;
/*
* Starting with neon-0.27.0 the ne_xml_dispatch_request() function will check
* for a valid XML content-type (following RFC 3023 rules) in the header.
* Unfortunately, (at least) the Transverse NMC doesn't follow this RFC, so
* we can't use this anymore and we'll have to roll our own here.
*/
do {
#ifndef HAVE_NE_SET_CONNECT_TIMEOUT
alarm(timeout+1);
#endif
ret = ne_begin_request(request);
#ifndef HAVE_NE_SET_CONNECT_TIMEOUT
alarm(0);
#endif
if (ret != NE_OK) {
break;
}
ret = ne_xml_parse_response(request, parser);
if (ret == NE_OK) {
ret = ne_end_request(request);
}
} while (ret == NE_RETRY);
return ret;
}
/* Supply the 'login' and 'password' when authentication is required */
static int netxml_authenticate(void *userdata, const char *realm, int attempt, char *username, char *password)
{
char *val;
upsdebugx(2, "%s: realm = [%s], attempt = %d", __func__, realm, attempt);
val = getval("login");
snprintf(username, NE_ABUFSIZ, "%s", val ? val : "");
val = getval("password");
snprintf(password, NE_ABUFSIZ, "%s", val ? val : "");
return attempt;
}
/* Convert the local status information to NUT format and set NUT
alarms. */
static void netxml_alarm_set(void)
{
if (STATUS_BIT(REPLACEBATT)) {
alarm_set("Replace battery!");
}
if (STATUS_BIT(SHUTDOWNIMM)) {
alarm_set("Shutdown imminent!");
}
if (STATUS_BIT(FANFAIL)) {
alarm_set("Fan failure!");
}
if (STATUS_BIT(NOBATTERY)) {
alarm_set("No battery installed!");
}
if (STATUS_BIT(BATTVOLTLO)) {
alarm_set("Battery voltage too low!");
}
if (STATUS_BIT(BATTVOLTHI)) {
alarm_set("Battery voltage too high!");
}
if (STATUS_BIT(CHARGERFAIL)) {
alarm_set("Battery charger fail!");
}
if (STATUS_BIT(OVERHEAT)) {
alarm_set("Temperature too high!");
}
if (STATUS_BIT(COMMFAULT)) {
alarm_set("Communication fault!");
}
if (STATUS_BIT(INTERNALFAULT)) {
alarm_set("Internal UPS fault!");
}
if (STATUS_BIT(FUSEFAULT)) {
alarm_set("Fuse fault!");
}
if (STATUS_BIT(BYPASSAUTO)) {
alarm_set("Automatic bypass mode!");
}
if (STATUS_BIT(BYPASSMAN)) {
alarm_set("Manual bypass mode!");
}
}
/* Convert the local status information to NUT format and set NUT
status. */
static void netxml_status_set(void)
{
if (STATUS_BIT(VRANGE)) {
dstate_setinfo("input.transfer.reason", "input voltage out of range");
} else if (STATUS_BIT(FRANGE)) {
dstate_setinfo("input.transfer.reason", "input frequency out of range");
} else {
dstate_delinfo("input.transfer.reason");
}
if (STATUS_BIT(ONLINE)) {
status_set("OL"); /* on line */
} else {
status_set("OB"); /* on battery */
}
if (STATUS_BIT(DISCHRG)) {
status_set("DISCHRG"); /* discharging */
}
if (STATUS_BIT(CHRG)) {
status_set("CHRG"); /* charging */
}
if (STATUS_BIT(LOWBATT)) {
status_set("LB"); /* low battery */
}
if (STATUS_BIT(OVERLOAD)) {
status_set("OVER"); /* overload */
}
if (STATUS_BIT(REPLACEBATT)) {
status_set("RB"); /* replace batt */
}
if (STATUS_BIT(TRIM)) {
status_set("TRIM"); /* SmartTrim */
}
if (STATUS_BIT(BOOST)) {
status_set("BOOST"); /* SmartBoost */
}
if (STATUS_BIT(BYPASSAUTO) || STATUS_BIT(BYPASSMAN)) {
status_set("BYPASS"); /* on bypass */
}
if (STATUS_BIT(OFF)) {
status_set("OFF"); /* ups is off */
}
if (STATUS_BIT(SHUTDOWNIMM)) {
status_set("FSD"); /* shutdown imminent */
}
}
|