root/src/modules/selfcheck.c

Revision 4ed37cf09ac9817ced9312616da97a3a1e90c6b3, 8.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 1 year ago)

cleanup of modules, verbose structure setting

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2009, 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 #include "noit_version.h"
35
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <assert.h>
40 #include <math.h>
41
42 #include "noit_module.h"
43 #include "noit_check.h"
44 #include "noit_check_tools.h"
45 #include "noit_jlog_listener.h"
46 #include "utils/noit_log.h"
47 #include "utils/noit_hash.h"
48
49 typedef struct {
50   noit_module_t *self;
51   noit_check_t *check;
52   noit_hash_table attrs;
53   size_t logsize;
54   int timed_out;
55 } selfcheck_info_t;
56
57 struct threadq_crutch {
58   noit_check_t *check;
59 };
60
61 static noit_log_stream_t nlerr = NULL;
62 static noit_log_stream_t nldeb = NULL;
63
64 static void selfcheck_cleanup(noit_module_t *self, noit_check_t *check) {
65   selfcheck_info_t *ci = check->closure;
66   if(ci) {
67     noit_check_release_attrs(&ci->attrs);
68     memset(ci, 0, sizeof(*ci));
69   }
70 }
71 static void jobq_thread_helper(eventer_jobq_t *jobq, void *closure) {
72   int s32;
73   char buffer[128];
74   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
75   s32 = jobq->concurrency;
76   if(s32 == 0) return; /* omit if no concurrency */
77   snprintf(buffer, sizeof(buffer), "%s_threads", jobq->queue_name);
78   noit_stats_set_metric(crutch->check, &crutch->check->stats.inprogress,
79                         buffer, METRIC_INT32, &s32);
80 }
81 static int selfcheck_feed_details(jlog_feed_stats_t *s, void *closure) {
82   char buff[256];
83   uint64_t ms;
84   struct timeval now, diff;
85   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
86   gettimeofday(&now, NULL);
87   stats_t *tmpstats = &crutch->check->stats.inprogress;
88
89   if(s->last_connection.tv_sec > 0) {
90     sub_timeval(now, s->last_connection, &diff);
91     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
92     snprintf(buff, sizeof(buff), "feed`%s`last_connection_ms", s->feed_name);
93     noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms);
94   }
95
96   if(s->last_checkpoint.tv_sec > 0) {
97     sub_timeval(now, s->last_checkpoint, &diff);
98     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
99     snprintf(buff, sizeof(buff), "feed`%s`last_checkpoint_ms", s->feed_name);
100     noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms);
101   }
102   return 1;
103 }
104 static void selfcheck_log_results(noit_module_t *self, noit_check_t *check) {
105   char buff[128];
106   u_int64_t u64;
107   int64_t s64;
108   int32_t s32;
109   struct threadq_crutch crutch;
110   struct timeval duration, epoch, diff;
111   selfcheck_info_t *ci = check->closure;
112
113   crutch.check = check;
114   noit_check_stats_clear(check, &check->stats.inprogress);
115
116   gettimeofday(&check->stats.inprogress.whence, NULL);
117   sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration);
118   check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
119   check->stats.inprogress.available = NP_UNAVAILABLE;
120   check->stats.inprogress.state = NP_BAD;
121   if(ci->timed_out) check->stats.inprogress.status = "timeout";
122   else {
123     check->stats.inprogress.available = NP_AVAILABLE;
124     check->stats.inprogress.state = NP_GOOD;
125     check->stats.inprogress.status = "ok";
126   }
127   /* Set all the metrics here */
128   s64 = (int64_t)ci->logsize;
129   noit_stats_set_metric(check, &check->stats.inprogress, "feed_bytes", METRIC_INT64, &s64);
130   s32 = noit_poller_check_count();
131   noit_stats_set_metric(check, &check->stats.inprogress, "check_cnt", METRIC_INT32, &s32);
132   s32 = noit_poller_transient_check_count();
133   noit_stats_set_metric(check, &check->stats.inprogress, "transient_cnt", METRIC_INT32, &s32);
134   if(eventer_get_epoch(&epoch)) s64 = 0;
135   else {
136     sub_timeval(check->stats.inprogress.whence, epoch, &diff);
137     s64 = diff.tv_sec;
138   }
139   noit_stats_set_metric(check, &check->stats.inprogress, "uptime", METRIC_INT64, &s64);
140   eventer_jobq_process_each(jobq_thread_helper, &crutch);
141   noit_build_version(buff, sizeof(buff));
142   noit_stats_set_metric(check, &check->stats.inprogress, "version", METRIC_STRING, buff);
143   u64 = noit_check_completion_count();
144   noit_stats_set_metric(check, &check->stats.inprogress, "checks_run", METRIC_UINT64, &u64);
145   u64 = noit_check_metric_count();
146   noit_stats_set_metric(check, &check->stats.inprogress, "metrics_collected", METRIC_UINT64, &u64);
147   /* feed pull info */
148   noit_jlog_foreach_feed_stats(selfcheck_feed_details, &crutch);
149
150   noit_check_set_stats(check, &check->stats.inprogress);
151   noit_check_stats_clear(check, &check->stats.inprogress);
152 }
153
154 #define FETCH_CONFIG_OR(key, str) do { \
155   if(!noit_hash_retr_str(check->config, #key, strlen(#key), &key)) \
156     key = str; \
157 } while(0)
158
159 static int selfcheck_log_size(eventer_t e, int mask, void *closure,
160                               struct timeval *now) {
161   selfcheck_info_t *ci = closure;
162   noit_check_t *check = ci->check;
163   const char *feedname;
164   char feedname_buff[128];
165   noit_log_stream_t feed;
166
167   if(mask & (EVENTER_READ | EVENTER_WRITE)) {
168     /* this case is impossible from the eventer.  It is called as
169      * such on the synchronous completion of the event.
170      */
171     selfcheck_log_results(ci->self, ci->check);
172     selfcheck_cleanup(ci->self, ci->check);
173     check->flags &= ~NP_RUNNING;
174     return 0;
175   }
176   switch(mask) {
177     case EVENTER_ASYNCH_WORK:
178       /* Check the length of the log */
179       FETCH_CONFIG_OR(feedname, "feed");
180       noit_check_interpolate(feedname_buff, sizeof(feedname_buff), feedname,
181                              &ci->attrs, check->config);
182       feed = noit_log_stream_find(feedname_buff);
183       if(!feed) ci->logsize = -1;
184       else ci->logsize = noit_log_stream_size(feed);
185       ci->timed_out = 0;
186       return 0;
187       break;
188     case EVENTER_ASYNCH_CLEANUP:
189       /* This sets us up for a completion call. */
190       e->mask = EVENTER_READ | EVENTER_WRITE;
191       break;
192     default:
193       abort();
194   }
195   return 0;
196 }
197
198 static int selfcheck_initiate(noit_module_t *self, noit_check_t *check,
199                               noit_check_t *cause) {
200   selfcheck_info_t *ci = check->closure;
201   struct timeval __now;
202
203   /* We cannot be running */
204   BAIL_ON_RUNNING_CHECK(check);
205   check->flags |= NP_RUNNING;
206
207   ci->self = self;
208   ci->check = check;
209
210   ci->timed_out = 1;
211   noit_check_make_attrs(check, &ci->attrs);
212   gettimeofday(&__now, NULL);
213   memcpy(&check->last_fire_time, &__now, sizeof(__now));
214
215   /* Register a handler for the worker */
216   noit_check_run_full_asynch(check, selfcheck_log_size);
217   return 0;
218 }
219
220 static int selfcheck_initiate_check(noit_module_t *self, noit_check_t *check,
221                                    int once, noit_check_t *cause) {
222   if(!check->closure) check->closure = calloc(1, sizeof(selfcheck_info_t));
223   INITIATE_CHECK(selfcheck_initiate, self, check, cause);
224   return 0;
225 }
226
227 static int selfcheck_onload(noit_image_t *self) {
228   nlerr = noit_log_stream_find("error/selfcheck");
229   nldeb = noit_log_stream_find("debug/selfcheck");
230   if(!nlerr) nlerr = noit_stderr;
231   if(!nldeb) nldeb = noit_debug;
232
233   eventer_name_callback("selfcheck/selfcheck_log_size", selfcheck_log_size);
234   return 0;
235 }
236
237 #include "selfcheck.xmlh"
238 noit_module_t selfcheck = {
239   {
240     .magic = NOIT_MODULE_MAGIC,
241     .version = NOIT_MODULE_ABI_VERSION,
242     .name = "selfcheck",
243     .description = "noitd self-checker",
244     .xml_description = selfcheck_xml_description,
245     .onload = selfcheck_onload
246   },
247   NULL,
248   NULL,
249   selfcheck_initiate_check,
250   selfcheck_cleanup
251 };
252
Note: See TracBrowser for help on using the browser.