| 169 | | scp = buff; |
|---|
| 170 | | PROCESS_NEXT_FIELD(token,len); /* Skip the leader */ |
|---|
| 171 | | if(buff[1] == '\t' && (buff[0] == 'M' || buff[0] == 'S')) { |
|---|
| 172 | | char target[256], module[256], name[256], uuid_str[UUID_STR_LEN+1]; |
|---|
| 173 | | noit_http_request *req = noit_http_session_request(ctx); |
|---|
| 174 | | noit_hash_table *qs; |
|---|
| 175 | | noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 176 | | const char *key; |
|---|
| 177 | | int klen, i=0; |
|---|
| 178 | | void *vval; |
|---|
| 179 | | char type[2] = { '\0', '\0' }; |
|---|
| 180 | | type[0] = buff[0]; |
|---|
| | 173 | noitL(noit_error, "recv(%s)\n", in_buff); |
|---|
| | 174 | if(in_buff[0] == 'B' && in_buff[1] != '\0' && in_buff[2] == '\t') { |
|---|
| | 175 | cnt = noit_check_log_b_to_sm(in_buff, strlen(in_buff), &outrows); |
|---|
| | 176 | } |
|---|
| | 177 | else { |
|---|
| | 178 | cnt = 1; |
|---|
| | 179 | outrows = malloc(sizeof(*outrows)); |
|---|
| | 180 | outrows[0] = strdup(in_buff); |
|---|
| | 181 | } |
|---|
| | 182 | for(i=0; i<cnt; i++) { |
|---|
| | 183 | buff = outrows[i]; |
|---|
| | 184 | if(!buff) continue; |
|---|
| | 185 | noitL(noit_error, "recv_xlt(%s)\n", buff); |
|---|
| | 186 | scp = buff; |
|---|
| | 187 | PROCESS_NEXT_FIELD(token,len); /* Skip the leader */ |
|---|
| | 188 | if(buff[1] == '\t' && (buff[0] == 'M' || buff[0] == 'S')) { |
|---|
| | 189 | char target[256], module[256], name[256], uuid_str[UUID_STR_LEN+1]; |
|---|
| | 190 | noit_http_request *req = noit_http_session_request(ctx); |
|---|
| | 191 | noit_hash_table *qs; |
|---|
| | 192 | noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| | 193 | const char *key; |
|---|
| | 194 | int klen, i=0; |
|---|
| | 195 | void *vval; |
|---|
| | 196 | char type[2] = { '\0', '\0' }; |
|---|
| | 197 | type[0] = buff[0]; |
|---|
| 184 | | snprintf(buffer, sizeof(buffer), "<script id=\"%s\">%s({", s_inc_id, cb); |
|---|
| 185 | | ra_write(buffer, strlen(buffer)); |
|---|
| 186 | | |
|---|
| 187 | | qs = noit_http_request_querystring_table(req); |
|---|
| 188 | | while(noit_hash_next(qs, &iter, &key, &klen, &vval)) { |
|---|
| 189 | | if(!strcmp(key, "cb")) continue; |
|---|
| 190 | | noit_hash_store(&json, key, klen, strdup(vval ?(char *)vval : "true")); |
|---|
| 191 | | } |
|---|
| 192 | | /* Time */ |
|---|
| 193 | | noit_hash_store(&json, "script_id", 9, strdup(s_inc_id)); |
|---|
| 194 | | noit_hash_store(&json, "type", 4, strdup(type)); |
|---|
| 195 | | PROCESS_NEXT_FIELD(token,len); |
|---|
| 196 | | noit_hash_store(&json, "time", 4, noit__strndup(token, len)); |
|---|
| 197 | | /* UUID */ |
|---|
| 198 | | PROCESS_NEXT_FIELD(token,len); |
|---|
| 199 | | noit_check_extended_id_split(token, len, target, sizeof(target), |
|---|
| 200 | | module, sizeof(module), name, sizeof(name), |
|---|
| 201 | | uuid_str, sizeof(uuid_str)); |
|---|
| 202 | | if(*uuid_str) |
|---|
| 203 | | noit_hash_store(&json, "id", 2, |
|---|
| 204 | | noit__strndup(uuid_str, strlen(uuid_str))); |
|---|
| 205 | | if(*target) |
|---|
| 206 | | noit_hash_store(&json, "check_target", 12, |
|---|
| 207 | | noit__strndup(target, strlen(target))); |
|---|
| 208 | | if(*module) |
|---|
| 209 | | noit_hash_store(&json, "check_module", 12, |
|---|
| 210 | | noit__strndup(module, strlen(module))); |
|---|
| 211 | | if(*name) |
|---|
| 212 | | noit_hash_store(&json, "check_name", 10, |
|---|
| 213 | | noit__strndup(name, strlen(name))); |
|---|
| 214 | | if(buff[0] == 'M') { |
|---|
| 215 | | /* name */ |
|---|
| | 201 | snprintf(s_inc_id, sizeof(s_inc_id), "script-%08x", (*inc_id)++); |
|---|
| | 202 | snprintf(buffer, sizeof(buffer), "<script id=\"%s\">%s({", s_inc_id, cb); |
|---|
| | 203 | ra_write(buffer, strlen(buffer)); |
|---|
| | 204 | |
|---|
| | 205 | qs = noit_http_request_querystring_table(req); |
|---|
| | 206 | while(noit_hash_next(qs, &iter, &key, &klen, &vval)) { |
|---|
| | 207 | if(!strcmp(key, "cb")) continue; |
|---|
| | 208 | noit_hash_store(&json, key, klen, strdup(vval ?(char *)vval : "true")); |
|---|
| | 209 | } |
|---|
| | 210 | /* Time */ |
|---|
| | 211 | noit_hash_store(&json, "script_id", 9, strdup(s_inc_id)); |
|---|
| | 212 | noit_hash_store(&json, "type", 4, strdup(type)); |
|---|
| 220 | | noit_hash_store(&json, "metric_type", 11, noit__strndup(token, len)); |
|---|
| 221 | | /* value */ |
|---|
| 222 | | PROCESS_LAST_FIELD(token,len); /* value */ |
|---|
| 223 | | noit_hash_store(&json, "value", 5, noit__strndup(token, len)); |
|---|
| 224 | | } |
|---|
| 225 | | else if(buff[0] == 'S') { |
|---|
| 226 | | /* state */ |
|---|
| 227 | | PROCESS_NEXT_FIELD(token,len); |
|---|
| 228 | | noit_hash_store(&json, "check_state", 11, noit__strndup(token, len)); |
|---|
| 229 | | /* availability */ |
|---|
| 230 | | PROCESS_NEXT_FIELD(token,len); |
|---|
| 231 | | noit_hash_store(&json, "check_availability", 18, noit__strndup(token, len)); |
|---|
| 232 | | /* duration */ |
|---|
| 233 | | PROCESS_NEXT_FIELD(token,len); |
|---|
| 234 | | noit_hash_store(&json, "check_duration_ms", 17, noit__strndup(token, len)); |
|---|
| 235 | | /* status */ |
|---|
| 236 | | PROCESS_LAST_FIELD(token,len); |
|---|
| 237 | | noit_hash_store(&json, "status_message", 14, noit__strndup(token, len)); |
|---|
| 238 | | } |
|---|
| 239 | | |
|---|
| 240 | | memset(&iter, 0, sizeof(iter)); |
|---|
| 241 | | while(noit_hash_next(&json, &iter, &key, &klen, &vval)) { |
|---|
| 242 | | char *val = (char *)vval; |
|---|
| 243 | | if(i++) ra_write(",", 1); |
|---|
| 244 | | ra_write("\"", 1); |
|---|
| 245 | | ra_write(key, klen); |
|---|
| 246 | | ra_write("\":\"", 3); |
|---|
| 247 | | while(*val) { |
|---|
| 248 | | if(*val == '\"' || *val == '\\') { |
|---|
| 249 | | ra_write((char *)"\\", 1); |
|---|
| | 217 | noit_check_extended_id_split(token, len, target, sizeof(target), |
|---|
| | 218 | module, sizeof(module), name, sizeof(name), |
|---|
| | 219 | uuid_str, sizeof(uuid_str)); |
|---|
| | 220 | if(*uuid_str) |
|---|
| | 221 | noit_hash_store(&json, "id", 2, |
|---|
| | 222 | noit__strndup(uuid_str, strlen(uuid_str))); |
|---|
| | 223 | if(*target) |
|---|
| | 224 | noit_hash_store(&json, "check_target", 12, |
|---|
| | 225 | noit__strndup(target, strlen(target))); |
|---|
| | 226 | if(*module) |
|---|
| | 227 | noit_hash_store(&json, "check_module", 12, |
|---|
| | 228 | noit__strndup(module, strlen(module))); |
|---|
| | 229 | if(*name) |
|---|
| | 230 | noit_hash_store(&json, "check_name", 10, |
|---|
| | 231 | noit__strndup(name, strlen(name))); |
|---|
| | 232 | if(buff[0] == 'M') { |
|---|
| | 233 | /* name */ |
|---|
| | 234 | PROCESS_NEXT_FIELD(token,len); |
|---|
| | 235 | noit_hash_store(&json, "metric_name", 11, noit__strndup(token, len)); |
|---|
| | 236 | /* type */ |
|---|
| | 237 | PROCESS_NEXT_FIELD(token,len); |
|---|
| | 238 | noit_hash_store(&json, "metric_type", 11, noit__strndup(token, len)); |
|---|
| | 239 | /* value */ |
|---|
| | 240 | PROCESS_LAST_FIELD(token,len); /* value */ |
|---|
| | 241 | noit_hash_store(&json, "value", 5, noit__strndup(token, len)); |
|---|
| | 242 | } |
|---|
| | 243 | else if(buff[0] == 'S') { |
|---|
| | 244 | /* state */ |
|---|
| | 245 | PROCESS_NEXT_FIELD(token,len); |
|---|
| | 246 | noit_hash_store(&json, "check_state", 11, noit__strndup(token, len)); |
|---|
| | 247 | /* availability */ |
|---|
| | 248 | PROCESS_NEXT_FIELD(token,len); |
|---|
| | 249 | noit_hash_store(&json, "check_availability", 18, noit__strndup(token, len)); |
|---|
| | 250 | /* duration */ |
|---|
| | 251 | PROCESS_NEXT_FIELD(token,len); |
|---|
| | 252 | noit_hash_store(&json, "check_duration_ms", 17, noit__strndup(token, len)); |
|---|
| | 253 | /* status */ |
|---|
| | 254 | PROCESS_LAST_FIELD(token,len); |
|---|
| | 255 | noit_hash_store(&json, "status_message", 14, noit__strndup(token, len)); |
|---|
| | 256 | } |
|---|
| | 257 | |
|---|
| | 258 | memset(&iter, 0, sizeof(iter)); |
|---|
| | 259 | while(noit_hash_next(&json, &iter, &key, &klen, &vval)) { |
|---|
| | 260 | char *val = (char *)vval; |
|---|
| | 261 | if(i++) ra_write(",", 1); |
|---|
| | 262 | ra_write("\"", 1); |
|---|
| | 263 | ra_write(key, klen); |
|---|
| | 264 | ra_write("\":\"", 3); |
|---|
| | 265 | while(*val) { |
|---|
| | 266 | if(*val == '\"' || *val == '\\') { |
|---|
| | 267 | ra_write((char *)"\\", 1); |
|---|
| | 268 | } |
|---|
| | 269 | if(isprint(*val)) { |
|---|
| | 270 | ra_write((char *)val, 1); |
|---|
| | 271 | } |
|---|
| | 272 | else { |
|---|
| | 273 | char od[5]; |
|---|
| | 274 | snprintf(od, sizeof(od), "\\%03o", *((unsigned char *)val)); |
|---|
| | 275 | ra_write(od, strlen(od)); |
|---|
| | 276 | } |
|---|
| | 277 | val++; |
|---|