root/src/noit_check_log.c

Revision 71803743714376fc12fb54b3506df6a4c17a8828, 6.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

oh my word... basic function, refs #49

  • 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 <uuid/uuid.h>
9 #include <netinet/in.h>
10
11 #include "noit_check.h"
12 #include "noit_filters.h"
13 #include "utils/noit_log.h"
14 #include "jlog/jlog.h"
15
16 /* Log format is tab delimited:
17  * NOIT CONFIG (implemented in noit_conf.c):
18  *  'n' TIMESTAMP strlen(xmlconfig) base64(gzip(xmlconfig))
19  *
20  * CHECK:
21  *  'C' TIMESTAMP UUID TARGET MODULE NAME
22  *
23  * STATUS:
24  *  'S' TIMESTAMP UUID STATE AVAILABILITY DURATION STATUS_MESSAGE
25  *
26  * METRICS:
27  *  'M' TIMESTAMP UUID NAME TYPE VALUE
28  */
29
30 static noit_log_stream_t check_log = NULL;
31 static noit_log_stream_t status_log = NULL;
32 static noit_log_stream_t metrics_log = NULL;
33 #define SECPART(a) ((unsigned long)(a)->tv_sec)
34 #define MSECPART(a) ((unsigned long)((a)->tv_usec / 1000))
35
36 static void
37 handle_extra_feeds(noit_check_t *check,
38                    void (*log_f)(noit_log_stream_t ls, noit_check_t *check)) {
39   noit_log_stream_t ls;
40   noit_skiplist_node *curr, *next;
41   const char *feed_name;
42
43   if(!check->feeds) return;
44   curr = next = noit_skiplist_getlist(check->feeds);
45   while(curr) {
46     /* We advance next here (before we try to use curr).
47      * We may need to remove the node we're looking at and that would
48      * disturb the iterator, so advance in advance. */
49     noit_skiplist_next(check->feeds, &next);
50     feed_name = (char *)curr->data;
51     ls = noit_log_stream_find(feed_name);
52     if(ls) log_f(ls, check);
53     else {
54       noit_skiplisti_remove(check->feeds, curr, free);
55     }
56     curr = next;
57   }
58   /* We're done... we may have destroyed the last feed.
59    * that combined with transience means we should kill the check */
60   noit_check_transient_remove_feed(check, NULL);
61 }
62
63 static void
64 _noit_check_log_check(noit_log_stream_t ls,
65                       noit_check_t *check) {
66   struct timeval __now;
67   char uuid_str[37];
68
69   uuid_unparse_lower(check->checkid, uuid_str);
70   gettimeofday(&__now, NULL);
71   noitL(ls, "C\t%lu.%03lu\t%s\t%s\t%s\t%s\n",
72         SECPART(&__now), MSECPART(&__now),
73         uuid_str, check->target, check->module, check->name);
74 }
75
76 void
77 noit_check_log_check(noit_check_t *check) {
78   handle_extra_feeds(check, _noit_check_log_check);
79   if(!(check->flags & NP_TRANSIENT)) {
80     SETUP_LOG(check, return);
81     _noit_check_log_check(check_log, check);
82   }
83 }
84
85 static void
86 _noit_check_log_status(noit_log_stream_t ls,
87                        noit_check_t *check) {
88   stats_t *c;
89   char uuid_str[37];
90
91   uuid_unparse_lower(check->checkid, uuid_str);
92   c = &check->stats.current;
93   noitL(ls, "S\t%lu.%03lu\t%s\t%c\t%c\t%d\t%s\n",
94         SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
95         (char)c->state, (char)c->available, c->duration, c->status);
96 }
97 void
98 noit_check_log_status(noit_check_t *check) {
99   handle_extra_feeds(check, _noit_check_log_status);
100   if(!(check->flags & NP_TRANSIENT)) {
101     SETUP_LOG(status, return);
102     _noit_check_log_status(status_log, check);
103   }
104 }
105 static void
106 _noit_check_log_metrics(noit_log_stream_t ls, noit_check_t *check) {
107   char uuid_str[37];
108   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
109   const char *key;
110   int klen;
111   metric_t *m;
112   stats_t *c;
113
114   uuid_unparse_lower(check->checkid, uuid_str);
115   c = &check->stats.current;
116   while(noit_hash_next(&c->metrics, &iter, &key, &klen, (void **)&m)) {
117     /* If we apply the filter set and it returns false, we don't log */
118     if(!noit_apply_filterset(check->filterset, check, m)) continue;
119
120     if(!m->metric_value.s) { /* they are all null */
121       noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t[[null]]\n",
122             SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
123             m->metric_name, m->metric_type);
124     }
125     else {
126       switch(m->metric_type) {
127         case METRIC_INT32:
128           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%d\n",
129                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
130                 m->metric_name, m->metric_type, *(m->metric_value.i));
131           break;
132         case METRIC_UINT32:
133           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%u\n",
134                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
135                 m->metric_name, m->metric_type, *(m->metric_value.I));
136           break;
137         case METRIC_INT64:
138           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%lld\n",
139                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
140                 m->metric_name, m->metric_type, *(m->metric_value.l));
141           break;
142         case METRIC_UINT64:
143           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%llu\n",
144                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
145                 m->metric_name, m->metric_type, *(m->metric_value.L));
146           break;
147         case METRIC_DOUBLE:
148           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%.12e\n",
149                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
150                 m->metric_name, m->metric_type, *(m->metric_value.n));
151           break;
152         case METRIC_STRING:
153           noitL(ls, "M\t%lu.%03lu\t%s\t%s\t%c\t%s\n",
154                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
155                 m->metric_name, m->metric_type, m->metric_value.s);
156           break;
157         default:
158           noitL(noit_error, "Unknown metric type '%c' 0x%x\n",
159                 m->metric_type, m->metric_type);
160       }
161     }
162   }
163 }
164 void
165 noit_check_log_metrics(noit_check_t *check) {
166   handle_extra_feeds(check, _noit_check_log_metrics);
167   if(!(check->flags & NP_TRANSIENT)) {
168     SETUP_LOG(metrics, return);
169     _noit_check_log_metrics(metrics_log, check);
170   }
171 }
172
173 int
174 noit_stats_snprint_metric(char *b, int l, metric_t *m) {
175   int rv;
176   if(!m->metric_value.s) { /* they are all null */
177     rv = snprintf(b, l, "%s[%c] = [[null]]", m->metric_name, m->metric_type);
178   }
179   else {
180     switch(m->metric_type) {
181       case METRIC_INT32:
182         rv = snprintf(b, l, "%s[%c] = %d",
183                       m->metric_name, m->metric_type, *(m->metric_value.i));
184         break;
185       case METRIC_UINT32:
186         rv = snprintf(b, l, "%s[%c] = %u",
187                       m->metric_name, m->metric_type, *(m->metric_value.I));
188         break;
189       case METRIC_INT64:
190         rv = snprintf(b, l, "%s[%c] = %lld",
191                       m->metric_name, m->metric_type, *(m->metric_value.l));
192         break;
193       case METRIC_UINT64:
194         rv = snprintf(b, l, "%s[%c] = %llu",
195                       m->metric_name, m->metric_type, *(m->metric_value.L));
196         break;
197       case METRIC_DOUBLE:
198         rv = snprintf(b, l, "%s[%c] = %.12e",
199                       m->metric_name, m->metric_type, *(m->metric_value.n));
200         break;
201       case METRIC_STRING:
202         rv = snprintf(b, l, "%s[%c] = %s",
203                       m->metric_name, m->metric_type, m->metric_value.s);
204         break;
205       default:
206         rv = snprintf(b, l, "%s has unknown metric type 0%02x",
207                       m->metric_name, m->metric_type);
208     }
209   }
210   return rv;
211 }
Note: See TracBrowser for help on using the browser.