root/src/noit_check_log.c

Revision 5e653fe60dcc517ff76c1f3bb82cee19b3c51fa5, 5.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

fixes #30

  • 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  *
18  * CHECK:
19  *  'C' TIMESTAMP UUID TARGET MODULE NAME
20  *
21  * STATUS:
22  *  'S' TIMESTAMP UUID STATE AVAILABILITY DURATION STATUS_MESSAGE
23  *
24  * METRICS:
25  *  'M' TIMESTAMP UUID NAME TYPE VALUE
26  */
27
28 static noit_log_stream_t check_log = NULL;
29 static noit_log_stream_t status_log = NULL;
30 static noit_log_stream_t metrics_log = NULL;
31 #define SETUP_LOG(a) do { if(!a##_log) a##_log = noit_log_stream_find(#a); \
32                           if(!a##_log) return; } while(0)
33 #define SECPART(a) ((unsigned long)(a)->tv_sec)
34 #define MSECPART(a) ((unsigned long)((a)->tv_usec / 1000))
35 void
36 noit_check_log_check(noit_check_t *check) {
37   struct timeval __now;
38   char uuid_str[37];
39   SETUP_LOG(check);
40
41   gettimeofday(&__now, NULL);
42   uuid_unparse_lower(check->checkid, uuid_str);
43   noitL(check_log, "C\t%lu.%03lu\t%s\t%s\t%s\t%s\n",
44         SECPART(&__now), MSECPART(&__now), uuid_str,
45         check->target, check->module, check->name);
46 }
47 void
48 noit_check_log_status(noit_check_t *check) {
49   char uuid_str[37];
50   stats_t *c;
51   SETUP_LOG(status);
52
53   uuid_unparse_lower(check->checkid, uuid_str);
54   c = &check->stats.current;
55   noitL(status_log, "S\t%lu.%03lu\t%s\t%c\t%c\t%d\t%s\n",
56         SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
57         (char)c->state, (char)c->available, c->duration, c->status);
58 }
59 void
60 noit_check_log_metrics(noit_check_t *check) {
61   char uuid_str[37];
62   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
63   const char *key;
64   int klen;
65   metric_t *m;
66   stats_t *c;
67   SETUP_LOG(metrics);
68
69   uuid_unparse_lower(check->checkid, uuid_str);
70   c = &check->stats.current;
71   while(noit_hash_next(&c->metrics, &iter, &key, &klen, (void **)&m)) {
72     /* If we apply the filter set and it returns false, we don't log */
73     if(!noit_apply_filterset(check->filterset, check, m)) continue;
74
75     if(!m->metric_value.s) { /* they are all null */
76       noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t[[null]]\n",
77             SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
78             m->metric_name, m->metric_type);
79     }
80     else {
81       switch(m->metric_type) {
82         case METRIC_INT32:
83           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%d\n",
84                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
85                 m->metric_name, m->metric_type, *(m->metric_value.i));
86           break;
87         case METRIC_UINT32:
88           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%u\n",
89                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
90                 m->metric_name, m->metric_type, *(m->metric_value.I));
91           break;
92         case METRIC_INT64:
93           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%lld\n",
94                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
95                 m->metric_name, m->metric_type, *(m->metric_value.l));
96           break;
97         case METRIC_UINT64:
98           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%llu\n",
99                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
100                 m->metric_name, m->metric_type, *(m->metric_value.L));
101           break;
102         case METRIC_DOUBLE:
103           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%.12e\n",
104                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
105                 m->metric_name, m->metric_type, *(m->metric_value.n));
106           break;
107         case METRIC_STRING:
108           noitL(metrics_log, "M\t%lu.%03lu\t%s\t%s\t%c\t%s\n",
109                 SECPART(&c->whence), MSECPART(&c->whence), uuid_str,
110                 m->metric_name, m->metric_type, m->metric_value.s);
111           break;
112         default:
113           noitL(noit_error, "Unknown metric type '%c' 0x%x\n",
114                 m->metric_type, m->metric_type);
115       }
116     }
117   }
118 }
119 int
120 noit_stats_snprint_metric(char *b, int l, metric_t *m) {
121   int rv;
122   if(!m->metric_value.s) { /* they are all null */
123     rv = snprintf(b, l, "%s[%c] = [[null]]", m->metric_name, m->metric_type);
124   }
125   else {
126     switch(m->metric_type) {
127       case METRIC_INT32:
128         rv = snprintf(b, l, "%s[%c] = %d",
129                       m->metric_name, m->metric_type, *(m->metric_value.i));
130         break;
131       case METRIC_UINT32:
132         rv = snprintf(b, l, "%s[%c] = %u",
133                       m->metric_name, m->metric_type, *(m->metric_value.I));
134         break;
135       case METRIC_INT64:
136         rv = snprintf(b, l, "%s[%c] = %lld",
137                       m->metric_name, m->metric_type, *(m->metric_value.l));
138         break;
139       case METRIC_UINT64:
140         rv = snprintf(b, l, "%s[%c] = %llu",
141                       m->metric_name, m->metric_type, *(m->metric_value.L));
142         break;
143       case METRIC_DOUBLE:
144         rv = snprintf(b, l, "%s[%c] = %.12e",
145                       m->metric_name, m->metric_type, *(m->metric_value.n));
146         break;
147       case METRIC_STRING:
148         rv = snprintf(b, l, "%s[%c] = %s",
149                       m->metric_name, m->metric_type, m->metric_value.s);
150         break;
151       default:
152         rv = snprintf(b, l, "%s has unknown metric type 0%02x",
153                       m->metric_name, m->metric_type);
154     }
155   }
156   return rv;
157 }
Note: See TracBrowser for help on using the browser.