@cryptotaxi247 / netdata-1 / commits / c70ca36eb

added support for titles and priorities through env variables; handled overflown data entries

Costa Tsaousis (ktsaou) committed Mar 29, 2014 at 22:39 UTC c70ca36eb8fbb91c492be8abeece4c982c535797
1 file changed +46 -1
netdata.c
+46 -1
@@ -340,6 +340,12 @@ struct rrd_stats {
340 char title[RRD_STATS_NAME_MAX + 1];
341 char vtitle[RRD_STATS_NAME_MAX + 1];
342
343 + char usertitle[RRD_STATS_NAME_MAX + 1];
344 + char userpriority[RRD_STATS_NAME_MAX + 1];
345 +
346 + char envtitle[RRD_STATS_NAME_MAX + 1];
347 + char envpriority[RRD_STATS_NAME_MAX + 1];
348 +
349 char type[RRD_STATS_NAME_MAX + 1];
350
351 size_t entries;
@@ -388,6 +394,26 @@ RRD_STATS *rrd_stats_create(const char *id, const char *name, unsigned long entr
394 strncpy(st->type, type, RRD_STATS_NAME_MAX);
395 st->type[RRD_STATS_NAME_MAX] = '\0';
396
397 + // check if there is a name for it in the environment
398 + char *p;
399 + sprintf(st->envtitle, "NETDATA_TITLE_%s", st->id);
400 + while((p = strchr(st->envtitle, '.'))) *p = '_';
401 + while((p = strchr(st->envtitle, '-'))) *p = '_';
402 + p = getenv(st->envtitle);
403 +
404 + if(p) strncpy(st->usertitle, p, RRD_STATS_NAME_MAX);
405 + else strncpy(st->usertitle, st->name, RRD_STATS_NAME_MAX);
406 + st->usertitle[RRD_STATS_NAME_MAX] = '\0';
407 +
408 + sprintf(st->envpriority, "NETDATA_PRIORITY_%s", st->id);
409 + while((p = strchr(st->envpriority, '.'))) *p = '_';
410 + while((p = strchr(st->envpriority, '-'))) *p = '_';
411 + p = getenv(st->envpriority);
412 +
413 + if(p) strncpy(st->userpriority, p, RRD_STATS_NAME_MAX);
414 + else strncpy(st->userpriority, st->name, RRD_STATS_NAME_MAX);
415 + st->userpriority[RRD_STATS_NAME_MAX] = '\0';
416 +
417 st->entries = entries;
418 st->current_entry = 0;
419 // st->last_entry = 0;
@@ -584,10 +610,15 @@ size_t rrd_stats_one_json(RRD_STATS *st, char *options, char *buffer, size_t len
610 i += sprintf(&buffer[i], "\t\t\t\"name\" : \"%s\",\n", st->name);
611 i += sprintf(&buffer[i], "\t\t\t\"type\" : \"%s\",\n", st->type);
612 i += sprintf(&buffer[i], "\t\t\t\"title\" : \"%s %s\",\n", st->title, st->name);
613 + i += sprintf(&buffer[i], "\t\t\t\"usertitle\" : \"%s %s (%s)\",\n", st->title, st->usertitle, st->name);
614 + i += sprintf(&buffer[i], "\t\t\t\"userpriority\" : \"%s\",\n", st->userpriority);
615 + i += sprintf(&buffer[i], "\t\t\t\"envtitle\" : \"%s\",\n", st->envtitle);
616 + i += sprintf(&buffer[i], "\t\t\t\"envpriority\" : \"%s\",\n", st->envpriority);
617 i += sprintf(&buffer[i], "\t\t\t\"vtitle\" : \"%s\",\n", st->vtitle);
618 i += sprintf(&buffer[i], "\t\t\t\"url\" : \"/data/%s/%s\",\n", st->name, options?options:"");
619 i += sprintf(&buffer[i], "\t\t\t\"entries\" : %ld,\n", st->entries);
620 i += sprintf(&buffer[i], "\t\t\t\"current\" : %ld,\n", st->current_entry);
621 + i += sprintf(&buffer[i], "\t\t\t\"update_every\" : %d,\n", update_every);
622 i += sprintf(&buffer[i], "\t\t\t\"last_updated\" : %lu,\n", st->last_updated);
623 i += sprintf(&buffer[i], "\t\t\t\"last_updated_secs_ago\" : %lu\n", time(NULL) - st->last_updated);
624 i += sprintf(&buffer[i], "\t\t}");
@@ -747,7 +778,8 @@ size_t rrd_stats_json(RRD_STATS *st, char *b, size_t length, size_t entries_to_s
778
779 switch(rd->type) {
780 case RRD_DIMENSION_INCREMENTAL:
750 - value -= oldvalue;
781 + if(oldvalue > value) value = 0; // detect overflows and resets
782 + else value -= oldvalue;
783 value = value * 1000000L / usec;
784 break;
785
@@ -1188,6 +1220,19 @@ void web_client_process(struct web_client *w)
1220 for ( ; st ; st = st->next )
1221 w->data->bytes += sprintf(&w->data->buffer[w->data->bytes], "%s\n", st->name);
1222 }
1223 + else if(strcmp(tok, "envlist") == 0) {
1224 + code = 200;
1225 +
1226 + debug(D_WEB_CLIENT_ACCESS, "%llu: Sending envlist of RRD_STATS...", w->id);
1227 +
1228 + w->data->bytes = 0;
1229 + RRD_STATS *st = root;
1230 +
1231 + for ( ; st ; st = st->next ) {
1232 + w->data->bytes += sprintf(&w->data->buffer[w->data->bytes], "%s=%s\n", st->envtitle, st->usertitle);
1233 + w->data->bytes += sprintf(&w->data->buffer[w->data->bytes], "%s=%s\n", st->envpriority, st->userpriority);
1234 + }
1235 + }
1236 else if(strcmp(tok, "all.json") == 0) {
1237 code = 200;
1238 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending all.json of RRD_STATS...", w->id);