root/src/modules/selfcheck.c

Revision 9c44e5a54c4ada69e5e61476fc5775b6e4d96c14, 8.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 2 months ago)

Make stats on checks use epoch memory reclamation.

  • 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, buffer, METRIC_INT32, &s32);
81 }
82 static int selfcheck_feed_details(jlog_feed_stats_t *s, void *closure) {
83   char buff[256];
84   uint64_t ms;
85   struct timeval now, diff;
86   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
87   gettimeofday(&now, NULL);
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, 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, 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 now, duration, epoch, diff;
111   selfcheck_info_t *ci = check->closure;
112
113   crutch.check = check;
114
115   gettimeofday(&now, NULL);
116   sub_timeval(now, check->last_fire_time, &duration);
117   noit_stats_set_whence(check, &now);
118   noit_stats_set_duration(check, duration.tv_sec * 1000 + duration.tv_usec / 1000);
119   noit_stats_set_available(check, NP_UNAVAILABLE);
120   noit_stats_set_state(check, NP_BAD);
121   if(ci->timed_out) noit_stats_set_status(check, "timeout");
122   else {
123     noit_stats_set_available(check, NP_AVAILABLE);
124     noit_stats_set_state(check, NP_GOOD);
125     noit_stats_set_status(check, "ok");
126   }
127   /* Set all the metrics here */
128   s64 = (int64_t)ci->logsize;
129   noit_stats_set_metric(check, "feed_bytes", METRIC_INT64, &s64);
130   s32 = noit_poller_check_count();
131   noit_stats_set_metric(check, "check_cnt", METRIC_INT32, &s32);
132   s32 = noit_poller_transient_check_count();
133   noit_stats_set_metric(check, "transient_cnt", METRIC_INT32, &s32);
134   if(eventer_get_epoch(&epoch)) s64 = 0;
135   else {
136     sub_timeval(now, epoch, &diff);
137     s64 = diff.tv_sec;
138   }
139   noit_stats_set_metric(check, "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, "version", METRIC_STRING, buff);
143   u64 = noit_check_completion_count();
144   noit_stats_set_metric(check, "checks_run", METRIC_UINT64, &u64);
145   u64 = noit_check_metric_count();
146   noit_stats_set_metric(check, "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);
151 }
152
153 #define FETCH_CONFIG_OR(key, str) do { \
154   if(!mtev_hash_retr_str(check->config, #key, strlen(#key), &key)) \
155     key = str; \
156 } while(0)
157
158 static int selfcheck_log_size(eventer_t e, int mask, void *closure,
159                               struct timeval *now) {
160   selfcheck_info_t *ci = closure;
161   noit_check_t *check = ci->check;
162   const char *feedname;
163   char feedname_buff[128];
164   mtev_log_stream_t feed;
165
166   if(mask & (EVENTER_READ | EVENTER_WRITE)) {
167     /* this case is impossible from the eventer.  It is called as
168      * such on the synchronous completion of the event.
169      */
170     selfcheck_log_results(ci->self, ci->check);
171     selfcheck_cleanup(ci->self, ci->check);
172     check->flags &= ~NP_RUNNING;
173     return 0;
174   }
175   switch(mask) {
176     case EVENTER_ASYNCH_WORK:
177       /* Check the length of the log */
178       FETCH_CONFIG_OR(feedname, "feed");
179       noit_check_interpolate(feedname_buff, sizeof(feedname_buff), feedname,
180                              &ci->attrs, check->config);
181       feed = mtev_log_stream_find(feedname_buff);
182       if(!feed) ci->logsize = -1;
183       else ci->logsize = mtev_log_stream_size(feed);
184       ci->timed_out = 0;
185       return 0;
186       break;
187     case EVENTER_ASYNCH_CLEANUP:
188       /* This sets us up for a completion call. */
189       e->mask = EVENTER_READ | EVENTER_WRITE;
190       break;
191     default:
192       abort();
193   }
194   return 0;
195 }
196
197 static int selfcheck_initiate(noit_module_t *self, noit_check_t *check,
198                               noit_check_t *cause) {
199   selfcheck_info_t *ci = check->closure;
200   struct timeval __now;
201
202   /* We cannot be running */
203   BAIL_ON_RUNNING_CHECK(check);
204   check->flags |= NP_RUNNING;
205
206   ci->self = self;
207   ci->check = check;
208
209   ci->timed_out = 1;
210   noit_check_make_attrs(check, &ci->attrs);
211   gettimeofday(&__now, NULL);
212   memcpy(&check->last_fire_time, &__now, sizeof(__now));
213
214   /* Register a handler for the worker */
215   noit_check_run_full_asynch(check, selfcheck_log_size);
216   return 0;
217 }
218
219 static int selfcheck_initiate_check(noit_module_t *self, noit_check_t *check,
220                                    int once, noit_check_t *cause) {
221   if(!check->closure) check->closure = calloc(1, sizeof(selfcheck_info_t));
222   INITIATE_CHECK(selfcheck_initiate, self, check, cause);
223   return 0;
224 }
225
226 static int selfcheck_onload(mtev_image_t *self) {
227   nlerr = mtev_log_stream_find("error/selfcheck");
228   nldeb = mtev_log_stream_find("debug/selfcheck");
229   if(!nlerr) nlerr = noit_stderr;
230   if(!nldeb) nldeb = noit_debug;
231
232   eventer_name_callback("selfcheck/selfcheck_log_size", selfcheck_log_size);
233   return 0;
234 }
235
236 #include "selfcheck.xmlh"
237 noit_module_t selfcheck = {
238   {
239     .magic = NOIT_MODULE_MAGIC,
240     .version = NOIT_MODULE_ABI_VERSION,
241     .name = "selfcheck",
242     .description = "noitd self-checker",
243     .xml_description = selfcheck_xml_description,
244     .onload = selfcheck_onload
245   },
246   NULL,
247   NULL,
248   selfcheck_initiate_check,
249   selfcheck_cleanup
250 };
251
Note: See TracBrowser for help on using the browser.