static void handle_metric_buffer(const char *payload, int payload_len, int has_noit) { if (payload_len <= 0) { return; } switch (payload[0]) { case 'C': case 'D': case 'S': case 'H': case 'M': { // mtev_fq will free the fq_msg -> copy the payload char *copy = mtev__strndup(payload, payload_len); noit_metric_message_t *message = calloc(1, sizeof(noit_metric_message_t)); message->type = copy[0]; message->original_message = copy; message->original_message_len = payload_len; noit_metric_director_message_ref(message); int rv = noit_message_decoder_parse_line(copy, payload_len, &message->id.id, &message->id.name, &message->id.name_len, &message->value, has_noit); if(rv == 1) { distribute_message(message); } noit_metric_director_message_deref(message); } break; case 'B': { int n_metrics, i; char **metrics = NULL; n_metrics = noit_check_log_b_to_sm((const char *)payload, payload_len, &metrics, has_noit); for(i = 0; i < n_metrics; i++) { handle_metric_buffer(metrics[i], strlen(metrics[i]), false); free(metrics[i]); } free(metrics); } break; default: ; /* ignored */ } }
static void filter_and_send(noit_websocket_closure_t *wcl, const char *buf, size_t len) { char **out = NULL; int count = 0; if (buf == NULL || len == 0) { return; } if (buf[0] == 'B') { count = noit_check_log_b_to_sm(buf, len, &out, 0); for (int i = 0; i < count; i++) { send_individual_metric(wcl, out[i], strlen(out[i])); free(out[i]); } free(out); } else { send_individual_metric(wcl, buf, len); } }
int stratcon_line_to_javascript(noit_http_session_ctx *ctx, char *in_buff, u_int32_t *inc_id) { char buffer[1024]; char *scp, *ecp, *token, *buff; int i, len, cnt; const char *v, *cb = NULL; noit_hash_table json = NOIT_HASH_EMPTY; noit_http_request *req = noit_http_session_request(ctx); char s_inc_id[42]; char **outrows = NULL; cb = noit_http_request_querystring(req, "cb"); for(v = cb; v && *v; v++) if(!((*v >= '0' && *v <= '9') || (*v >= 'a' && *v <= 'z') || (*v >= 'A' && *v <= 'Z') || (*v == '_') || (*v == '.'))) { cb = NULL; break; } if(!cb) cb = "window.parent.plot_iframe_data"; #define BAIL_HTTP_WRITE do { \ if(outrows) { \ for(i=0;i<cnt;i++) if(outrows[i]) free(outrows[i]); \ free(outrows); \ } \ noit_hash_destroy(&json, NULL, free); \ noitL(noit_error, "javascript emit failed: %s:%s:%d\n", \ __FILE__, __FUNCTION__, __LINE__); \ return -1; \ } while(0) #define PROCESS_NEXT_FIELD(t,l) do { \ if(!*scp) goto bad_row; \ ecp = strchr(scp, '\t'); \ if(!ecp) goto bad_row; \ t = scp; \ l = (ecp-scp); \ scp = ecp + 1; \ } while(0) #define PROCESS_LAST_FIELD(t,l) do { \ if(!*scp) ecp = scp; \ else { \ ecp = scp + strlen(scp); /* Puts us at the '\0' */ \ if(*(ecp-1) == '\n') ecp--; /* We back up on letter if we ended in \n */ \ } \ t = scp; \ l = (ecp-scp); \ } while(0) noitL(noit_error, "recv(%s)\n", in_buff); if(in_buff[0] == 'B' && in_buff[1] != '\0' && in_buff[2] == '\t') { cnt = noit_check_log_b_to_sm(in_buff, strlen(in_buff), &outrows); } else { cnt = 1; outrows = malloc(sizeof(*outrows)); outrows[0] = strdup(in_buff); } for(i=0; i<cnt; i++) { buff = outrows[i]; if(!buff) continue; noitL(noit_error, "recv_xlt(%s)\n", buff); scp = buff; PROCESS_NEXT_FIELD(token,len); /* Skip the leader */ if(buff[1] == '\t' && (buff[0] == 'M' || buff[0] == 'S')) { char target[256], module[256], name[256], uuid_str[UUID_STR_LEN+1]; noit_http_request *req = noit_http_session_request(ctx); noit_hash_table *qs; noit_hash_iter iter = NOIT_HASH_ITER_ZERO; const char *key; int klen, i=0; void *vval; char type[2] = { '\0', '\0' }; type[0] = buff[0]; #define ra_write(a,b) if(noit_http_response_append(ctx, a, b) == noit_false) BAIL_HTTP_WRITE snprintf(s_inc_id, sizeof(s_inc_id), "script-%08x", (*inc_id)++); snprintf(buffer, sizeof(buffer), "<script id=\"%s\">%s({", s_inc_id, cb); ra_write(buffer, strlen(buffer)); qs = noit_http_request_querystring_table(req); while(noit_hash_next(qs, &iter, &key, &klen, &vval)) { if(!strcmp(key, "cb")) continue; noit_hash_store(&json, key, klen, strdup(vval ?(char *)vval : "true")); } /* Time */ noit_hash_store(&json, "script_id", 9, strdup(s_inc_id)); noit_hash_store(&json, "type", 4, strdup(type)); PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "time", 4, noit__strndup(token, len)); /* UUID */ PROCESS_NEXT_FIELD(token,len); noit_check_extended_id_split(token, len, target, sizeof(target), module, sizeof(module), name, sizeof(name), uuid_str, sizeof(uuid_str)); if(*uuid_str) noit_hash_store(&json, "id", 2, noit__strndup(uuid_str, strlen(uuid_str))); if(*target) noit_hash_store(&json, "check_target", 12, noit__strndup(target, strlen(target))); if(*module) noit_hash_store(&json, "check_module", 12, noit__strndup(module, strlen(module))); if(*name) noit_hash_store(&json, "check_name", 10, noit__strndup(name, strlen(name))); if(buff[0] == 'M') { /* name */ PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "metric_name", 11, noit__strndup(token, len)); /* type */ PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "metric_type", 11, noit__strndup(token, len)); /* value */ PROCESS_LAST_FIELD(token,len); /* value */ noit_hash_store(&json, "value", 5, noit__strndup(token, len)); } else if(buff[0] == 'S') { /* state */ PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "check_state", 11, noit__strndup(token, len)); /* availability */ PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "check_availability", 18, noit__strndup(token, len)); /* duration */ PROCESS_NEXT_FIELD(token,len); noit_hash_store(&json, "check_duration_ms", 17, noit__strndup(token, len)); /* status */ PROCESS_LAST_FIELD(token,len); noit_hash_store(&json, "status_message", 14, noit__strndup(token, len)); } memset(&iter, 0, sizeof(iter)); while(noit_hash_next(&json, &iter, &key, &klen, &vval)) { char *val = (char *)vval; if(i++) ra_write(",", 1); ra_write("\"", 1); ra_write(key, klen); ra_write("\":\"", 3); while(*val) { if(*val == '\"' || *val == '\\') { ra_write((char *)"\\", 1); } if(isprint(*val)) { ra_write((char *)val, 1); } else { char od[5]; snprintf(od, sizeof(od), "\\%03o", *((unsigned char *)val)); ra_write(od, strlen(od)); } val++; } ra_write("\"", 1); } snprintf(buffer, sizeof(buffer), "});</script>\n"); ra_write(buffer, strlen(buffer)); if(noit_http_response_flush(ctx, noit_false) == noit_false) BAIL_HTTP_WRITE; } noit_hash_destroy(&json, NULL, free); memset(&json, 0, sizeof(json)); } if(outrows) { for(i=0;i<cnt;i++) if(outrows[i]) free(outrows[i]); free(outrows); } return 0; bad_row: BAIL_HTTP_WRITE; }