root/src/modules/selfcheck.c

Revision 45068c689abe579e645627e4019472d38469e80e, 8.9 kB (checked in by Ihimu Ukpo <ihimu.ukpo@circonus.com>, 2 weeks ago)

Added operating system information to selfcheck output.

  • 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 #include <sys/utsname.h>
51
52 typedef struct {
53   noit_module_t *self;
54   noit_check_t *check;
55   mtev_hash_table attrs;
56   size_t logsize;
57   int timed_out;
58 } selfcheck_info_t;
59
60 struct threadq_crutch {
61   noit_check_t *check;
62 };
63
64 static mtev_log_stream_t nlerr = NULL;
65 static mtev_log_stream_t nldeb = NULL;
66
67 /*Function to return operating system information*/
68 static int selfcheck_os_version(char *buff, int len) {
69   struct utsname unameData;
70  
71   /*If error returned, print that operating system information is not available.*/
72   if (uname(&unameData)<0)
73   {
74     return snprintf(buff, len, "%s", "N/A");
75    
76   }
77  
78   /*Else, print operating system information.*/
79   else
80   {
81     return snprintf(buff, len, "%s %s %s", unameData.sysname,unameData.release,unameData.version);
82   }
83 }
84 static void selfcheck_cleanup(noit_module_t *self, noit_check_t *check) {
85   selfcheck_info_t *ci = check->closure;
86   if(ci) {
87     noit_check_release_attrs(&ci->attrs);
88     memset(ci, 0, sizeof(*ci));
89   }
90 }
91 static void jobq_thread_helper(eventer_jobq_t *jobq, void *closure) {
92   int s32;
93   char buffer[128];
94   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
95   s32 = jobq->concurrency;
96   if(s32 == 0) return; /* omit if no concurrency */
97   snprintf(buffer, sizeof(buffer), "%s_threads", jobq->queue_name);
98   noit_stats_set_metric(crutch->check, buffer, METRIC_INT32, &s32);
99 }
100 static int selfcheck_feed_details(jlog_feed_stats_t *s, void *closure) {
101   char buff[256];
102   uint64_t ms;
103   struct timeval now, diff;
104   struct threadq_crutch *crutch = (struct threadq_crutch *)closure;
105   gettimeofday(&now, NULL);
106
107   if(s->last_connection.tv_sec > 0) {
108     sub_timeval(now, s->last_connection, &diff);
109     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
110     snprintf(buff, sizeof(buff), "feed`%s`last_connection_ms", s->feed_name);
111     noit_stats_set_metric(crutch->check, buff, METRIC_UINT64, &ms);
112   }
113
114   if(s->last_checkpoint.tv_sec > 0) {
115     sub_timeval(now, s->last_checkpoint, &diff);
116     ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
117     snprintf(buff, sizeof(buff), "feed`%s`last_checkpoint_ms", s->feed_name);
118     noit_stats_set_metric(crutch->check, buff, METRIC_UINT64, &ms);
119   }
120   return 1;
121 }
122 static void selfcheck_log_results(noit_module_t *self, noit_check_t *check) {
123   char buff[128];
124   u_int64_t u64;
125   int64_t s64;
126   int32_t s32;
127   struct threadq_crutch crutch;
128   struct timeval now, duration, epoch, diff;
129   selfcheck_info_t *ci = check->closure;
130
131   crutch.check = check;
132
133   gettimeofday(&now, NULL);
134   sub_timeval(now, check->last_fire_time, &duration);
135   noit_stats_set_whence(check, &now);
136   noit_stats_set_duration(check, duration.tv_sec * 1000 + duration.tv_usec / 1000);
137   noit_stats_set_available(check, NP_UNAVAILABLE);
138   noit_stats_set_state(check, NP_BAD);
139   if(ci->timed_out) noit_stats_set_status(check, "timeout");
140   else {
141     noit_stats_set_available(check, NP_AVAILABLE);
142     noit_stats_set_state(check, NP_GOOD);
143     noit_stats_set_status(check, "ok");
144   }
145   /* Set all the metrics here */
146   s64 = (int64_t)ci->logsize;
147   noit_stats_set_metric(check, "feed_bytes", METRIC_INT64, &s64);
148   s32 = noit_poller_check_count();
149   noit_stats_set_metric(check, "check_cnt", METRIC_INT32, &s32);
150   s32 = noit_poller_transient_check_count();
151   noit_stats_set_metric(check, "transient_cnt", METRIC_INT32, &s32);
152   if(eventer_get_epoch(&epoch)) s64 = 0;
153   else {
154     sub_timeval(now, epoch, &diff);
155     s64 = diff.tv_sec;
156   }
157   noit_stats_set_metric(check, "uptime", METRIC_INT64, &s64);
158   eventer_jobq_process_each(jobq_thread_helper, &crutch);
159   noit_build_version(buff, sizeof(buff));
160   noit_stats_set_metric(check, "version", METRIC_STRING, buff);
161  
162   /*Clear buffer, store operating system in it*/
163   memset(buff,'\0',sizeof(buff));
164   selfcheck_os_version(buff, sizeof(buff));
165   noit_stats_set_metric(check, "OS version", METRIC_STRING, buff);
166  
167   u64 = noit_check_completion_count();
168   noit_stats_set_metric(check, "checks_run", METRIC_UINT64, &u64);
169   u64 = noit_check_metric_count();
170   noit_stats_set_metric(check, "metrics_collected", METRIC_UINT64, &u64);
171   /* feed pull info */
172   noit_jlog_foreach_feed_stats(selfcheck_feed_details, &crutch);
173
174   noit_check_set_stats(check);
175 }
176
177 #define FETCH_CONFIG_OR(key, str) do { \
178   if(!mtev_hash_retr_str(check->config, #key, strlen(#key), &key)) \
179     key = str; \
180 } while(0)
181
182 static int selfcheck_log_size(eventer_t e, int mask, void *closure,
183                               struct timeval *now) {
184   selfcheck_info_t *ci = closure;
185   noit_check_t *check = ci->check;
186   const char *feedname;
187   char feedname_buff[128];
188   mtev_log_stream_t feed;
189
190   if(mask & (EVENTER_READ | EVENTER_WRITE)) {
191     /* this case is impossible from the eventer.  It is called as
192      * such on the synchronous completion of the event.
193      */
194     selfcheck_log_results(ci->self, ci->check);
195     selfcheck_cleanup(ci->self, ci->check);
196     check->flags &= ~NP_RUNNING;
197     return 0;
198   }
199   switch(mask) {
200     case EVENTER_ASYNCH_WORK:
201       /* Check the length of the log */
202       FETCH_CONFIG_OR(feedname, "feed");
203       noit_check_interpolate(feedname_buff, sizeof(feedname_buff), feedname,
204                              &ci->attrs, check->config);
205       feed = mtev_log_stream_find(feedname_buff);
206       if(!feed) ci->logsize = -1;
207       else ci->logsize = mtev_log_stream_size(feed);
208       ci->timed_out = 0;
209       return 0;
210       break;
211     case EVENTER_ASYNCH_CLEANUP:
212       /* This sets us up for a completion call. */
213       e->mask = EVENTER_READ | EVENTER_WRITE;
214       break;
215     default:
216       abort();
217   }
218   return 0;
219 }
220
221 static int selfcheck_initiate(noit_module_t *self, noit_check_t *check,
222                               noit_check_t *cause) {
223   selfcheck_info_t *ci = check->closure;
224   struct timeval __now;
225
226   /* We cannot be running */
227   BAIL_ON_RUNNING_CHECK(check);
228   check->flags |= NP_RUNNING;
229
230   ci->self = self;
231   ci->check = check;
232
233   ci->timed_out = 1;
234   noit_check_make_attrs(check, &ci->attrs);
235   gettimeofday(&__now, NULL);
236   memcpy(&check->last_fire_time, &__now, sizeof(__now));
237
238   /* Register a handler for the worker */
239   noit_check_run_full_asynch(check, selfcheck_log_size);
240   return 0;
241 }
242
243 static int selfcheck_initiate_check(noit_module_t *self, noit_check_t *check,
244                                    int once, noit_check_t *cause) {
245   if(!check->closure) check->closure = calloc(1, sizeof(selfcheck_info_t));
246   INITIATE_CHECK(selfcheck_initiate, self, check, cause);
247   return 0;
248 }
249
250 static int selfcheck_onload(mtev_image_t *self) {
251   nlerr = mtev_log_stream_find("error/selfcheck");
252   nldeb = mtev_log_stream_find("debug/selfcheck");
253   if(!nlerr) nlerr = noit_stderr;
254   if(!nldeb) nldeb = noit_debug;
255
256   eventer_name_callback("selfcheck/selfcheck_log_size", selfcheck_log_size);
257   return 0;
258 }
259
260
261 #include "selfcheck.xmlh"
262 noit_module_t selfcheck = {
263   {
264     .magic = NOIT_MODULE_MAGIC,
265     .version = NOIT_MODULE_ABI_VERSION,
266     .name = "selfcheck",
267     .description = "noitd self-checker",
268     .xml_description = selfcheck_xml_description,
269     .onload = selfcheck_onload
270   },
271   NULL,
272   NULL,
273   selfcheck_initiate_check,
274   selfcheck_cleanup
275 };
276
Note: See TracBrowser for help on using the browser.