int main(int argc, char** argv) { // Parse cmd line args. int pointCount = 0; if(argc == 2) { pointCount = atoi(argv[1]); } else { printf("usage: quickhull <points>\n"); exit(1); } long* x1 = malloc(pointCount * sizeof(long)); long* y1 = malloc(pointCount * sizeof(long)); long* x2 = malloc(pointCount * sizeof(long)); long* y2 = malloc(pointCount * sizeof(long)); long* out = malloc(pointCount * sizeof(long)); for (int i = 0; i < pointCount; i++) { x1[i] = i; y1[i] = i; x2[i] = i; y2[i] = i; } // Timing setup struct timeval start, finish; struct rusage start_ru, finish_ru; gettimeofday( &start, NULL ); getrusage( RUSAGE_SELF, &start_ru ); // Do the deed. dotp(pointCount, x1, y1, x2, y2, out); // Print how long it took. gettimeofday( &finish, NULL ); getrusage( RUSAGE_SELF, &finish_ru ); // printf("depth = %d\n", depth); // printf("points on hull = %d\n", hull->length); sub_timeval( &finish, &start ); sub_timeval( &finish_ru.ru_utime, &start_ru.ru_utime ); sub_timeval( &finish_ru.ru_stime, &start_ru.ru_stime ); add_timeval( &finish_ru.ru_utime, &finish_ru.ru_stime ); printf("elapsedTimeMS = "); print_timeval( &finish ); putchar( '\n' ); printf("cpuTimeMS = "); print_timeval( &finish_ru.ru_utime); putchar( '\n' ); }
int main(int argc, char** argv) { // Parse cmd line args. int size = 0; if(argc == 2) { size = atoi(argv[1]); } else { printf("usage: filtermax <size>\n"); exit(1); } // Input vector. long* uvec = malloc(size * sizeof(long)); // for (int i = 0; i < size; i++) { uvec[i] = i; } // Output vector. long* out = malloc(size * sizeof(long)); // Timing setup struct timeval start, finish; struct rusage start_ru, finish_ru; gettimeofday( &start, NULL ); getrusage( RUSAGE_SELF, &start_ru ); // Do the deed. long out_len, out_max; filtermax(size, uvec, out, &out_len, &out_max); // Print how long it took. gettimeofday( &finish, NULL ); getrusage( RUSAGE_SELF, &finish_ru ); sub_timeval( &finish, &start ); sub_timeval( &finish_ru.ru_utime, &start_ru.ru_utime ); sub_timeval( &finish_ru.ru_stime, &start_ru.ru_stime ); add_timeval( &finish_ru.ru_utime, &finish_ru.ru_stime ); printf("elapsedTimeMS = "); print_timeval( &finish ); putchar( '\n' ); printf("cpuTimeMS = "); print_timeval( &finish_ru.ru_utime); putchar( '\n' ); }
static void ssh2_log_results(noit_module_t *self, noit_check_t *check) { struct timeval duration; ssh2_check_info_t *ci = check->closure; noit_check_stats_clear(check, &check->stats.inprogress); gettimeofday(&check->stats.inprogress.whence, NULL); sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration); check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; check->stats.inprogress.available = ci->available ? NP_AVAILABLE : NP_UNAVAILABLE; check->stats.inprogress.state = ci->fingerprint[0] ? NP_GOOD : NP_BAD; if(ci->error) check->stats.inprogress.status = ci->error; else if(ci->timed_out) check->stats.inprogress.status = "timeout"; else if(ci->fingerprint[0]) check->stats.inprogress.status = ci->fingerprint; else check->stats.inprogress.status = "internal error"; if(ci->fingerprint[0]) { u_int32_t mduration = check->stats.inprogress.duration; noit_stats_set_metric(check, &check->stats.inprogress, "duration", METRIC_UINT32, &mduration); noit_stats_set_metric(check, &check->stats.inprogress, "fingerprint", METRIC_STRING, ci->fingerprint); } noit_check_set_stats(check, &check->stats.inprogress); noit_check_stats_clear(check, &check->stats.inprogress); }
static void mysql_log_results(noit_module_t *self, noit_check_t *check) { struct timeval duration, now; mysql_check_info_t *ci = check->closure; gettimeofday(&now, NULL); sub_timeval(now, check->last_fire_time, &duration); noit_stats_set_whence(check, &now); noit_stats_set_duration(check, duration.tv_sec * 1000 + duration.tv_usec / 1000); noit_stats_set_available(check, NP_UNAVAILABLE); noit_stats_set_state(check, NP_BAD); if(ci->error) noit_stats_set_status(check, ci->error); else if(ci->timed_out) noit_stats_set_status(check, "timeout"); else if(ci->rv == 0) { noit_stats_set_available(check, NP_AVAILABLE); noit_stats_set_state(check, NP_GOOD); noit_stats_set_status(check, "no rows, ok"); } else { noit_stats_set_available(check, NP_AVAILABLE); noit_stats_set_state(check, NP_GOOD); noit_stats_set_status(check, "got rows, ok"); } if(ci->rv >= 0) noit_stats_set_metric(check, "row_count", METRIC_INT32, &ci->rv); if(ci->connect_duration) noit_stats_set_metric(check, "connect_duration", METRIC_DOUBLE, ci->connect_duration); if(ci->query_duration) noit_stats_set_metric(check, "query_duration", METRIC_DOUBLE, ci->query_duration); noit_check_set_stats(check); }
/* Smooth progress bar. * Doesn't update the bar more than once every 100ms, since this * might give flicker, and would be bad if we are displaying on * a slow link anyway. */ static void pretty_progress_bar(off_t progress, off_t total) { int len, n; double pc; static struct timeval last_call = {0}; struct timeval this_call; if (total < 0) return; if (progress < total && gettimeofday(&this_call, NULL) == 0) { struct timeval diff; sub_timeval(&diff, &this_call, &last_call); if (diff.tv_sec == 0 && diff.tv_usec < 100000) { return; } last_call = this_call; } if (progress == 0 || total == 0) { pc = 0; } else { pc = (double)progress / total; } len = pc * 30; printf("\rProgress: ["); for (n = 0; n<30; n++) { putchar((n<len-1)?'=': (n==(len-1)?'>':' ')); } printf("] %5.1f%% of %" NE_FMT_OFF_T " bytes", pc*100, total); fflush(stdout); }
static void postgres_log_results(noit_module_t *self, noit_check_t *check) { struct timeval duration; postgres_check_info_t *ci = check->closure; gettimeofday(&ci->current.whence, NULL); sub_timeval(ci->current.whence, check->last_fire_time, &duration); ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; ci->current.available = NP_UNAVAILABLE; ci->current.state = NP_BAD; if(ci->connect_duration) noit_stats_set_metric(&ci->current, "connect_duration", METRIC_DOUBLE, ci->connect_duration); if(ci->query_duration) noit_stats_set_metric(&ci->current, "query_duration", METRIC_DOUBLE, ci->query_duration); if(ci->error) ci->current.status = ci->error; else if(ci->timed_out) ci->current.status = "timeout"; else if(ci->rv == PGRES_COMMAND_OK) { ci->current.available = NP_AVAILABLE; ci->current.state = NP_GOOD; ci->current.status = "command ok"; } else if(ci->rv == PGRES_TUPLES_OK) { ci->current.available = NP_AVAILABLE; ci->current.state = NP_GOOD; ci->current.status = "tuples ok"; } else ci->current.status = "internal error"; noit_check_set_stats(self, check, &ci->current); }
static void dns_check_log_results(struct dns_check_info *ci) { struct timeval duration; double rtt; gettimeofday(&ci->current.whence, NULL); sub_timeval(ci->current.whence, ci->check->last_fire_time, &duration); rtt = duration.tv_sec * 1000.0 + duration.tv_usec / 1000.0; ci->current.duration = rtt; ci->current.state = (ci->error || ci->nrr == 0) ? NP_BAD : NP_GOOD; ci->current.available = ci->timed_out ? NP_UNAVAILABLE : NP_AVAILABLE; if(ci->error) { ci->current.status = strdup(ci->error); } else if(!ci->current.status) { char buff[48]; snprintf(buff, sizeof(buff), "%d %s", ci->nrr, ci->nrr == 1 ? "record" : "records"); ci->current.status = strdup(buff); } noit_stats_set_metric(ci->check, &ci->current, "rtt", METRIC_DOUBLE, ci->timed_out ? NULL : &rtt); noit_check_set_stats(ci->check, &ci->current); if(ci->error) free(ci->error); if(ci->current.status) free(ci->current.status); ci->error = NULL; memset(&ci->current, 0, sizeof(ci->current)); }
static int example_initiate(noit_module_t *self, noit_check_t *check, noit_check_t *cause) { struct example_check_info *ci = check->closure; const char *limit = "0"; struct timeval now, diff; BAIL_ON_RUNNING_CHECK(check); check->flags |= NP_RUNNING; mtev_hash_retrieve(check->config, "limit", strlen("limit"), (void **)&limit); ci->limit = atoi(limit); mtev_gettimeofday(&now, NULL); sub_timeval(now, check->last_fire_time, &diff); noit_stats_set_whence(check, &now); noit_stats_set_duration(check, diff.tv_sec * 1000 + diff.tv_usec / 1000); noit_stats_set_available(check, NP_AVAILABLE); noit_stats_set_status(check, "hello world"); if(ci->limit) { int value = (int)(lrand48() % ci->limit); noit_stats_set_metric(check, "random", METRIC_INT32, &value); noit_stats_set_state(check, NP_GOOD); } else { noit_stats_set_metric(check, "random", METRIC_INT32, NULL); noit_stats_set_state(check, NP_BAD); } noit_check_set_stats(check); check->flags &= ~NP_RUNNING; return 0; }
static void noit_console_spit_event(eventer_t e, void *c) { struct timeval now, diff; noit_console_closure_t ncct = c; char fdstr[12]; char wfn[42]; char funcptr[20]; const char *cname; cname = eventer_name_for_callback(e->callback); snprintf(fdstr, sizeof(fdstr), " fd: %d", e->fd); gettimeofday(&now, NULL); sub_timeval(e->whence, now, &diff); snprintf(wfn, sizeof(wfn), " fires: %lld.%06ds", (long long)diff.tv_sec, (int)diff.tv_usec); snprintf(funcptr, sizeof(funcptr), "%p", e->callback); nc_printf(ncct, " [%p]%s%s [%c%c%c%c] -> %s(%p)\n", e, e->mask & (EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION) ? fdstr : "", e->mask & (EVENTER_TIMER) ? wfn : "", e->mask & EVENTER_READ ? 'r' : '-', e->mask & EVENTER_WRITE ? 'w' : '-', e->mask & EVENTER_EXCEPTION ? 'e' : '-', e->mask & EVENTER_TIMER ? 't' : '-', cname ? cname : funcptr, e->closure); }
static void selfcheck_log_results(noit_module_t *self, noit_check_t *check) { char buff[128]; u_int64_t u64; int64_t s64; int32_t s32; struct threadq_crutch crutch; struct timeval duration, epoch, diff; selfcheck_info_t *ci = check->closure; crutch.check = check; noit_check_stats_clear(check, &check->stats.inprogress); gettimeofday(&check->stats.inprogress.whence, NULL); sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration); check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; check->stats.inprogress.available = NP_UNAVAILABLE; check->stats.inprogress.state = NP_BAD; if(ci->timed_out) check->stats.inprogress.status = "timeout"; else { check->stats.inprogress.available = NP_AVAILABLE; check->stats.inprogress.state = NP_GOOD; check->stats.inprogress.status = "ok"; } /* Set all the metrics here */ s64 = (int64_t)ci->logsize; noit_stats_set_metric(check, &check->stats.inprogress, "feed_bytes", METRIC_INT64, &s64); s32 = noit_poller_check_count(); noit_stats_set_metric(check, &check->stats.inprogress, "check_cnt", METRIC_INT32, &s32); s32 = noit_poller_transient_check_count(); noit_stats_set_metric(check, &check->stats.inprogress, "transient_cnt", METRIC_INT32, &s32); if(eventer_get_epoch(&epoch)) s64 = 0; else { sub_timeval(check->stats.inprogress.whence, epoch, &diff); s64 = diff.tv_sec; } noit_stats_set_metric(check, &check->stats.inprogress, "uptime", METRIC_INT64, &s64); eventer_jobq_process_each(jobq_thread_helper, &crutch); noit_build_version(buff, sizeof(buff)); noit_stats_set_metric(check, &check->stats.inprogress, "version", METRIC_STRING, buff); u64 = noit_check_completion_count(); noit_stats_set_metric(check, &check->stats.inprogress, "checks_run", METRIC_UINT64, &u64); /* feed pull info */ noit_jlog_foreach_feed_stats(selfcheck_feed_details, &crutch); noit_check_set_stats(check, &check->stats.inprogress); noit_check_stats_clear(check, &check->stats.inprogress); }
static void ping_icmp_log_results(noit_module_t *self, noit_check_t *check) { struct check_info *data; double avail = 0.0, min = MAXFLOAT, max = 0.0, avg = 0.0, cnt; int avail_needed = 100; const char *config_val = NULL; int i, points = 0; char human_buffer[256]; struct timeval duration; noit_check_stats_clear(check, &check->stats.inprogress); data = (struct check_info *)check->closure; for(i=0; i<data->expected_count; i++) { if(data->turnaround[i] >= 0.0) { points++; avg += data->turnaround[i]; if(data->turnaround[i] > max) max = data->turnaround[i]; if(data->turnaround[i] < min) min = data->turnaround[i]; } } cnt = data->expected_count; if(points == 0) { min = 0.0 / 0.0; max = 0.0 / 0.0; avg = 0.0 / 0.0; } else { avail = (float)points /cnt; avg /= (float)points; } if(noit_hash_retr_str(check->config, "avail_needed", strlen("avail_needed"), &config_val)) avail_needed = atoi(config_val); snprintf(human_buffer, sizeof(human_buffer), "cnt=%d,avail=%0.0f,min=%0.4f,max=%0.4f,avg=%0.4f", (int)cnt, 100.0*avail, min, max, avg); noitL(nldeb, "ping_icmp(%s) [%s]\n", check->target_ip, human_buffer); gettimeofday(&check->stats.inprogress.whence, NULL); sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration); check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; check->stats.inprogress.available = (avail > 0.0) ? NP_AVAILABLE : NP_UNAVAILABLE; check->stats.inprogress.state = (avail < ((float)avail_needed / 100.0)) ? NP_BAD : NP_GOOD; check->stats.inprogress.status = human_buffer; noit_stats_set_metric(check, &check->stats.inprogress, "count", METRIC_INT32, &data->expected_count); avail *= 100.0; noit_stats_set_metric(check, &check->stats.inprogress, "available", METRIC_DOUBLE, &avail); noit_stats_set_metric(check, &check->stats.inprogress, "minimum", METRIC_DOUBLE, avail > 0.0 ? &min : NULL); noit_stats_set_metric(check, &check->stats.inprogress, "maximum", METRIC_DOUBLE, avail > 0.0 ? &max : NULL); noit_stats_set_metric(check, &check->stats.inprogress, "average", METRIC_DOUBLE, avail > 0.0 ? &avg : NULL); noit_check_set_stats(check, &check->stats.inprogress); noit_check_stats_clear(check, &check->stats.inprogress); }
static int statsd_submit(noit_module_t *self, noit_check_t *check, noit_check_t *cause) { statsd_closure_t *ccl; struct timeval duration; statsd_mod_config_t *conf; conf = noit_module_get_userdata(self); if(!conf->primary_active) conf->check = NULL; if(0 == memcmp(conf->primary, check->checkid, sizeof(uuid_t))) { conf->check = check; if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) { conf->check = NULL; return 0; } } /* We are passive, so we don't do anything for transient checks */ if(check->flags & NP_TRANSIENT) return 0; if(!check->closure) { ccl = check->closure = calloc(1, sizeof(*ccl)); ccl->self = self; memset(&check->stats.inprogress, 0, sizeof(check->stats.inprogress)); } else { // Don't count the first run char human_buffer[256]; ccl = (statsd_closure_t*)check->closure; gettimeofday(&check->stats.inprogress.whence, NULL); sub_timeval(check->stats.inprogress.whence, check->last_fire_time, &duration); check->stats.inprogress.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; snprintf(human_buffer, sizeof(human_buffer), "dur=%d,run=%d,stats=%d", check->stats.inprogress.duration, check->generation, ccl->stats_count); noitL(nldeb, "statsd(%s) [%s]\n", check->target, human_buffer); // Not sure what to do here check->stats.inprogress.available = (ccl->stats_count > 0) ? NP_AVAILABLE : NP_UNAVAILABLE; check->stats.inprogress.state = (ccl->stats_count > 0) ? NP_GOOD : NP_BAD; check->stats.inprogress.status = human_buffer; if(check->last_fire_time.tv_sec) noit_check_passive_set_stats(check, &check->stats.inprogress); memcpy(&check->last_fire_time, &check->stats.inprogress.whence, sizeof(duration)); } ccl->stats_count = 0; noit_check_stats_clear(check, &check->stats.inprogress); return 0; }
int main(int argc, char *argv[]) { struct timeval s_time, e_time; gettimeofday(&s_time, NULL); init_rpc(); gettimeofday(&e_time, NULL); double diff = sub_timeval(&s_time, &e_time); printf("%f\n", diff); return 0; }
static int selfcheck_feed_details(jlog_feed_stats_t *s, void *closure) { char buff[256]; uint64_t ms; struct timeval now, diff; struct threadq_crutch *crutch = (struct threadq_crutch *)closure; gettimeofday(&now, NULL); stats_t *tmpstats = &crutch->check->stats.inprogress; if(s->last_connection.tv_sec > 0) { sub_timeval(now, s->last_connection, &diff); ms = diff.tv_sec * 1000 + diff.tv_usec / 1000; snprintf(buff, sizeof(buff), "feed`%s`last_connection_ms", s->feed_name); noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms); } if(s->last_checkpoint.tv_sec > 0) { sub_timeval(now, s->last_checkpoint, &diff); ms = diff.tv_sec * 1000 + diff.tv_usec / 1000; snprintf(buff, sizeof(buff), "feed`%s`last_checkpoint_ms", s->feed_name); noit_stats_set_metric(crutch->check, tmpstats, buff, METRIC_UINT64, &ms); } return 1; }
static void noit_rabbitmq_heartbeat(struct amqp_driver *dr) { struct timeval n, d; if(!dr->connection) return; mtev_gettimeofday(&n, NULL); sub_timeval(n, dr->last_hb, &d); if(d.tv_sec >= dr->heartbeat) { amqp_frame_t f; f.frame_type = AMQP_FRAME_HEARTBEAT; f.channel = 0; amqp_send_frame(dr->connection, &f); mtevL(mtev_debug, "amqp -> hearbeat\n"); memcpy(&dr->last_hb, &n, sizeof(n)); } }
static void nc_printf_dns_handle_brief(noit_console_closure_t ncct, dns_ctx_handle_t *h) { nc_printf(ncct, "== %s ==\n", h->hkey); nc_printf(ncct, " ns: %s\n refcnt: %d\n", h->ns, h->refcnt); nc_printf(ncct, " e: %d\n", h->e ? h->e->fd : -1); if(h->timeout) { struct timeval now, diff; gettimeofday(&now, NULL); sub_timeval(h->timeout->whence, now, &diff); nc_printf(ncct, " timeout: %f\n", diff.tv_sec + (double)diff.tv_usec/1000000.0); } }
static unsigned long last_tick_time() { static struct timeval lastchange = { 0, 0 }; static int lastcheck = 0; struct timeval now, diff; gettimeofday(&now, NULL); if(lastcheck != *lifeline) { lastcheck = *lifeline; memcpy(&lastchange, &now, sizeof(lastchange)); } if(lastchange.tv_sec == 0) return 0; sub_timeval(now, lastchange, &diff); return (unsigned long)diff.tv_sec; }
static double get_tcp_bw(struct sockaddr_in *addr, const int count) { double diff; struct timeval t1, t2; gettimeofday(&t1, NULL); tcp_connect(addr, count); gettimeofday(&t2, NULL); diff = sub_timeval(&t1, &t2); printf("diff time %f\n", diff); diff = ((double)count)/(diff*BUFSIZE); printf("bw %f MB\n", diff); return diff; }
static int httptrap_submit(noit_module_t *self, noit_check_t *check, noit_check_t *cause) { httptrap_closure_t *ccl; struct timeval duration; /* We are passive, so we don't do anything for transient checks */ if(check->flags & NP_TRANSIENT) return 0; mtev_httptrap_check_aynsch(self, check); if(!check->closure) { ccl = check->closure = (void *)calloc(1, sizeof(httptrap_closure_t)); memset(ccl, 0, sizeof(httptrap_closure_t)); ccl->self = self; } else { // Don't count the first run struct timeval now, *last; stats_t *inprogress; char human_buffer[256]; ccl = (httptrap_closure_t*)check->closure; gettimeofday(&now, NULL); sub_timeval(now, check->last_fire_time, &duration); noit_stats_set_whence(check, &now); noit_stats_set_duration(check, duration.tv_sec * 1000 + duration.tv_usec / 1000); snprintf(human_buffer, sizeof(human_buffer), "dur=%ld,run=%d,stats=%d", duration.tv_sec * 1000 + duration.tv_usec / 1000, check->generation, ccl->stats_count); mtevL(nldeb, "httptrap(%s) [%s]\n", check->target, human_buffer); // Not sure what to do here noit_stats_set_available(check, (ccl->stats_count > 0) ? NP_AVAILABLE : NP_UNAVAILABLE); noit_stats_set_state(check, (ccl->stats_count > 0) ? NP_GOOD : NP_BAD); noit_stats_set_status(check, human_buffer); if(check->last_fire_time.tv_sec) noit_check_passive_set_stats(check); inprogress = noit_check_get_stats_inprogress(check); last = noit_check_stats_whence(inprogress, NULL); memcpy(&check->last_fire_time, last, sizeof(duration)); } ccl->stats_count = 0; return 0; }
void noit_check_fake_last_check(noit_check_t *check, struct timeval *lc, struct timeval *_now) { struct timeval now, period; double r; int offset = 0, max; if(!(check->flags & NP_TRANSIENT)) { r = drand48(); max = noit_check_max_initial_stutter(); offset = r * (MIN(max, check->period)); } period.tv_sec = (check->period - offset) / 1000; period.tv_usec = ((check->period - offset) % 1000) * 1000; if(!_now) { gettimeofday(&now, NULL); _now = &now; } sub_timeval(*_now, period, lc); }
static int noit_console_show_check(mtev_console_closure_t ncct, int argc, char **argv, mtev_console_state_t *state, void *closure) { int i, cnt; mtev_conf_t_userdata_t *info; char xpath[1024]; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; mtev_conf_xml_xpath(NULL, &xpath_ctxt); if(argc > 1) { nc_printf(ncct, "requires zero or one arguments\n"); return -1; } info = mtev_console_userdata_get(ncct, MTEV_CONF_T_USERDATA); /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argc ? argv[0] : NULL)) { nc_printf(ncct, "could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found\n"); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } for(i=0; i<cnt; i++) { mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; uuid_t checkid; noit_check_t *check; mtev_hash_table *config; xmlNodePtr node, anode, mnode = NULL; char *uuid_conf; char *module, *value; node = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); continue; } nc_printf(ncct, "==== %s ====\n", uuid_conf); xmlFree(uuid_conf); #define MYATTR(a,n,b) _mtev_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(a,n,b) \ _mtev_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(a) do { \ anode = NULL; \ value = NULL; \ INHERIT(a, anode, value); \ nc_attr_show(ncct, #a, node, anode, value); \ if(value != NULL) free(value); \ } while(0) if(!INHERIT(module, mnode, module)) module = NULL; if(MYATTR(name, anode, value)) { nc_printf(ncct, " name: %s\n", value); free(value); } else nc_printf(ncct, " name: %s [from module]\n", module ? module : "[undef]"); nc_attr_show(ncct, "module", node, mnode, module); if(module) free(module); SHOW_ATTR(target); SHOW_ATTR(seq); SHOW_ATTR(resolve_rtype); SHOW_ATTR(period); SHOW_ATTR(timeout); SHOW_ATTR(oncheck); SHOW_ATTR(filterset); SHOW_ATTR(disable); /* Print out all the config settings */ config = mtev_conf_get_hash(node, "config"); while(mtev_hash_next(config, &iter, &k, &klen, &data)) { nc_printf(ncct, " config::%s: %s\n", k, (const char *)data); } mtev_hash_destroy(config, free, free); free(config); check = noit_poller_lookup(checkid); if(!check) { nc_printf(ncct, " ERROR: not in running system\n"); } else { int idx = 0; stats_t *c; struct timeval *whence; mtev_hash_table *metrics; nc_printf(ncct, " target_ip: %s\n", check->target_ip); nc_printf(ncct, " currently: %08x ", check->flags); if(NOIT_CHECK_RUNNING(check)) { nc_printf(ncct, "running"); idx++; } if(NOIT_CHECK_KILLED(check)) nc_printf(ncct, "%skilled", idx++?",":""); if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":""); if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":""); if(!idx) nc_printf(ncct, "idle"); nc_write(ncct, "\n", 1); if (check->fire_event != NULL) { struct timeval now, diff; mtev_gettimeofday(&now, NULL); sub_timeval(check->fire_event->whence, now, &diff); nc_printf(ncct, " next run: %0.3f seconds\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); } else { nc_printf(ncct, " next run: unscheduled\n"); } c = noit_check_get_stats_current(check); whence = noit_check_stats_whence(c, NULL); if(whence->tv_sec == 0) { nc_printf(ncct, " last run: never\n"); } else { const char *status; struct timeval now, *then, diff; mtev_gettimeofday(&now, NULL); then = noit_check_stats_whence(c, NULL); sub_timeval(now, *then, &diff); nc_printf(ncct, " last run: %0.3f seconds ago\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); nc_printf(ncct, " availability/state: %s/%s\n", noit_check_available_string(noit_check_stats_available(c, NULL)), noit_check_state_string(noit_check_stats_state(c, NULL))); status = noit_check_stats_status(c, NULL); nc_printf(ncct, " status: %s\n", status); nc_printf(ncct, " feeds: %d\n", check->feeds ? check->feeds->size : 0); } c = noit_check_get_stats_inprogress(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics) > 0) { nc_printf(ncct, " metrics (inprogress):\n"); nc_print_stat_metrics(ncct, check, c); } c = noit_check_get_stats_current(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics)) { nc_printf(ncct, " metrics (current):\n"); nc_print_stat_metrics(ncct, check, c); } c = noit_check_get_stats_previous(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics) > 0) { nc_printf(ncct, " metrics (previous):\n"); nc_print_stat_metrics(ncct, check, c); } } } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
static int postgres_drive_session(eventer_t e, int mask, void *closure, struct timeval *now) { const char *dsn, *sql; char sql_buff[8192]; char dsn_buff[512]; struct timeval t1, t2, diff; postgres_check_info_t *ci = closure; noit_check_t *check = ci->check; if(mask & (EVENTER_READ | EVENTER_WRITE)) { /* this case is impossible from the eventer. It is called as * such on the synchronous completion of the event. */ postgres_log_results(ci->self, ci->check); postgres_cleanup(ci->self, ci->check); check->flags &= ~NP_RUNNING; return 0; } switch(mask) { case EVENTER_ASYNCH_WORK: noit_check_stats_clear(&ci->current); ci->connect_duration = NULL; ci->query_duration = NULL; FETCH_CONFIG_OR(dsn, ""); noit_check_interpolate(dsn_buff, sizeof(dsn_buff), dsn, &ci->attrs, check->config); ci->conn = PQconnectdb(dsn_buff); if(!ci->conn) AVAIL_BAIL("PQconnectdb failed"); if(PQstatus(ci->conn) != CONNECTION_OK) AVAIL_BAIL(PQerrorMessage(ci->conn)); FETCH_CONFIG_OR(sql, ""); noit_check_interpolate(sql_buff, sizeof(sql_buff), sql, &ci->attrs, check->config); gettimeofday(&t1, NULL); sub_timeval(t1, check->last_fire_time, &diff); ci->connect_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; ci->connect_duration = &ci->connect_duration_d; ci->result = PQexec(ci->conn, sql_buff); gettimeofday(&t2, NULL); sub_timeval(t2, t1, &diff); ci->query_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; ci->query_duration = &ci->query_duration_d; if(!ci->result) AVAIL_BAIL("PQexec failed"); ci->rv = PQresultStatus(ci->result); switch(ci->rv) { case PGRES_TUPLES_OK: postgres_ingest_stats(ci); case PGRES_COMMAND_OK: break; default: AVAIL_BAIL(PQresultErrorMessage(ci->result)); } if(ci->result) { PGresult *result_swap = ci->result; ci->result = NULL; PQclear(result_swap); } if(ci->conn) { PGconn *conn_swap = ci->conn; ci->conn = NULL; PQfinish(conn_swap); } ci->timed_out = 0; return 0; break; case EVENTER_ASYNCH_CLEANUP: /* This sets us up for a completion call. */ e->mask = EVENTER_READ | EVENTER_WRITE; break; default: abort(); } return 0; }
int main(int argc, char** argv) { // Parse cmd line args. int pointCount = 0; char* outSVG = 0; if(argc == 2) { pointCount = atoi(argv[1]); outSVG = 0; } else if (argc == 3) { pointCount = atoi(argv[1]); outSVG = argv[2]; } else { printf("usage: quickhull <points> [out.svg]\n"); exit(1); } // Initialise the vector to hold the hull. Vector* hull = vector_new(pointCount); // Use random points for test data. Vector* points = vector_new(pointCount); double originX = 300; double originY = 300; long maxDist = 250; srandom(170); for (int i = 0; i < pointCount; i++) { double r = (random() % (maxDist * 2)) - maxDist; double a = M_PI * (random() % 360) / 360; vector_append ( points , originX + r * cos (a) , originY + r * sin (a)); } // Timing setup struct timeval start, finish; struct rusage start_ru, finish_ru; gettimeofday( &start, NULL ); getrusage( RUSAGE_SELF, &start_ru ); // Do the deed. int depth = quickHull (points, hull); // Print how long it took. gettimeofday( &finish, NULL ); getrusage( RUSAGE_SELF, &finish_ru ); // printf("depth = %d\n", depth); // printf("points on hull = %d\n", hull->length); sub_timeval( &finish, &start ); sub_timeval( &finish_ru.ru_utime, &start_ru.ru_utime ); sub_timeval( &finish_ru.ru_stime, &start_ru.ru_stime ); add_timeval( &finish_ru.ru_utime, &finish_ru.ru_stime ); printf("elapsedTimeMS = "); print_timeval( &finish ); putchar( '\n' ); printf("cpuTimeMS = "); print_timeval( &finish_ru.ru_utime); putchar( '\n' ); // Write output to file if requested. if(outSVG != 0) { FILE* file = fopen(outSVG, "w"); dumpSVG (file, points, hull); fclose (file); } }
static void external_log_results(noit_module_t *self, noit_check_t *check) { external_data_t *data; struct check_info *ci; stats_t current; struct timeval duration; noit_check_stats_clear(¤t); data = noit_module_get_userdata(self); ci = (struct check_info *)check->closure; noitL(data->nldeb, "external(%s) (timeout: %d, exit: %x)\n", check->target, ci->timedout, ci->exit_code); gettimeofday(¤t.whence, NULL); sub_timeval(current.whence, check->last_fire_time, &duration); current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; if(ci->timedout) { current.available = NP_UNAVAILABLE; current.state = NP_BAD; } else if(WEXITSTATUS(ci->exit_code) == 3) { current.available = NP_UNKNOWN; current.state = NP_UNKNOWN; } else { current.available = NP_AVAILABLE; current.state = (WEXITSTATUS(ci->exit_code) == 0) ? NP_GOOD : NP_BAD; } /* Hack the output into metrics */ if(ci->output && ci->matcher) { int rc, len, startoffset = 0; int ovector[30]; len = strlen(ci->output); noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len); while((rc = pcre_exec(ci->matcher, NULL, ci->output, len, startoffset, 0, ovector, sizeof(ovector)/sizeof(*ovector))) > 0) { char metric[128]; char value[128]; startoffset = ovector[1]; noitL(data->nldeb, "matched at offset %d\n", rc); if(pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc, "key", metric, sizeof(metric)) > 0 && pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc, "value", value, sizeof(value)) > 0) { /* We're able to extract something... */ noit_stats_set_metric(¤t, metric, METRIC_GUESS, value); } noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len); } noitL(data->nldeb, "match failed.... %d\n", rc); } current.status = ci->output; noit_check_set_stats(self, check, ¤t); /* If we didn't exit normally, or we core, or we have stderr to report... * provide a full report. */ if((WTERMSIG(ci->exit_code) != SIGQUIT && WTERMSIG(ci->exit_code) != 0) || WCOREDUMP(ci->exit_code) || (ci->error && *ci->error)) { char uuid_str[37]; uuid_unparse_lower(check->checkid, uuid_str); noitL(data->nlerr, "external/%s: (sig:%d%s) [%s]\n", uuid_str, WTERMSIG(ci->exit_code), WCOREDUMP(ci->exit_code)?", cored":"", ci->error ? ci->error : ""); } }
/* Handling of results */ static void noit_snmp_log_results(noit_module_t *self, noit_check_t *check, struct snmp_pdu *pdu) { struct check_info *info = check->closure; struct variable_list *vars; struct timeval duration; char buff[128]; stats_t current; int nresults = 0; noit_check_stats_clear(check, ¤t); if(pdu) for(vars = pdu->variables; vars; vars = vars->next_variable) nresults++; gettimeofday(¤t.whence, NULL); sub_timeval(current.whence, check->last_fire_time, &duration); current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; current.available = pdu ? NP_AVAILABLE : NP_UNAVAILABLE; current.state = (nresults == info->noids) ? NP_GOOD : NP_BAD; snprintf(buff, sizeof(buff), "%d/%d gets", nresults, info->noids); current.status = buff; /* We have no results over which to iterate. */ if(!pdu) { noit_check_set_stats(check, ¤t); return; } /* manipulate the information ourselves */ nresults = 0; for(vars = pdu->variables; vars; vars = vars->next_variable) { char *sp; int oid_idx; double float_conv; u_int64_t u64; int64_t i64; char *endptr; char varbuff[256]; /* find the oid to which this is the response */ oid_idx = nresults; /* our current idx is the most likely */ if(info->oids[oid_idx].oidlen != vars->name_length || memcmp(info->oids[oid_idx].oid, vars->name, vars->name_length * sizeof(oid))) { /* Not the most obvious guess */ for(oid_idx = info->noids - 1; oid_idx >= 0; oid_idx--) { if(info->oids[oid_idx].oidlen == vars->name_length && memcmp(info->oids[oid_idx].oid, vars->name, vars->name_length * sizeof(oid))) break; } } if(oid_idx < 0) { snprint_variable(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); noitL(nlerr, "Unexpected oid results to %s`%s`%s: %s\n", check->target, check->module, check->name, varbuff); nresults++; continue; } #define SETM(a,b) noit_stats_set_metric(check, ¤t, \ info->oids[oid_idx].confname, a, b) if(info->oids[oid_idx].type_should_override) { snprint_value(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); sp = strchr(varbuff, ' '); if(sp) sp++; noit_stats_set_metric_coerce(check, ¤t, info->oids[oid_idx].confname, info->oids[oid_idx].type_override, sp); } else { switch(vars->type) { case ASN_OCTET_STR: sp = malloc(1 + vars->val_len); memcpy(sp, vars->val.string, vars->val_len); sp[vars->val_len] = '\0'; SETM(METRIC_STRING, sp); free(sp); break; case ASN_INTEGER: case ASN_GAUGE: SETM(METRIC_INT32, vars->val.integer); break; case ASN_TIMETICKS: case ASN_COUNTER: SETM(METRIC_UINT32, vars->val.integer); break; case ASN_INTEGER64: printI64(varbuff, vars->val.counter64); i64 = strtoll(varbuff, &endptr, 10); SETM(METRIC_INT64, (varbuff == endptr) ? NULL : &i64); break; case ASN_COUNTER64: printU64(varbuff, vars->val.counter64); u64 = strtoull(varbuff, &endptr, 10); SETM(METRIC_UINT64, (varbuff == endptr) ? NULL : &u64); break; case ASN_FLOAT: if(vars->val.floatVal) float_conv = *(vars->val.floatVal); SETM(METRIC_DOUBLE, vars->val.floatVal ? &float_conv : NULL); break; case ASN_DOUBLE: SETM(METRIC_DOUBLE, vars->val.doubleVal); break; case SNMP_NOSUCHOBJECT: case SNMP_NOSUCHINSTANCE: SETM(METRIC_STRING, NULL); break; default: snprint_variable(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars); /* Advance passed the first space and use that unless there * is no space or we have no more string left. */ sp = strchr(varbuff, ' '); if(sp) sp++; SETM(METRIC_STRING, (sp && *sp) ? sp : NULL); } } nresults++; } noit_check_set_stats(check, ¤t); }
static int ping_icmp_handler(eventer_t e, int mask, void *closure, struct timeval *now, u_int8_t family) { noit_module_t *self = (noit_module_t *)closure; ping_icmp_data_t *ping_data; struct check_info *data; char packet[1500]; int packet_len = sizeof(packet); union { struct sockaddr_in in4; struct sockaddr_in6 in6; } from; unsigned int from_len; struct ping_payload *payload; if(family != AF_INET && family != AF_INET6) return EVENTER_READ; ping_data = noit_module_get_userdata(self); while(1) { struct ping_session_key k; int inlen; u_int8_t iphlen = 0; void *vcheck; noit_check_t *check; struct timeval tt, whence; from_len = sizeof(from); inlen = recvfrom(e->fd, packet, packet_len, 0, (struct sockaddr *)&from, &from_len); mtev_gettimeofday(now, NULL); /* set it, as we care about accuracy */ if(inlen < 0) { if(errno == EAGAIN || errno == EINTR) break; mtevLT(nldeb, now, "ping_icmp recvfrom: %s\n", strerror(errno)); break; } if(family == AF_INET) { struct icmp *icp4; iphlen = ((struct ip *)packet)->ip_hl << 2; if((inlen-iphlen) != sizeof(struct icmp)+PING_PAYLOAD_LEN) { mtevLT(nldeb, now, "ping_icmp bad size: %d+%d\n", iphlen, inlen-iphlen); continue; } icp4 = (struct icmp *)(packet + iphlen); payload = (struct ping_payload *)(icp4 + 1); if(icp4->icmp_type != ICMP_ECHOREPLY) { mtevLT(nldeb, now, "ping_icmp bad type: %d\n", icp4->icmp_type); continue; } if(icp4->icmp_id != (((vpsized_uint)self) & 0xffff)) { mtevLT(nldeb, now, "ping_icmp not sent from this instance (%d:%d) vs. %lu\n", icp4->icmp_id, ntohs(icp4->icmp_seq), (unsigned long)(((vpsized_uint)self) & 0xffff)); continue; } } else if(family == AF_INET6) { struct icmp6_hdr *icp6 = (struct icmp6_hdr *)packet; if((inlen) != sizeof(struct icmp6_hdr)+PING_PAYLOAD_LEN) { mtevLT(nldeb, now, "ping_icmp bad size: %d+%d\n", iphlen, inlen-iphlen); continue; } payload = (struct ping_payload *)(icp6+1); if(icp6->icmp6_type != ICMP6_ECHO_REPLY) { mtevLT(nldeb, now, "ping_icmp bad type: %d\n", icp6->icmp6_type); continue; } if(icp6->icmp6_id != (((vpsized_uint)self) & 0xffff)) { mtevLT(nldeb, now, "ping_icmp not sent from this instance (%d:%d) vs. %lu\n", icp6->icmp6_id, ntohs(icp6->icmp6_seq), (unsigned long)(((vpsized_uint)self) & 0xffff)); continue; } } else { /* This should be unreachable */ continue; } check = NULL; k.addr_of_check = payload->addr_of_check; uuid_copy(k.checkid, payload->checkid); if(mtev_hash_retrieve(ping_data->in_flight, (const char *)&k, sizeof(k), &vcheck)) check = vcheck; /* make sure this check is from this generation! */ if(!check) { char uuid_str[37]; uuid_unparse_lower(payload->checkid, uuid_str); mtevLT(nldeb, now, "ping_icmp response for unknown check '%s'\n", uuid_str); continue; } if((check->generation & 0xffff) != payload->generation) { mtevLT(nldeb, now, "ping_icmp response in generation gap\n"); continue; } data = (struct check_info *)check->closure; /* If there is no timeout_event, the check must have completed. * We have nothing to do. */ if(!data->timeout_event) continue; /* Sanity check the payload */ if(payload->check_no != data->check_no) continue; if(payload->check_pack_cnt != data->expected_count) continue; if(payload->check_pack_no >= data->expected_count) continue; whence.tv_sec = payload->tv_sec; whence.tv_usec = payload->tv_usec; sub_timeval(*now, whence, &tt); data->turnaround[payload->check_pack_no] = (float)tt.tv_sec + (float)tt.tv_usec / 1000000.0; if(ping_icmp_is_complete(self, check)) { ping_icmp_log_results(self, check); eventer_remove(data->timeout_event); free(data->timeout_event->closure); eventer_free(data->timeout_event); data->timeout_event = NULL; check->flags &= ~NP_RUNNING; k.addr_of_check = (vpsized_uint)check ^ random_num; uuid_copy(k.checkid, check->checkid); mtev_hash_delete(ping_data->in_flight, (const char *)&k, sizeof(k), free, NULL); } } return EVENTER_READ; }
static int noit_console_show_check(noit_console_closure_t ncct, int argc, char **argv, noit_console_state_t *state, void *closure) { int i, cnt; noit_conf_t_userdata_t *info; char xpath[1024]; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; noit_conf_xml_xpath(NULL, &xpath_ctxt); if(argc > 1) { nc_printf(ncct, "requires zero or one arguments\n"); return -1; } info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argc ? argv[0] : NULL)) { nc_printf(ncct, "could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found\n"); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } for(i=0; i<cnt; i++) { noit_hash_iter iter = NOIT_HASH_ITER_ZERO; const char *k; int klen; void *data; uuid_t checkid; noit_check_t *check; noit_hash_table *config; xmlNodePtr node, anode, mnode = NULL; char *uuid_conf; char *module, *value; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); continue; } nc_printf(ncct, "==== %s ====\n", uuid_conf); #define MYATTR(a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(a,n,b) \ _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(a) do { \ anode = NULL; \ value = NULL; \ INHERIT(a, anode, value); \ nc_attr_show(ncct, #a, node, anode, value); \ } while(0) if(!INHERIT(module, mnode, module)) module = NULL; if(MYATTR(name, anode, value)) nc_printf(ncct, " name: %s\n", value); else nc_printf(ncct, " name: %s [from module]\n", module ? module : "[undef]"); nc_attr_show(ncct, "module", node, mnode, module); SHOW_ATTR(target); SHOW_ATTR(period); SHOW_ATTR(timeout); SHOW_ATTR(oncheck); SHOW_ATTR(filterset); SHOW_ATTR(disable); /* Print out all the config settings */ config = noit_conf_get_hash(node, "config"); while(noit_hash_next(config, &iter, &k, &klen, &data)) { nc_printf(ncct, " config::%s: %s\n", k, (const char *)data); } noit_hash_destroy(config, free, free); free(config); check = noit_poller_lookup(checkid); if(!check) { nc_printf(ncct, " ERROR: not in running system\n"); } else { int idx = 0; nc_printf(ncct, " currently: "); if(NOIT_CHECK_RUNNING(check)) nc_printf(ncct, "%srunning", idx++?",":""); if(NOIT_CHECK_KILLED(check)) nc_printf(ncct, "%skilled", idx++?",":""); if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":""); if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":""); if(!idx) nc_printf(ncct, "idle"); nc_write(ncct, "\n", 1); if(check->stats.current.whence.tv_sec == 0) { nc_printf(ncct, " last run: never\n"); } else { stats_t *c = &check->stats.current; struct timeval now, diff; gettimeofday(&now, NULL); sub_timeval(now, c->whence, &diff); nc_printf(ncct, " last run: %0.3f seconds ago\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); nc_printf(ncct, " availability/state: %s/%s\n", noit_check_available_string(c->available), noit_check_state_string(c->state)); nc_printf(ncct, " status: %s\n", c->status ? c->status : "[[null]]"); nc_printf(ncct, " metrics:\n"); memset(&iter, 0, sizeof(iter)); while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { char buff[256]; noit_boolean filtered; noit_stats_snprint_metric(buff, sizeof(buff), (metric_t *)data); filtered = !noit_apply_filterset(check->filterset, check, (metric_t *)data); nc_printf(ncct, " %c%s\n", filtered ? '*' : ' ', buff); } } } } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
static int ssh2_drive_session(eventer_t e, int mask, void *closure, struct timeval *now) { int i; const char *fingerprint; ssh2_check_info_t *ci = closure; struct timeval diff; int timeout_ms = 10; /* 10ms, gets set below */ if(ci->state == WANT_CLOSE) { noit_check_t *check = ci->check; ssh2_log_results(ci->self, ci->check); ssh2_cleanup(ci->self, ci->check); eventer_remove_fd(e->fd); e->opset->close(e->fd, &mask, e); check->flags &= ~NP_RUNNING; return 0; } switch(mask) { case EVENTER_ASYNCH_WORK: if(eventer_set_fd_blocking(e->fd)) { ci->timed_out = 0; ci->error = strdup("socket error"); return 0; } ci->session = libssh2_session_init(); #define set_method(a,b) do { \ int rv; \ if(ci->methods.a && \ (rv = libssh2_session_method_pref(ci->session, b, ci->methods.a)) != 0) { \ ci->timed_out = 0; \ ci->error = strdup((rv == LIBSSH2_ERROR_METHOD_NOT_SUPPORTED) ? \ #a " method not supported" : "error setting " #a); \ return 0; \ } \ } while(0) set_method(kex, LIBSSH2_METHOD_KEX); set_method(hostkey, LIBSSH2_METHOD_HOSTKEY); set_method(crypt_cs, LIBSSH2_METHOD_CRYPT_CS); set_method(crypt_sc, LIBSSH2_METHOD_CRYPT_SC); set_method(mac_cs, LIBSSH2_METHOD_MAC_CS); set_method(mac_sc, LIBSSH2_METHOD_MAC_SC); set_method(comp_cs, LIBSSH2_METHOD_COMP_CS); set_method(comp_sc, LIBSSH2_METHOD_COMP_SC); if(compare_timeval(*now, e->whence) < 0) { sub_timeval(e->whence, *now, &diff); timeout_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000; } #if LIBSSH2_VERSION_NUM >= 0x010209 libssh2_session_set_timeout(ci->session, timeout_ms); #endif if (libssh2_session_startup(ci->session, e->fd)) { ci->timed_out = 0; ci->error = strdup("ssh session startup failed"); return 0; } fingerprint = libssh2_hostkey_hash(ci->session, LIBSSH2_HOSTKEY_HASH_MD5); for(i=0;i<16;i++) { snprintf(ci->fingerprint + (i*2), 3, "%02x", (unsigned char)fingerprint[i]); } ci->fingerprint[32] = '\0'; ci->timed_out = 0; return 0; break; case EVENTER_ASYNCH_CLEANUP: if(ci->session) { libssh2_session_disconnect(ci->session, "Bye!"); libssh2_session_free(ci->session); ci->session = NULL; } ci->state = WANT_CLOSE; break; default: abort(); } return 0; }
static int mysql_drive_session(eventer_t e, int mask, void *closure, struct timeval *now) { const char *dsn, *sql; char sql_buff[8192]; char dsn_buff[512]; mysql_check_info_t *ci = closure; noit_check_t *check = ci->check; struct timeval t1, t2, diff; mtev_hash_table dsn_h = MTEV_HASH_EMPTY; const char *host=NULL; const char *user=NULL; const char *password=NULL; const char *dbname=NULL; const char *port_s=NULL; const char *socket=NULL; const char *sslmode=NULL; u_int32_t port; unsigned long client_flag = CLIENT_IGNORE_SIGPIPE; unsigned int timeout; if(mask & (EVENTER_READ | EVENTER_WRITE)) { /* this case is impossible from the eventer. It is called as * such on the synchronous completion of the event. */ mysql_log_results(ci->self, ci->check); mysql_cleanup(ci->self, ci->check); check->flags &= ~NP_RUNNING; return 0; } switch(mask) { case EVENTER_ASYNCH_WORK: ci->connect_duration = NULL; ci->query_duration = NULL; FETCH_CONFIG_OR(dsn, ""); noit_check_interpolate(dsn_buff, sizeof(dsn_buff), dsn, &ci->attrs, check->config); mysql_parse_dsn(dsn_buff, &dsn_h); mtev_hash_retrieve(&dsn_h, "host", strlen("host"), (void**)&host); mtev_hash_retrieve(&dsn_h, "user", strlen("user"), (void**)&user); mtev_hash_retrieve(&dsn_h, "password", strlen("password"), (void**)&password); mtev_hash_retrieve(&dsn_h, "dbname", strlen("dbname"), (void**)&dbname); mtev_hash_retrieve(&dsn_h, "port", strlen("port"), (void**)&port_s); if(mtev_hash_retrieve(&dsn_h, "sslmode", strlen("sslmode"), (void**)&sslmode) && !strcmp(sslmode, "require")) client_flag |= CLIENT_SSL; port = port_s ? strtol(port_s, NULL, 10) : 3306; mtev_hash_retrieve(&dsn_h, "socket", strlen("socket"), (void**)&socket); ci->conn = mysql_init(NULL); /* allocate us a handle */ if(!ci->conn) AVAIL_BAIL("mysql_init failed"); timeout = check->timeout / 1000; mysql_options(ci->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout); if(!mysql_real_connect(ci->conn, host, user, password, dbname, port, socket, client_flag)) { mtevL(noit_stderr, "error during mysql_real_connect: %s\n", mysql_error(ci->conn)); AVAIL_BAIL(mysql_error(ci->conn)); } if(mysql_ping(ci->conn)) AVAIL_BAIL(mysql_error(ci->conn)); #if MYSQL_VERSION_ID >= 50000 if (sslmode && !strcmp(sslmode, "require")) { /* mysql has a bad habit of silently failing to establish ssl and * falling back to unencrypted, so after making the connection, let's * check that we're actually using SSL by checking for a non-NULL * return value from mysql_get_ssl_cipher(). */ if (mysql_get_ssl_cipher(ci->conn) == NULL) { mtevL(nldeb, "mysql_get_ssl_cipher() returns NULL, but SSL mode required."); AVAIL_BAIL("mysql_get_ssl_cipher() returns NULL, but SSL mode required."); } } #endif gettimeofday(&t1, NULL); sub_timeval(t1, check->last_fire_time, &diff); ci->connect_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; ci->connect_duration = &ci->connect_duration_d; FETCH_CONFIG_OR(sql, ""); noit_check_interpolate(sql_buff, sizeof(sql_buff), sql, &ci->attrs, check->config); if (mysql_query(ci->conn, sql_buff)) AVAIL_BAIL(mysql_error(ci->conn)); gettimeofday(&t2, NULL); sub_timeval(t2, t1, &diff); ci->query_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; ci->query_duration = &ci->query_duration_d; ci->result = mysql_store_result(ci->conn); if(!ci->result) AVAIL_BAIL("mysql_store_result failed"); ci->rv = mysql_num_rows(ci->result); mysql_ingest_stats(ci); if(ci->result) { MYSQL_RES *result_swap = ci->result; ci->result = NULL; mysql_free_result(result_swap); } if(ci->conn) { MYSQL *conn_swap = ci->conn; ci->conn = NULL; mysql_close(conn_swap); } ci->timed_out = 0; mtev_hash_destroy(&dsn_h, free, free); return 0; break; case EVENTER_ASYNCH_CLEANUP: /* This sets us up for a completion call. */ e->mask = EVENTER_READ | EVENTER_WRITE; break; default: abort(); } return 0; }
static int test_abort_drive_session(eventer_t e, int mask, void *closure, struct timeval *passed_now) { struct timespec rqtp; struct timeval target_time, now, diff; double i, r; test_abort_check_info_t *ci = closure; noit_check_t *check = ci->check; if(mask & (EVENTER_READ | EVENTER_WRITE)) { /* this case is impossible from the eventer. It is called as * such on the synchronous completion of the event. */ noit_check_stats_clear(check, &check->stats.inprogress); check->stats.inprogress.available = NP_AVAILABLE; check->stats.inprogress.state = ci->timed_out ? NP_BAD : NP_GOOD; noitL(nlerr, "test_abort: EVENTER_READ | EVENTER_WRITE\n"); noit_check_set_stats(check, &check->stats.inprogress); noit_check_stats_clear(check, &check->stats.inprogress); check->flags &= ~NP_RUNNING; return 0; } switch(mask) { case EVENTER_ASYNCH_WORK: noitL(nlerr, "test_abort: EVENTER_ASYNCH_WORK\n"); r = modf(ci->timeout, &i); ci->timed_out = 1; if(ci->ignore_signals) { /* compuational loop */ double trash = 1.0; gettimeofday(&now, NULL); diff.tv_sec = (int)i; diff.tv_usec = (int)(r * 1000000.0); add_timeval(now, diff, &target_time); do { for(i=0; i<100000; i++) { trash += drand48(); trash = log(trash); trash += 1.1; trash = exp(trash); } gettimeofday(&now, NULL); sub_timeval(target_time, now, &diff); } while(diff.tv_sec >= 0 && diff.tv_usec >= 0); } else { rqtp.tv_sec = (int)i; rqtp.tv_nsec = (int)(r * 1000000000.0); nanosleep(&rqtp,NULL); } noitL(nlerr, "test_abort: EVENTER_ASYNCH_WORK (done)\n"); ci->timed_out = 0; return 0; break; case EVENTER_ASYNCH_CLEANUP: /* This sets us up for a completion call. */ noitL(nlerr, "test_abort: EVENTER_ASYNCH_CLEANUP\n"); e->mask = EVENTER_READ | EVENTER_WRITE; break; default: abort(); } return 0; }