static int json_spit_log(u_int64_t idx, const struct timeval *whence, const char *log, size_t len, void *closure) { struct json_object *doc = (struct json_object *)closure; struct json_object *o, *wo; u_int64_t ms; o = json_object_new_object(); wo = json_object_new_int(idx); json_object_set_int_overflow(wo, json_overflow_uint64); json_object_set_uint64(wo, idx); json_object_object_add(o, "idx", wo); ms = whence->tv_sec; ms *= 1000ULL; ms += whence->tv_usec/1000; wo = json_object_new_int(ms); json_object_set_int_overflow(wo, json_overflow_uint64); json_object_set_uint64(wo, ms); json_object_object_add(o, "whence", wo); json_object_object_add(o, "line", json_object_new_string_len(log, len)); json_object_array_add(doc, o); return 0; }
static void json_spit_jobq(eventer_jobq_t *jobq, void *closure) { struct json_object *doc = closure; struct json_object *jo = json_object_new_object(); json_object_object_add(jo, "concurrency", json_object_new_int(jobq->concurrency)); json_object_object_add(jo, "desired_concurrency", json_object_new_int(jobq->desired_concurrency)); struct json_object *li = json_object_new_int(0); json_object_set_int_overflow(li, json_overflow_int64); json_object_set_int64(li, (long long int)jobq->total_jobs); json_object_object_add(jo, "total_jobs", li); json_object_object_add(jo, "backlog", json_object_new_int(jobq->backlog)); json_object_object_add(jo, "inflight", json_object_new_int(jobq->inflight)); li = json_object_new_int(0); json_object_set_int_overflow(li, json_overflow_int64); json_object_set_int64(li, (long long int)jobq->timeouts); json_object_object_add(jo, "timeouts", li); json_object_object_add(jo, "avg_wait_ms", json_object_new_double((double)jobq->avg_wait_ns/1000000.0)); json_object_object_add(jo, "avg_run_ms", json_object_new_double((double)jobq->avg_run_ns/1000000.0)); json_object_object_add(doc, jobq->queue_name, jo); }
static void json_spit_event(eventer_t e, void *closure) { struct json_object *doc = closure; struct json_object *eo, *ao = NULL; const char *cbname; char ip[INET6_ADDRSTRLEN]; union { struct sockaddr a; struct sockaddr_in ip4; struct sockaddr_in6 ip6; } addr; socklen_t addrlen; eo = json_object_new_object(); cbname = eventer_name_for_callback_e(e->callback, e); if(!cbname) cbname = "unknown"; json_object_object_add(eo, "callback", json_object_new_string(cbname)); if(e->mask & (EVENTER_READ|EVENTER_WRITE|EVENTER_EXCEPTION)) { json_object_object_add(eo, "fd", json_object_new_int(e->fd)); ip[0] = '\0'; addrlen = sizeof(addr); if(getsockname(e->fd, &addr.a, &addrlen) == 0) { switch(addr.a.sa_family) { case AF_INET: if(inet_ntop(AF_INET, &addr.ip4.sin_addr, ip, sizeof(ip))) { ao = json_object_new_object(); json_object_object_add(ao, "address", json_object_new_string(ip)); json_object_object_add(ao, "port", json_object_new_int(ntohs(addr.ip4.sin_port))); } break; case AF_INET6: if(inet_ntop(AF_INET, &addr.ip6.sin6_addr, ip, sizeof(ip))) { ao = json_object_new_object(); json_object_object_add(ao, "address", json_object_new_string(ip)); json_object_object_add(ao, "port", json_object_new_int(ntohs(addr.ip6.sin6_port))); } break; default: break; } if(ao) json_object_object_add(eo, "local", ao); } ao = NULL; ip[0] = '\0'; addrlen = sizeof(addr); if(getpeername(e->fd, &addr.a, &addrlen) == 0) { switch(addr.a.sa_family) { case AF_INET: if(inet_ntop(AF_INET, &addr.ip4.sin_addr, ip, sizeof(ip))) { ao = json_object_new_object(); json_object_object_add(ao, "address", json_object_new_string(ip)); json_object_object_add(ao, "port", json_object_new_int(ntohs(addr.ip4.sin_port))); } break; case AF_INET6: if(inet_ntop(AF_INET, &addr.ip6.sin6_addr, ip, sizeof(ip))) { ao = json_object_new_object(); json_object_object_add(ao, "address", json_object_new_string(ip)); json_object_object_add(ao, "port", json_object_new_int(ntohs(addr.ip6.sin6_port))); } break; default: break; } if(ao) json_object_object_add(eo, "remote", ao); } json_object_object_add(eo, "impl", json_object_new_string(e->opset->name)); json_object_object_add(eo, "mask", json_object_new_int(e->mask)); } else if(e->mask & EVENTER_TIMER) { struct json_object *wo; u_int64_t ms = e->whence.tv_sec; ms *= 1000ULL; ms += e->whence.tv_usec/1000; wo = json_object_new_int(ms); json_object_set_int_overflow(wo, json_overflow_uint64); json_object_set_uint64(wo, ms); json_object_object_add(eo, "whence", wo); } json_object_array_add(doc, eo); }
struct json_object * noit_check_state_as_json(noit_check_t *check, int full) { stats_t *c; char seq_str[64]; char id_str[UUID_STR_LEN+1]; struct json_object *j_last_run, *j_next_run; struct timeval *t; u_int64_t ms = 0; struct json_object *doc; uuid_unparse_lower(check->checkid, id_str); doc = json_object_new_object(); json_object_object_add(doc, "id", json_object_new_string(id_str)); json_object_object_add(doc, "name", json_object_new_string(check->name)); json_object_object_add(doc, "module", json_object_new_string(check->module)); json_object_object_add(doc, "target", json_object_new_string(check->target)); json_object_object_add(doc, "target_ip", json_object_new_string(check->target_ip)); json_object_object_add(doc, "filterset", json_object_new_string(check->filterset)); snprintf(seq_str, sizeof(seq_str), "%lld", (long long)check->config_seq); json_object_object_add(doc, "seq", json_object_new_string(seq_str)); json_object_object_add(doc, "period", json_object_new_int(check->period)); json_object_object_add(doc, "timeout", json_object_new_int(check->timeout)); json_object_object_add(doc, "flags", json_object_new_int(check->flags)); c = noit_check_get_stats_current(check); t = noit_check_stats_whence(c, NULL); j_last_run = json_object_new_int(ms); json_object_set_int_overflow(j_last_run, json_overflow_uint64); ms = t->tv_sec; ms *= 1000ULL; ms += t->tv_usec/1000; json_object_set_uint64(j_last_run, ms); json_object_object_add(doc, "last_run", j_last_run); t = check->fire_event ? &check->fire_event->whence : NULL; if(t) { j_next_run = json_object_new_int(ms); json_object_set_int_overflow(j_next_run, json_overflow_uint64); ms = t->tv_sec; ms *= 1000ULL; ms += t->tv_usec/1000; json_object_set_uint64(j_next_run, ms); json_object_object_add(doc, "next_run", j_next_run); } if(full) { mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; mtev_hash_table *configh; char timestr[20]; struct json_object *status, *metrics, *config; /* config */ config = json_object_new_object(); configh = check->config; while(mtev_hash_next(configh, &iter, &k, &klen, &data)) json_object_object_add(config, k, json_object_new_string(data)); json_object_object_add(doc, "config", config); /* status */ status = json_object_new_object(); switch(noit_check_stats_available(c, NULL)) { case NP_UNKNOWN: break; case NP_AVAILABLE: json_object_object_add(status, "available", json_object_new_boolean(1)); break; case NP_UNAVAILABLE: json_object_object_add(status, "available", json_object_new_boolean(0)); break; } switch(noit_check_stats_state(c, NULL)) { case NP_UNKNOWN: break; case NP_GOOD: json_object_object_add(status, "good", json_object_new_boolean(1)); break; case NP_BAD: json_object_object_add(status, "good", json_object_new_boolean(0)); break; } json_object_object_add(doc, "status", status); metrics = json_object_new_object(); t = noit_check_stats_whence(c, NULL); if(t->tv_sec) { json_object_object_add(metrics, "current", stats_to_json(c)); snprintf(timestr, sizeof(timestr), "%llu%03d", (unsigned long long int)t->tv_sec, (int)(t->tv_usec / 1000)); json_object_object_add(metrics, "current_timestamp", json_object_new_string(timestr)); } c = noit_check_get_stats_inprogress(check); t = noit_check_stats_whence(c, NULL); if(t->tv_sec) { json_object_object_add(metrics, "inprogress", stats_to_json(c)); snprintf(timestr, sizeof(timestr), "%llu%03d", (unsigned long long int)t->tv_sec, (int)(t->tv_usec / 1000)); json_object_object_add(metrics, "inprogress_timestamp", json_object_new_string(timestr)); } c = noit_check_get_stats_previous(check); t = noit_check_stats_whence(c, NULL); if(t->tv_sec) { json_object_object_add(metrics, "previous", stats_to_json(c)); snprintf(timestr, sizeof(timestr), "%llu%03d", (unsigned long long int)t->tv_sec, (int)(t->tv_usec / 1000)); json_object_object_add(metrics, "previous_timestamp", json_object_new_string(timestr)); } json_object_object_add(doc, "metrics", metrics); } return doc; }