nut-debian/clients/upsrw.c

712 lines
18 KiB
C
Raw Normal View History

2010-03-26 01:20:59 +02:00
/* upsrw - simple client for read/write variable access (formerly upsct2)
2022-07-10 10:23:45 +03:00
Copyright (C)
1999 Russell Kroll <rkroll@exploits.org>
2019 EATON (author: Arnaud Quette <ArnaudQuette@eaton.com>)
2010-03-26 01:20:59 +02:00
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 "common.h"
2013-11-24 17:00:12 +02:00
#include "nut_platform.h"
2010-03-26 01:20:59 +02:00
#include <pwd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
2022-07-10 10:23:45 +03:00
#include "nut_stdint.h"
2010-03-26 01:20:59 +02:00
#include "upsclient.h"
2022-07-10 10:23:45 +03:00
#include "extstate.h"
2010-03-26 01:20:59 +02:00
2022-07-10 10:23:45 +03:00
static char *upsname = NULL, *hostname = NULL;
2011-01-26 11:35:08 +02:00
static UPSCONN_t *ups = NULL;
2022-07-10 10:23:45 +03:00
static int tracking_enabled = 0;
static unsigned int timeout = DEFAULT_TRACKING_TIMEOUT;
2011-01-26 11:35:08 +02:00
2010-03-26 01:20:59 +02:00
struct list_t {
char *name;
struct list_t *next;
};
static void usage(const char *prog)
{
2011-01-26 11:35:08 +02:00
printf("Network UPS Tools %s %s\n\n", prog, UPS_VERSION);
2010-03-26 01:20:59 +02:00
printf("usage: %s [-h]\n", prog);
2022-07-10 10:23:45 +03:00
printf(" %s [-s <variable>] [-u <username>] [-p <password>] [-w] [-t <timeout>] <ups>\n\n", prog);
2010-03-26 01:20:59 +02:00
printf("Demo program to set variables within UPS hardware.\n");
printf("\n");
printf(" -h display this help text\n");
printf(" -s <variable> specify variable to be changed\n");
printf(" use -s VAR=VALUE to avoid prompting for value\n");
2022-07-10 10:23:45 +03:00
printf(" -l show all possible read/write variables.\n");
2010-03-26 01:20:59 +02:00
printf(" -u <username> set username for command authentication\n");
printf(" -p <password> set password for command authentication\n");
2022-07-10 10:23:45 +03:00
printf(" -w wait for the completion of setting by the driver\n");
printf(" and return its actual result from the device\n");
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %u)\n", DEFAULT_TRACKING_TIMEOUT);
2010-03-26 01:20:59 +02:00
printf("\n");
printf(" <ups> UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
printf("\n");
2022-07-10 10:23:45 +03:00
printf("Call without -s to show all possible read/write variables (same as -l).\n");
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
static void clean_exit(void)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
if (ups) {
upscli_disconnect(ups);
}
2010-03-26 01:20:59 +02:00
free(upsname);
free(hostname);
2011-01-26 11:35:08 +02:00
free(ups);
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_BESIDEFUNC) && (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC) )
# pragma GCC diagnostic push
#endif
#if (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC)
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#if (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC)
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
2011-01-26 11:35:08 +02:00
static void do_set(const char *varname, const char *newval)
2010-03-26 01:20:59 +02:00
{
2022-07-10 10:23:45 +03:00
int cmd_complete = 0;
2010-03-26 01:20:59 +02:00
char buf[SMALLBUF], enc[SMALLBUF];
2022-07-10 10:23:45 +03:00
char tracking_id[UUID4_LEN];
time_t start, now;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
snprintf(buf, sizeof(buf), "SET VAR %s %s \"%s\"\n", upsname, varname, pconf_encode(newval, enc, sizeof(enc)));
2010-03-26 01:20:59 +02:00
if (upscli_sendline(ups, buf, strlen(buf)) < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Can't set variable: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
if (upscli_readline(ups, buf, sizeof(buf)) < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Set variable failed: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
/* verify answer */
2010-03-26 01:20:59 +02:00
if (strncmp(buf, "OK", 2) != 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Unexpected response from upsd: %s", buf);
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
/* check for status tracking id */
if (
!tracking_enabled ||
/* sanity check on the size: "OK TRACKING " + UUID4_LEN */
strlen(buf) != (UUID4_LEN - 1 + strlen("OK TRACKING "))
) {
/* reply as usual */
fprintf(stderr, "%s\n", buf);
return;
}
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
/* From the check above, we know that we have exactly UUID4_LEN chars
* (aka sizeof(tracking_id)) in the buf after "OK TRACKING " prefix,
* plus the null-byte.
*/
assert (UUID4_LEN == 1 + snprintf(tracking_id, sizeof(tracking_id), "%s", buf + strlen("OK TRACKING ")));
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic pop
#endif
time(&start);
/* send status tracking request, looping if status is PENDING */
while (!cmd_complete) {
/* check for timeout */
time(&now);
if (difftime(now, start) >= timeout)
fatalx(EXIT_FAILURE, "Can't receive status tracking information: timeout");
snprintf(buf, sizeof(buf), "GET TRACKING %s\n", tracking_id);
if (upscli_sendline(ups, buf, strlen(buf)) < 0)
fatalx(EXIT_FAILURE, "Can't send status tracking request: %s", upscli_strerror(ups));
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
/* Note for gating macros above: unsuffixed HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP
* means support of contexts both inside and outside function body, so the push
* above and pop below (outside this finction) are not used.
*/
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
/* Note that the individual warning pragmas for use inside function bodies
* are named without a _INSIDEFUNC suffix, for simplicity and legacy reasons
*/
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
/* and get status tracking reply */
assert(timeout < LONG_MAX);
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
# pragma GCC diagnostic pop
#endif
if (upscli_readline_timeout(ups, buf, sizeof(buf), (long)timeout) < 0)
fatalx(EXIT_FAILURE, "Can't receive status tracking information: %s", upscli_strerror(ups));
if (strncmp(buf, "PENDING", 7))
cmd_complete = 1;
else
/* wait a second before retrying */
sleep(1);
}
2011-01-26 11:35:08 +02:00
fprintf(stderr, "%s\n", buf);
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_BESIDEFUNC) && (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC) )
# pragma GCC diagnostic pop
#endif
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static void do_setvar(const char *varname, char *uin, const char *pass)
2010-03-26 01:20:59 +02:00
{
2022-07-10 10:23:45 +03:00
char newval[SMALLBUF], temp[SMALLBUF * 2], user[SMALLBUF], *ptr;
2011-01-26 11:35:08 +02:00
struct passwd *pw;
2010-03-26 01:20:59 +02:00
if (uin) {
snprintf(user, sizeof(user), "%s", uin);
} else {
memset(user, '\0', sizeof(user));
pw = getpwuid(getuid());
2011-01-26 11:35:08 +02:00
if (pw) {
2010-03-26 01:20:59 +02:00
printf("Username (%s): ", pw->pw_name);
2011-01-26 11:35:08 +02:00
} else {
2010-03-26 01:20:59 +02:00
printf("Username: ");
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
if (fgets(user, sizeof(user), stdin) == NULL) {
upsdebug_with_errno(LOG_INFO, "%s", __func__);
}
/* deal with that pesky newline */
2011-01-26 11:35:08 +02:00
if (strlen(user) > 1) {
2010-03-26 01:20:59 +02:00
user[strlen(user) - 1] = '\0';
2011-01-26 11:35:08 +02:00
} else {
if (!pw) {
2010-03-26 01:20:59 +02:00
fatalx(EXIT_FAILURE, "No username available - even tried getpwuid");
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
snprintf(user, sizeof(user), "%s", pw->pw_name);
}
}
/* leaks - use -p when running in valgrind */
if (!pass) {
pass = GETPASS("Password: " );
if (!pass) {
2011-01-26 11:35:08 +02:00
fatal_with_errno(EXIT_FAILURE, "getpass failed");
2010-03-26 01:20:59 +02:00
}
}
/* Check if varname is in VAR=VALUE form */
if ((ptr = strchr(varname, '=')) != NULL) {
*ptr++ = 0;
snprintf(newval, sizeof(newval), "%s", ptr);
} else {
printf("Enter new value for %s: ", varname);
fflush(stdout);
if (fgets(newval, sizeof(newval), stdin) == NULL) {
upsdebug_with_errno(LOG_INFO, "%s", __func__);
}
newval[strlen(newval) - 1] = '\0';
}
snprintf(temp, sizeof(temp), "USERNAME %s\n", user);
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Can't set username: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Set username failed due to an unknown command. You probably need to upgrade upsd.");
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Set username failed: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
snprintf(temp, sizeof(temp), "PASSWORD %s\n", pass);
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Can't set password: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Set password failed: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
/* no upsname means die */
if (!upsname) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
2010-03-26 01:20:59 +02:00
}
/* old variable names are no longer supported */
if (!strchr(varname, '.')) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: old variable names are not supported");
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
/* enable status tracking ID */
if (tracking_enabled) {
snprintf(temp, sizeof(temp), "SET TRACKING ON\n");
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
fatalx(EXIT_FAILURE, "Can't enable set variable status tracking: %s", upscli_strerror(ups));
}
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
fatalx(EXIT_FAILURE, "Enabling set variable status tracking failed: %s", upscli_strerror(ups));
}
/* Verify the result */
if (strncmp(temp, "OK", 2) != 0) {
fatalx(EXIT_FAILURE, "Enabling set variable status tracking failed. upsd answered: %s", temp);
}
}
2011-01-26 11:35:08 +02:00
do_set(varname, newval);
}
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static const char *get_data(const char *type, const char *varname)
2010-03-26 01:20:59 +02:00
{
int ret;
2022-07-10 10:23:45 +03:00
size_t numq, numa;
2010-03-26 01:20:59 +02:00
char **answer;
2011-01-26 11:35:08 +02:00
const char *query[4];
2010-03-26 01:20:59 +02:00
query[0] = type;
query[1] = upsname;
query[2] = varname;
2011-01-26 11:35:08 +02:00
2010-03-26 01:20:59 +02:00
numq = 3;
ret = upscli_get(ups, numq, query, &numa, &answer);
2011-01-26 11:35:08 +02:00
if ((ret < 0) || (numa < numq)) {
2010-03-26 01:20:59 +02:00
return NULL;
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
/* <type> <upsname> <varname> <desc> */
return answer[3];
}
2022-07-10 10:23:45 +03:00
static void do_string(const char *varname, const long len)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
const char *val;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
val = get_data("VAR", varname);
2010-03-26 01:20:59 +02:00
if (!val) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "do_string: can't get current value of %s", varname);
2010-03-26 01:20:59 +02:00
}
printf("Type: STRING\n");
2022-07-10 10:23:45 +03:00
printf("Maximum length: %ld\n", len);
printf("Value: %s\n", val);
}
static void do_number(const char *varname)
{
const char *val;
val = get_data("VAR", varname);
if (!val) {
fatalx(EXIT_FAILURE, "do_number: can't get current value of %s", varname);
}
printf("Type: NUMBER\n");
2010-03-26 01:20:59 +02:00
printf("Value: %s\n", val);
}
2022-07-10 10:23:45 +03:00
/**
* Display ENUM information
* @param varname the name of the NUT variable
* @param vartype the type of the NUT variable (ST_FLAG_STRING, ST_FLAG_NUMBER
* @param len the length of the NUT variable, if type == ST_FLAG_STRING
*/
static void do_enum(const char *varname, const int vartype, const long len)
2010-03-26 01:20:59 +02:00
{
int ret;
2022-07-10 10:23:45 +03:00
size_t numq, numa;
2011-01-26 11:35:08 +02:00
char **answer, buf[SMALLBUF];
const char *query[4], *val;
2010-03-26 01:20:59 +02:00
/* get current value */
2011-01-26 11:35:08 +02:00
val = get_data("VAR", varname);
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
if (!val) {
fatalx(EXIT_FAILURE, "do_enum: can't get current value of %s", varname);
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
snprintf(buf, sizeof(buf), "%s", val);
2010-03-26 01:20:59 +02:00
query[0] = "ENUM";
query[1] = upsname;
query[2] = varname;
numq = 3;
ret = upscli_list_start(ups, numq, query);
if (ret < 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
ret = upscli_list_next(ups, numq, query, &numa, &answer);
2022-07-10 10:23:45 +03:00
/* Fallback for older upsd versions */
if (vartype != ST_FLAG_NONE)
printf("Type: ENUM %s\n", (vartype == ST_FLAG_STRING)?"STRING":"NUMBER");
else
printf("Type: ENUM\n");
if (vartype == ST_FLAG_STRING)
printf("Maximum length: %ld\n", len);
2010-03-26 01:20:59 +02:00
while (ret == 1) {
/* ENUM <upsname> <varname> <value> */
if (numa < 4) {
2022-07-10 10:23:45 +03:00
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
2010-03-26 01:20:59 +02:00
}
printf("Option: \"%s\"", answer[3]);
2011-01-26 11:35:08 +02:00
if (!strcmp(answer[3], buf)) {
2010-03-26 01:20:59 +02:00
printf(" SELECTED");
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
printf("\n");
ret = upscli_list_next(ups, numq, query, &numa, &answer);
}
}
2012-06-01 16:55:19 +03:00
static void do_range(const char *varname)
{
int ret;
2022-07-10 10:23:45 +03:00
size_t numq, numa;
2012-06-01 16:55:19 +03:00
char **answer;
const char *query[4], *val;
int ival, min, max;
/* get current value */
val = get_data("VAR", varname);
if (!val) {
fatalx(EXIT_FAILURE, "do_range: can't get current value of %s", varname);
}
ival = atoi(val);
query[0] = "RANGE";
query[1] = upsname;
query[2] = varname;
numq = 3;
ret = upscli_list_start(ups, numq, query);
if (ret < 0) {
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
}
ret = upscli_list_next(ups, numq, query, &numa, &answer);
2022-07-10 10:23:45 +03:00
/* Ranges implies a type "NUMBER" */
printf("Type: RANGE NUMBER\n");
2012-06-01 16:55:19 +03:00
while (ret == 1) {
/* RANGE <upsname> <varname> <min> <max> */
if (numa < 5) {
2022-07-10 10:23:45 +03:00
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
2012-06-01 16:55:19 +03:00
}
min = atoi(answer[3]);
max = atoi(answer[4]);
printf("Option: \"%i-%i\"", min, max);
if ((ival >= min) && (ival <= max)) {
printf(" SELECTED");
}
printf("\n");
ret = upscli_list_next(ups, numq, query, &numa, &answer);
}
}
2011-01-26 11:35:08 +02:00
static void do_type(const char *varname)
2010-03-26 01:20:59 +02:00
{
int ret;
2022-07-10 10:23:45 +03:00
int is_enum = 0; /* 1 if ENUM; FIXME: add a boolean type in common.h */
size_t i, numq, numa;
2010-03-26 01:20:59 +02:00
char **answer;
2011-01-26 11:35:08 +02:00
const char *query[4];
2010-03-26 01:20:59 +02:00
query[0] = "TYPE";
query[1] = upsname;
query[2] = varname;
numq = 3;
ret = upscli_get(ups, numq, query, &numa, &answer);
if ((ret < 0) || (numa < numq)) {
2022-07-10 10:23:45 +03:00
printf("Unknown type\n");
2010-03-26 01:20:59 +02:00
return;
}
/* TYPE <upsname> <varname> <type>... */
for (i = 3; i < numa; i++) {
2022-07-10 10:23:45 +03:00
/* ENUM can be NUMBER or STRING
* just flag it for latter processing */
2010-03-26 01:20:59 +02:00
if (!strcasecmp(answer[i], "ENUM")) {
2022-07-10 10:23:45 +03:00
is_enum = 1;
continue;
2010-03-26 01:20:59 +02:00
}
2012-06-01 16:55:19 +03:00
if (!strcasecmp(answer[i], "RANGE")) {
do_range(varname);
return;
}
2010-03-26 01:20:59 +02:00
if (!strncasecmp(answer[i], "STRING:", 7)) {
2013-11-24 17:00:12 +02:00
char *len = answer[i] + 7;
2022-07-10 10:23:45 +03:00
long length = strtol(len, NULL, 10);
2013-11-24 17:00:12 +02:00
2022-07-10 10:23:45 +03:00
if (is_enum == 1)
do_enum(varname, ST_FLAG_STRING, length);
else
do_string(varname, length);
2010-03-26 01:20:59 +02:00
return;
2013-11-24 17:00:12 +02:00
2010-03-26 01:20:59 +02:00
}
2016-07-18 03:11:41 +03:00
if (!strcasecmp(answer[i], "NUMBER")) {
2022-07-10 10:23:45 +03:00
if (is_enum == 1)
do_enum(varname, ST_FLAG_NUMBER, 0);
else
do_number(varname);
2016-07-18 03:11:41 +03:00
return;
}
2010-03-26 01:20:59 +02:00
/* ignore this one */
2011-01-26 11:35:08 +02:00
if (!strcasecmp(answer[i], "RW")) {
2010-03-26 01:20:59 +02:00
continue;
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
printf("Type: %s (unrecognized)\n", answer[i]);
}
2022-07-10 10:23:45 +03:00
/* Fallback for older upsd versions, where STRING|NUMBER is not
* appended to ENUM */
if (is_enum == 1)
do_enum(varname, ST_FLAG_NONE, 0);
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
static void print_rw(const char *varname)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
const char *tmp;
2010-03-26 01:20:59 +02:00
printf("[%s]\n", varname);
2011-01-26 11:35:08 +02:00
tmp = get_data("DESC", varname);
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
if (tmp) {
2010-03-26 01:20:59 +02:00
printf("%s\n", tmp);
2011-01-26 11:35:08 +02:00
} else {
2010-03-26 01:20:59 +02:00
printf("Description unavailable\n");
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
do_type(varname);
2010-03-26 01:20:59 +02:00
printf("\n");
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static void print_rwlist(void)
2010-03-26 01:20:59 +02:00
{
int ret;
2022-07-10 10:23:45 +03:00
size_t numq, numa;
2011-01-26 11:35:08 +02:00
const char *query[2];
2010-03-26 01:20:59 +02:00
char **answer;
struct list_t *lhead, *llast, *ltmp, *lnext;
/* the upsname is now required */
if (!upsname) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: a UPS name must be specified (upsname[@hostname[:port]])");
2010-03-26 01:20:59 +02:00
}
llast = lhead = NULL;
query[0] = "RW";
query[1] = upsname;
numq = 2;
ret = upscli_list_start(ups, numq, query);
if (ret < 0) {
/* old upsd --> fall back on old LISTRW technique */
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: upsd is too old to support this query");
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
ret = upscli_list_next(ups, numq, query, &numa, &answer);
while (ret == 1) {
/* RW <upsname> <varname> <value> */
if (numa < 4) {
2022-07-10 10:23:45 +03:00
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
2010-03-26 01:20:59 +02:00
}
/* sock this entry away for later */
ltmp = xmalloc(sizeof(struct list_t));
ltmp->name = xstrdup(answer[2]);
ltmp->next = NULL;
2011-01-26 11:35:08 +02:00
if (llast) {
2010-03-26 01:20:59 +02:00
llast->next = ltmp;
2011-01-26 11:35:08 +02:00
} else {
2010-03-26 01:20:59 +02:00
lhead = ltmp;
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
llast = ltmp;
ret = upscli_list_next(ups, numq, query, &numa, &answer);
}
/* use the list to get descriptions and types */
ltmp = lhead;
while (ltmp) {
lnext = ltmp->next;
2011-01-26 11:35:08 +02:00
print_rw(ltmp->name);
2010-03-26 01:20:59 +02:00
free(ltmp->name);
free(ltmp);
ltmp = lnext;
}
}
int main(int argc, char **argv)
{
2022-07-10 10:23:45 +03:00
int i;
uint16_t port;
2011-01-26 11:35:08 +02:00
const char *prog = xbasename(argv[0]);
char *password = NULL, *username = NULL, *setvar = NULL;
2010-03-26 01:20:59 +02:00
2022-07-10 10:23:45 +03:00
while ((i = getopt(argc, argv, "+hls:p:t:u:wV")) != -1) {
2011-01-26 11:35:08 +02:00
switch (i)
{
2010-03-26 01:20:59 +02:00
case 's':
setvar = optarg;
break;
2022-07-10 10:23:45 +03:00
case 'l':
if (setvar) {
upslogx(LOG_WARNING, "Listing mode requested, overriding setvar specified earlier!");
setvar = NULL;
}
break;
2010-03-26 01:20:59 +02:00
case 'p':
password = optarg;
break;
2022-07-10 10:23:45 +03:00
case 't':
if (!str_to_uint(optarg, &timeout, 10))
fatal_with_errno(EXIT_FAILURE, "Could not convert the provided value for timeout ('-t' option) to unsigned int");
break;
2010-03-26 01:20:59 +02:00
case 'u':
username = optarg;
break;
2022-07-10 10:23:45 +03:00
case 'w':
tracking_enabled = 1;
break;
2010-03-26 01:20:59 +02:00
case 'V':
2011-01-26 11:35:08 +02:00
printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);
2010-03-26 01:20:59 +02:00
exit(EXIT_SUCCESS);
2011-06-01 23:31:49 +03:00
case 'h':
2010-03-26 01:20:59 +02:00
default:
usage(prog);
2011-01-26 11:35:08 +02:00
exit(EXIT_SUCCESS);
2010-03-26 01:20:59 +02:00
}
}
argc -= optind;
argv += optind;
2011-01-26 11:35:08 +02:00
if (argc < 1) {
2010-03-26 01:20:59 +02:00
usage(prog);
2011-01-26 11:35:08 +02:00
exit(EXIT_SUCCESS);
}
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
/* be a good little client that cleans up after itself */
atexit(clean_exit);
2010-03-26 01:20:59 +02:00
if (upscli_splitname(argv[0], &upsname, &hostname, &port) != 0) {
2011-01-26 11:35:08 +02:00
fatalx(EXIT_FAILURE, "Error: invalid UPS definition. Required format: upsname[@hostname[:port]]");
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
ups = xcalloc(1, sizeof(*ups));
if (upscli_connect(ups, hostname, port, 0) < 0) {
fatalx(EXIT_FAILURE, "Error: %s", upscli_strerror(ups));
2010-03-26 01:20:59 +02:00
}
if (setvar) {
2011-01-26 11:35:08 +02:00
/* setting a variable */
do_setvar(setvar, username, password);
} else {
/* if not, get the list of supported read/write variables */
print_rwlist();
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
exit(EXIT_SUCCESS);
2010-03-26 01:20:59 +02:00
}
2013-11-24 17:00:12 +02:00
/* Formal do_upsconf_args implementation to satisfy linker on AIX */
#if (defined NUT_PLATFORM_AIX)
void do_upsconf_args(char *upsname, char *var, char *val) {
fatalx(EXIT_FAILURE, "INTERNAL ERROR: formal do_upsconf_args called");
}
#endif /* end of #if (defined NUT_PLATFORM_AIX) */