root/src/modules/external.c

Revision 876ca61caf4e64c6bfab8b212d12e198d7c2ed11, 18.6 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

work on Linux, refs #101

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <netdb.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <assert.h>
14 #include <sys/ioctl.h>
15 #include <sys/uio.h>
16 #ifdef HAVE_SYS_WAIT_H
17 #include <sys/wait.h>
18 #endif
19 #ifdef HAVE_SYS_FILIO_H
20 #include <sys/filio.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_SYSTM_H
23 #include <netinet/in_systm.h>
24 #endif
25 #include <pcre.h>
26
27 #include "noit_module.h"
28 #include "noit_check.h"
29 #include "noit_check_tools.h"
30 #include "utils/noit_log.h"
31 #include "utils/noit_security.h"
32 #include "external_proc.h"
33
34 struct check_info {
35   int64_t check_no;
36   u_int16_t argcnt;
37   u_int16_t *arglens;
38   char **args;
39   u_int16_t envcnt;
40   u_int16_t *envlens;
41   char **envs;
42   noit_check_t *check;
43   int exit_code;
44
45   int timedout;
46   char *output;
47   char *error;
48   pcre *matcher;
49   eventer_t timeout_event;
50 };
51
52 typedef struct external_closure {
53   noit_module_t *self;
54   noit_check_t *check;
55 } external_closure_t;
56
57 /* Protocol:
58  *   noit 2 ext:
59  *     int64(check_no)
60  *     uint16(0) -> cancel .end
61
62  *     int64(check_no)
63  *     uint16(argcnt) [argcnt > 0]
64  *     uint16(arglen) x argcnt  (arglen includes \0)
65  *     string of sum(arglen)
66  *     uint16(envcnt)
67  *     uint16(envlen) x envcnt  (envlen includes \0)
68  *     string of sum(envlen) -> execve .end
69  *
70  *   ext 2 noit:
71  *     int64(check_no)
72  *     int32(exitcode) [0 -> good, {1,2} -> bad, 3 -> unknown]
73  *     uint16(outlen) (includes \0)
74  *     string of outlen
75  *     uint16(errlen) (includes \0)
76  *     string of errlen -> complete .end
77  */
78
79 static int external_config(noit_module_t *self, noit_hash_table *options) {
80   external_data_t *data;
81   data = noit_module_get_userdata(self);
82   if(data) {
83     if(data->options) {
84       noit_hash_destroy(data->options, free, free);
85       free(data->options);
86     }
87   }
88   else
89     data = calloc(1, sizeof(*data));
90   data->options = options;
91   if(!data->options) data->options = calloc(1, sizeof(*data->options));
92   noit_module_set_userdata(self, data);
93   return 1;
94 }
95
96 static void external_log_results(noit_module_t *self, noit_check_t *check) {
97   external_data_t *data;
98   struct check_info *ci;
99   stats_t current;
100   struct timeval duration;
101
102   noit_check_stats_clear(&current);
103
104   data = noit_module_get_userdata(self);
105   ci = (struct check_info *)check->closure;
106
107   noitL(data->nldeb, "external(%s) (timeout: %d, exit: %x)\n",
108         check->target, ci->timedout, ci->exit_code);
109
110   gettimeofday(&current.whence, NULL);
111   sub_timeval(current.whence, check->last_fire_time, &duration);
112   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
113   if(ci->timedout) {
114     current.available = NP_UNAVAILABLE;
115     current.state = NP_BAD;
116   }
117   else if(WEXITSTATUS(ci->exit_code) == 3) {
118     current.available = NP_UNKNOWN;
119     current.state = NP_UNKNOWN;
120   }
121   else {
122     current.available = NP_AVAILABLE;
123     current.state = (WEXITSTATUS(ci->exit_code) == 0) ? NP_GOOD : NP_BAD;
124   }
125
126   /* Hack the output into metrics */
127   if(ci->output && ci->matcher) {
128     int rc, len, startoffset = 0;
129     int ovector[30];
130     len = strlen(ci->output);
131     noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len);
132     while((rc = pcre_exec(ci->matcher, NULL, ci->output, len, startoffset, 0,
133                           ovector, sizeof(ovector)/sizeof(*ovector))) > 0) {
134       char metric[128];
135       char value[128];
136       startoffset = ovector[1];
137       noitL(data->nldeb, "matched at offset %d\n", rc);
138       if(pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc,
139                                    "key", metric, sizeof(metric)) > 0 &&
140          pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc,
141                                    "value", value, sizeof(value)) > 0) {
142         /* We're able to extract something... */
143         noit_stats_set_metric(&current, metric, METRIC_GUESS, value);
144       }
145       noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len);
146     }
147     noitL(data->nldeb, "match failed.... %d\n", rc);
148   }
149
150   current.status = ci->output;
151   noit_check_set_stats(self, check, &current);
152
153   /* If we didn't exit normally, or we core, or we have stderr to report...
154    * provide a full report.
155    */
156   if((WTERMSIG(ci->exit_code) != SIGQUIT && WTERMSIG(ci->exit_code) != 0) ||
157      WCOREDUMP(ci->exit_code) ||
158      (ci->error && *ci->error)) {
159     char uuid_str[37];
160     uuid_unparse_lower(check->checkid, uuid_str);
161     noitL(data->nlerr, "external/%s: (sig:%d%s) [%s]\n", uuid_str,
162           WTERMSIG(ci->exit_code), WCOREDUMP(ci->exit_code)?", cored":"",
163           ci->error ? ci->error : "");
164   }
165 }
166 static int external_timeout(eventer_t e, int mask,
167                             void *closure, struct timeval *now) {
168   external_closure_t *ecl = (external_closure_t *)closure;
169   struct check_info *data;
170   if(!NOIT_CHECK_KILLED(ecl->check) && !NOIT_CHECK_DISABLED(ecl->check)) {
171     data = (struct check_info *)ecl->check->closure;
172     data->timedout = 1;
173     data->exit_code = 3;
174     external_log_results(ecl->self, ecl->check);
175     data->timeout_event = NULL;
176   }
177   ecl->check->flags &= ~NP_RUNNING;
178   free(ecl);
179   return 0;
180 }
181 static void check_info_clean(struct check_info *ci) {
182   int i;
183   for(i=0; i<ci->argcnt; i++)
184     if(ci->args[i]) free(ci->args[i]);
185   if(ci->arglens) free(ci->arglens);
186   if(ci->args) free(ci->args);
187   for(i=0; i<ci->envcnt; i++)
188     if(ci->envs[i]) free(ci->envs[i]);
189   if(ci->envlens) free(ci->envlens);
190   if(ci->envs) free(ci->envs);
191   if(ci->matcher) pcre_free(ci->matcher);
192   memset(ci, 0, sizeof(*ci));
193 }
194 static int external_handler(eventer_t e, int mask,
195                             void *closure, struct timeval *now) {
196   noit_module_t *self = (noit_module_t *)closure;
197   external_data_t *data;
198
199   data = noit_module_get_userdata(self);
200   while(1) {
201     int inlen, expectlen;
202     noit_check_t *check;
203     struct check_info *ci;
204
205     if(!data->cr) {
206       struct external_response r;
207       struct msghdr msg;
208       struct iovec v[3];
209       memset(&r, 0, sizeof(r));
210       v[0].iov_base = &r.check_no;
211       v[0].iov_len = sizeof(r.check_no);
212       v[1].iov_base = &r.exit_code;
213       v[1].iov_len = sizeof(r.exit_code);
214       v[2].iov_base = &r.stdoutlen;
215       v[2].iov_len = sizeof(r.stdoutlen);
216       expectlen = v[0].iov_len + v[1].iov_len + v[2].iov_len;
217
218       /* Make this into a recv'ble message so we can PEEK */
219       memset(&msg, 0, sizeof(msg));
220       msg.msg_iov = v;
221       msg.msg_iovlen = 3;
222       inlen = recvmsg(e->fd, &msg, MSG_PEEK);
223       if(inlen == 0) goto widowed;
224       if((inlen == -1 && errno == EAGAIN) ||
225          (inlen > 0 && inlen < expectlen))
226         return EVENTER_READ | EVENTER_EXCEPTION;
227       if(inlen == -1)
228         noitL(noit_error, "recvmsg() failed: %s\n", strerror(errno));
229       assert(inlen == expectlen);
230       while(-1 == (inlen = recvmsg(e->fd, &msg, 0)) && errno == EINTR);
231       assert(inlen == expectlen);
232       data->cr = calloc(sizeof(*data->cr), 1);
233       memcpy(data->cr, &r, sizeof(r));
234       data->cr->stdoutbuff = malloc(data->cr->stdoutlen);
235     }
236     if(data->cr) {
237       while(data->cr->stdoutlen_sofar < data->cr->stdoutlen) {
238         while((inlen =
239                  read(e->fd,
240                       data->cr->stdoutbuff + data->cr->stdoutlen_sofar,
241                       data->cr->stdoutlen - data->cr->stdoutlen_sofar)) == -1 &&
242                errno == EINTR);
243         if(inlen == -1 && errno == EAGAIN)
244           return EVENTER_READ | EVENTER_EXCEPTION;
245         if(inlen == 0) goto widowed;
246         data->cr->stdoutlen_sofar += inlen;
247       }
248       assert(data->cr->stdoutbuff[data->cr->stdoutlen-1] == '\0');
249       if(!data->cr->stderrbuff) {
250         while((inlen = read(e->fd, &data->cr->stderrlen,
251                             sizeof(data->cr->stderrlen))) == -1 &&
252               errno == EINTR);
253         if(inlen == -1 && errno == EAGAIN)
254           return EVENTER_READ | EVENTER_EXCEPTION;
255         if(inlen == 0) goto widowed;
256         assert(inlen == sizeof(data->cr->stderrlen));
257         data->cr->stderrbuff = malloc(data->cr->stderrlen);
258       }
259       while(data->cr->stderrlen_sofar < data->cr->stderrlen) {
260         while((inlen =
261                  read(e->fd,
262                       data->cr->stderrbuff + data->cr->stderrlen_sofar,
263                       data->cr->stderrlen - data->cr->stderrlen_sofar)) == -1 &&
264                errno == EINTR);
265         if(inlen == -1 && errno == EAGAIN)
266           return EVENTER_READ | EVENTER_EXCEPTION;
267         if(inlen == 0) goto widowed;
268         data->cr->stderrlen_sofar += inlen;
269       }
270       assert(data->cr->stderrbuff[data->cr->stderrlen-1] == '\0');
271     }
272     assert(data->cr && data->cr->stdoutbuff && data->cr->stderrbuff);
273
274     gettimeofday(now, NULL); /* set it, as we care about accuracy */
275
276     /* Lookup data in check_no hash */
277     if(noit_hash_retrieve(&data->external_checks,
278                           (const char *)&data->cr->check_no,
279                           sizeof(data->cr->check_no),
280                           (void **)&ci) == 0)
281       ci = NULL;
282
283     /* We've seen it, it ain't coming again...
284      * remove it, we'll free it ourselves */
285     noit_hash_delete(&data->external_checks,
286                      (const char *)&data->cr->check_no,
287                      sizeof(data->cr->check_no), NULL, NULL);
288
289     /* If there is no timeout_event, the check must have completed.
290      * We have nothing to do. */
291     if(!ci || !ci->timeout_event) {
292       free(data->cr->stdoutbuff);
293       free(data->cr->stderrbuff);
294       free(data->cr);
295       data->cr = NULL;
296       continue;
297     }
298     ci->exit_code = data->cr->exit_code;
299     ci->output = data->cr->stdoutbuff;
300     ci->error = data->cr->stderrbuff;
301     free(data->cr);
302     data->cr = NULL;
303     check = ci->check;
304     external_log_results(self, check);
305     eventer_remove(ci->timeout_event);
306     free(ci->timeout_event->closure);
307     eventer_free(ci->timeout_event);
308     ci->timeout_event = NULL;
309     check->flags &= ~NP_RUNNING;
310   }
311   return EVENTER_READ;
312
313  widowed:
314   noitL(noit_error, "external module terminated, must restart.\n");
315   exit(1);
316 }
317
318 static int external_init(noit_module_t *self) {
319   external_data_t *data;
320
321   data = noit_module_get_userdata(self);
322   if(!data) data = malloc(sizeof(*data));
323   data->nlerr = noit_log_stream_find("error/external");
324   data->nldeb = noit_log_stream_find("debug/external");
325
326   data->jobq = calloc(1, sizeof(*data->jobq));
327   eventer_jobq_init(data->jobq, "external");
328   data->jobq->backq = eventer_default_backq();
329   eventer_jobq_increase_concurrency(data->jobq);
330
331   if(socketpair(AF_UNIX, SOCK_STREAM, 0, data->pipe_n2e) != 0 ||
332      socketpair(AF_UNIX, SOCK_STREAM, 0, data->pipe_e2n) != 0) {
333     noitL(noit_error, "external: pipe() failed: %s\n", strerror(errno));
334     return -1;
335   }
336
337   data->child = fork();
338   if(data->child == -1) {
339     /* No child, bail. */
340     noitL(noit_error, "external: fork() failed: %s\n", strerror(errno));
341     return -1;
342   }
343
344   /* parent must close the read side of n2e and the write side of e2n */
345   /* The child must do the opposite */
346   close(data->pipe_n2e[(data->child == 0) ? 1 : 0]);
347   close(data->pipe_e2n[(data->child == 0) ? 0 : 1]);
348
349   /* Now the parent must set its bits non-blocking, the child need not */
350   if(data->child != 0) {
351     long on = 1;
352     /* in the parent */
353     if(ioctl(data->pipe_e2n[0], FIONBIO, &on) == -1) {
354       close(data->pipe_n2e[1]);
355       close(data->pipe_e2n[0]);
356       noitL(noit_error,
357             "external: could not set pipe non-blocking: %s\n",
358             strerror(errno));
359       return -1;
360     }
361     eventer_t newe;
362     newe = eventer_alloc();
363     newe->fd = data->pipe_e2n[0];
364     newe->mask = EVENTER_READ | EVENTER_EXCEPTION;
365     newe->callback = external_handler;
366     newe->closure = self;
367     eventer_add(newe);
368   }
369   else {
370     const char *user = NULL, *group = NULL;
371     if(data->options) {
372       noit_hash_retrieve(data->options, "user", 4, (void **)&user);
373       noit_hash_retrieve(data->options, "group", 4, (void **)&group);
374     }
375     noit_security_usergroup(user, group);
376     exit(external_child(data));
377   }
378   noit_module_set_userdata(self, data);
379   return 0;
380 }
381
382 static void external_cleanup(noit_module_t *self, noit_check_t *check) {
383   struct check_info *ci = (struct check_info *)check->closure;
384   if(ci) {
385     if(ci->timeout_event) {
386       eventer_remove(ci->timeout_event);
387       free(ci->timeout_event->closure);
388       eventer_free(ci->timeout_event);
389       ci->timeout_event = NULL;
390     }
391   }
392 }
393 #define assert_write(fd, w, s) assert(write(fd, w, s) == s)
394 static int external_enqueue(eventer_t e, int mask, void *closure,
395                             struct timeval *now) {
396   external_closure_t *ecl = (external_closure_t *)closure;
397   struct check_info *ci = (struct check_info *)ecl->check->closure;
398   external_data_t *data;
399   int fd, i;
400
401   if(mask == EVENTER_ASYNCH_CLEANUP) {
402     e->mask = 0;
403     return 0;
404   }
405   if(!(mask & EVENTER_ASYNCH_WORK)) return 0;
406   data = noit_module_get_userdata(ecl->self);
407   fd = data->pipe_n2e[1];
408   assert_write(fd, &ci->check_no, sizeof(ci->check_no));
409   assert_write(fd, &ci->argcnt, sizeof(ci->argcnt));
410   assert_write(fd, ci->arglens, sizeof(*ci->arglens)*ci->argcnt);
411   for(i=0; i<ci->argcnt; i++)
412     assert_write(fd, ci->args[i], ci->arglens[i]);
413   assert_write(fd, &ci->envcnt, sizeof(ci->envcnt));
414   assert_write(fd, ci->envlens, sizeof(*ci->envlens)*ci->envcnt);
415   for(i=0; i<ci->envcnt; i++)
416     assert_write(fd, ci->envs[i], ci->envlens[i]);
417   return 0;
418 }
419 static int external_invoke(noit_module_t *self, noit_check_t *check) {
420   struct timeval when, p_int;
421   external_closure_t *ecl;
422   struct check_info *ci = (struct check_info *)check->closure;
423   eventer_t newe;
424   external_data_t *data;
425   noit_hash_table check_attrs_hash = NOIT_HASH_EMPTY;
426   int i, klen;
427   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
428   const char *name, *value;
429   char interp_fmt[4096], interp_buff[4096];
430
431   data = noit_module_get_userdata(self);
432
433   check->flags |= NP_RUNNING;
434   noitL(data->nldeb, "external_invoke(%p,%s)\n",
435         self, check->target);
436
437   /* remove a timeout if we still have one -- we should unless someone
438    * has set a lower timeout than the period.
439    */
440   if(ci->timeout_event) {
441     eventer_remove(ci->timeout_event);
442     free(ci->timeout_event->closure);
443     eventer_free(ci->timeout_event);
444     ci->timeout_event = NULL;
445   }
446
447   check_info_clean(ci);
448
449   gettimeofday(&when, NULL);
450   memcpy(&check->last_fire_time, &when, sizeof(when));
451
452   /* Setup all our check bits */
453   ci->check_no = noit_atomic_inc64(&data->check_no_seq);
454   ci->check = check;
455   /* We might want to extract metrics */
456   if(noit_hash_retrieve(check->config,
457                         "output_extract", strlen("output_extract"),
458                         (void **)&value) != 0) {
459     const char *error;
460     int erroffset;
461     ci->matcher = pcre_compile(value, 0, &error, &erroffset, NULL);
462     if(!ci->matcher) {
463       noitL(data->nlerr, "external pcre /%s/ failed @ %d: %s\n",
464             value, erroffset, error);
465     }
466   }
467
468   noit_check_make_attrs(check, &check_attrs_hash);
469
470   /* Count the args */
471   i = 1;
472   while(1) {
473     char argname[10];
474     snprintf(argname, sizeof(argname), "arg%d", i);
475     if(noit_hash_retrieve(check->config, argname, strlen(argname),
476                           (void **)&value) == 0) break;
477     i++;
478   }
479   ci->argcnt = i + 1; /* path, arg0, (i-1 more args) */
480   ci->arglens = calloc(ci->argcnt, sizeof(*ci->arglens));
481   ci->args = calloc(ci->argcnt, sizeof(*ci->args));
482
483   /* Make the command */
484   if(noit_hash_retrieve(check->config, "command", strlen("command"),
485                         (void **)&value) == 0) {
486     value = "/bin/true";
487   }
488   ci->args[0] = strdup(value);
489   ci->arglens[0] = strlen(ci->args[0]) + 1;
490
491   i = 0;
492   while(1) {
493     char argname[10];
494     snprintf(argname, sizeof(argname), "arg%d", i);
495     if(noit_hash_retrieve(check->config, argname, strlen(argname),
496                           (void **)&value) == 0) {
497       if(i == 0) {
498         /* if we don't have arg0, make it last element of path */
499         char *cp = ci->args[0] + strlen(ci->args[0]);
500         while(cp > ci->args[0] && *(cp-1) != '/') cp--;
501         value = cp;
502       }
503       else break; /* if we don't have argn, we're done */
504     }
505     noit_check_interpolate(interp_buff, sizeof(interp_buff), value,
506                            &check_attrs_hash, check->config);
507     ci->args[i+1] = strdup(interp_buff);
508     ci->arglens[i+1] = strlen(ci->args[i+1]) + 1;
509     i++;
510   }
511
512   /* Make the environment */
513   memset(&iter, 0, sizeof(iter));
514   ci->envcnt = 0;
515   while(noit_hash_next(check->config, &iter, &name, &klen, (void **)&value))
516     if(!strncasecmp(name, "env_", 4))
517       ci->envcnt++;
518   memset(&iter, 0, sizeof(iter));
519   ci->envlens = calloc(ci->envcnt, sizeof(*ci->envlens));
520   ci->envs = calloc(ci->envcnt, sizeof(*ci->envs));
521   ci->envcnt = 0;
522   while(noit_hash_next(check->config, &iter, &name, &klen, (void **)&value))
523     if(!strncasecmp(name, "env_", 4)) {
524       snprintf(interp_fmt, sizeof(interp_fmt), "%s=%s", name+4, value);
525       noit_check_interpolate(interp_buff, sizeof(interp_buff), interp_fmt,
526                              &check_attrs_hash, check->config);
527       ci->envs[ci->envcnt] = strdup(interp_buff);
528       ci->envlens[ci->envcnt] = strlen(ci->envs[ci->envcnt]) + 1;
529       ci->envcnt++;
530     }
531
532   noit_hash_destroy(&check_attrs_hash, NULL, NULL);
533
534   noit_hash_store(&data->external_checks,
535                   (const char *)&ci->check_no, sizeof(ci->check_no),
536                   ci);
537
538   /* Setup a timeout */
539   newe = eventer_alloc();
540   newe->mask = EVENTER_TIMER;
541   gettimeofday(&when, NULL);
542   p_int.tv_sec = check->timeout / 1000;
543   p_int.tv_usec = (check->timeout % 1000) * 1000;
544   add_timeval(when, p_int, &newe->whence);
545   ecl = calloc(1, sizeof(*ecl));
546   ecl->self = self;
547   ecl->check = check;
548   newe->closure = ecl;
549   newe->callback = external_timeout;
550   eventer_add(newe);
551   ci->timeout_event = newe;
552
553   /* Setup push */
554   newe = eventer_alloc();
555   newe->mask = EVENTER_ASYNCH;
556   add_timeval(when, p_int, &newe->whence);
557   ecl = calloc(1, sizeof(*ecl));
558   ecl->self = self;
559   ecl->check = check;
560   newe->closure = ecl;
561   newe->callback = external_enqueue;
562   eventer_add(newe);
563
564   return 0;
565 }
566 static int external_initiate_check(noit_module_t *self, noit_check_t *check,
567                                     int once, noit_check_t *cause) {
568   if(!check->closure) check->closure = calloc(1, sizeof(struct check_info));
569   INITIATE_CHECK(external_invoke, self, check);
570   return 0;
571 }
572
573 static int external_onload(noit_image_t *self) {
574   eventer_name_callback("external/timeout", external_timeout);
575   eventer_name_callback("external/handler", external_handler);
576   return 0;
577 }
578
579 #include "external.xmlh"
580 noit_module_t external = {
581   {
582     NOIT_MODULE_MAGIC,
583     NOIT_MODULE_ABI_VERSION,
584     "external",
585     "checks via external programs",
586     external_xml_description,
587     external_onload
588   },
589   external_config,
590   external_init,
591   external_initiate_check,
592   external_cleanup
593 };
594
Note: See TracBrowser for help on using the browser.