root/src/modules/selfcheck.c

Revision 304ec80b8cf842fc0abe5f9029790908b6455957, 8.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 1 month ago)

Convert to libmtev.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2009, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  * Copyright (c) 2015, Circonus, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <mtev_defines.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 <mtev_hash.h>
43
44 #include "noit_mtev_bridge.h"
45 #include "noit_module.h"
46 #include "noit_jlog_listener.h"
47 #include "noit_check.h"
48 #include "noit_check_tools.h"
49 #include "noit_version.h"
50
51 typedef struct {
52   noit_module_t *self;
53   noit_check_t *check;
54   mtev_hash_table attrs;
55   size_t logsize;
56   int timed_out;
57 } selfcheck_info_t;
58
59 struct threadq_crutch {
60   noit_check_t *check;
61 };
62
63 static mtev_log_stream_t nlerr = NULL;
64 static mtev_log_stream_t nldeb = NULL;
65
66 static void selfcheck_cleanup(noit_module_t *self, noit_check_t *check) {
67   selfcheck_info_t *ci = check->closure;
68   if(ci) {
69     noit_check_release_attrs(&ci->attrs);
70     memset(ci, 0, sizeof(*ci));
71   }
72 }
73 static void jobq_thread_helper(eventer_jobq_t *jobq, void *closure) {
74   int s32;
75   char buffer[128];
76   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
77   s32 = jobq->concurrency;
78   if(s32 == 0) return; /* omit if no concurrency */
79   snprintf(buffer, sizeof(buffer), "%s_threads", jobq->queue_name);
80   noit_stats_set_metric(crutch->check, &crutch->check->stats.inprogress,
81                         buffer, METRIC_INT32, &s32);
82 }
83 static int selfcheck_feed_details(jlog_feed_stats_t *s, void *closure) {
84   char buff[256];
85   uint64_t ms;
86   struct timeval now, diff;
87   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
88   gettimeofday(&now, NULL);
89   stats_t *tmpstats = &crutch->check->stats.inprogress;
90
91   if(s->last_connection.tv_sec > 0) {
92     sub_timeval(now, s->last_connection, &diff);
93     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
94     snprintf(buff, sizeof(buff), "feed`%s`last_connection_ms", s->feed_name);
95     noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms);
96   }
97
98   if(s->last_checkpoint.tv_sec > 0) {
99     sub_timeval(now, s->last_checkpoint, &diff);
100     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
101     snprintf(buff, sizeof(buff), "feed`%s`last_checkpoint_ms", s->feed_name);
102     noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms);
103   }
104   return 1;
105 }
106 static void selfcheck_log_results(noit_module_t *self, noit_check_t *check) {
107   char buff[128];
108   u_int64_t u64;
109   int64_t s64;
110   int32_t s32;
111   struct threadq_crutch crutch;
112   struct timeval duration, epoch, diff;
113   selfcheck_info_t *ci = check->closure;
114
115   crutch.check = check;
116   noit_check_stats_clear(check, &check->stats.inprogress);
117
118   gettimeofday(&check->stats.inprogress.whence, NULL);
119   sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration);
120   check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
121   check->stats.inprogress.available = NP_UNAVAILABLE;
122   check->stats.inprogress.state = NP_BAD;
123   if(ci->timed_out) check->stats.inprogress.status = "timeout";
124   else {
125     check->stats.inprogress.available = NP_AVAILABLE;
126     check->stats.inprogress.state = NP_GOOD;
127     check->stats.inprogress.status = "ok";
128   }
129   /* Set all the metrics here */
130   s64 = (int64_t)ci->logsize;
131   noit_stats_set_metric(check, &check->stats.inprogress, "feed_bytes", METRIC_INT64, &s64);
132   s32 = noit_poller_check_count();
133   noit_stats_set_metric(check, &check->stats.inprogress, "check_cnt", METRIC_INT32, &s32);
134   s32 = noit_poller_transient_check_count();
135   noit_stats_set_metric(check, &check->stats.inprogress, "transient_cnt", METRIC_INT32, &s32);
136   if(eventer_get_epoch(&epoch)) s64 = 0;
137   else {
138     sub_timeval(check->stats.inprogress.whence, epoch, &diff);
139     s64 = diff.tv_sec;
140   }
141   noit_stats_set_metric(check, &check->stats.inprogress, "uptime", METRIC_INT64, &s64);
142   eventer_jobq_process_each(jobq_thread_helper, &crutch);
143   noit_build_version(buff, sizeof(buff));
144   noit_stats_set_metric(check, &check->stats.inprogress, "version", METRIC_STRING, buff);
145   u64 = noit_check_completion_count();
146   noit_stats_set_metric(check, &check->stats.inprogress, "checks_run", METRIC_UINT64, &u64);
147   u64 = noit_check_metric_count();
148   noit_stats_set_metric(check, &check->stats.inprogress, "metrics_collected", METRIC_UINT64, &u64);
149   /* feed pull info */
150   noit_jlog_foreach_feed_stats(selfcheck_feed_details, &crutch);
151
152   noit_check_set_stats(check, &check->stats.inprogress);
153   noit_check_stats_clear(check, &check->stats.inprogress);
154 }
155
156 #define FETCH_CONFIG_OR(key, str) do { \
157   if(!mtev_hash_retr_str(check->config, #key, strlen(#key), &key)) \
158     key = str; \
159 } while(0)
160
161 static int selfcheck_log_size(eventer_t e, int mask, void *closure,
162                               struct timeval *now) {
163   selfcheck_info_t *ci = closure;
164   noit_check_t *check = ci->check;
165   const char *feedname;
166   char feedname_buff[128];
167   mtev_log_stream_t feed;
168
169   if(mask & (EVENTER_READ | EVENTER_WRITE)) {
170     /* this case is impossible from the eventer.  It is called as
171      * such on the synchronous completion of the event.
172      */
173     selfcheck_log_results(ci->self, ci->check);
174     selfcheck_cleanup(ci->self, ci->check);
175     check->flags &= ~NP_RUNNING;
176     return 0;
177   }
178   switch(mask) {
179     case EVENTER_ASYNCH_WORK:
180       /* Check the length of the log */
181       FETCH_CONFIG_OR(feedname, "feed");
182       noit_check_interpolate(feedname_buff, sizeof(feedname_buff), feedname,
183                              &ci->attrs, check->config);
184       feed = mtev_log_stream_find(feedname_buff);
185       if(!feed) ci->logsize = -1;
186       else ci->logsize = mtev_log_stream_size(feed);
187       ci->timed_out = 0;
188       return 0;
189       break;
190     case EVENTER_ASYNCH_CLEANUP:
191       /* This sets us up for a completion call. */
192       e->mask = EVENTER_READ | EVENTER_WRITE;
193       break;
194     default:
195       abort();
196   }
197   return 0;
198 }
199
200 static int selfcheck_initiate(noit_module_t *self, noit_check_t *check,
201                               noit_check_t *cause) {
202   selfcheck_info_t *ci = check->closure;
203   struct timeval __now;
204
205   /* We cannot be running */
206   BAIL_ON_RUNNING_CHECK(check);
207   check->flags |= NP_RUNNING;
208
209   ci->self = self;
210   ci->check = check;
211
212   ci->timed_out = 1;
213   noit_check_make_attrs(check, &ci->attrs);
214   gettimeofday(&__now, NULL);
215   memcpy(&check->last_fire_time, &__now, sizeof(__now));
216
217   /* Register a handler for the worker */
218   noit_check_run_full_asynch(check, selfcheck_log_size);
219   return 0;
220 }
221
222 static int selfcheck_initiate_check(noit_module_t *self, noit_check_t *check,
223                                    int once, noit_check_t *cause) {
224   if(!check->closure) check->closure = calloc(1, sizeof(selfcheck_info_t));
225   INITIATE_CHECK(selfcheck_initiate, self, check, cause);
226   return 0;
227 }
228
229 static int selfcheck_onload(mtev_image_t *self) {
230   nlerr = mtev_log_stream_find("error/selfcheck");
231   nldeb = mtev_log_stream_find("debug/selfcheck");
232   if(!nlerr) nlerr = noit_stderr;
233   if(!nldeb) nldeb = noit_debug;
234
235   eventer_name_callback("selfcheck/selfcheck_log_size", selfcheck_log_size);
236   return 0;
237 }
238
239 #include "selfcheck.xmlh"
240 noit_module_t selfcheck = {
241   {
242     .magic = NOIT_MODULE_MAGIC,
243     .version = NOIT_MODULE_ABI_VERSION,
244     .name = "selfcheck",
245     .description = "noitd self-checker",
246     .xml_description = selfcheck_xml_description,
247     .onload = selfcheck_onload
248   },
249   NULL,
250   NULL,
251   selfcheck_initiate_check,
252   selfcheck_cleanup
253 };
254
Note: See TracBrowser for help on using the browser.