root/src/noit_check.c

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

more complete tab completion, refs #37

  • 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 int
348 noit_poller_check_count() {
349   return polls_by_name.size;
350 };
351
352 int
353 noit_poller_transient_check_count() {
354   return watchlist.size;
355 };
356
357 noit_check_t *
358 noit_check_clone(uuid_t in) {
359   noit_check_t *checker, *new_check;
360   if(noit_hash_retrieve(&polls,
361                         (char *)in, UUID_SIZE,
362                         (void **)&checker) == 0) {
363     return NULL;
364   }
365   if(checker->oncheck) {
366     return NULL;
367   }
368   new_check = calloc(1, sizeof(*new_check));
369   memcpy(new_check, checker, sizeof(*new_check));
370   new_check->target = strdup(new_check->target);
371   new_check->module = strdup(new_check->module);
372   new_check->name = strdup(new_check->name);
373   new_check->filterset = strdup(new_check->filterset);
374   new_check->flags = 0;
375   new_check->fire_event = NULL;
376   memset(&new_check->last_fire_time, 0, sizeof(new_check->last_fire_time));
377   memset(&new_check->stats, 0, sizeof(new_check->stats));
378   new_check->closure = NULL;
379   new_check->config = calloc(1, sizeof(*new_check->config));
380   noit_hash_merge_as_dict(new_check->config, checker->config);
381   return new_check;
382 }
383
384 noit_check_t *
385 noit_check_watch(uuid_t in, int period) {
386   /* First look for a copy that is being watched */
387   noit_check_t n, *f;
388
389   uuid_copy(n.checkid, in);
390   n.period = period;
391
392   f = noit_skiplist_find(&watchlist, &n, NULL);
393   if(f) return f;
394   f = noit_check_clone(in);
395   if(!f) return NULL;
396   f->period = period;
397   f->timeout = period - 10;
398   f->flags |= NP_TRANSIENT;
399   noit_skiplist_insert(&watchlist, f);
400   return f;
401 }
402
403 noit_check_t *
404 noit_check_get_watch(uuid_t in, int period) {
405   noit_check_t n, *f;
406
407   uuid_copy(n.checkid, in);
408   n.period = period;
409
410   f = noit_skiplist_find(&watchlist, &n, NULL);
411   return f;
412 }
413
414 void
415 noit_check_transient_add_feed(noit_check_t *check, const char *feed) {
416   char *feedcopy;
417   if(!check->feeds) {
418     check->feeds = calloc(1, sizeof(*check->feeds));
419     noit_skiplist_init(check->feeds);
420     noit_skiplist_set_compare(check->feeds,
421                               (noit_skiplist_comparator_t)strcmp,
422                               (noit_skiplist_comparator_t)strcmp);
423   }
424   feedcopy = strdup(feed);
425   /* No error on failure -- it's already there */
426   if(noit_skiplist_insert(check->feeds, feedcopy) == NULL) free(feedcopy);
427   noitL(noit_debug, "check %s`%s @ %dms has %d feed(s): %s.\n",
428         check->target, check->name, check->period, check->feeds->size, feed);
429 }
430 void
431 noit_check_transient_remove_feed(noit_check_t *check, const char *feed) {
432   if(!check->feeds) return;
433   if(feed) {
434     noitL(noit_debug, "check %s`%s @ %dms removing 1 of %d feeds: %s.\n",
435           check->target, check->name, check->period, check->feeds->size, feed);
436     noit_skiplist_remove(check->feeds, feed, free);
437   }
438   if(check->feeds->size == 0) {
439     noit_skiplist_remove(&watchlist, check, NULL);
440     noit_skiplist_destroy(check->feeds, free);
441     free(check->feeds);
442     check->feeds = NULL;
443     if(check->flags & NP_TRANSIENT) {
444       noitL(noit_debug, "check %s`%s @ %dms has no more listeners.\n",
445             check->target, check->name, check->period);
446       check->flags |= NP_KILLED;
447     }
448   }
449 }
450
451 int
452 noit_check_update(noit_check_t *new_check,
453                   const char *target,
454                   const char *name,
455                   const char *filterset,
456                   noit_hash_table *config,
457                   u_int32_t period,
458                   u_int32_t timeout,
459                   const char *oncheck,
460                   int flags) {
461   int8_t family;
462   int rv;
463   int mask = NP_DISABLED | NP_UNCONFIG;
464   union {
465     struct in_addr addr4;
466     struct in6_addr addr6;
467   } a;
468
469
470   family = AF_INET;
471   rv = inet_pton(family, target, &a);
472   if(rv != 1) {
473     family = AF_INET6;
474     rv = inet_pton(family, target, &a);
475     if(rv != 1) {
476       noitL(noit_stderr, "Cannot translate '%s' to IP\n", target);
477       memset(&a, 0, sizeof(a));
478       flags |= (NP_UNCONFIG & NP_DISABLED);
479     }
480   }
481
482   new_check->generation = __config_load_generation;
483   new_check->target_family = family;
484   memcpy(&new_check->target_addr, &a, sizeof(a));
485   if(new_check->target) free(new_check->target);
486   new_check->target = strdup(target);
487   if(new_check->name) free(new_check->name);
488   new_check->name = name ? strdup(name): NULL;
489   if(new_check->filterset) free(new_check->filterset);
490   new_check->filterset = filterset ? strdup(filterset): NULL;
491
492   if(config != NULL) {
493     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
494     const char *k;
495     int klen;
496     void *data;
497     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
498     else new_check->config = calloc(1, sizeof(*new_check->config));
499     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
500       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
501     }
502   }
503   if(new_check->oncheck) free(new_check->oncheck);
504   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
505   new_check->period = period;
506   new_check->timeout = timeout;
507
508   /* Unset what could be set.. then set what should be set */
509   new_check->flags = (new_check->flags & ~mask) | flags;
510
511   if(!(new_check->flags & NP_TRANSIENT)) {
512     /* This remove could fail -- no big deal */
513     noit_skiplist_remove(&polls_by_name, new_check, NULL);
514
515     /* This insert could fail.. which means we have a conflict on
516      * target`name.  That should result in the check being disabled. */
517     if(!noit_skiplist_insert(&polls_by_name, new_check)) {
518       noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
519             new_check->target, new_check->name);
520       new_check->flags |= NP_DISABLED;
521     }
522   }
523   noit_check_log_check(new_check);
524   return 0;
525 }
526 int
527 noit_poller_schedule(const char *target,
528                      const char *module,
529                      const char *name,
530                      const char *filterset,
531                      noit_hash_table *config,
532                      u_int32_t period,
533                      u_int32_t timeout,
534                      const char *oncheck,
535                      int flags,
536                      uuid_t in,
537                      uuid_t out) {
538   noit_check_t *new_check;
539   new_check = calloc(1, sizeof(*new_check));
540   if(!new_check) return -1;
541
542   /* The module and the UUID can never be changed */
543   new_check->module = strdup(module);
544   if(uuid_is_null(in))
545     uuid_generate(new_check->checkid);
546   else
547     uuid_copy(new_check->checkid, in);
548
549   noit_check_update(new_check, target, name, filterset, config,
550                     period, timeout, oncheck, flags);
551   assert(noit_hash_store(&polls,
552                          (char *)new_check->checkid, UUID_SIZE,
553                          new_check));
554   uuid_copy(out, new_check->checkid);
555
556   return 0;
557 }
558
559 int
560 noit_poller_deschedule(uuid_t in) {
561   noit_check_t *checker;
562   noit_module_t *mod;
563   if(noit_hash_retrieve(&polls,
564                         (char *)in, UUID_SIZE,
565                         (void **)&checker) == 0) {
566     return -1;
567   }
568   if(checker->flags & NP_RUNNING) {
569     checker->flags |= NP_KILLED;
570     return 0;
571   }
572   checker->flags |= NP_KILLED;
573
574   noit_skiplist_remove(&polls_by_name, checker, NULL);
575   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
576
577   mod = noit_module_lookup(checker->module);
578   if(mod->cleanup) mod->cleanup(mod, checker);
579   if(checker->fire_event) {
580      eventer_remove(checker->fire_event);
581      free(checker->fire_event->closure);
582      eventer_free(checker->fire_event);
583      checker->fire_event = NULL;
584   }
585   if(checker->closure) free(checker->closure);
586   if(checker->target) free(checker->target);
587   if(checker->module) free(checker->module);
588   if(checker->name) free(checker->name);
589   if(checker->config) {
590     noit_hash_destroy(checker->config, free, free);
591     free(checker->config);
592     checker->config = NULL;
593   }
594   free(checker);
595   return 0;
596 }
597
598 noit_check_t *
599 noit_poller_lookup(uuid_t in) {
600   noit_check_t *check;
601   if(noit_hash_retrieve(&polls,
602                         (char *)in, UUID_SIZE,
603                         (void **)&check)) {
604     return check;
605   }
606   return NULL;
607 }
608 noit_check_t *
609 noit_poller_lookup_by_name(char *target, char *name) {
610   noit_check_t *check, *tmp_check;
611   tmp_check = calloc(1, sizeof(*tmp_check));
612   tmp_check->target = target;
613   tmp_check->name = name;
614   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
615   free(tmp_check);
616   return check;
617 }
618
619 void
620 noit_check_stats_clear(stats_t *s) {
621   memset(s, 0, sizeof(*s));
622   s->state = NP_UNKNOWN;
623   s->available = NP_UNKNOWN;
624 }
625 static void
626 __free_metric(void *vm) {
627   metric_t *m = vm;
628   free(m->metric_name);
629   if(m->metric_value.i) free(m->metric_value.i);
630   free(m);
631 }
632
633 void
634 __stats_add_metric(stats_t *newstate, metric_t *m) {
635   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
636                     m, NULL, __free_metric);
637 }
638
639 static size_t
640 noit_metric_sizes(metric_type_t type, void *value) {
641   switch(type) {
642     case METRIC_INT32:
643     case METRIC_UINT32:
644       return sizeof(int32_t);
645     case METRIC_INT64:
646     case METRIC_UINT64:
647       return sizeof(int64_t);
648     case METRIC_DOUBLE:
649       return sizeof(double);
650     case METRIC_STRING:
651       return strlen((char *)value) + 1;
652     case METRIC_GUESS:
653       break;
654   }
655   assert(type != type);
656   return 0;
657 }
658 static metric_type_t
659 noit_metric_guess_type(const char *s, void **replacement) {
660   char *copy, *cp, *trailer, *rpl;
661   int negative = 0, bigF = 0;
662   metric_type_t type = METRIC_STRING;
663
664   if(!s) return METRIC_GUESS;
665   copy = cp = strdup(s);
666
667   /* TRIM the string */
668   while(*cp && isspace(*cp)) cp++; /* ltrim */
669   s = cp; /* found a good starting point */
670   while(*cp) cp++; /* advance to \0 */
671   cp--; /* back up one */
672   while(cp > s && isspace(*cp)) *cp-- = '\0'; /* rtrim */
673
674   /* Find the first space */
675   cp = (char *)s;
676   while(*cp && !isspace(*cp)) cp++;
677   trailer = cp;
678   cp--; /* backup one */
679   if(cp > s && *cp == '%') *cp-- = '\0'; /* chop a last % is there is one */
680
681   while(*trailer && isspace(*trailer)) *trailer++ = '\0'; /* rtrim */
682
683   /* string was       '  -1.23e-01%  inodes used  ' */
684   /* copy is (~ = \0) '  -1.23e-01~  inodes used~~' */
685   /*                     ^           ^              */
686   /*                     s           trailer        */
687
688   /* So, the trailer must not contain numbers */
689   while(*trailer) { if(isdigit(*trailer)) goto notanumber; trailer++; }
690
691   /* And the 's' must be of the form:
692    *  0) may start with a sign [-+]?
693    *  1) [1-9][0-9]*
694    *  2) [0]?.[0-9]+
695    *  3) 0
696    *  4) [1-9][0-9]*.[0-9]+
697    *  5) all of the above ending with e[+-][0-9]+
698    */
699    rpl = (char *)s;
700    /* CASE 0 */
701    if(s[0] == '-' || s[0] == '+') {
702      if(s[0] == '-') negative = 1;
703      s++;
704    }
705
706    if(s[0] == '.') goto decimal; /* CASE 2 */
707    if(s[0] == '0') { /* CASE 2 & 3 */
708      s++;
709      if(!s[0]) goto scanint; /* CASE 3 */
710      if(s[0] == '.') goto decimal; /* CASE 2 */
711      goto notanumber;
712    }
713    if(s[0] >= '1' && s[0] <= '9') { /* CASE 1 & 4 */
714      s++;
715      while(isdigit(s[0])) s++; /* CASE 1 & 4 */
716      if(!s[0]) goto scanint; /* CASE 1 */
717      if(s[0] == '.') goto decimal; /* CASE 4 */
718      goto notanumber;
719    }
720    /* Not case 1,2,3,4 */
721    goto notanumber;
722
723   decimal:
724    s++;
725    if(!isdigit(s[0])) goto notanumber;
726    s++;
727    while(isdigit(s[0])) s++;
728    if(!s[0]) goto scandouble;
729    if(s[0] == 'e' || s[0] == 'E') goto exponent; /* CASE 5 */
730    goto notanumber;
731
732   exponent:
733    if(s[0] == 'E') bigF = 1; /* We want the caps variant */
734    s++;
735    if(s[0] != '-' && s[0] != '+') goto notanumber;
736    s++;
737    if(!isdigit(s[0])) goto notanumber;
738    s++;
739    while(isdigit(s[0])) s++;
740    if(!s[0]) goto scandouble;
741    goto notanumber;
742
743  scanint:
744    if(negative) {
745      int64_t *v;
746      v = calloc(1, sizeof(*v));
747      *v = strtoll(rpl, NULL, 10);
748      *replacement = v;
749      type = METRIC_INT64;
750      goto alldone;
751    }
752    else {
753      u_int64_t *v;
754      v = calloc(1, sizeof(*v));
755      *v = strtoull(rpl, NULL, 10);
756      *replacement = v;
757      type = METRIC_UINT64;
758      goto alldone;
759    }
760  scandouble:
761    {
762      double *v;
763      v = calloc(1, sizeof(*v));
764      *v = strtod(rpl, NULL);
765      *replacement = v;
766      type = METRIC_DOUBLE;
767      goto alldone;
768    }
769
770  alldone:
771  notanumber:
772   free(copy);
773   return type;
774 }
775 void
776 noit_stats_set_metric(stats_t *newstate, const char *name, metric_type_t type,
777                       void *value) {
778   metric_t *m;
779   void *replacement = NULL;
780   if(type == METRIC_GUESS)
781     type = noit_metric_guess_type((char *)value, &replacement);
782   if(type == METRIC_GUESS) return;
783
784   m = calloc(1, sizeof(*m));
785   m->metric_name = strdup(name);
786   m->metric_type = type;
787   if(replacement)
788     m->metric_value.vp = replacement;
789   else if(value) {
790     size_t len;
791     len = noit_metric_sizes(type, value);
792     m->metric_value.vp = calloc(1, len);
793     memcpy(m->metric_value.vp, value, len);
794   }
795   __stats_add_metric(newstate, m);
796 }
797
798 void
799 noit_check_set_stats(struct _noit_module *module,
800                      noit_check_t *check, stats_t *newstate) {
801   int report_change = 0;
802   dep_list_t *dep;
803   if(check->stats.previous.status)
804     free(check->stats.previous.status);
805   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
806   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
807   memcpy(&check->stats.current, newstate, sizeof(stats_t));
808   if(check->stats.current.status)
809     check->stats.current.status = strdup(check->stats.current.status);
810
811   /* check for state changes */
812   if(check->stats.current.available != NP_UNKNOWN &&
813      check->stats.previous.available != NP_UNKNOWN &&
814      check->stats.current.available != check->stats.previous.available)
815     report_change = 1;
816   if(check->stats.current.state != NP_UNKNOWN &&
817      check->stats.previous.state != NP_UNKNOWN &&
818      check->stats.current.state != check->stats.previous.state)
819     report_change = 1;
820
821   noitL(noit_debug, "%s`%s <- [%s]\n", check->target, check->name,
822         check->stats.current.status);
823   if(report_change) {
824     noitL(noit_debug, "%s`%s -> [%s:%s]\n",
825           check->target, check->name,
826           noit_check_available_string(check->stats.current.available),
827           noit_check_state_string(check->stats.current.state));
828   }
829
830   /* Write out our status */
831   noit_check_log_status(check);
832   /* Write out all metrics */
833   noit_check_log_metrics(check);
834
835   for(dep = check->causal_checks; dep; dep = dep->next) {
836     noit_module_t *mod;
837     mod = noit_module_lookup(dep->check->module);
838     assert(mod);
839     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
840           dep->check->target, dep->check->name,
841           check->target, check->name);
842     if((dep->check->flags & NP_DISABLED) == 0)
843       if(mod->initiate_check)
844         mod->initiate_check(mod, dep->check, 1, check);
845   }
846 }
847
848 static void
849 nc_printf_check_brief(noit_console_closure_t ncct,
850                       noit_check_t *check) {
851   char out[512];
852   char uuid_str[37];
853   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
854   uuid_unparse_lower(check->checkid, uuid_str);
855   nc_printf(ncct, "%s %s\n", uuid_str, out);
856   if(check->stats.current.status)
857     nc_printf(ncct, "\t%s\n", check->stats.current.status);
858 }
859
860 char *
861 noit_console_conf_check_opts(noit_console_closure_t ncct,
862                              noit_console_state_stack_t *stack,
863                              noit_console_state_t *dstate,
864                              int argc, char **argv, int idx) {
865   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
866   uuid_t key_id;
867   int klen, i = 0;
868   noit_check_t *check;
869
870   if(argc == 1) {
871     if(!strncmp("new", argv[0], strlen(argv[0]))) {
872       if(idx == i) return strdup("new");
873       i++;
874     }
875     while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
876                          (void **)&check)) {
877       char out[512];
878       char uuid_str[37];
879       snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
880       uuid_unparse_lower(check->checkid, uuid_str);
881       if(!strncmp(out, argv[0], strlen(argv[0]))) {
882         if(idx == i) return strdup(out);
883         i++;
884       }
885       if(!strncmp(uuid_str, argv[0], strlen(argv[0]))) {
886         if(idx == i) return strdup(uuid_str);
887         i++;
888       }
889     }
890   }
891   if(argc == 2) {
892     cmd_info_t *cmd;
893     if(!strcmp("new", argv[0])) return NULL;
894     cmd = noit_skiplist_find(&dstate->cmds, "attribute", NULL);
895     if(!cmd) return NULL;
896     return noit_console_opt_delegate(ncct, stack, cmd->dstate, argc-1, argv+1, idx);
897   }
898   return NULL;
899 }
900
901 char *
902 noit_console_check_opts(noit_console_closure_t ncct,
903                         noit_console_state_stack_t *stack,
904                         noit_console_state_t *dstate,
905                         int argc, char **argv, int idx) {
906   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
907   uuid_t key_id;
908   int klen, i = 0;
909   noit_check_t *check;
910
911   if(argc == 1) {
912     while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
913                          (void **)&check)) {
914       char out[512];
915       char uuid_str[37];
916       snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
917       uuid_unparse_lower(check->checkid, uuid_str);
918       if(!strncmp(out, argv[0], strlen(argv[0]))) {
919         if(idx == i) return strdup(out);
920         i++;
921       }
922       if(!strncmp(uuid_str, argv[0], strlen(argv[0]))) {
923         if(idx == i) return strdup(uuid_str);
924         i++;
925       }
926     }
927   }
928   if(argc == 2) {
929     return noit_console_opt_delegate(ncct, stack, dstate, argc-1, argv+1, idx);
930   }
931   return NULL;
932 }
933
934 static int
935 noit_console_show_checks(noit_console_closure_t ncct,
936                          int argc, char **argv,
937                          noit_console_state_t *dstate,
938                          void *closure) {
939   struct timeval _now;
940   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
941   uuid_t key_id;
942   int klen;
943   noit_check_t *check;
944
945   gettimeofday(&_now, NULL);
946   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
947                        (void **)&check)) {
948     nc_printf_check_brief(ncct, check);
949   }
950   return 0;
951 }
952
953 static void
954 register_console_check_commands() {
955   noit_console_state_t *tl;
956   cmd_info_t *showcmd;
957
958   tl = noit_console_state_initial();
959   showcmd = noit_console_state_get_cmd(tl, "show");
960   assert(showcmd && showcmd->dstate);
961
962   noit_console_state_add_cmd(showcmd->dstate,
963     NCSCMD("checks", noit_console_show_checks, NULL, NULL, NULL));
964 }
965
Note: See TracBrowser for help on using the browser.