37
37
printf(" <variable> - optional, display this variable only.\n");
38
38
printf(" Default: list all variables for <host>\n");
43
43
static void clean_exit(UPSCONN *ups, char *upsname, char *hostname, int code)
56
static int old_printvar(UPSCONN *ups, const char *upsname, const char *var)
60
if (upscli_getvar(ups, upsname, var, value, sizeof(value)) < 0) {
61
fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
65
printf("%s\n", value);
70
static int new_printvar(UPSCONN *ups, const char *upsname, const char *var)
56
static int printvar(UPSCONN *ups, const char *upsname, const char *var)
59
unsigned int numq, numa;
73
60
const char *query[4];
76
63
/* old-style variable name? */
77
if (!strchr(var, '.'))
78
return old_printvar(ups, upsname, var);
64
if (!strchr(var, '.')) {
65
fprintf(stderr, "Error: old-style variable names are not supported\n");
81
70
fprintf(stderr, "Error: ups name must be defined\n");
95
84
/* new var and old upsd? try to explain the situation */
96
85
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
97
86
fprintf(stderr, "Error: variable unknown (old upsd detected)\n");
101
90
fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
105
94
if (numa < numq) {
106
95
fprintf(stderr, "Error: insufficient data "
107
96
"(got %d args, need at least %d)\n", numa, numq);
111
100
printf("%s\n", answer[3]);
115
static int old_list(UPSCONN *ups, const char *upsname)
117
char vars[LARGEBUF], *v, *ptr;
119
if (upscli_getlist(ups, upsname, UPSCLI_LIST_VARS, vars,
121
fprintf(stderr, "Unable to get variable list - %s\n",
122
upscli_strerror(ups));
128
printf("UPS: %s\n", upsname);
130
printf("UPS: (default)\n");
132
if (strlen(vars) == 0) {
133
fprintf(stderr, "No data available - check your "
134
"configuration (ups.conf)\n");
141
ptr = strchr(v, ' ');
146
old_printvar(ups, upsname, v);
154
static int new_list(UPSCONN *ups, const char *upsname)
104
static int list_vars(UPSCONN *ups, const char *upsname)
107
unsigned int numq, numa;
157
108
const char *query[4];
161
return old_list(ups, upsname);
112
fprintf(stderr, "Error: a UPS name must be specified (upsname@hostname)\n");
163
116
query[0] = "VAR";
164
117
query[1] = upsname;
171
/* old upsd --> fall back on old LISTVARS technique */
172
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND)
173
return old_list(ups, upsname);
124
/* check for an old upsd */
125
if (upscli_upserror(ups) == UPSCLI_ERR_UNKCOMMAND) {
126
fprintf(stderr, "Error: upsd is too old to support this query\n");
175
130
fprintf(stderr, "Error: %s\n", upscli_strerror(ups));
179
134
ret = upscli_list_next(ups, numq, query, &numa, &answer);
185
140
fprintf(stderr, "Error: insufficient data "
186
141
"(got %d args, need at least 4)\n", numa);
190
145
printf("%s: %s\n", answer[2], answer[3]);
192
147
ret = upscli_list_next(ups, numq, query, &numa, &answer);
153
static void check_upsdef(const char *ups)
157
ptr = strchr(ups, '@');
162
fprintf(stderr, "Error: invalid UPS definition. Required format: upsname@hostname[:port]\n");
198
167
int main(int argc, char **argv)
207
176
/* special cases since we're not using getopt */
208
177
if (!strcmp(argv[1], "-V")) {
209
178
printf("Network UPS Tools upsc %s\n", UPS_VERSION);
213
182
if (!strcmp(argv[1], "-h"))
216
upscli_splitname(argv[1], &upsname, &hostname, &port);
185
check_upsdef(argv[1]);
187
upsname = hostname = NULL;
189
if (upscli_splitname(argv[1], &upsname, &hostname, &port) != 0)
190
clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
218
192
if (upscli_connect(&ups, hostname, port, UPSCLI_CONN_TRYSSL) < 0) {
219
193
fprintf(stderr, "Error: %s\n", upscli_strerror(&ups));
221
clean_exit(&ups, upsname, hostname, 1);
195
clean_exit(&ups, upsname, hostname, EXIT_FAILURE);
225
ret = new_printvar(&ups, upsname, argv[2]);
199
ret = printvar(&ups, upsname, argv[2]);
226
200
clean_exit(&ups, upsname, hostname, ret);
229
ret = new_list(&ups, upsname);
203
ret = list_vars(&ups, upsname);
230
204
clean_exit(&ups, upsname, hostname, ret);