root/src/noit_check.c

Revision 5d557e747257b9a91c7870bfda3448f767b74f14, 25.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

set offset to zero if the check is NP_TRANSIENT, closes #75

  • 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 <stdlib.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <assert.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15
16 #include "utils/noit_log.h"
17 #include "utils/noit_hash.h"
18 #include "utils/noit_skiplist.h"
19 #include "noit_conf.h"
20 #include "noit_check.h"
21 #include "noit_module.h"
22 #include "noit_console.h"
23 #include "eventer/eventer.h"
24
25 /* 60 seconds of possible stutter */
26 #define MAX_INITIAL_STUTTER 60
27
28 static noit_hash_table polls = NOIT_HASH_EMPTY;
29 static noit_skiplist watchlist = { 0 };
30 static noit_skiplist polls_by_name = { 0 };
31 static u_int32_t __config_load_generation = 0;
32 struct uuid_dummy {
33   uuid_t foo;
34 };
35
36 static void register_console_check_commands();
37
38 #define UUID_SIZE sizeof(struct uuid_dummy)
39
40 const char *
41 noit_check_available_string(int16_t available) {
42   switch(available) {
43     case NP_AVAILABLE:    return "available";
44     case NP_UNAVAILABLE:  return "unavailable";
45     case NP_UNKNOWN:      return "unknown";
46   }
47   return "???";
48 }
49 const char *
50 noit_check_state_string(int16_t state) {
51   switch(state) {
52     case NP_GOOD:         return "good";
53     case NP_BAD:          return "bad";
54     case NP_UNKNOWN:      return "unknown";
55   }
56   return "???";
57 }
58 static int __check_name_compare(const void *a, const void *b) {
59   const noit_check_t *ac = a;
60   const noit_check_t *bc = b;
61   int rv;
62   if((rv = strcmp(ac->target, bc->target)) != 0) return rv;
63   if((rv = strcmp(ac->name, bc->name)) != 0) return rv;
64   return 0;
65 }
66 static int __watchlist_compare(const void *a, const void *b) {
67   const noit_check_t *ac = a;
68   const noit_check_t *bc = b;
69   int rv;
70   if((rv = memcmp(ac->checkid, bc->checkid, sizeof(ac->checkid))) != 0) return rv;
71   if(ac->period < bc->period) return -1;
72   if(ac->period == bc->period) return 0;
73   return 1;
74 }
75 int
76 noit_check_max_initial_stutter() {
77   int stutter;
78   if(!noit_conf_get_int(NULL, "/noit/checks/@max_initial_stutter", &stutter))
79     stutter = MAX_INITIAL_STUTTER;
80   return stutter * 1000;
81 }
82 void
83 noit_check_fake_last_check(noit_check_t *check,
84                            struct timeval *lc, struct timeval *_now) {
85   struct timeval now, period;
86   double r;
87   int offset = 0, max;
88
89   if(!(check->flags & NP_TRANSIENT)) {
90     r = drand48();
91     max = noit_check_max_initial_stutter();
92     offset = r * (MIN(max, check->period));
93   }
94   period.tv_sec = (check->period - offset) / 1000;
95   period.tv_usec = ((check->period - offset) % 1000) * 1000;
96   if(!_now) {
97     gettimeofday(&now, NULL);
98     _now = &now;
99   }
100   sub_timeval(*_now, period, lc);
101 }
102 void
103 noit_poller_process_checks(const char *xpath) {
104   int i, flags, cnt = 0;
105   noit_conf_section_t *sec;
106   __config_load_generation++;
107   sec = noit_conf_get_sections(NULL, xpath, &cnt);
108   for(i=0; i<cnt; i++) {
109     noit_check_t *existing_check;
110     char uuid_str[37];
111     char target[256];
112     char module[256];
113     char name[256];
114     char filterset[256];
115     char oncheck[1024] = "";
116     int no_period = 0;
117     int no_oncheck = 0;
118     int period = 0, timeout = 0;
119     noit_boolean disabled = noit_false, busted = noit_false;
120     uuid_t uuid, out_uuid;
121     noit_hash_table *options;
122
123 #define NEXT(...) noitL(noit_stderr, __VA_ARGS__); continue
124 #define MYATTR(type,a,...) noit_conf_get_##type(sec[i], "@" #a, __VA_ARGS__)
125 #define INHERIT(type,a,...) \
126   noit_conf_get_##type(sec[i], "ancestor-or-self::node()/@" #a, __VA_ARGS__)
127
128     if(!MYATTR(stringbuf, uuid, uuid_str, sizeof(uuid_str))) {
129       noitL(noit_stderr, "check %d has no uuid\n", i+1);
130       continue;
131     }
132
133     if(uuid_parse(uuid_str, uuid)) {
134       noitL(noit_stderr, "check uuid: '%s' is invalid\n", uuid_str);
135       continue;
136     }
137
138     if(!INHERIT(stringbuf, target, target, sizeof(target))) {
139       noitL(noit_stderr, "check uuid: '%s' has no target\n", uuid_str);
140       busted = noit_true;
141     }
142     if(!INHERIT(stringbuf, module, module, sizeof(module))) {
143       noitL(noit_stderr, "check uuid: '%s' has no module\n", uuid_str);
144       busted = noit_true;
145     }
146
147     if(!INHERIT(stringbuf, filterset, filterset, sizeof(filterset)))
148       filterset[0] = '\0';
149
150     if(!MYATTR(stringbuf, name, name, sizeof(name)))
151       strlcpy(name, module, sizeof(name));
152
153     if(!INHERIT(int, period, &period) || period == 0)
154       no_period = 1;
155
156     if(!INHERIT(stringbuf, oncheck, oncheck, sizeof(oncheck)) || !oncheck[0])
157       no_oncheck = 1;
158
159     if(no_period && no_oncheck) {
160       noitL(noit_stderr, "check uuid: '%s' has neither period nor oncheck\n",
161             uuid_str);
162       busted = noit_true;
163     }
164     if(!(no_period || no_oncheck)) {
165       noitL(noit_stderr, "check uuid: '%s' has oncheck and period.\n",
166             uuid_str);
167       busted = noit_true;
168     }
169     if(!INHERIT(int, timeout, &timeout)) {
170       noitL(noit_stderr, "check uuid: '%s' has no timeout\n", uuid_str);
171       busted = noit_true;
172     }
173     if(!no_period && timeout >= period) {
174       noitL(noit_stderr, "check uuid: '%s' timeout > period\n", uuid_str);
175       timeout = period/2;
176     }
177     options = noit_conf_get_hash(sec[i], "config");
178
179     INHERIT(boolean, disable, &disabled);
180     flags = 0;
181     if(busted) flags |= NP_UNCONFIG;
182     if(disabled) flags |= NP_DISABLED;
183
184     if(noit_hash_retrieve(&polls, (char *)uuid, UUID_SIZE,
185                           (void **)&existing_check)) {
186       /* Once set, we can never change it. */
187       assert(!existing_check->module || !existing_check->module[0] ||
188              !strcmp(existing_check->module, module));
189       /* Only set it if it is not yet set */
190       if(!existing_check->module || !existing_check->module[0]) {
191         if(existing_check->module) free(existing_check->module);
192         existing_check->module = strdup(module);
193       }
194       noit_check_update(existing_check, target, name, filterset, options,
195                            period, timeout, oncheck[0] ? oncheck : NULL,
196                            flags);
197       noitL(noit_debug, "reloaded uuid: %s\n", uuid_str);
198     }
199     else {
200       noit_poller_schedule(target, module, name, filterset, options,
201                            period, timeout, oncheck[0] ? oncheck : NULL,
202                            flags, uuid, out_uuid);
203       noitL(noit_debug, "loaded uuid: %s\n", uuid_str);
204     }
205
206     noit_hash_destroy(options, free, free);
207     free(options);
208   }
209   if(sec) free(sec);
210 }
211
212 int
213 noit_check_activate(noit_check_t *check) {
214   noit_module_t *mod;
215   mod = noit_module_lookup(check->module);
216   if(mod && mod->initiate_check && !NOIT_CHECK_LIVE(check)) {
217     if((check->flags & NP_DISABLED) == 0) {
218       mod->initiate_check(mod, check, 0, NULL);
219       return 1;
220     }
221     else
222       noitL(noit_debug, "Skipping %s`%s, disabled.\n",
223             check->target, check->name);
224   }
225   else {
226     noitL(noit_stderr, "Cannot find module '%s'\n", check->module);
227     check->flags |= NP_DISABLED;
228   }
229   return 0;
230 }
231
232 void
233 noit_poller_initiate() {
234   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
235   uuid_t key_id;
236   int klen;
237   noit_check_t *check;
238   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
239                        (void **)&check)) {
240     noit_check_activate(check);
241   }
242 }
243
244 void
245 noit_poller_flush_epoch(int oldest_allowed) {
246   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
247   uuid_t key_id;
248   int klen;
249   noit_check_t *check, *tofree = NULL;
250
251   /* Cleanup any previous causal map */
252   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
253                        (void **)&check)) {
254     /* We don't free the one we're looking at... we free it on the next
255      * pass.  This leaves out iterator in good shape.  We just need to
256      * remember to free it one last time outside the while loop, down...
257      */
258     if(tofree) {
259       noit_poller_deschedule(tofree->checkid);
260       tofree = NULL;
261     }
262     if(check->generation < oldest_allowed) {
263       tofree = check;
264     }
265   }
266   /* ... here */
267   if(tofree) noit_poller_deschedule(tofree->checkid);
268 }
269
270 void
271 noit_poller_make_causal_map() {
272   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
273   uuid_t key_id;
274   int klen;
275   noit_check_t *check, *parent;
276
277   /* Cleanup any previous causal map */
278   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
279                        (void **)&check)) {
280     dep_list_t *dep;
281     while((dep = check->causal_checks) != NULL) {
282       check->causal_checks = dep->next;
283       free(dep);
284     }
285   }
286
287   memset(&iter, 0, sizeof(iter));
288   /* Walk all checks and add check dependencies to their parents */
289   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
290                        (void **)&check)) {
291     if(check->oncheck) {
292       /* This service is causally triggered by another service */
293       char fullcheck[1024];
294       char *name = check->oncheck;
295       char *target = NULL;
296
297       noitL(noit_debug, "Searching for upstream trigger on %s\n", name);
298       if((target = strchr(check->oncheck, '`')) != NULL) {
299         strlcpy(fullcheck, check->oncheck, target - check->oncheck);
300         name = target + 1;
301         target = fullcheck;
302       }
303       else
304        target = check->target;
305
306       parent = noit_poller_lookup_by_name(target, name);
307       if(!parent) {
308         check->flags |= NP_DISABLED;
309         noitL(noit_stderr, "Disabling check %s`%s, can't find oncheck %s`%s\n",
310               check->target, check->name, target, name);
311       }
312       else {
313         dep_list_t *dep;
314         dep = malloc(sizeof(*dep));
315         dep->check = check;
316         dep->next = parent->causal_checks;
317         parent->causal_checks = dep;
318         noitL(noit_debug, "Causal map %s`%s --> %s`%s\n",
319               parent->target, parent->name, check->target, check->name);
320       }
321     }
322   }
323 }
324 void
325 noit_poller_reload(const char *xpath)
326 {
327   noit_poller_process_checks(xpath ? xpath : "/noit/checks//check");
328   if(!xpath) {
329     /* Full reload, we need to wipe old checks */
330     noit_poller_flush_epoch(__config_load_generation);
331   }
332   noit_poller_make_causal_map();
333   noit_poller_initiate();
334 }
335 void
336 noit_poller_init() {
337   noit_skiplist_init(&polls_by_name);
338   noit_skiplist_set_compare(&polls_by_name, __check_name_compare,
339                             __check_name_compare);
340   noit_skiplist_init(&watchlist);
341   noit_skiplist_set_compare(&watchlist, __watchlist_compare,
342                             __watchlist_compare);
343   register_console_check_commands();
344   noit_poller_reload(NULL);
345 }
346
347 noit_check_t *
348 noit_check_clone(uuid_t in) {
349   noit_check_t *checker, *new_check;
350   if(noit_hash_retrieve(&polls,
351                         (char *)in, UUID_SIZE,
352                         (void **)&checker) == 0) {
353     return NULL;
354   }
355   if(checker->oncheck) {
356     return NULL;
357   }
358   new_check = calloc(1, sizeof(*new_check));
359   memcpy(new_check, checker, sizeof(*new_check));
360   new_check->target = strdup(new_check->target);
361   new_check->module = strdup(new_check->module);
362   new_check->name = strdup(new_check->name);
363   new_check->filterset = strdup(new_check->filterset);
364   new_check->flags = 0;
365   new_check->fire_event = NULL;
366   memset(&new_check->last_fire_time, 0, sizeof(new_check->last_fire_time));
367   memset(&new_check->stats, 0, sizeof(new_check->stats));
368   new_check->closure = NULL;
369   new_check->config = calloc(1, sizeof(*new_check->config));
370   noit_hash_merge_as_dict(new_check->config, checker->config);
371   return new_check;
372 }
373
374 noit_check_t *
375 noit_check_watch(uuid_t in, int period) {
376   /* First look for a copy that is being watched */
377   noit_check_t n, *f;
378
379   uuid_copy(n.checkid, in);
380   n.period = period;
381
382   f = noit_skiplist_find(&watchlist, &n, NULL);
383   if(f) return f;
384   f = noit_check_clone(in);
385   if(!f) return NULL;
386   f->period = period;
387   f->timeout = period - 10;
388   f->flags |= NP_TRANSIENT;
389   noit_skiplist_insert(&watchlist, f);
390   return f;
391 }
392
393 noit_check_t *
394 noit_check_get_watch(uuid_t in, int period) {
395   noit_check_t n, *f;
396
397   uuid_copy(n.checkid, in);
398   n.period = period;
399
400   f = noit_skiplist_find(&watchlist, &n, NULL);
401   return f;
402 }
403
404 void
405 noit_check_transient_add_feed(noit_check_t *check, const char *feed) {
406   char *feedcopy;
407   if(!check->feeds) {
408     check->feeds = calloc(1, sizeof(*check->feeds));
409     noit_skiplist_init(check->feeds);
410     noit_skiplist_set_compare(check->feeds,
411                               (noit_skiplist_comparator_t)strcmp,
412                               (noit_skiplist_comparator_t)strcmp);
413   }
414   feedcopy = strdup(feed);
415   /* No error on failure -- it's already there */
416   if(noit_skiplist_insert(check->feeds, feedcopy) == NULL) free(feedcopy);
417   noitL(noit_debug, "check %s`%s @ %dms has %d feed(s): %s.\n",
418         check->target, check->name, check->period, check->feeds->size, feed);
419 }
420 void
421 noit_check_transient_remove_feed(noit_check_t *check, const char *feed) {
422   if(!check->feeds) return;
423   if(feed) {
424     noitL(noit_debug, "check %s`%s @ %dms removing 1 of %d feeds: %s.\n",
425           check->target, check->name, check->period, check->feeds->size, feed);
426     noit_skiplist_remove(check->feeds, feed, free);
427   }
428   if(check->feeds->size == 0) {
429     noit_skiplist_remove(&watchlist, check, NULL);
430     noit_skiplist_destroy(check->feeds, free);
431     free(check->feeds);
432     check->feeds = NULL;
433     if(check->flags & NP_TRANSIENT) {
434       noitL(noit_debug, "check %s`%s @ %dms has no more listeners.\n",
435             check->target, check->name, check->period);
436       check->flags |= NP_KILLED;
437     }
438   }
439 }
440
441 int
442 noit_check_update(noit_check_t *new_check,
443                   const char *target,
444                   const char *name,
445                   const char *filterset,
446                   noit_hash_table *config,
447                   u_int32_t period,
448                   u_int32_t timeout,
449                   const char *oncheck,
450                   int flags) {
451   int8_t family;
452   int rv;
453   int mask = NP_DISABLED | NP_UNCONFIG;
454   union {
455     struct in_addr addr4;
456     struct in6_addr addr6;
457   } a;
458
459
460   family = AF_INET;
461   rv = inet_pton(family, target, &a);
462   if(rv != 1) {
463     family = AF_INET6;
464     rv = inet_pton(family, target, &a);
465     if(rv != 1) {
466       noitL(noit_stderr, "Cannot translate '%s' to IP\n", target);
467       memset(&a, 0, sizeof(a));
468       flags |= (NP_UNCONFIG & NP_DISABLED);
469     }
470   }
471
472   new_check->generation = __config_load_generation;
473   new_check->target_family = family;
474   memcpy(&new_check->target_addr, &a, sizeof(a));
475   if(new_check->target) free(new_check->target);
476   new_check->target = strdup(target);
477   if(new_check->name) free(new_check->name);
478   new_check->name = name ? strdup(name): NULL;
479   if(new_check->filterset) free(new_check->filterset);
480   new_check->filterset = filterset ? strdup(filterset): NULL;
481
482   if(config != NULL) {
483     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
484     const char *k;
485     int klen;
486     void *data;
487     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
488     else new_check->config = calloc(1, sizeof(*new_check->config));
489     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
490       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
491     }
492   }
493   if(new_check->oncheck) free(new_check->oncheck);
494   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
495   new_check->period = period;
496   new_check->timeout = timeout;
497
498   /* Unset what could be set.. then set what should be set */
499   new_check->flags = (new_check->flags & ~mask) | flags;
500
501   if(!(new_check->flags & NP_TRANSIENT)) {
502     /* This remove could fail -- no big deal */
503     noit_skiplist_remove(&polls_by_name, new_check, NULL);
504
505     /* This insert could fail.. which means we have a conflict on
506      * target`name.  That should result in the check being disabled. */
507     if(!noit_skiplist_insert(&polls_by_name, new_check)) {
508       noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
509             new_check->target, new_check->name);
510       new_check->flags |= NP_DISABLED;
511     }
512   }
513   noit_check_log_check(new_check);
514   return 0;
515 }
516 int
517 noit_poller_schedule(const char *target,
518                      const char *module,
519                      const char *name,
520                      const char *filterset,
521                      noit_hash_table *config,
522                      u_int32_t period,
523                      u_int32_t timeout,
524                      const char *oncheck,
525                      int flags,
526                      uuid_t in,
527                      uuid_t out) {
528   noit_check_t *new_check;
529   new_check = calloc(1, sizeof(*new_check));
530   if(!new_check) return -1;
531
532   /* The module and the UUID can never be changed */
533   new_check->module = strdup(module);
534   if(uuid_is_null(in))
535     uuid_generate(new_check->checkid);
536   else
537     uuid_copy(new_check->checkid, in);
538
539   noit_check_update(new_check, target, name, filterset, config,
540                     period, timeout, oncheck, flags);
541   assert(noit_hash_store(&polls,
542                          (char *)new_check->checkid, UUID_SIZE,
543                          new_check));
544   uuid_copy(out, new_check->checkid);
545
546   return 0;
547 }
548
549 int
550 noit_poller_deschedule(uuid_t in) {
551   noit_check_t *checker;
552   noit_module_t *mod;
553   if(noit_hash_retrieve(&polls,
554                         (char *)in, UUID_SIZE,
555                         (void **)&checker) == 0) {
556     return -1;
557   }
558   if(checker->flags & NP_RUNNING) {
559     checker->flags |= NP_KILLED;
560     return 0;
561   }
562   checker->flags |= NP_KILLED;
563
564   noit_skiplist_remove(&polls_by_name, checker, NULL);
565   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
566
567   mod = noit_module_lookup(checker->module);
568   if(mod->cleanup) mod->cleanup(mod, checker);
569   if(checker->fire_event) {
570      eventer_remove(checker->fire_event);
571      free(checker->fire_event->closure);
572      eventer_free(checker->fire_event);
573      checker->fire_event = NULL;
574   }
575   if(checker->closure) free(checker->closure);
576   if(checker->target) free(checker->target);
577   if(checker->module) free(checker->module);
578   if(checker->name) free(checker->name);
579   if(checker->config) {
580     noit_hash_destroy(checker->config, free, free);
581     free(checker->config);
582     checker->config = NULL;
583   }
584   free(checker);
585   return 0;
586 }
587
588 noit_check_t *
589 noit_poller_lookup(uuid_t in) {
590   noit_check_t *check;
591   if(noit_hash_retrieve(&polls,
592                         (char *)in, UUID_SIZE,
593                         (void **)&check)) {
594     return check;
595   }
596   return NULL;
597 }
598 noit_check_t *
599 noit_poller_lookup_by_name(char *target, char *name) {
600   noit_check_t *check, *tmp_check;
601   tmp_check = calloc(1, sizeof(*tmp_check));
602   tmp_check->target = target;
603   tmp_check->name = name;
604   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
605   free(tmp_check);
606   return check;
607 }
608
609 void
610 noit_check_stats_clear(stats_t *s) {
611   memset(s, 0, sizeof(*s));
612   s->state = NP_UNKNOWN;
613   s->available = NP_UNKNOWN;
614 }
615 static void
616 __free_metric(void *vm) {
617   metric_t *m = vm;
618   free(m->metric_name);
619   if(m->metric_value.i) free(m->metric_value.i);
620   free(m);
621 }
622
623 void
624 __stats_add_metric(stats_t *newstate, metric_t *m) {
625   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
626                     m, NULL, __free_metric);
627 }
628
629 static size_t
630 noit_metric_sizes(metric_type_t type, void *value) {
631   switch(type) {
632     case METRIC_INT32:
633     case METRIC_UINT32:
634       return sizeof(int32_t);
635     case METRIC_INT64:
636     case METRIC_UINT64:
637       return sizeof(int64_t);
638     case METRIC_DOUBLE:
639       return sizeof(double);
640     case METRIC_STRING:
641       return strlen((char *)value) + 1;
642     case METRIC_GUESS:
643       break;
644   }
645   assert(type != type);
646   return 0;
647 }
648 static metric_type_t
649 noit_metric_guess_type(const char *s, void **replacement) {
650   char *copy, *cp, *trailer, *rpl;
651   int negative = 0, bigF = 0;
652   metric_type_t type = METRIC_STRING;
653
654   if(!s) return METRIC_GUESS;
655   copy = cp = strdup(s);
656
657   /* TRIM the string */
658   while(*cp && isspace(*cp)) cp++; /* ltrim */
659   s = cp; /* found a good starting point */
660   while(*cp) cp++; /* advance to \0 */
661   cp--; /* back up one */
662   while(cp > s && isspace(*cp)) *cp-- = '\0'; /* rtrim */
663
664   /* Find the first space */
665   cp = (char *)s;
666   while(*cp && !isspace(*cp)) cp++;
667   trailer = cp;
668   cp--; /* backup one */
669   if(cp > s && *cp == '%') *cp-- = '\0'; /* chop a last % is there is one */
670
671   while(*trailer && isspace(*trailer)) *trailer++ = '\0'; /* rtrim */
672
673   /* string was       '  -1.23e-01%  inodes used  ' */
674   /* copy is (~ = \0) '  -1.23e-01~  inodes used~~' */
675   /*                     ^           ^              */
676   /*                     s           trailer        */
677
678   /* So, the trailer must not contain numbers */
679   while(*trailer) { if(isdigit(*trailer)) goto notanumber; trailer++; }
680
681   /* And the 's' must be of the form:
682    *  0) may start with a sign [-+]?
683    *  1) [1-9][0-9]*
684    *  2) [0]?.[0-9]+
685    *  3) 0
686    *  4) [1-9][0-9]*.[0-9]+
687    *  5) all of the above ending with e[+-][0-9]+
688    */
689    rpl = (char *)s;
690    /* CASE 0 */
691    if(s[0] == '-' || s[0] == '+') {
692      if(s[0] == '-') negative = 1;
693      s++;
694    }
695
696    if(s[0] == '.') goto decimal; /* CASE 2 */
697    if(s[0] == '0') { /* CASE 2 & 3 */
698      s++;
699      if(!s[0]) goto scanint; /* CASE 3 */
700      if(s[0] == '.') goto decimal; /* CASE 2 */
701      goto notanumber;
702    }
703    if(s[0] >= '1' && s[0] <= '9') { /* CASE 1 & 4 */
704      s++;
705      while(isdigit(s[0])) s++; /* CASE 1 & 4 */
706      if(!s[0]) goto scanint; /* CASE 1 */
707      if(s[0] == '.') goto decimal; /* CASE 4 */
708      goto notanumber;
709    }
710    /* Not case 1,2,3,4 */
711    goto notanumber;
712
713   decimal:
714    s++;
715    if(!isdigit(s[0])) goto notanumber;
716    s++;
717    while(isdigit(s[0])) s++;
718    if(!s[0]) goto scandouble;
719    if(s[0] == 'e' || s[0] == 'E') goto exponent; /* CASE 5 */
720    goto notanumber;
721
722   exponent:
723    if(s[0] == 'E') bigF = 1; /* We want the caps variant */
724    s++;
725    if(s[0] != '-' && s[0] != '+') goto notanumber;
726    s++;
727    if(!isdigit(s[0])) goto notanumber;
728    s++;
729    while(isdigit(s[0])) s++;
730    if(!s[0]) goto scandouble;
731    goto notanumber;
732
733  scanint:
734    if(negative) {
735      int64_t *v;
736      v = calloc(1, sizeof(*v));
737      *v = strtoll(rpl, NULL, 10);
738      *replacement = v;
739      type = METRIC_INT64;
740      goto alldone;
741    }
742    else {
743      u_int64_t *v;
744      v = calloc(1, sizeof(*v));
745      *v = strtoull(rpl, NULL, 10);
746      *replacement = v;
747      type = METRIC_UINT64;
748      goto alldone;
749    }
750  scandouble:
751    {
752      double *v;
753      v = calloc(1, sizeof(*v));
754      *v = strtod(rpl, NULL);
755      *replacement = v;
756      type = METRIC_DOUBLE;
757      goto alldone;
758    }
759
760  alldone:
761  notanumber:
762   free(copy);
763   return type;
764 }
765 void
766 noit_stats_set_metric(stats_t *newstate, const char *name, metric_type_t type,
767                       void *value) {
768   metric_t *m;
769   void *replacement = NULL;
770   if(type == METRIC_GUESS)
771     type = noit_metric_guess_type((char *)value, &replacement);
772   if(type == METRIC_GUESS) return;
773
774   m = calloc(1, sizeof(*m));
775   m->metric_name = strdup(name);
776   m->metric_type = type;
777   if(replacement)
778     m->metric_value.vp = replacement;
779   else if(value) {
780     size_t len;
781     len = noit_metric_sizes(type, value);
782     m->metric_value.vp = calloc(1, len);
783     memcpy(m->metric_value.vp, value, len);
784   }
785   __stats_add_metric(newstate, m);
786 }
787
788 void
789 noit_check_set_stats(struct _noit_module *module,
790                      noit_check_t *check, stats_t *newstate) {
791   int report_change = 0;
792   dep_list_t *dep;
793   if(check->stats.previous.status)
794     free(check->stats.previous.status);
795   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
796   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
797   memcpy(&check->stats.current, newstate, sizeof(stats_t));
798   if(check->stats.current.status)
799     check->stats.current.status = strdup(check->stats.current.status);
800
801   /* check for state changes */
802   if(check->stats.current.available != NP_UNKNOWN &&
803      check->stats.previous.available != NP_UNKNOWN &&
804      check->stats.current.available != check->stats.previous.available)
805     report_change = 1;
806   if(check->stats.current.state != NP_UNKNOWN &&
807      check->stats.previous.state != NP_UNKNOWN &&
808      check->stats.current.state != check->stats.previous.state)
809     report_change = 1;
810
811   noitL(noit_debug, "%s`%s <- [%s]\n", check->target, check->name,
812         check->stats.current.status);
813   if(report_change) {
814     noitL(noit_debug, "%s`%s -> [%s:%s]\n",
815           check->target, check->name,
816           noit_check_available_string(check->stats.current.available),
817           noit_check_state_string(check->stats.current.state));
818   }
819
820   /* Write out our status */
821   noit_check_log_status(check);
822   /* Write out all metrics */
823   noit_check_log_metrics(check);
824
825   for(dep = check->causal_checks; dep; dep = dep->next) {
826     noit_module_t *mod;
827     mod = noit_module_lookup(dep->check->module);
828     assert(mod);
829     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
830           dep->check->target, dep->check->name,
831           check->target, check->name);
832     if((dep->check->flags & NP_DISABLED) == 0)
833       if(mod->initiate_check)
834         mod->initiate_check(mod, dep->check, 1, check);
835   }
836 }
837
838 static void
839 nc_printf_check_brief(noit_console_closure_t ncct,
840                       noit_check_t *check) {
841   char out[512];
842   char uuid_str[37];
843   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
844   uuid_unparse_lower(check->checkid, uuid_str);
845   nc_printf(ncct, "%s %s\n", uuid_str, out);
846   if(check->stats.current.status)
847     nc_printf(ncct, "\t%s\n", check->stats.current.status);
848 }
849
850 static int
851 noit_console_show_checks(noit_console_closure_t ncct,
852                          int argc, char **argv,
853                          noit_console_state_t *dstate,
854                          void *closure) {
855   struct timeval _now;
856   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
857   uuid_t key_id;
858   int klen;
859   noit_check_t *check;
860
861   gettimeofday(&_now, NULL);
862   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
863                        (void **)&check)) {
864     nc_printf_check_brief(ncct, check);
865   }
866   return 0;
867 }
868
869 static void
870 register_console_check_commands() {
871   noit_console_state_t *tl;
872   cmd_info_t *showcmd;
873
874   tl = noit_console_state_initial();
875   showcmd = noit_console_state_get_cmd(tl, "show");
876   assert(showcmd && showcmd->dstate);
877
878   noit_console_state_add_cmd(showcmd->dstate,
879     NCSCMD("checks", noit_console_show_checks, NULL, NULL));
880 }
881
Note: See TracBrowser for help on using the browser.