root/src/noit_check_log.c

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

make these guys pass errors back, closes #73

  • 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                    int (*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       noit_check_transient_remove_feed(check, feed_name);
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 int
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   return noit_log(ls, &__now, __FILE__, __LINE__,
72                   "C\t%lu.%03lu\t%s\t%s\t%s\t%s\n",
73                   SECPART(&__now), MSECPART(&__now),
74                   uuid_str, check->target, check->module, check->name);
75 }
76
77 void
78 noit_check_log_check(noit_check_t *check) {
79   handle_extra_feeds(check, _noit_check_log_check);
80   if(!(check->flags & NP_TRANSIENT)) {
81     SETUP_LOG(check, return);
82     _noit_check_log_check(check_log, check);
83   }
84 }
85
86 static int
87 _noit_check_log_status(noit_log_stream_t ls,
88                        noit_check_t *check) {
89   stats_t *c;
90   char uuid_str[37];
91
92   uuid_unparse_lower(check->checkid, uuid_str);
93   c = &check->stats.current;
94   return noit_log(ls, &c->whence, __FILE__, __LINE__,
95                   "S\t%lu.%03lu\t%s\t%c\t%c\t%d\t%s\n",
96                   SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
97                   (char)c->state, (char)c->available, c->duration, c->status);
98 }
99 void
100 noit_check_log_status(noit_check_t *check) {
101   handle_extra_feeds(check, _noit_check_log_status);
102   if(!(check->flags & NP_TRANSIENT)) {
103     SETUP_LOG(status, return);
104     _noit_check_log_status(status_log, check);
105   }
106 }
107 static int
108 _noit_check_log_metrics(noit_log_stream_t ls, noit_check_t *check) {
109   int rv = 0;
110   int srv;
111   char uuid_str[37];
112   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
113   const char *key;
114   int klen;
115   metric_t *m;
116   stats_t *c;
117
118   uuid_unparse_lower(check->checkid, uuid_str);
119   c = &check->stats.current;
120   while(noit_hash_next(&c->metrics, &iter, &key, &klen, (void **)&m)) {
121     /* If we apply the filter set and it returns false, we don't log */
122     if(!noit_apply_filterset(check->filterset, check, m)) continue;
123     if(!ls->enabled) continue;
124
125     srv = 0;
126     if(!m->metric_value.s) { /* they are all null */
127       srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
128                      "M\t%lu.%03lu\t%s\t%s\t%c\t[[null]]\n",
129                      SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
130                      m->metric_name, m->metric_type);
131     }
132     else {
133       switch(m->metric_type) {
134         case METRIC_INT32:
135           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
136                          "M\t%lu.%03lu\t%s\t%s\t%c\t%d\n",
137                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
138                          m->metric_name, m->metric_type, *(m->metric_value.i));
139           break;
140         case METRIC_UINT32:
141           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
142                          "M\t%lu.%03lu\t%s\t%s\t%c\t%u\n",
143                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
144                          m->metric_name, m->metric_type, *(m->metric_value.I));
145           break;
146         case METRIC_INT64:
147           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
148                          "M\t%lu.%03lu\t%s\t%s\t%c\t%lld\n",
149                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
150                          m->metric_name, m->metric_type, *(m->metric_value.l));
151           break;
152         case METRIC_UINT64:
153           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
154                          "M\t%lu.%03lu\t%s\t%s\t%c\t%llu\n",
155                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
156                          m->metric_name, m->metric_type, *(m->metric_value.L));
157           break;
158         case METRIC_DOUBLE:
159           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
160                          "M\t%lu.%03lu\t%s\t%s\t%c\t%.12e\n",
161                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
162                          m->metric_name, m->metric_type, *(m->metric_value.n));
163           break;
164         case METRIC_STRING:
165           srv = noit_log(ls, &c->whence, __FILE__, __LINE__,
166                          "M\t%lu.%03lu\t%s\t%s\t%c\t%s\n",
167                          SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
168                          m->metric_name, m->metric_type, m->metric_value.s);
169           break;
170         default:
171           noitL(noit_error, "Unknown metric type '%c' 0x%x\n",
172                 m->metric_type, m->metric_type);
173       }
174     }
175     if(srv) rv = srv;
176   }
177   return rv;
178 }
179 void
180 noit_check_log_metrics(noit_check_t *check) {
181   handle_extra_feeds(check, _noit_check_log_metrics);
182   if(!(check->flags & NP_TRANSIENT)) {
183     SETUP_LOG(metrics, return);
184     _noit_check_log_metrics(metrics_log, check);
185   }
186 }
187
188 int
189 noit_stats_snprint_metric(char *b, int l, metric_t *m) {
190   int rv;
191   if(!m->metric_value.s) { /* they are all null */
192     rv = snprintf(b, l, "%s[%c] = [[null]]", m->metric_name, m->metric_type);
193   }
194   else {
195     switch(m->metric_type) {
196       case METRIC_INT32:
197         rv = snprintf(b, l, "%s[%c] = %d",
198                       m->metric_name, m->metric_type, *(m->metric_value.i));
199         break;
200       case METRIC_UINT32:
201         rv = snprintf(b, l, "%s[%c] = %u",
202                       m->metric_name, m->metric_type, *(m->metric_value.I));
203         break;
204       case METRIC_INT64:
205         rv = snprintf(b, l, "%s[%c] = %lld",
206                       m->metric_name, m->metric_type, *(m->metric_value.l));
207         break;
208       case METRIC_UINT64:
209         rv = snprintf(b, l, "%s[%c] = %llu",
210                       m->metric_name, m->metric_type, *(m->metric_value.L));
211         break;
212       case METRIC_DOUBLE:
213         rv = snprintf(b, l, "%s[%c] = %.12e",
214                       m->metric_name, m->metric_type, *(m->metric_value.n));
215         break;
216       case METRIC_STRING:
217         rv = snprintf(b, l, "%s[%c] = %s",
218                       m->metric_name, m->metric_type, m->metric_value.s);
219         break;
220       default:
221         rv = snprintf(b, l, "%s has unknown metric type 0%02x",
222                       m->metric_name, m->metric_type);
223     }
224   }
225   return rv;
226 }
Note: See TracBrowser for help on using the browser.