root/src/noit_check.c

Revision e06e00e9272d5e12448881e0b1331671143eed34, 37.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

we allow dynamic resolution, so this isn't an error -- it's actually not even interesting

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <assert.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43 #include "utils/noit_log.h"
44 #include "utils/noit_hash.h"
45 #include "utils/noit_skiplist.h"
46 #include "noit_conf.h"
47 #include "noit_check.h"
48 #include "noit_module.h"
49 #include "noit_console.h"
50 #include "noit_check_tools.h"
51 #include "noit_check_resolver.h"
52 #include "eventer/eventer.h"
53
54 /* 60 seconds of possible stutter */
55 #define MAX_INITIAL_STUTTER 60000
56
57 static u_int64_t check_completion_count = 0;
58 static noit_hash_table polls = NOIT_HASH_EMPTY;
59 static noit_skiplist watchlist = { 0 };
60 static noit_skiplist polls_by_name = { 0 };
61 static u_int32_t __config_load_generation = 0;
62
63 u_int64_t noit_check_completion_count() {
64   return check_completion_count;
65 }
66 static void register_console_check_commands();
67 static int check_recycle_bin_processor(eventer_t, int, void *,
68                                        struct timeval *);
69
70 const char *
71 noit_check_available_string(int16_t available) {
72   switch(available) {
73     case NP_AVAILABLE:    return "available";
74     case NP_UNAVAILABLE:  return "unavailable";
75     case NP_UNKNOWN:      return "unknown";
76   }
77   return NULL;
78 }
79 const char *
80 noit_check_state_string(int16_t state) {
81   switch(state) {
82     case NP_GOOD:         return "good";
83     case NP_BAD:          return "bad";
84     case NP_UNKNOWN:      return "unknown";
85   }
86   return NULL;
87 }
88 static int __check_name_compare(const void *a, const void *b) {
89   const noit_check_t *ac = a;
90   const noit_check_t *bc = b;
91   int rv;
92   if((rv = strcmp(ac->target, bc->target)) != 0) return rv;
93   if((rv = strcmp(ac->name, bc->name)) != 0) return rv;
94   return 0;
95 }
96 static int __watchlist_compare(const void *a, const void *b) {
97   const noit_check_t *ac = a;
98   const noit_check_t *bc = b;
99   int rv;
100   if((rv = memcmp(ac->checkid, bc->checkid, sizeof(ac->checkid))) != 0) return rv;
101   if(ac->period < bc->period) return -1;
102   if(ac->period == bc->period) return 0;
103   return 1;
104 }
105 int
106 noit_check_max_initial_stutter() {
107   int stutter;
108   if(!noit_conf_get_int(NULL, "/noit/checks/@max_initial_stutter", &stutter))
109     stutter = MAX_INITIAL_STUTTER;
110   return stutter;
111 }
112 void
113 noit_check_fake_last_check(noit_check_t *check,
114                            struct timeval *lc, struct timeval *_now) {
115   struct timeval now, period;
116   double r;
117   int offset = 0, max;
118
119   if(!(check->flags & NP_TRANSIENT)) {
120     r = drand48();
121     max = noit_check_max_initial_stutter();
122     offset = r * (MIN(max, check->period));
123   }
124   period.tv_sec = (check->period - offset) / 1000;
125   period.tv_usec = ((check->period - offset) % 1000) * 1000;
126   if(!_now) {
127     gettimeofday(&now, NULL);
128     _now = &now;
129   }
130   sub_timeval(*_now, period, lc);
131 }
132 void
133 noit_poller_process_checks(const char *xpath) {
134   int i, flags, cnt = 0;
135   noit_conf_section_t *sec;
136   __config_load_generation++;
137   sec = noit_conf_get_sections(NULL, xpath, &cnt);
138   for(i=0; i<cnt; i++) {
139     void *vcheck;
140     char uuid_str[37];
141     char target[256] = "";
142     char module[256] = "";
143     char name[256] = "";
144     char filterset[256] = "";
145     char oncheck[1024] = "";
146     int no_period = 0;
147     int no_oncheck = 0;
148     int period = 0, timeout = 0;
149     noit_boolean disabled = noit_false, busted = noit_false;
150     uuid_t uuid, out_uuid;
151     noit_hash_table *options;
152
153 #define NEXT(...) noitL(noit_stderr, __VA_ARGS__); continue
154 #define MYATTR(type,a,...) noit_conf_get_##type(sec[i], "@" #a, __VA_ARGS__)
155 #define INHERIT(type,a,...) \
156   noit_conf_get_##type(sec[i], "ancestor-or-self::node()/@" #a, __VA_ARGS__)
157
158     if(!MYATTR(stringbuf, uuid, uuid_str, sizeof(uuid_str))) {
159       noitL(noit_stderr, "check %d has no uuid\n", i+1);
160       continue;
161     }
162
163     if(uuid_parse(uuid_str, uuid)) {
164       noitL(noit_stderr, "check uuid: '%s' is invalid\n", uuid_str);
165       continue;
166     }
167
168     if(!INHERIT(stringbuf, target, target, sizeof(target))) {
169       noitL(noit_stderr, "check uuid: '%s' has no target\n", uuid_str);
170       busted = noit_true;
171     }
172     if(!INHERIT(stringbuf, module, module, sizeof(module))) {
173       noitL(noit_stderr, "check uuid: '%s' has no module\n", uuid_str);
174       busted = noit_true;
175     }
176
177     if(!INHERIT(stringbuf, filterset, filterset, sizeof(filterset)))
178       filterset[0] = '\0';
179
180     if(!MYATTR(stringbuf, name, name, sizeof(name)))
181       strlcpy(name, module, sizeof(name));
182
183     if(!INHERIT(int, period, &period) || period == 0)
184       no_period = 1;
185
186     if(!INHERIT(stringbuf, oncheck, oncheck, sizeof(oncheck)) || !oncheck[0])
187       no_oncheck = 1;
188
189     if(no_period && no_oncheck) {
190       noitL(noit_stderr, "check uuid: '%s' has neither period nor oncheck\n",
191             uuid_str);
192       busted = noit_true;
193     }
194     if(!(no_period || no_oncheck)) {
195       noitL(noit_stderr, "check uuid: '%s' has oncheck and period.\n",
196             uuid_str);
197       busted = noit_true;
198     }
199     if(!INHERIT(int, timeout, &timeout)) {
200       noitL(noit_stderr, "check uuid: '%s' has no timeout\n", uuid_str);
201       busted = noit_true;
202     }
203     if(!no_period && timeout >= period) {
204       noitL(noit_stderr, "check uuid: '%s' timeout > period\n", uuid_str);
205       timeout = period/2;
206     }
207     options = noit_conf_get_hash(sec[i], "config");
208
209     INHERIT(boolean, disable, &disabled);
210     flags = 0;
211     if(busted) flags |= (NP_UNCONFIG|NP_DISABLED);
212     else if(disabled) flags |= NP_DISABLED;
213
214     if(noit_hash_retrieve(&polls, (char *)uuid, UUID_SIZE,
215                           &vcheck)) {
216       noit_check_t *existing_check = (noit_check_t *)vcheck;
217       /* Once set, it cannot be checked if the check is live */
218       assert(!existing_check->module || !existing_check->module[0] ||
219              !strcmp(existing_check->module, module) ||
220              !NOIT_CHECK_LIVE(existing_check));
221       /* Set it if it is unset or being changed */
222       if(!existing_check->module || !existing_check->module[0] ||
223          strcmp(existing_check->module, module)) {
224         if(existing_check->module) free(existing_check->module);
225         existing_check->module = strdup(module);
226       }
227       noit_check_update(existing_check, target, name, filterset, options,
228                            period, timeout, oncheck[0] ? oncheck : NULL,
229                            flags);
230       noitL(noit_debug, "reloaded uuid: %s\n", uuid_str);
231     }
232     else {
233       noit_poller_schedule(target, module, name, filterset, options,
234                            period, timeout, oncheck[0] ? oncheck : NULL,
235                            flags, uuid, out_uuid);
236       noitL(noit_debug, "loaded uuid: %s\n", uuid_str);
237     }
238
239     noit_hash_destroy(options, free, free);
240     free(options);
241   }
242   if(sec) free(sec);
243 }
244
245 int
246 noit_check_activate(noit_check_t *check) {
247   noit_module_t *mod;
248   if(NOIT_CHECK_LIVE(check)) return 0;
249   mod = noit_module_lookup(check->module);
250   if(mod && mod->initiate_check) {
251     if((check->flags & NP_DISABLED) == 0) {
252       mod->initiate_check(mod, check, 0, NULL);
253       return 1;
254     }
255     else
256       noitL(noit_debug, "Skipping %s`%s, disabled.\n",
257             check->target, check->name);
258   }
259   else {
260     if(!mod) {
261       noitL(noit_stderr, "Cannot find module '%s'\n", check->module);
262       check->flags |= NP_DISABLED;
263     }
264   }
265   return 0;
266 }
267
268 void
269 noit_poller_initiate() {
270   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
271   uuid_t key_id;
272   int klen;
273   void *vcheck;
274   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
275                        &vcheck)) {
276     noit_check_activate((noit_check_t *)vcheck);
277   }
278 }
279
280 void
281 noit_poller_flush_epoch(int oldest_allowed) {
282   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
283   uuid_t key_id;
284   int klen;
285   noit_check_t *tofree = NULL;
286   void *vcheck;
287
288   /* Cleanup any previous causal map */
289   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
290                        &vcheck)) {
291     noit_check_t *check = (noit_check_t *)vcheck;
292     /* We don't free the one we're looking at... we free it on the next
293      * pass.  This leaves out iterator in good shape.  We just need to
294      * remember to free it one last time outside the while loop, down...
295      */
296     if(tofree) {
297       noit_poller_deschedule(tofree->checkid);
298       tofree = NULL;
299     }
300     if(check->generation < oldest_allowed) {
301       tofree = check;
302     }
303   }
304   /* ... here */
305   if(tofree) noit_poller_deschedule(tofree->checkid);
306 }
307
308 void
309 noit_poller_make_causal_map() {
310   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
311   uuid_t key_id;
312   int klen;
313   void *vcheck;
314
315   /* Cleanup any previous causal map */
316   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
317                        &vcheck)) {
318     noit_check_t *check = (noit_check_t *)vcheck;
319     dep_list_t *dep;
320     while((dep = check->causal_checks) != NULL) {
321       check->causal_checks = dep->next;
322       free(dep);
323     }
324   }
325
326   memset(&iter, 0, sizeof(iter));
327   /* Walk all checks and add check dependencies to their parents */
328   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
329                        &vcheck)) {
330     noit_check_t *check = (noit_check_t *)vcheck, *parent;
331     if(check->oncheck) {
332       /* This service is causally triggered by another service */
333       char fullcheck[1024];
334       char *name = check->oncheck;
335       char *target = NULL;
336
337       noitL(noit_debug, "Searching for upstream trigger on %s\n", name);
338       if((target = strchr(check->oncheck, '`')) != NULL) {
339         strlcpy(fullcheck, check->oncheck, target - check->oncheck);
340         name = target + 1;
341         target = fullcheck;
342       }
343       else
344        target = check->target;
345
346       parent = noit_poller_lookup_by_name(target, name);
347       if(!parent) {
348         check->flags |= NP_DISABLED;
349         noitL(noit_stderr, "Disabling check %s`%s, can't find oncheck %s`%s\n",
350               check->target, check->name, target, name);
351       }
352       else {
353         dep_list_t *dep;
354         dep = malloc(sizeof(*dep));
355         dep->check = check;
356         dep->next = parent->causal_checks;
357         parent->causal_checks = dep;
358         noitL(noit_debug, "Causal map %s`%s --> %s`%s\n",
359               parent->target, parent->name, check->target, check->name);
360       }
361     }
362   }
363 }
364 void
365 noit_poller_reload(const char *xpath)
366 {
367   noit_poller_process_checks(xpath ? xpath : "/noit/checks//check");
368   if(!xpath) {
369     /* Full reload, we need to wipe old checks */
370     noit_poller_flush_epoch(__config_load_generation);
371   }
372   noit_poller_make_causal_map();
373   noit_poller_initiate();
374 }
375 void
376 noit_poller_init() {
377   noit_check_resolver_init();
378   noit_check_tools_init();
379   noit_skiplist_init(&polls_by_name);
380   noit_skiplist_set_compare(&polls_by_name, __check_name_compare,
381                             __check_name_compare);
382   noit_skiplist_init(&watchlist);
383   noit_skiplist_set_compare(&watchlist, __watchlist_compare,
384                             __watchlist_compare);
385   register_console_check_commands();
386   eventer_name_callback("check_recycle_bin_processor",
387                         check_recycle_bin_processor);
388   eventer_add_in_s_us(check_recycle_bin_processor, NULL, 60, 0);
389   noit_poller_reload(NULL);
390 }
391
392 int
393 noit_poller_check_count() {
394   return polls_by_name.size;
395 }
396
397 int
398 noit_poller_transient_check_count() {
399   return watchlist.size;
400 }
401
402 noit_check_t *
403 noit_check_clone(uuid_t in) {
404   noit_check_t *checker, *new_check;
405   void *vcheck;
406   if(noit_hash_retrieve(&polls,
407                         (char *)in, UUID_SIZE,
408                         &vcheck) == 0) {
409     return NULL;
410   }
411   checker = (noit_check_t *)vcheck;
412   if(checker->oncheck) {
413     return NULL;
414   }
415   new_check = calloc(1, sizeof(*new_check));
416   memcpy(new_check, checker, sizeof(*new_check));
417   new_check->target = strdup(new_check->target);
418   new_check->module = strdup(new_check->module);
419   new_check->name = strdup(new_check->name);
420   new_check->filterset = strdup(new_check->filterset);
421   new_check->flags = 0;
422   new_check->fire_event = NULL;
423   memset(&new_check->last_fire_time, 0, sizeof(new_check->last_fire_time));
424   memset(&new_check->stats, 0, sizeof(new_check->stats));
425   new_check->closure = NULL;
426   new_check->config = calloc(1, sizeof(*new_check->config));
427   noit_hash_merge_as_dict(new_check->config, checker->config);
428   return new_check;
429 }
430
431 noit_check_t *
432 noit_check_watch(uuid_t in, int period) {
433   /* First look for a copy that is being watched */
434   int minimum_pi = 1000, granularity_pi = 500;
435   noit_conf_section_t check_node;
436   char uuid_str[UUID_STR_LEN + 1];
437   char xpath[1024];
438   noit_check_t n, *f;
439
440   uuid_unparse_lower(in, uuid_str);
441   /* Find the check */
442   snprintf(xpath, sizeof(xpath), "//checks//check[@uuid=\"%s\"]", uuid_str);
443   check_node = noit_conf_get_section(NULL, xpath);
444   noit_conf_get_int(NULL, "//checks/@transient_min_period", &minimum_pi);
445   noit_conf_get_int(NULL, "//checks/@transient_period_granularity", &granularity_pi);
446   if(check_node) {
447     noit_conf_get_int(check_node,
448                       "ancestor-or-self::node()/@transient_min_period",
449                       &minimum_pi);
450     noit_conf_get_int(check_node,
451                       "ancestor-or-self::node()/@transient_period_granularity",
452                       &granularity_pi);
453   }
454
455   /* apply the bounds */
456   period /= granularity_pi;
457   period *= granularity_pi;
458   period = MAX(period, minimum_pi);
459
460   uuid_copy(n.checkid, in);
461   n.period = period;
462
463   f = noit_skiplist_find(&watchlist, &n, NULL);
464   if(f) return f;
465   f = noit_check_clone(in);
466   if(!f) return NULL;
467   f->period = period;
468   f->timeout = period - 10;
469   f->flags |= NP_TRANSIENT;
470   noitL(noit_debug, "Watching %s@%d\n", uuid_str, period);
471   noit_skiplist_insert(&watchlist, f);
472   return f;
473 }
474
475 noit_check_t *
476 noit_check_get_watch(uuid_t in, int period) {
477   noit_check_t n, *f;
478
479   uuid_copy(n.checkid, in);
480   n.period = period;
481
482   f = noit_skiplist_find(&watchlist, &n, NULL);
483   return f;
484 }
485
486 void
487 noit_check_transient_add_feed(noit_check_t *check, const char *feed) {
488   char *feedcopy;
489   if(!check->feeds) {
490     check->feeds = calloc(1, sizeof(*check->feeds));
491     noit_skiplist_init(check->feeds);
492     noit_skiplist_set_compare(check->feeds,
493                               (noit_skiplist_comparator_t)strcmp,
494                               (noit_skiplist_comparator_t)strcmp);
495   }
496   feedcopy = strdup(feed);
497   /* No error on failure -- it's already there */
498   if(noit_skiplist_insert(check->feeds, feedcopy) == NULL) free(feedcopy);
499   noitL(noit_debug, "check %s`%s @ %dms has %d feed(s): %s.\n",
500         check->target, check->name, check->period, check->feeds->size, feed);
501 }
502 void
503 noit_check_transient_remove_feed(noit_check_t *check, const char *feed) {
504   if(!check->feeds) return;
505   if(feed) {
506     noitL(noit_debug, "check %s`%s @ %dms removing 1 of %d feeds: %s.\n",
507           check->target, check->name, check->period, check->feeds->size, feed);
508     noit_skiplist_remove(check->feeds, feed, free);
509   }
510   if(check->feeds->size == 0) {
511     char uuid_str[UUID_STR_LEN + 1];
512     uuid_unparse_lower(check->checkid, uuid_str);
513     noitL(noit_debug, "Unwatching %s@%d\n", uuid_str, check->period);
514     noit_skiplist_remove(&watchlist, check, NULL);
515     noit_skiplist_destroy(check->feeds, free);
516     free(check->feeds);
517     check->feeds = NULL;
518     if(check->flags & NP_TRANSIENT) {
519       noitL(noit_debug, "check %s`%s @ %dms has no more listeners.\n",
520             check->target, check->name, check->period);
521       check->flags |= NP_KILLED;
522     }
523   }
524 }
525
526 noit_boolean
527 noit_check_is_valid_target(const char *target) {
528   int8_t family;
529   int rv;
530   union {
531     struct in_addr addr4;
532     struct in6_addr addr6;
533   } a;
534
535   family = AF_INET;
536   rv = inet_pton(family, target, &a);
537   if(rv != 1) {
538     family = AF_INET6;
539     rv = inet_pton(family, target, &a);
540     if(rv != 1) {
541       return noit_false;
542     }
543   }
544   return noit_true;
545 }
546 int
547 noit_check_set_ip(noit_check_t *new_check,
548                   const char *ip_str) {
549   int8_t family;
550   int rv, failed = 0;
551   union {
552     struct in_addr addr4;
553     struct in6_addr addr6;
554   } a;
555
556
557   family = AF_INET;
558   rv = inet_pton(family, ip_str, &a);
559   if(rv != 1) {
560     family = AF_INET6;
561     rv = inet_pton(family, ip_str, &a);
562     if(rv != 1) {
563       family = AF_INET;
564       memset(&a, 0, sizeof(a));
565       failed = -1;
566     }
567   }
568
569   new_check->target_family = family;
570   memcpy(&new_check->target_addr, &a, sizeof(a));
571   new_check->target_ip[0] = '\0';
572   if(failed == 0)
573     inet_ntop(new_check->target_family,
574               &new_check->target_addr,
575               new_check->target_ip,
576               sizeof(new_check->target_ip));
577   return failed;
578 }
579 int
580 noit_check_resolve(noit_check_t *check) {
581   uint8_t family_pref = AF_INET;
582   char ipaddr[INET6_ADDRSTRLEN];
583   if(!NOIT_CHECK_SHOULD_RESOLVE(check)) return 1; /* success, not required */
584   noit_check_resolver_remind(check->target);
585   if(noit_check_resolver_fetch(check->target, ipaddr, sizeof(ipaddr),
586                                family_pref) >= 0) {
587     check->flags |= NP_RESOLVED;
588     noit_check_set_ip(check, ipaddr);
589     return 0;
590   }
591   check->flags &= ~NP_RESOLVED;
592   return -1;
593 }
594 int
595 noit_check_update(noit_check_t *new_check,
596                   const char *target,
597                   const char *name,
598                   const char *filterset,
599                   noit_hash_table *config,
600                   u_int32_t period,
601                   u_int32_t timeout,
602                   const char *oncheck,
603                   int flags) {
604   int mask = NP_DISABLED | NP_UNCONFIG;
605
606   new_check->generation = __config_load_generation;
607   if(new_check->target) free(new_check->target);
608   new_check->target = strdup(target);
609
610   if(noit_check_set_ip(new_check, target)) {
611     noit_boolean should_resolve;
612     new_check->flags |= NP_RESOLVE;
613     new_check->flags &= ~NP_RESOLVED;
614     if(noit_conf_get_boolean(NULL, "//checks/@resolve_targets",
615                              &should_resolve) && should_resolve == noit_false)
616      
617       flags |= NP_DISABLED | NP_UNCONFIG;
618     noit_check_resolve(new_check);
619   }
620
621   if(new_check->name) free(new_check->name);
622   new_check->name = name ? strdup(name): NULL;
623   if(new_check->filterset) free(new_check->filterset);
624   new_check->filterset = filterset ? strdup(filterset): NULL;
625
626   if(config != NULL) {
627     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
628     const char *k;
629     int klen;
630     void *data;
631     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
632     else new_check->config = calloc(1, sizeof(*new_check->config));
633     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
634       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
635     }
636   }
637   if(new_check->oncheck) free(new_check->oncheck);
638   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
639   new_check->period = period;
640   new_check->timeout = timeout;
641
642   /* Unset what could be set.. then set what should be set */
643   new_check->flags = (new_check->flags & ~mask) | flags;
644
645   if(!(new_check->flags & NP_TRANSIENT)) {
646     /* This remove could fail -- no big deal */
647     noit_skiplist_remove(&polls_by_name, new_check, NULL);
648
649     /* This insert could fail.. which means we have a conflict on
650      * target`name.  That should result in the check being disabled. */
651     if(!noit_skiplist_insert(&polls_by_name, new_check)) {
652       noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
653             new_check->target, new_check->name);
654       new_check->flags |= NP_DISABLED;
655     }
656   }
657   noit_check_log_check(new_check);
658   return 0;
659 }
660 int
661 noit_poller_schedule(const char *target,
662                      const char *module,
663                      const char *name,
664                      const char *filterset,
665                      noit_hash_table *config,
666                      u_int32_t period,
667                      u_int32_t timeout,
668                      const char *oncheck,
669                      int flags,
670                      uuid_t in,
671                      uuid_t out) {
672   noit_check_t *new_check;
673   new_check = calloc(1, sizeof(*new_check));
674   if(!new_check) return -1;
675
676   /* The module and the UUID can never be changed */
677   new_check->module = strdup(module);
678   if(uuid_is_null(in))
679     uuid_generate(new_check->checkid);
680   else
681     uuid_copy(new_check->checkid, in);
682
683   noit_check_update(new_check, target, name, filterset, config,
684                     period, timeout, oncheck, flags);
685   assert(noit_hash_store(&polls,
686                          (char *)new_check->checkid, UUID_SIZE,
687                          new_check));
688   uuid_copy(out, new_check->checkid);
689
690   return 0;
691 }
692
693 /* A quick little list of recycleable checks.  This list never really
694  * grows large, so no sense in thinking too hard about the algorithmic
695  * complexity.
696  */
697 struct _checker_rcb {
698   noit_check_t *checker;
699   struct _checker_rcb *next;
700 };
701 static struct _checker_rcb *checker_rcb = NULL;
702 static void recycle_check(noit_check_t *checker) {
703   struct _checker_rcb *n = malloc(sizeof(*n));
704   n->checker = checker;
705   n->next = checker_rcb;
706   checker_rcb = n;
707 }
708 void
709 noit_poller_free_check(noit_check_t *checker) {
710   noit_module_t *mod;
711
712   if(checker->flags & NP_RUNNING) {
713     recycle_check(checker);
714     return;
715   }
716
717   mod = noit_module_lookup(checker->module);
718   if(mod && mod->cleanup) mod->cleanup(mod, checker);
719   if(checker->fire_event) {
720      eventer_remove(checker->fire_event);
721      free(checker->fire_event->closure);
722      eventer_free(checker->fire_event);
723      checker->fire_event = NULL;
724   }
725   if(checker->closure) free(checker->closure);
726   if(checker->target) free(checker->target);
727   if(checker->module) free(checker->module);
728   if(checker->name) free(checker->name);
729   if(checker->config) {
730     noit_hash_destroy(checker->config, free, free);
731     free(checker->config);
732     checker->config = NULL;
733   }
734   free(checker);
735 }
736 static int
737 check_recycle_bin_processor(eventer_t e, int mask, void *closure,
738                             struct timeval *now) {
739   static struct timeval one_minute = { 60L, 0L };
740   struct _checker_rcb *prev = NULL, *curr = checker_rcb;
741   noitL(noit_debug, "Scanning check recycle bin\n");
742   while(curr) {
743     if(!(curr->checker->flags & NP_RUNNING)) {
744       noitL(noit_debug, "Check is ready to free.\n");
745       noit_poller_free_check(curr->checker);
746       if(prev) prev->next = curr->next;
747       else checker_rcb = curr->next;
748       free(curr);
749       curr = prev ? prev->next : checker_rcb;
750     }
751     else {
752       prev = curr;
753       curr = curr->next;
754     }
755   }
756   add_timeval(*now, one_minute, &e->whence);
757   return EVENTER_TIMER;
758 }
759
760 int
761 noit_poller_deschedule(uuid_t in) {
762   void *vcheck;
763   noit_check_t *checker;
764   if(noit_hash_retrieve(&polls,
765                         (char *)in, UUID_SIZE,
766                         &vcheck) == 0) {
767     return -1;
768   }
769   checker = (noit_check_t *)vcheck;
770   checker->flags |= (NP_DISABLED|NP_KILLED);
771
772   noit_skiplist_remove(&polls_by_name, checker, NULL);
773   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
774
775   noit_poller_free_check(checker);
776   return 0;
777 }
778
779 noit_check_t *
780 noit_poller_lookup(uuid_t in) {
781   void *vcheck;
782   if(noit_hash_retrieve(&polls, (char *)in, UUID_SIZE, &vcheck))
783     return (noit_check_t *)vcheck;
784   return NULL;
785 }
786 noit_check_t *
787 noit_poller_lookup_by_name(char *target, char *name) {
788   noit_check_t *check, *tmp_check;
789   tmp_check = calloc(1, sizeof(*tmp_check));
790   tmp_check->target = target;
791   tmp_check->name = name;
792   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
793   free(tmp_check);
794   return check;
795 }
796
797 int
798 noit_check_xpath(char *xpath, int len,
799                  const char *base, const char *arg) {
800   uuid_t checkid;
801   int base_trailing_slash;
802   char argcopy[1024], *target, *module, *name;
803
804   base_trailing_slash = (base[strlen(base)-1] == '/');
805   xpath[0] = '\0';
806   argcopy[0] = '\0';
807   if(arg) strlcpy(argcopy, arg, sizeof(argcopy));
808
809   if(uuid_parse(argcopy, checkid) == 0) {
810     /* If they kill by uuid, we'll seek and destroy -- find it anywhere */
811     snprintf(xpath, len, "/noit/checks%s%s/check[@uuid=\"%s\"]",
812              base, base_trailing_slash ? "" : "/", argcopy);
813   }
814   else if((module = strchr(argcopy, '`')) != NULL) {
815     noit_check_t *check;
816     char uuid_str[37];
817     target = argcopy;
818     *module++ = '\0';
819     if((name = strchr(module+1, '`')) == NULL)
820       name = module;
821     else
822       name++;
823     check = noit_poller_lookup_by_name(target, name);
824     if(!check) {
825       return -1;
826     }
827     uuid_unparse_lower(check->checkid, uuid_str);
828     snprintf(xpath, len, "/noit/checks%s%s/check[@uuid=\"%s\"]",
829              base, base_trailing_slash ? "" : "/", uuid_str);
830   }
831   return strlen(xpath);
832 }
833
834 void
835 noit_check_stats_clear(stats_t *s) {
836   memset(s, 0, sizeof(*s));
837   s->state = NP_UNKNOWN;
838   s->available = NP_UNKNOWN;
839 }
840 static void
841 __free_metric(void *vm) {
842   metric_t *m = vm;
843   free(m->metric_name);
844   if(m->metric_value.i) free(m->metric_value.i);
845   free(m);
846 }
847
848 void
849 __stats_add_metric(stats_t *newstate, metric_t *m) {
850   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
851                     m, NULL, __free_metric);
852 }
853
854 static size_t
855 noit_metric_sizes(metric_type_t type, void *value) {
856   switch(type) {
857     case METRIC_INT32:
858     case METRIC_UINT32:
859       return sizeof(int32_t);
860     case METRIC_INT64:
861     case METRIC_UINT64:
862       return sizeof(int64_t);
863     case METRIC_DOUBLE:
864       return sizeof(double);
865     case METRIC_STRING:
866       return strlen((char *)value) + 1;
867     case METRIC_GUESS:
868       break;
869   }
870   assert(type != type);
871   return 0;
872 }
873 static metric_type_t
874 noit_metric_guess_type(const char *s, void **replacement) {
875   char *copy, *cp, *trailer, *rpl;
876   int negative = 0;
877   metric_type_t type = METRIC_STRING;
878
879   if(!s) return METRIC_GUESS;
880   copy = cp = strdup(s);
881
882   /* TRIM the string */
883   while(*cp && isspace(*cp)) cp++; /* ltrim */
884   s = cp; /* found a good starting point */
885   while(*cp) cp++; /* advance to \0 */
886   cp--; /* back up one */
887   while(cp > s && isspace(*cp)) *cp-- = '\0'; /* rtrim */
888
889   /* Find the first space */
890   cp = (char *)s;
891   while(*cp && !isspace(*cp)) cp++;
892   trailer = cp;
893   cp--; /* backup one */
894   if(cp > s && *cp == '%') *cp-- = '\0'; /* chop a last % is there is one */
895
896   while(*trailer && isspace(*trailer)) *trailer++ = '\0'; /* rtrim */
897
898   /* string was       '  -1.23e-01%  inodes used  ' */
899   /* copy is (~ = \0) '  -1.23e-01~  inodes used~~' */
900   /*                     ^           ^              */
901   /*                     s           trailer        */
902
903   /* So, the trailer must not contain numbers */
904   while(*trailer) { if(isdigit(*trailer)) goto notanumber; trailer++; }
905
906   /* And the 's' must be of the form:
907    *  0) may start with a sign [-+]?
908    *  1) [1-9][0-9]*
909    *  2) [0]?.[0-9]+
910    *  3) 0
911    *  4) [1-9][0-9]*.[0-9]+
912    *  5) all of the above ending with e[+-][0-9]+
913    */
914    rpl = (char *)s;
915    /* CASE 0 */
916    if(s[0] == '-' || s[0] == '+') {
917      if(s[0] == '-') negative = 1;
918      s++;
919    }
920
921    if(s[0] == '.') goto decimal; /* CASE 2 */
922    if(s[0] == '0') { /* CASE 2 & 3 */
923      s++;
924      if(!s[0]) goto scanint; /* CASE 3 */
925      if(s[0] == '.') goto decimal; /* CASE 2 */
926      goto notanumber;
927    }
928    if(s[0] >= '1' && s[0] <= '9') { /* CASE 1 & 4 */
929      s++;
930      while(isdigit(s[0])) s++; /* CASE 1 & 4 */
931      if(!s[0]) goto scanint; /* CASE 1 */
932      if(s[0] == '.') goto decimal; /* CASE 4 */
933      goto notanumber;
934    }
935    /* Not case 1,2,3,4 */
936    goto notanumber;
937
938   decimal:
939    s++;
940    if(!isdigit(s[0])) goto notanumber;
941    s++;
942    while(isdigit(s[0])) s++;
943    if(!s[0]) goto scandouble;
944    if(s[0] == 'e' || s[0] == 'E') goto exponent; /* CASE 5 */
945    goto notanumber;
946
947   exponent:
948    s++;
949    if(s[0] != '-' && s[0] != '+') goto notanumber;
950    s++;
951    if(!isdigit(s[0])) goto notanumber;
952    s++;
953    while(isdigit(s[0])) s++;
954    if(!s[0]) goto scandouble;
955    goto notanumber;
956
957  scanint:
958    if(negative) {
959      int64_t *v;
960      v = calloc(1, sizeof(*v));
961      *v = strtoll(rpl, NULL, 10);
962      *replacement = v;
963      type = METRIC_INT64;
964      goto alldone;
965    }
966    else {
967      u_int64_t *v;
968      v = calloc(1, sizeof(*v));
969      *v = strtoull(rpl, NULL, 10);
970      *replacement = v;
971      type = METRIC_UINT64;
972      goto alldone;
973    }
974  scandouble:
975    {
976      double *v;
977      v = calloc(1, sizeof(*v));
978      *v = strtod(rpl, NULL);
979      *replacement = v;
980      type = METRIC_DOUBLE;
981      goto alldone;
982    }
983
984  alldone:
985  notanumber:
986   free(copy);
987   return type;
988 }
989 void
990 noit_stats_set_metric(stats_t *newstate, const char *name, metric_type_t type,
991                       void *value) {
992   metric_t *m;
993   void *replacement = NULL;
994   if(type == METRIC_GUESS)
995     type = noit_metric_guess_type((char *)value, &replacement);
996   if(type == METRIC_GUESS) return;
997
998   m = calloc(1, sizeof(*m));
999   m->metric_name = strdup(name);
1000   m->metric_type = type;
1001   if(replacement)
1002     m->metric_value.vp = replacement;
1003   else if(value) {
1004     size_t len;
1005     len = noit_metric_sizes(type, value);
1006     m->metric_value.vp = calloc(1, len);
1007     memcpy(m->metric_value.vp, value, len);
1008   }
1009   __stats_add_metric(newstate, m);
1010 }
1011
1012 void
1013 noit_check_passive_set_stats(struct _noit_module *module,
1014                              noit_check_t *check, stats_t *newstate) {
1015   noit_skiplist_node *next;
1016   noit_check_t n;
1017
1018   uuid_copy(n.checkid, check->checkid);
1019   n.period = 0;
1020
1021   noit_check_set_stats(module,check,newstate);
1022   noit_skiplist_find_neighbors(&watchlist, &n, NULL, NULL, &next);
1023   while(next && next->data) {
1024     stats_t backup;
1025     noit_check_t *wcheck = next->data;
1026     if(uuid_compare(n.checkid, wcheck->checkid)) break;
1027
1028     /* Swap the real check's stats into place */
1029     memcpy(&backup, &wcheck->stats.current, sizeof(stats_t));
1030     memcpy(&wcheck->stats.current, newstate, sizeof(stats_t));
1031     /* Write out our status */
1032     noit_check_log_status(wcheck);
1033     /* Write out all metrics */
1034     noit_check_log_metrics(wcheck);
1035     /* Swap them back out */
1036     memcpy(&wcheck->stats.current, &backup, sizeof(stats_t));
1037
1038     noit_skiplist_next(&watchlist, &next);
1039   }
1040 }
1041 void
1042 noit_check_set_stats(struct _noit_module *module,
1043                      noit_check_t *check, stats_t *newstate) {
1044   int report_change = 0;
1045   char *cp;
1046   dep_list_t *dep;
1047   if(check->stats.previous.status)
1048     free(check->stats.previous.status);
1049   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
1050   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
1051   memcpy(&check->stats.current, newstate, sizeof(stats_t));
1052   if(check->stats.current.status)
1053     check->stats.current.status = strdup(check->stats.current.status);
1054   for(cp = check->stats.current.status; cp && *cp; cp++)
1055     if(*cp == '\r' || *cp == '\n') *cp = ' ';
1056
1057   /* check for state changes */
1058   if(check->stats.current.available != NP_UNKNOWN &&
1059      check->stats.previous.available != NP_UNKNOWN &&
1060      check->stats.current.available != check->stats.previous.available)
1061     report_change = 1;
1062   if(check->stats.current.state != NP_UNKNOWN &&
1063      check->stats.previous.state != NP_UNKNOWN &&
1064      check->stats.current.state != check->stats.previous.state)
1065     report_change = 1;
1066
1067   noitL(noit_debug, "%s`%s <- [%s]\n", check->target, check->name,
1068         check->stats.current.status);
1069   if(report_change) {
1070     noitL(noit_debug, "%s`%s -> [%s:%s]\n",
1071           check->target, check->name,
1072           noit_check_available_string(check->stats.current.available),
1073           noit_check_state_string(check->stats.current.state));
1074   }
1075
1076   /* Write out our status */
1077   noit_check_log_status(check);
1078   /* Write out all metrics */
1079   noit_check_log_metrics(check);
1080   /* count the check as complete */
1081   check_completion_count++;
1082
1083   for(dep = check->causal_checks; dep; dep = dep->next) {
1084     noit_module_t *mod;
1085     mod = noit_module_lookup(dep->check->module);
1086     assert(mod);
1087     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
1088           dep->check->target, dep->check->name,
1089           check->target, check->name);
1090     if((dep->check->flags & NP_DISABLED) == 0)
1091       if(mod->initiate_check)
1092         mod->initiate_check(mod, dep->check, 1, check);
1093   }
1094 }
1095
1096 static int
1097 noit_console_show_watchlist(noit_console_closure_t ncct,
1098                             int argc, char **argv,
1099                             noit_console_state_t *dstate,
1100                             void *closure) {
1101   noit_skiplist_node *iter, *fiter;
1102   nc_printf(ncct, "%d active watches.\n", watchlist.size);
1103   for(iter = noit_skiplist_getlist(&watchlist); iter;
1104       noit_skiplist_next(&watchlist, &iter)) {
1105     char uuid_str[UUID_STR_LEN + 1];
1106     noit_check_t *check = iter->data;
1107
1108     uuid_unparse_lower(check->checkid, uuid_str);
1109     nc_printf(ncct, "%s:\n\t[%s`%s`%s]\n\tPeriod: %dms\n\tFeeds[%d]:\n",
1110               uuid_str, check->target, check->module, check->name,
1111               check->period, check->feeds ? check->feeds->size : 0);
1112     if(check->feeds && check->feeds->size) {
1113       for(fiter = noit_skiplist_getlist(check->feeds); fiter;
1114           noit_skiplist_next(check->feeds, &fiter)) {
1115         nc_printf(ncct, "\t\t%s\n", (const char *)fiter->data);
1116       }
1117     }
1118   }
1119   return 0;
1120 }
1121
1122 static void
1123 nc_printf_check_brief(noit_console_closure_t ncct,
1124                       noit_check_t *check) {
1125   char out[512];
1126   char uuid_str[37];
1127   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
1128   uuid_unparse_lower(check->checkid, uuid_str);
1129   nc_printf(ncct, "%s %s\n", uuid_str, out);
1130   if(check->stats.current.status)
1131     nc_printf(ncct, "\t%s\n", check->stats.current.status);
1132 }
1133
1134 char *
1135 noit_console_conf_check_opts(noit_console_closure_t ncct,
1136                              noit_console_state_stack_t *stack,
1137                              noit_console_state_t *dstate,
1138                              int argc, char **argv, int idx) {
1139   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1140   uuid_t key_id;
1141   int klen, i = 0;
1142   void *vcheck;
1143
1144   if(argc == 1) {
1145     if(!strncmp("new", argv[0], strlen(argv[0]))) {
1146       if(idx == i) return strdup("new");
1147       i++;
1148     }
1149     while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
1150                          &vcheck)) {
1151       noit_check_t *check = (noit_check_t *)vcheck;
1152       char out[512];
1153       char uuid_str[37];
1154       snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
1155       uuid_unparse_lower(check->checkid, uuid_str);
1156       if(!strncmp(out, argv[0], strlen(argv[0]))) {
1157         if(idx == i) return strdup(out);
1158         i++;
1159       }
1160       if(!strncmp(uuid_str, argv[0], strlen(argv[0]))) {
1161         if(idx == i) return strdup(uuid_str);
1162         i++;
1163       }
1164     }
1165   }
1166   if(argc == 2) {
1167     cmd_info_t *cmd;
1168     if(!strcmp("new", argv[0])) return NULL;
1169     cmd = noit_skiplist_find(&dstate->cmds, "attribute", NULL);
1170     if(!cmd) return NULL;
1171     return noit_console_opt_delegate(ncct, stack, cmd->dstate, argc-1, argv+1, idx);
1172   }
1173   return NULL;
1174 }
1175
1176 char *
1177 noit_console_check_opts(noit_console_closure_t ncct,
1178                         noit_console_state_stack_t *stack,
1179                         noit_console_state_t *dstate,
1180                         int argc, char **argv, int idx) {
1181   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1182   uuid_t key_id;
1183   int klen, i = 0;
1184
1185   if(argc == 1) {
1186     void *vcheck;
1187     while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
1188                          &vcheck)) {
1189       char out[512];
1190       char uuid_str[37];
1191       noit_check_t *check = (noit_check_t *)vcheck;
1192       snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
1193       uuid_unparse_lower(check->checkid, uuid_str);
1194       if(!strncmp(out, argv[0], strlen(argv[0]))) {
1195         if(idx == i) return strdup(out);
1196         i++;
1197       }
1198       if(!strncmp(uuid_str, argv[0], strlen(argv[0]))) {
1199         if(idx == i) return strdup(uuid_str);
1200         i++;
1201       }
1202     }
1203   }
1204   if(argc == 2) {
1205     return noit_console_opt_delegate(ncct, stack, dstate, argc-1, argv+1, idx);
1206   }
1207   return NULL;
1208 }
1209
1210 static int
1211 noit_console_show_checks(noit_console_closure_t ncct,
1212                          int argc, char **argv,
1213                          noit_console_state_t *dstate,
1214                          void *closure) {
1215   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1216   uuid_t key_id;
1217   int klen;
1218   void *vcheck;
1219
1220   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
1221                        &vcheck)) {
1222     nc_printf_check_brief(ncct, (noit_check_t *)vcheck);
1223   }
1224   return 0;
1225 }
1226
1227 static void
1228 register_console_check_commands() {
1229   noit_console_state_t *tl;
1230   cmd_info_t *showcmd;
1231
1232   tl = noit_console_state_initial();
1233   showcmd = noit_console_state_get_cmd(tl, "show");
1234   assert(showcmd && showcmd->dstate);
1235
1236   noit_console_state_add_cmd(showcmd->dstate,
1237     NCSCMD("checks", noit_console_show_checks, NULL, NULL, NULL));
1238
1239   noit_console_state_add_cmd(showcmd->dstate,
1240     NCSCMD("watches", noit_console_show_watchlist, NULL, NULL, NULL));
1241 }
1242
Note: See TracBrowser for help on using the browser.