Added
Link Here
|
1 |
--- clients/upslog.c.orig 2022-08-29 22:20:20.954722000 -0700 |
2 |
+++ clients/upslog.c 2022-08-29 22:21:18.844395000 -0700 |
3 |
@@ -32,6 +32,10 @@ |
4 |
*/ |
5 |
|
6 |
#include "common.h" |
7 |
+#include <signal.h> |
8 |
+#include <unistd.h> |
9 |
+#include <sys/types.h> |
10 |
+#include <sys/wait.h> |
11 |
#include "nut_platform.h" |
12 |
#include "upsclient.h" |
13 |
|
14 |
@@ -41,32 +45,49 @@ |
15 |
#include "upslog.h" |
16 |
|
17 |
static int reopen_flag = 0, exit_flag = 0; |
18 |
- static uint16_t port; |
19 |
- static char *upsname, *hostname; |
20 |
- static UPSCONN_t ups; |
21 |
+ static char *upsname; |
22 |
+ static UPSCONN_t *ups; |
23 |
|
24 |
- static FILE *logfile; |
25 |
- static const char *logfn, *monhost; |
26 |
+ static char *logfn, *monhost; |
27 |
static sigset_t nut_upslog_sigmask; |
28 |
static char logbuffer[LARGEBUF], *logformat; |
29 |
|
30 |
static flist_t *fhead = NULL; |
31 |
+ struct monhost_ups { |
32 |
+ char *monhost; |
33 |
+ char *logfn; |
34 |
+ char *upsname; |
35 |
+ char *hostname; |
36 |
+ uint16_t port; |
37 |
+ UPSCONN_t *ups; |
38 |
+ FILE *logfile; |
39 |
+ struct monhost_ups *next; |
40 |
+ }; |
41 |
+ static struct monhost_ups *monhost_ups_anchor = NULL; |
42 |
+ static struct monhost_ups *monhost_ups_current = NULL; |
43 |
+ static struct monhost_ups *monhost_ups_prev = NULL; |
44 |
|
45 |
+ |
46 |
#define DEFAULT_LOGFORMAT "%TIME @Y@m@d @H@M@S% %VAR battery.charge% " \ |
47 |
"%VAR input.voltage% %VAR ups.load% [%VAR ups.status%] " \ |
48 |
"%VAR ups.temperature% %VAR input.frequency%" |
49 |
|
50 |
static void reopen_log(void) |
51 |
{ |
52 |
- if (logfile == stdout) { |
53 |
- upslogx(LOG_INFO, "logging to stdout"); |
54 |
- return; |
55 |
- } |
56 |
+ for (monhost_ups_current = monhost_ups_anchor; |
57 |
+ monhost_ups_current != NULL; |
58 |
+ monhost_ups_current = monhost_ups_current->next) { |
59 |
+ if (monhost_ups_current->logfile == stdout) { |
60 |
+ upslogx(LOG_INFO, "logging to stdout"); |
61 |
+ return; |
62 |
+ } |
63 |
|
64 |
- fclose(logfile); |
65 |
- logfile = fopen(logfn, "a"); |
66 |
- if (logfile == NULL) |
67 |
- fatal_with_errno(EXIT_FAILURE, "could not reopen logfile %s", logfn); |
68 |
+ if ((monhost_ups_current->logfile = freopen( |
69 |
+ monhost_ups_current->logfn, "a", |
70 |
+ monhost_ups_current->logfile)) == NULL) |
71 |
+ fatal_with_errno(EXIT_FAILURE, |
72 |
+ "could not reopen logfile %s", logfn); |
73 |
+ } |
74 |
} |
75 |
|
76 |
static void set_reopen_flag(int sig) |
77 |
@@ -131,6 +152,8 @@ |
78 |
printf(" -p <pidbase> - Base name for PID file (defaults to \"%s\")\n", prog); |
79 |
printf(" -s <ups> - Monitor UPS <ups> - <upsname>@<host>[:<port>]\n"); |
80 |
printf(" - Example: -s myups@server\n"); |
81 |
+ printf(" -m <tuple> - Monitor UPS <ups,logfile>\n"); |
82 |
+ printf(" - Example: -m myups@server,/var/log/myups.log\n"); |
83 |
printf(" -u <user> - Switch to <user> if started as root\n"); |
84 |
|
85 |
printf("\n"); |
86 |
@@ -215,7 +238,7 @@ |
87 |
query[2] = var; |
88 |
numq = 3; |
89 |
|
90 |
- ret = upscli_get(&ups, numq, query, &numa, &answer); |
91 |
+ ret = upscli_get(ups, numq, query, &numa, &answer); |
92 |
|
93 |
if ((ret < 0) || (numa < numq)) { |
94 |
snprintfcat(logbuffer, sizeof(logbuffer), "NA"); |
95 |
@@ -368,7 +391,7 @@ |
96 |
} |
97 |
|
98 |
/* go through the list of functions and call them in order */ |
99 |
-static void run_flist(void) |
100 |
+static void run_flist(struct monhost_ups *monhost_ups_print) |
101 |
{ |
102 |
flist_t *tmp; |
103 |
|
104 |
@@ -382,8 +405,8 @@ |
105 |
tmp = tmp->next; |
106 |
} |
107 |
|
108 |
- fprintf(logfile, "%s\n", logbuffer); |
109 |
- fflush(logfile); |
110 |
+ fprintf(monhost_ups_print->logfile, "%s\n", logbuffer); |
111 |
+ fflush(monhost_ups_print->logfile); |
112 |
} |
113 |
|
114 |
/* -s <monhost> |
115 |
@@ -396,6 +419,7 @@ |
116 |
int main(int argc, char **argv) |
117 |
{ |
118 |
int interval = 30, i, foreground = -1; |
119 |
+ size_t monhost_len = 0; |
120 |
const char *prog = xbasename(argv[0]); |
121 |
time_t now, nextpoll = 0; |
122 |
const char *user = NULL; |
123 |
@@ -407,7 +431,7 @@ |
124 |
|
125 |
printf("Network UPS Tools %s %s\n", prog, UPS_VERSION); |
126 |
|
127 |
- while ((i = getopt(argc, argv, "+hs:l:i:f:u:Vp:FB")) != -1) { |
128 |
+ while ((i = getopt(argc, argv, "+hs:l:i:f:u:Vp:FBm:")) != -1) { |
129 |
switch(i) { |
130 |
case 'h': |
131 |
help(prog); |
132 |
@@ -415,6 +439,33 @@ |
133 |
break; |
134 |
#endif |
135 |
|
136 |
+ case 'm': { /* var scope */ |
137 |
+ char *m_arg, *s; |
138 |
+ |
139 |
+ monhost_ups_prev = monhost_ups_current; |
140 |
+ monhost_ups_current = xmalloc(sizeof(struct monhost_ups)); |
141 |
+ if (monhost_ups_anchor == NULL) |
142 |
+ monhost_ups_anchor = monhost_ups_current; |
143 |
+ else |
144 |
+ monhost_ups_prev->next = monhost_ups_current; |
145 |
+ monhost_ups_current->next = NULL; |
146 |
+ monhost_len++; |
147 |
+ |
148 |
+ /* Be sure to not mangle original optarg, nor rely on its longevity */ |
149 |
+ s = xstrdup(optarg); |
150 |
+ m_arg = s; |
151 |
+ monhost_ups_current->monhost = xstrdup(strsep(&m_arg, ",")); |
152 |
+ if (!m_arg) |
153 |
+ fatalx(EXIT_FAILURE, "Argument '-m upsspec,logfile' requires exactly 2 components in the tuple"); |
154 |
+ monhost_ups_current->logfn = xstrdup(strsep(&m_arg, ",")); |
155 |
+ if (m_arg) /* Had a third comma - also unexpected! */ |
156 |
+ fatalx(EXIT_FAILURE, "Argument '-m upsspec,logfile' requires exactly 2 components in the tuple"); |
157 |
+ if (upscli_splitname(monhost_ups_current->monhost, &(monhost_ups_current->upsname), &(monhost_ups_current->hostname), &(monhost_ups_current->port)) != 0) { |
158 |
+ fatalx(EXIT_FAILURE, "Error: invalid UPS definition. Required format: upsname[@hostname[:port]]\n"); |
159 |
+ } |
160 |
+ free(s); |
161 |
+ } /* var scope */ |
162 |
+ break; |
163 |
case 's': |
164 |
monhost = optarg; |
165 |
break; |
166 |
@@ -479,42 +530,59 @@ |
167 |
snprintfcat(logformat, LARGEBUF, "%s ", argv[i]); |
168 |
} |
169 |
|
170 |
- if (!monhost) |
171 |
- fatalx(EXIT_FAILURE, "No UPS defined for monitoring - use -s <system>"); |
172 |
+ if (monhost_ups_anchor == NULL) { |
173 |
+ if (monhost) { |
174 |
+ monhost_ups_current = xmalloc(sizeof(struct monhost_ups)); |
175 |
+ monhost_ups_anchor = monhost_ups_current; |
176 |
+ monhost_ups_current->next = NULL; |
177 |
+ monhost_ups_current->monhost = monhost; |
178 |
+ monhost_len=1; |
179 |
+ } else { |
180 |
+ fatalx(EXIT_FAILURE, "No UPS defined for monitoring - use -s <system>"); |
181 |
+ } |
182 |
|
183 |
- if (!logfn) |
184 |
- fatalx(EXIT_FAILURE, "No filename defined for logging - use -l <file>"); |
185 |
+ if (logfn) |
186 |
+ monhost_ups_current->logfn = logfn; |
187 |
+ else |
188 |
+ fatalx(EXIT_FAILURE, "No filename defined for logging - use -l <file>"); |
189 |
+ } |
190 |
|
191 |
/* shouldn't happen */ |
192 |
if (!logformat) |
193 |
fatalx(EXIT_FAILURE, "No format defined - but this should be impossible"); |
194 |
|
195 |
- printf("logging status of %s to %s (%is intervals)\n", |
196 |
- monhost, logfn, interval); |
197 |
+ for (monhost_ups_current = monhost_ups_anchor; |
198 |
+ monhost_ups_current != NULL; |
199 |
+ monhost_ups_current = monhost_ups_current->next) { |
200 |
+ printf("logging status of %s to %s (%is intervals)\n", |
201 |
+ monhost_ups_current->monhost, monhost_ups_current->logfn, interval); |
202 |
+ if (upscli_splitname(monhost_ups_current->monhost, &(monhost_ups_current->upsname), &(monhost_ups_current->hostname), &(monhost_ups_current->port)) != 0) { |
203 |
+ fatalx(EXIT_FAILURE, "Error: invalid UPS definition. Required format: upsname[@hostname[:port]]\n"); |
204 |
+ } |
205 |
|
206 |
- if (upscli_splitname(monhost, &upsname, &hostname, &port) != 0) { |
207 |
- fatalx(EXIT_FAILURE, "Error: invalid UPS definition. Required format: upsname[@hostname[:port]]\n"); |
208 |
- } |
209 |
+ monhost_ups_current->ups = xmalloc(sizeof(UPSCONN_t)); |
210 |
+ if (upscli_connect(monhost_ups_current->ups, monhost_ups_current->hostname, monhost_ups_current->port, UPSCLI_CONN_TRYSSL) < 0) |
211 |
+ fprintf(stderr, "Warning: initial connect failed: %s\n", |
212 |
+ upscli_strerror(monhost_ups_current->ups)); |
213 |
|
214 |
- if (upscli_connect(&ups, hostname, port, UPSCLI_CONN_TRYSSL) < 0) |
215 |
- fprintf(stderr, "Warning: initial connect failed: %s\n", |
216 |
- upscli_strerror(&ups)); |
217 |
+ if (strcmp(monhost_ups_current->logfn, "-") == 0) |
218 |
+ monhost_ups_current->logfile = stdout; |
219 |
+ else |
220 |
+ monhost_ups_current->logfile = fopen(monhost_ups_current->logfn, "a"); |
221 |
|
222 |
- if (strcmp(logfn, "-") == 0) |
223 |
- logfile = stdout; |
224 |
- else |
225 |
- logfile = fopen(logfn, "a"); |
226 |
+ if (monhost_ups_current->logfile == NULL) |
227 |
+ fatal_with_errno(EXIT_FAILURE, "could not open logfile %s", logfn); |
228 |
|
229 |
- if (logfile == NULL) |
230 |
- fatal_with_errno(EXIT_FAILURE, "could not open logfile %s", logfn); |
231 |
+ } |
232 |
|
233 |
+ |
234 |
/* now drop root if we have it */ |
235 |
new_uid = get_user_pwent(user); |
236 |
|
237 |
open_syslog(prog); |
238 |
|
239 |
if (foreground < 0) { |
240 |
- if (logfile == stdout) { |
241 |
+ if (monhost_ups_anchor->logfile == stdout) { |
242 |
foreground = 1; |
243 |
} else { |
244 |
foreground = 0; |
245 |
@@ -552,25 +620,35 @@ |
246 |
reopen_flag = 0; |
247 |
} |
248 |
|
249 |
- /* reconnect if necessary */ |
250 |
- if (upscli_fd(&ups) < 0) { |
251 |
- upscli_connect(&ups, hostname, port, 0); |
252 |
- } |
253 |
+ for (monhost_ups_current = monhost_ups_anchor; |
254 |
+ monhost_ups_current != NULL; |
255 |
+ monhost_ups_current = monhost_ups_current->next) { |
256 |
+ ups = monhost_ups_current->ups; /* XXX Not ideal */ |
257 |
+ upsname = monhost_ups_current->upsname; /* XXX Not ideal */ |
258 |
+ /* reconnect if necessary */ |
259 |
+ if (upscli_fd(ups) < 0) { |
260 |
+ upscli_connect(ups, monhost_ups_current->hostname, monhost_ups_current->port, 0); |
261 |
+ } |
262 |
|
263 |
- run_flist(); |
264 |
+ run_flist(monhost_ups_current); |
265 |
|
266 |
- /* don't keep connection open if we don't intend to use it shortly */ |
267 |
- if (interval > 30) { |
268 |
- upscli_disconnect(&ups); |
269 |
+ /* don't keep connection open if we don't intend to use it shortly */ |
270 |
+ if (interval > 30) { |
271 |
+ upscli_disconnect(ups); |
272 |
+ } |
273 |
} |
274 |
} |
275 |
|
276 |
upslogx(LOG_INFO, "Signal %d: exiting", exit_flag); |
277 |
+ for (monhost_ups_current = monhost_ups_anchor; |
278 |
+ monhost_ups_current != NULL; |
279 |
+ monhost_ups_current = monhost_ups_current->next) { |
280 |
|
281 |
- if (logfile != stdout) |
282 |
- fclose(logfile); |
283 |
+ if (monhost_ups_current->logfile != stdout) |
284 |
+ fclose(monhost_ups_current->logfile); |
285 |
|
286 |
- upscli_disconnect(&ups); |
287 |
+ upscli_disconnect(monhost_ups_current->ups); |
288 |
+ } |
289 |
|
290 |
exit(EXIT_SUCCESS); |
291 |
} |