inline int web_client_api_old_graph_request(RRDHOST *host, struct web_client *w, char *url) { // get the name of the data to show char *tok = mystrsep(&url, "/?&"); if(tok && *tok) { debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok); // do we have such a data set? RRDSET *st = rrdset_find_byname(host, tok); if(!st) st = rrdset_find(host, tok); if(!st) { // we don't have it // try to send a file with that name buffer_flush(w->response.data); return mysendfile(w, tok); } st->last_accessed_time = now_realtime_sec(); debug(D_WEB_CLIENT_ACCESS, "%llu: Sending %s.json of RRD_STATS...", w->id, st->name); w->response.data->contenttype = CT_APPLICATION_JSON; buffer_flush(w->response.data); rrd_graph2json_api_old(st, url, w->response.data); return 200; } buffer_flush(w->response.data); buffer_strcat(w->response.data, "Graph name?\r\n"); return 400; }
static void registry_set_cookie(struct web_client *w, const char *guid) { char edate[100]; time_t et = now_realtime_sec() + registry.persons_expiration; struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf); strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm); snprintfz(w->cookie1, NETDATA_WEB_REQUEST_COOKIE_SIZE, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate); if(registry.registry_domain && registry.registry_domain[0]) snprintfz(w->cookie2, NETDATA_WEB_REQUEST_COOKIE_SIZE, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate); }
static inline void mountinfo_reload(int force) { static time_t last_loaded = 0; time_t now = now_realtime_sec(); if(force || now - last_loaded >= check_for_new_mountpoints_every) { // mountinfo_free() can be called with NULL disk_mountinfo_root mountinfo_free(disk_mountinfo_root); // re-read mountinfo in case something changed disk_mountinfo_root = mountinfo_read(0); last_loaded = now; } }
void log_date(FILE *out) { char outstr[26]; time_t t; struct tm *tmp, tmbuf; t = now_realtime_sec(); tmp = localtime_r(&t, &tmbuf); if (tmp == NULL) return; if (unlikely(strftime(outstr, sizeof(outstr), "%Y-%m-%d %H:%M:%S", tmp) == 0)) return; fprintf(out, "%s: ", outstr); }
inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) { int ret = 400; char *chart = NULL; buffer_flush(w->response.data); while(url) { char *value = mystrsep(&url, "?&"); if(!value || !*value) continue; char *name = mystrsep(&value, "="); if(!name || !*name) continue; if(!value || !*value) continue; // name and value are now the parameters // they are not null and not empty if(!strcmp(name, "chart")) chart = value; //else { /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name); // goto cleanup; //} } if(!chart || !*chart) { buffer_sprintf(w->response.data, "No chart id is given at the request."); goto cleanup; } RRDSET *st = rrdset_find(host, chart); if(!st) st = rrdset_find_byname(host, chart); if(!st) { buffer_strcat(w->response.data, "Chart is not found: "); buffer_strcat_htmlescape(w->response.data, chart); ret = 404; goto cleanup; } w->response.data->contenttype = CT_APPLICATION_JSON; st->last_accessed_time = now_realtime_sec(); callback(st, w->response.data); return 200; cleanup: return ret; }
int run_test(struct test *test) { fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description); rrd_memory_mode = RRD_MEMORY_MODE_RAM; rrd_update_every = test->update_every; char name[101]; snprintfz(name, 100, "unittest-%s", test->name); // create the chart RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, test->update_every, RRDSET_TYPE_LINE); RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm); RRDDIM *rd2 = NULL; if(test->feed2) rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm); st->debug = 1; // feed it with the test data time_t time_now = 0, time_start = now_realtime_sec(); unsigned long c; collected_number last = 0; for(c = 0; c < test->feed_entries; c++) { if(debug_flags) fprintf(stderr, "\n\n"); if(c) { time_now += test->feed[c].microseconds; fprintf(stderr, " > %s: feeding position %lu, after %0.3f seconds (%0.3f seconds from start), delta " CALCULATED_NUMBER_FORMAT ", rate " CALCULATED_NUMBER_FORMAT "\n", test->name, c+1, (float)test->feed[c].microseconds / 1000000.0, (float)time_now / 1000000.0, ((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor, (((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor) / (calculated_number)test->feed[c].microseconds * (calculated_number)1000000); rrdset_next_usec_unfiltered(st, test->feed[c].microseconds); } else { fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1); } fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value); rrddim_set(st, "dim1", test->feed[c].value); last = test->feed[c].value; if(rd2) { fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]); rrddim_set(st, "dim2", test->feed2[c]); } rrdset_done(st); // align the first entry to second boundary if(!c) { fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds); rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds; // time_start = st->last_collected_time.tv_sec; } } // check the result int errors = 0; if(st->counter != test->result_entries) { fprintf(stderr, " %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries); errors++; } unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries; for(c = 0 ; c < max ; c++) { calculated_number v = unpack_storage_number(rd->values[c]); calculated_number n = test->results[c]; int same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0; fprintf(stderr, " %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n", test->name, rd->name, c+1, (rrdset_first_entry_t(st) + c * st->update_every) - time_start, n, v, (same)?"OK":"### E R R O R ###"); if(!same) errors++; if(rd2) { v = unpack_storage_number(rd2->values[c]); n = test->results2[c]; same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0; fprintf(stderr, " %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n", test->name, rd2->name, c+1, (rrdset_first_entry_t(st) + c * st->update_every) - time_start, n, v, (same)?"OK":"### E R R O R ###"); if(!same) errors++; } } return errors; }
inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) { static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0, hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0, hash_to = 0 /*, hash_redirects = 0 */; if(unlikely(!hash_action)) { hash_action = simple_hash("action"); hash_access = simple_hash("access"); hash_hello = simple_hash("hello"); hash_delete = simple_hash("delete"); hash_search = simple_hash("search"); hash_switch = simple_hash("switch"); hash_machine = simple_hash("machine"); hash_url = simple_hash("url"); hash_name = simple_hash("name"); hash_delete_url = simple_hash("delete_url"); hash_for = simple_hash("for"); hash_to = simple_hash("to"); /* hash_redirects = simple_hash("redirects"); */ } char person_guid[GUID_LEN + 1] = ""; debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url); // TODO // The browser may send multiple cookies with our id char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "="); if(cookie) strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36); char action = '\0'; char *machine_guid = NULL, *machine_url = NULL, *url_name = NULL, *search_machine_guid = NULL, *delete_url = NULL, *to_person_guid = NULL; /* int redirects = 0; */ while(url) { char *value = mystrsep(&url, "?&"); if (!value || !*value) continue; char *name = mystrsep(&value, "="); if (!name || !*name) continue; if (!value || !*value) continue; debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value); uint32_t hash = simple_hash(name); if(hash == hash_action && !strcmp(name, "action")) { uint32_t vhash = simple_hash(value); if(vhash == hash_access && !strcmp(value, "access")) action = 'A'; else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H'; else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D'; else if(vhash == hash_search && !strcmp(value, "search")) action = 'S'; else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W'; #ifdef NETDATA_INTERNAL_CHECKS else error("unknown registry action '%s'", value); #endif /* NETDATA_INTERNAL_CHECKS */ } /* else if(hash == hash_redirects && !strcmp(name, "redirects")) redirects = atoi(value); */ else if(hash == hash_machine && !strcmp(name, "machine")) machine_guid = value; else if(hash == hash_url && !strcmp(name, "url")) machine_url = value; else if(action == 'A') { if(hash == hash_name && !strcmp(name, "name")) url_name = value; } else if(action == 'D') { if(hash == hash_delete_url && !strcmp(name, "delete_url")) delete_url = value; } else if(action == 'S') { if(hash == hash_for && !strcmp(name, "for")) search_machine_guid = value; } else if(action == 'W') { if(hash == hash_to && !strcmp(name, "to")) to_person_guid = value; } #ifdef NETDATA_INTERNAL_CHECKS else error("unused registry URL parameter '%s' with value '%s'", name, value); #endif /* NETDATA_INTERNAL_CHECKS */ } if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) { buffer_flush(w->response.data); buffer_sprintf(w->response.data, "Your web browser is sending 'DNT: 1' (Do Not Track). The registry requires persistent cookies on your browser to work."); return 400; } if(unlikely(action == 'H')) { // HELLO request, dashboard ACL if(unlikely(!web_client_can_access_dashboard(w))) return web_client_permission_denied(w); } else { // everything else, registry ACL if(unlikely(!web_client_can_access_registry(w))) return web_client_permission_denied(w); } switch(action) { case 'A': if(unlikely(!machine_guid || !machine_url || !url_name)) { error("Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')", machine_guid ? machine_guid : "UNSET", machine_url ? machine_url : "UNSET", url_name ? url_name : "UNSET"); buffer_flush(w->response.data); buffer_strcat(w->response.data, "Invalid registry Access request."); return 400; } web_client_enable_tracking_required(w); return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec()); case 'D': if(unlikely(!machine_guid || !machine_url || !delete_url)) { error("Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET"); buffer_flush(w->response.data); buffer_strcat(w->response.data, "Invalid registry Delete request."); return 400; } web_client_enable_tracking_required(w); return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec()); case 'S': if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) { error("Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET"); buffer_flush(w->response.data); buffer_strcat(w->response.data, "Invalid registry Search request."); return 400; } web_client_enable_tracking_required(w); return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec()); case 'W': if(unlikely(!machine_guid || !machine_url || !to_person_guid)) { error("Invalid registry request - switching identity requires these parameters: machine ('%s'), url ('%s'), to ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", to_person_guid?to_person_guid:"UNSET"); buffer_flush(w->response.data); buffer_strcat(w->response.data, "Invalid registry Switch request."); return 400; } web_client_enable_tracking_required(w); return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec()); case 'H': return registry_request_hello_json(host, w); default: buffer_flush(w->response.data); buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search"); return 400; } }
// returns the HTTP code inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) { debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url); int ret = 400; BUFFER *dimensions = NULL; buffer_flush(w->response.data); char *google_version = "0.6", *google_reqId = "0", *google_sig = "0", *google_out = "json", *responseHandler = NULL, *outFileName = NULL; time_t last_timestamp_in_data = 0, google_timestamp = 0; char *chart = NULL , *before_str = NULL , *after_str = NULL , *group_time_str = NULL , *points_str = NULL; int group = RRDR_GROUPING_AVERAGE; uint32_t format = DATASOURCE_JSON; uint32_t options = 0x00000000; while(url) { char *value = mystrsep(&url, "?&"); if(!value || !*value) continue; char *name = mystrsep(&value, "="); if(!name || !*name) continue; if(!value || !*value) continue; debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value); // name and value are now the parameters // they are not null and not empty if(!strcmp(name, "chart")) chart = value; else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) { if(!dimensions) dimensions = buffer_create(100); buffer_strcat(dimensions, "|"); buffer_strcat(dimensions, value); } else if(!strcmp(name, "after")) after_str = value; else if(!strcmp(name, "before")) before_str = value; else if(!strcmp(name, "points")) points_str = value; else if(!strcmp(name, "gtime")) group_time_str = value; else if(!strcmp(name, "group")) { group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE); } else if(!strcmp(name, "format")) { format = web_client_api_request_v1_data_format(value); } else if(!strcmp(name, "options")) { options |= web_client_api_request_v1_data_options(value); } else if(!strcmp(name, "callback")) { responseHandler = value; } else if(!strcmp(name, "filename")) { outFileName = value; } else if(!strcmp(name, "tqx")) { // parse Google Visualization API options // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source char *tqx_name, *tqx_value; while(value) { tqx_value = mystrsep(&value, ";"); if(!tqx_value || !*tqx_value) continue; tqx_name = mystrsep(&tqx_value, ":"); if(!tqx_name || !*tqx_name) continue; if(!tqx_value || !*tqx_value) continue; if(!strcmp(tqx_name, "version")) google_version = tqx_value; else if(!strcmp(tqx_name, "reqId")) google_reqId = tqx_value; else if(!strcmp(tqx_name, "sig")) { google_sig = tqx_value; google_timestamp = strtoul(google_sig, NULL, 0); } else if(!strcmp(tqx_name, "out")) { google_out = tqx_value; format = web_client_api_request_v1_data_google_format(google_out); } else if(!strcmp(tqx_name, "responseHandler")) responseHandler = tqx_value; else if(!strcmp(tqx_name, "outFileName")) outFileName = tqx_value; } } } if(!chart || !*chart) { buffer_sprintf(w->response.data, "No chart id is given at the request."); goto cleanup; } RRDSET *st = rrdset_find(host, chart); if(!st) st = rrdset_find_byname(host, chart); if(!st) { buffer_strcat(w->response.data, "Chart is not found: "); buffer_strcat_htmlescape(w->response.data, chart); ret = 404; goto cleanup; } st->last_accessed_time = now_realtime_sec(); long long before = (before_str && *before_str)?str2l(before_str):0; long long after = (after_str && *after_str) ?str2l(after_str):0; int points = (points_str && *points_str)?str2i(points_str):0; long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0; debug(D_WEB_CLIENT, "%llu: API command 'data' for chart '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', format '%u', options '0x%08x'" , w->id , chart , (dimensions)?buffer_tostring(dimensions):"" , after , before , points , group , format , options ); if(outFileName && *outFileName) { buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName); debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName); } if(format == DATASOURCE_DATATABLE_JSONP) { if(responseHandler == NULL) responseHandler = "google.visualization.Query.setResponse"; debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'", w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName ); buffer_sprintf(w->response.data, "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:", responseHandler, google_version, google_reqId, st->last_updated.tv_sec); } else if(format == DATASOURCE_JSONP) { if(responseHandler == NULL) responseHandler = "callback"; buffer_strcat(w->response.data, responseHandler); buffer_strcat(w->response.data, "("); } ret = rrdset2anything_api_v1(st, w->response.data, dimensions, format, points, after, before, group, group_time , options, &last_timestamp_in_data); if(format == DATASOURCE_DATATABLE_JSONP) { if(google_timestamp < last_timestamp_in_data) buffer_strcat(w->response.data, "});"); else { // the client already has the latest data buffer_flush(w->response.data); buffer_sprintf(w->response.data, "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});", responseHandler, google_version, google_reqId); } } else if(format == DATASOURCE_JSONP) buffer_strcat(w->response.data, ");"); cleanup: buffer_free(dimensions); return ret; }
static void get_system_timezone(void) { // avoid flood calls to stat(/etc/localtime) // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux const char *tz = getenv("TZ"); if(!tz || !*tz) setenv("TZ", config_get(CONFIG_SECTION_GLOBAL, "TZ environment variable", ":/etc/localtime"), 0); char buffer[FILENAME_MAX + 1] = ""; const char *timezone = NULL; ssize_t ret; // use the TZ variable if(tz && *tz && *tz != ':') { timezone = tz; // info("TIMEZONE: using TZ variable '%s'", timezone); } // use the contents of /etc/timezone if(!timezone && !read_file("/etc/timezone", buffer, FILENAME_MAX)) { timezone = buffer; // info("TIMEZONE: using the contents of /etc/timezone: '%s'", timezone); } // read the link /etc/localtime if(!timezone) { ret = readlink("/etc/localtime", buffer, FILENAME_MAX); if(ret > 0) { buffer[ret] = '\0'; char *cmp = "/usr/share/zoneinfo/"; size_t cmp_len = strlen(cmp); char *s = strstr(buffer, cmp); if (s && s[cmp_len]) { timezone = &s[cmp_len]; // info("TIMEZONE: using the link of /etc/localtime: '%s'", timezone); } } else buffer[0] = '\0'; } // find the timezone from strftime() if(!timezone) { time_t t; struct tm *tmp, tmbuf; t = now_realtime_sec(); tmp = localtime_r(&t, &tmbuf); if (tmp != NULL) { if(strftime(buffer, FILENAME_MAX, "%Z", tmp) == 0) buffer[0] = '\0'; else { buffer[FILENAME_MAX] = '\0'; timezone = buffer; // info("TIMEZONE: using strftime(): '%s'", timezone); } } } if(timezone && *timezone) { // make sure it does not have illegal characters // info("TIMEZONE: fixing '%s'", timezone); size_t len = strlen(timezone); char tmp[len + 1]; char *d = tmp; *d = '\0'; while(*timezone) { if(isalnum(*timezone) || *timezone == '_' || *timezone == '/') *d++ = *timezone++; else timezone++; } *d = '\0'; strncpyz(buffer, tmp, len); timezone = buffer; // info("TIMEZONE: fixed as '%s'", timezone); } if(!timezone || !*timezone) timezone = "unknown"; netdata_configured_timezone = config_get(CONFIG_SECTION_GLOBAL, "timezone", timezone); }
RRDHOST *rrdhost_create(const char *hostname, const char *guid, const char *os, int update_every, long entries, RRD_MEMORY_MODE memory_mode, int health_enabled, int rrdpush_enabled, char *rrdpush_destination, char *rrdpush_api_key, int is_localhost ) { debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid); rrd_check_wrlock(); RRDHOST *host = callocz(1, sizeof(RRDHOST)); host->rrd_update_every = (update_every > 0)?update_every:1; host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries); host->rrd_memory_mode = memory_mode; host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled; host->rrdpush_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key); host->rrdpush_destination = (host->rrdpush_enabled)?strdupz(rrdpush_destination):NULL; host->rrdpush_api_key = (host->rrdpush_enabled)?strdupz(rrdpush_api_key):NULL; host->rrdpush_pipe[0] = -1; host->rrdpush_pipe[1] = -1; host->rrdpush_socket = -1; netdata_mutex_init(&host->rrdpush_mutex); netdata_rwlock_init(&host->rrdhost_rwlock); rrdhost_init_hostname(host, hostname); rrdhost_init_machine_guid(host, guid); rrdhost_init_os(host, os); avl_init_lock(&(host->rrdset_root_index), rrdset_compare); avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name); avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare); avl_init_lock(&(host->variables_root_index), rrdvar_compare); if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1)) rrdhost_flag_set(host, RRDHOST_DELETE_OBSOLETE_FILES); if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost) rrdhost_flag_set(host, RRDHOST_DELETE_ORPHAN_FILES); // ------------------------------------------------------------------------ // initialize health variables host->health_log.next_log_id = 1; host->health_log.next_alarm_id = 1; host->health_log.max = 1000; host->health_log.next_log_id = host->health_log.next_alarm_id = (uint32_t)now_realtime_sec(); long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max); if(n < 10) { error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max); config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max); } else host->health_log.max = (unsigned int)n; netdata_rwlock_init(&host->health_log.alarm_log_rwlock); char filename[FILENAME_MAX + 1]; if(is_localhost) { host->cache_dir = strdupz(netdata_configured_cache_dir); host->varlib_dir = strdupz(netdata_configured_varlib_dir); } else { // this is not localhost - append our GUID to localhost path snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid); host->cache_dir = strdupz(filename); if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) { int r = mkdir(host->cache_dir, 0775); if(r != 0 && errno != EEXIST) error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir); } snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid); host->varlib_dir = strdupz(filename); if(host->health_enabled) { int r = mkdir(host->varlib_dir, 0775); if(r != 0 && errno != EEXIST) error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir); } } if(host->health_enabled) { snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir); int r = mkdir(filename, 0775); if(r != 0 && errno != EEXIST) error("Host '%s': cannot create directory '%s'", host->hostname, filename); } snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir); host->health_log_filename = strdupz(filename); snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir); host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename)); host->health_default_recipient = strdup("root"); // ------------------------------------------------------------------------ // load health configuration if(host->health_enabled) { health_alarm_log_load(host); health_alarm_log_open(host); rrdhost_wrlock(host); health_readdir(host, health_config_dir()); rrdhost_unlock(host); } // ------------------------------------------------------------------------ // link it and add it to the index if(is_localhost) { host->next = localhost; localhost = host; } else { if(localhost) { host->next = localhost->next; localhost->next = host; } else localhost = host; } RRDHOST *t = rrdhost_index_add(host); if(t != host) { error("Host '%s': cannot add host with machine guid '%s' to index. It already exists as host '%s' with machine guid '%s'.", host->hostname, host->machine_guid, t->hostname, t->machine_guid); rrdhost_free(host); host = NULL; } else { info("Host '%s' with guid '%s' initialized" ", os %s" ", update every %d" ", memory mode %s" ", history entries %ld" ", streaming %s" " (to '%s' with api key '%s')" ", health %s" ", cache_dir '%s'" ", varlib_dir '%s'" ", health_log '%s'" ", alarms default handler '%s'" ", alarms default recipient '%s'" , host->hostname , host->machine_guid , host->os , host->rrd_update_every , rrd_memory_mode_name(host->rrd_memory_mode) , host->rrd_history_entries , host->rrdpush_enabled?"enabled":"disabled" , host->rrdpush_destination?host->rrdpush_destination:"" , host->rrdpush_api_key?host->rrdpush_api_key:"" , host->health_enabled?"enabled":"disabled" , host->cache_dir , host->varlib_dir , host->health_log_filename , host->health_default_exec , host->health_default_recipient ); } rrd_hosts_available++; return host; }
int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *url) { int ret = 400; buffer_flush(w->response.data); BUFFER *dimensions = NULL; const char *chart = NULL , *before_str = NULL , *after_str = NULL , *points_str = NULL , *multiply_str = NULL , *divide_str = NULL , *label = NULL , *units = NULL , *label_color = NULL , *value_color = NULL , *refresh_str = NULL , *precision_str = NULL , *scale_str = NULL , *alarm = NULL; int group = GROUP_AVERAGE; uint32_t options = 0x00000000; while(url) { char *value = mystrsep(&url, "/?&"); if(!value || !*value) continue; char *name = mystrsep(&value, "="); if(!name || !*name) continue; if(!value || !*value) continue; debug(D_WEB_CLIENT, "%llu: API v1 badge.svg query param '%s' with value '%s'", w->id, name, value); // name and value are now the parameters // they are not null and not empty if(!strcmp(name, "chart")) chart = value; else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) { if(!dimensions) dimensions = buffer_create(100); buffer_strcat(dimensions, "|"); buffer_strcat(dimensions, value); } else if(!strcmp(name, "after")) after_str = value; else if(!strcmp(name, "before")) before_str = value; else if(!strcmp(name, "points")) points_str = value; else if(!strcmp(name, "group")) { group = web_client_api_request_v1_data_group(value, GROUP_AVERAGE); } else if(!strcmp(name, "options")) { options |= web_client_api_request_v1_data_options(value); } else if(!strcmp(name, "label")) label = value; else if(!strcmp(name, "units")) units = value; else if(!strcmp(name, "label_color")) label_color = value; else if(!strcmp(name, "value_color")) value_color = value; else if(!strcmp(name, "multiply")) multiply_str = value; else if(!strcmp(name, "divide")) divide_str = value; else if(!strcmp(name, "refresh")) refresh_str = value; else if(!strcmp(name, "precision")) precision_str = value; else if(!strcmp(name, "scale")) scale_str = value; else if(!strcmp(name, "alarm")) alarm = value; } if(!chart || !*chart) { buffer_no_cacheable(w->response.data); buffer_sprintf(w->response.data, "No chart id is given at the request."); goto cleanup; } int scale = (scale_str && *scale_str)?str2i(scale_str):100; RRDSET *st = rrdset_find(host, chart); if(!st) st = rrdset_find_byname(host, chart); if(!st) { buffer_no_cacheable(w->response.data); buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1, scale, 0); ret = 200; goto cleanup; } st->last_accessed_time = now_realtime_sec(); RRDCALC *rc = NULL; if(alarm) { rc = rrdcalc_find(st, alarm); if (!rc) { buffer_no_cacheable(w->response.data); buffer_svg(w->response.data, "alarm not found", NAN, "", NULL, NULL, -1, scale, 0); ret = 200; goto cleanup; } } long long multiply = (multiply_str && *multiply_str )?str2l(multiply_str):1; long long divide = (divide_str && *divide_str )?str2l(divide_str):1; long long before = (before_str && *before_str )?str2l(before_str):0; long long after = (after_str && *after_str )?str2l(after_str):-st->update_every; int points = (points_str && *points_str )?str2i(points_str):1; int precision = (precision_str && *precision_str)?str2i(precision_str):-1; if(!multiply) multiply = 1; if(!divide) divide = 1; int refresh = 0; if(refresh_str && *refresh_str) { if(!strcmp(refresh_str, "auto")) { if(rc) refresh = rc->update_every; else if(options & RRDR_OPTION_NOT_ALIGNED) refresh = st->update_every; else { refresh = (int)(before - after); if(refresh < 0) refresh = -refresh; } } else { refresh = str2i(refresh_str); if(refresh < 0) refresh = -refresh; } } if(!label) { if(alarm) { char *s = (char *)alarm; while(*s) { if(*s == '_') *s = ' '; s++; } label = alarm; } else if(dimensions) { const char *dim = buffer_tostring(dimensions); if(*dim == '|') dim++; label = dim; } else label = st->name; } if(!units) { if(alarm) { if(rc->units) units = rc->units; else units = ""; } else if(options & RRDR_OPTION_PERCENTAGE) units = "%"; else units = st->units; } debug(D_WEB_CLIENT, "%llu: API command 'badge.svg' for chart '%s', alarm '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', options '0x%08x'" , w->id , chart , alarm?alarm:"" , (dimensions)?buffer_tostring(dimensions):"" , after , before , points , group , options ); if(rc) { if (refresh > 0) { buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh); w->response.data->expires = now_realtime_sec() + refresh; } else buffer_no_cacheable(w->response.data); if(!value_color) { switch(rc->status) { case RRDCALC_STATUS_CRITICAL: value_color = "red"; break; case RRDCALC_STATUS_WARNING: value_color = "orange"; break; case RRDCALC_STATUS_CLEAR: value_color = "brightgreen"; break; case RRDCALC_STATUS_UNDEFINED: value_color = "lightgrey"; break; case RRDCALC_STATUS_UNINITIALIZED: value_color = "#000"; break; default: value_color = "grey"; break; } } buffer_svg(w->response.data, label, (isnan(rc->value)||isinf(rc->value)) ? rc->value : rc->value * multiply / divide, units, label_color, value_color, precision, scale, options ); ret = 200; } else { time_t latest_timestamp = 0; int value_is_null = 1; calculated_number n = NAN; ret = 500; // if the collected value is too old, don't calculate its value if (rrdset_last_entry_t(st) >= (now_realtime_sec() - (st->update_every * st->gap_when_lost_iterations_above))) ret = rrdset2value_api_v1(st, w->response.data, &n, (dimensions) ? buffer_tostring(dimensions) : NULL , points, after, before, group, 0, options, NULL, &latest_timestamp, &value_is_null); // if the value cannot be calculated, show empty badge if (ret != 200) { buffer_no_cacheable(w->response.data); value_is_null = 1; n = 0; ret = 200; } else if (refresh > 0) { buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh); w->response.data->expires = now_realtime_sec() + refresh; } else buffer_no_cacheable(w->response.data); // render the badge buffer_svg(w->response.data, label, (value_is_null)?NAN:(n * multiply / divide), units, label_color, value_color, precision, scale, options ); } cleanup: buffer_free(dimensions); return ret; }
unsigned long rrdset_info2json_api_old(RRDSET *st, char *options, BUFFER *wb) { time_t now = now_realtime_sec(); rrdset_rdlock(st); st->last_accessed_time = now; buffer_sprintf(wb, "\t\t{\n" "\t\t\t\"id\": \"%s\",\n" "\t\t\t\"name\": \"%s\",\n" "\t\t\t\"type\": \"%s\",\n" "\t\t\t\"family\": \"%s\",\n" "\t\t\t\"context\": \"%s\",\n" "\t\t\t\"title\": \"%s\",\n" "\t\t\t\"priority\": %ld,\n" "\t\t\t\"enabled\": %d,\n" "\t\t\t\"units\": \"%s\",\n" "\t\t\t\"url\": \"/data/%s/%s\",\n" "\t\t\t\"chart_type\": \"%s\",\n" "\t\t\t\"counter\": %lu,\n" "\t\t\t\"entries\": %ld,\n" "\t\t\t\"first_entry_t\": %ld,\n" "\t\t\t\"last_entry\": %lu,\n" "\t\t\t\"last_entry_t\": %ld,\n" "\t\t\t\"last_entry_secs_ago\": %ld,\n" "\t\t\t\"update_every\": %d,\n" "\t\t\t\"isdetail\": %d,\n" "\t\t\t\"usec_since_last_update\": %llu,\n" "\t\t\t\"collected_total\": " TOTAL_NUMBER_FORMAT ",\n" "\t\t\t\"last_collected_total\": " TOTAL_NUMBER_FORMAT ",\n" "\t\t\t\"dimensions\": [\n" , st->id , st->name , st->type , st->family , st->context , st->title , st->priority , rrdset_flag_check(st, RRDSET_FLAG_ENABLED)?1:0 , st->units , st->name, options?options:"" , rrdset_type_name(st->chart_type) , st->counter , st->entries , rrdset_first_entry_t(st) , rrdset_last_slot(st) , rrdset_last_entry_t(st) , (now < rrdset_last_entry_t(st)) ? (time_t)0 : now - rrdset_last_entry_t(st) , st->update_every , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?1:0 , st->usec_since_last_update , st->collected_total , st->last_collected_total ); unsigned long memory = st->memsize; RRDDIM *rd; rrddim_foreach_read(rd, st) { memory += rd->memsize; buffer_sprintf(wb, "\t\t\t\t{\n" "\t\t\t\t\t\"id\": \"%s\",\n" "\t\t\t\t\t\"name\": \"%s\",\n" "\t\t\t\t\t\"entries\": %ld,\n" "\t\t\t\t\t\"isHidden\": %d,\n" "\t\t\t\t\t\"algorithm\": \"%s\",\n" "\t\t\t\t\t\"multiplier\": " COLLECTED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"divisor\": " COLLECTED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"last_entry_t\": %ld,\n" "\t\t\t\t\t\"collected_value\": " COLLECTED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"last_collected_value\": " COLLECTED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"last_calculated_value\": " CALCULATED_NUMBER_FORMAT ",\n" "\t\t\t\t\t\"memory\": %lu\n" "\t\t\t\t}%s\n" , rd->id , rd->name , rd->entries , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?1:0 , rrd_algorithm_name(rd->algorithm) , rd->multiplier , rd->divisor , rd->last_collected_time.tv_sec , rd->collected_value , rd->calculated_value , rd->last_collected_value , rd->last_calculated_value , rd->memsize , rd->next?",":"" ); }
void *backends_main(void *ptr) { netdata_thread_cleanup_push(backends_main_cleanup, ptr); int default_port = 0; int sock = -1; BUFFER *b = buffer_create(1), *response = buffer_create(1); int (*backend_request_formatter)(BUFFER *, const char *, RRDHOST *, const char *, RRDSET *, RRDDIM *, time_t, time_t, uint32_t) = NULL; int (*backend_response_checker)(BUFFER *) = NULL; // ------------------------------------------------------------------------ // collect configuration options struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 }; int enabled = config_get_boolean(CONFIG_SECTION_BACKEND, "enabled", 0); const char *source = config_get(CONFIG_SECTION_BACKEND, "data source", "average"); const char *type = config_get(CONFIG_SECTION_BACKEND, "type", "graphite"); const char *destination = config_get(CONFIG_SECTION_BACKEND, "destination", "localhost"); backend_prefix = config_get(CONFIG_SECTION_BACKEND, "prefix", "netdata"); const char *hostname = config_get(CONFIG_SECTION_BACKEND, "hostname", localhost->hostname); backend_update_every = (int)config_get_number(CONFIG_SECTION_BACKEND, "update every", backend_update_every); int buffer_on_failures = (int)config_get_number(CONFIG_SECTION_BACKEND, "buffer on failures", 10); long timeoutms = config_get_number(CONFIG_SECTION_BACKEND, "timeout ms", backend_update_every * 2 * 1000); backend_send_names = config_get_boolean(CONFIG_SECTION_BACKEND, "send names instead of ids", backend_send_names); charts_pattern = simple_pattern_create(config_get(CONFIG_SECTION_BACKEND, "send charts matching", "*"), NULL, SIMPLE_PATTERN_EXACT); hosts_pattern = simple_pattern_create(config_get(CONFIG_SECTION_BACKEND, "send hosts matching", "localhost *"), NULL, SIMPLE_PATTERN_EXACT); // ------------------------------------------------------------------------ // validate configuration options // and prepare for sending data to our backend backend_options = backend_parse_data_source(source, backend_options); if(timeoutms < 1) { error("BACKEND: invalid timeout %ld ms given. Assuming %d ms.", timeoutms, backend_update_every * 2 * 1000); timeoutms = backend_update_every * 2 * 1000; } timeout.tv_sec = (timeoutms * 1000) / 1000000; timeout.tv_usec = (timeoutms * 1000) % 1000000; if(!enabled || backend_update_every < 1) goto cleanup; // ------------------------------------------------------------------------ // select the backend type if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) { default_port = 2003; backend_response_checker = process_graphite_response; if((backend_options & BACKEND_SOURCE_BITS) == BACKEND_SOURCE_DATA_AS_COLLECTED) backend_request_formatter = format_dimension_collected_graphite_plaintext; else backend_request_formatter = format_dimension_stored_graphite_plaintext; } else if(!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) { default_port = 4242; backend_response_checker = process_opentsdb_response; if((backend_options & BACKEND_SOURCE_BITS) == BACKEND_SOURCE_DATA_AS_COLLECTED) backend_request_formatter = format_dimension_collected_opentsdb_telnet; else backend_request_formatter = format_dimension_stored_opentsdb_telnet; } else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext")) { default_port = 5448; backend_response_checker = process_json_response; if ((backend_options & BACKEND_SOURCE_BITS) == BACKEND_SOURCE_DATA_AS_COLLECTED) backend_request_formatter = format_dimension_collected_json_plaintext; else backend_request_formatter = format_dimension_stored_json_plaintext; } else { error("BACKEND: Unknown backend type '%s'", type); goto cleanup; } if(backend_request_formatter == NULL || backend_response_checker == NULL) { error("BACKEND: backend is misconfigured - disabling it."); goto cleanup; } // ------------------------------------------------------------------------ // prepare the charts for monitoring the backend operation struct rusage thread; collected_number chart_buffered_metrics = 0, chart_lost_metrics = 0, chart_sent_metrics = 0, chart_buffered_bytes = 0, chart_received_bytes = 0, chart_sent_bytes = 0, chart_receptions = 0, chart_transmission_successes = 0, chart_transmission_failures = 0, chart_data_lost_events = 0, chart_lost_bytes = 0, chart_backend_reconnects = 0, chart_backend_latency = 0; RRDSET *chart_metrics = rrdset_create_localhost("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", "backends", NULL, 130600, backend_update_every, RRDSET_TYPE_LINE); rrddim_add(chart_metrics, "buffered", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_metrics, "lost", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_metrics, "sent", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); RRDSET *chart_bytes = rrdset_create_localhost("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", "backends", NULL, 130610, backend_update_every, RRDSET_TYPE_AREA); rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_bytes, "lost", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_bytes, "sent", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE); RRDSET *chart_ops = rrdset_create_localhost("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", "backends", NULL, 130630, backend_update_every, RRDSET_TYPE_LINE); rrddim_add(chart_ops, "write", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_ops, "discard", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_ops, "failure", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); rrddim_add(chart_ops, "read", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); /* * this is misleading - we can only measure the time we need to send data * this time is not related to the time required for the data to travel to * the backend database and the time that server needed to process them * * issue #1432 and https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html * RRDSET *chart_latency = rrdset_create_localhost("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", "backends", NULL, 130620, backend_update_every, RRDSET_TYPE_AREA); rrddim_add(chart_latency, "latency", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE); */ RRDSET *chart_rusage = rrdset_create_localhost("netdata", "backend_thread_cpu", NULL, "backend", NULL, "NetData Backend Thread CPU usage", "milliseconds/s", "backends", NULL, 130630, backend_update_every, RRDSET_TYPE_STACKED); rrddim_add(chart_rusage, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL); rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL); // ------------------------------------------------------------------------ // prepare the backend main loop info("BACKEND: configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, backend_update_every, hostname, backend_prefix); usec_t step_ut = backend_update_every * USEC_PER_SEC; time_t after = now_realtime_sec(); int failures = 0; heartbeat_t hb; heartbeat_init(&hb); while(!netdata_exit) { // ------------------------------------------------------------------------ // Wait for the next iteration point. heartbeat_next(&hb, step_ut); time_t before = now_realtime_sec(); debug(D_BACKEND, "BACKEND: preparing buffer for timeframe %lu to %lu", (unsigned long)after, (unsigned long)before); // ------------------------------------------------------------------------ // add to the buffer the data we need to send to the backend netdata_thread_disable_cancelability(); size_t count_hosts = 0; size_t count_charts_total = 0; size_t count_dims_total = 0; rrd_rdlock(); RRDHOST *host; rrdhost_foreach_read(host) { if(unlikely(!rrdhost_flag_check(host, RRDHOST_FLAG_BACKEND_SEND|RRDHOST_FLAG_BACKEND_DONT_SEND))) { char *name = (host == localhost)?"localhost":host->hostname; if (!hosts_pattern || simple_pattern_matches(hosts_pattern, name)) { rrdhost_flag_set(host, RRDHOST_FLAG_BACKEND_SEND); info("enabled backend for host '%s'", name); } else { rrdhost_flag_set(host, RRDHOST_FLAG_BACKEND_DONT_SEND); info("disabled backend for host '%s'", name); } } if(unlikely(!rrdhost_flag_check(host, RRDHOST_FLAG_BACKEND_SEND))) continue; rrdhost_rdlock(host); count_hosts++; size_t count_charts = 0; size_t count_dims = 0; size_t count_dims_skipped = 0; const char *__hostname = (host == localhost)?hostname:host->hostname; RRDSET *st; rrdset_foreach_read(st, host) { if(likely(backends_can_send_rrdset(backend_options, st))) { rrdset_rdlock(st); count_charts++; RRDDIM *rd; rrddim_foreach_read(rd, st) { if (likely(rd->last_collected_time.tv_sec >= after)) { chart_buffered_metrics += backend_request_formatter(b, backend_prefix, host, __hostname, st, rd, after, before, backend_options); count_dims++; } else { debug(D_BACKEND, "BACKEND: not sending dimension '%s' of chart '%s' from host '%s', its last data collection (%lu) is not within our timeframe (%lu to %lu)", rd->id, st->id, __hostname, (unsigned long)rd->last_collected_time.tv_sec, (unsigned long)after, (unsigned long)before); count_dims_skipped++; } } rrdset_unlock(st); } } debug(D_BACKEND, "BACKEND: sending host '%s', metrics of %zu dimensions, of %zu charts. Skipped %zu dimensions.", __hostname, count_dims, count_charts, count_dims_skipped); count_charts_total += count_charts; count_dims_total += count_dims; rrdhost_unlock(host); } rrd_unlock(); netdata_thread_enable_cancelability(); debug(D_BACKEND, "BACKEND: buffer has %zu bytes, added metrics for %zu dimensions, of %zu charts, from %zu hosts", buffer_strlen(b), count_dims_total, count_charts_total, count_hosts); // ------------------------------------------------------------------------ chart_buffered_bytes = (collected_number)buffer_strlen(b); // reset the monitoring chart counters chart_received_bytes = chart_sent_bytes = chart_sent_metrics = chart_lost_metrics = chart_transmission_successes = chart_transmission_failures = chart_data_lost_events = chart_lost_bytes = chart_backend_reconnects = chart_backend_latency = 0; if(unlikely(netdata_exit)) break; //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME //fprintf(stderr, "after = %lu, before = %lu\n", after, before); // prepare for the next iteration // to add incrementally data to buffer after = before; // ------------------------------------------------------------------------ // if we are connected, receive a response, without blocking if(likely(sock != -1)) { errno = 0; // loop through to collect all data while(sock != -1 && errno != EWOULDBLOCK) { buffer_need_bytes(response, 4096); ssize_t r = recv(sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT); if(likely(r > 0)) { // we received some data response->len += r; chart_received_bytes += r; chart_receptions++; } else if(r == 0) { error("BACKEND: '%s' closed the socket", destination); close(sock); sock = -1; } else { // failed to receive data if(errno != EAGAIN && errno != EWOULDBLOCK) { error("BACKEND: cannot receive data from backend '%s'.", destination); } } } // if we received data, process them if(buffer_strlen(response)) backend_response_checker(response); } // ------------------------------------------------------------------------ // if we are not connected, connect to a backend server if(unlikely(sock == -1)) { usec_t start_ut = now_monotonic_usec(); size_t reconnects = 0; sock = connect_to_one_of(destination, default_port, &timeout, &reconnects, NULL, 0); chart_backend_reconnects += reconnects; chart_backend_latency += now_monotonic_usec() - start_ut; } if(unlikely(netdata_exit)) break; // ------------------------------------------------------------------------ // if we are connected, send our buffer to the backend server if(likely(sock != -1)) { size_t len = buffer_strlen(b); usec_t start_ut = now_monotonic_usec(); int flags = 0; #ifdef MSG_NOSIGNAL flags += MSG_NOSIGNAL; #endif ssize_t written = send(sock, buffer_tostring(b), len, flags); chart_backend_latency += now_monotonic_usec() - start_ut; if(written != -1 && (size_t)written == len) { // we sent the data successfully chart_transmission_successes++; chart_sent_bytes += written; chart_sent_metrics = chart_buffered_metrics; // reset the failures count failures = 0; // empty the buffer buffer_flush(b); } else { // oops! we couldn't send (all or some of the) data error("BACKEND: failed to write data to database backend '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.", destination, len, written); chart_transmission_failures++; if(written != -1) chart_sent_bytes += written; // increment the counter we check for data loss failures++; // close the socket - we will re-open it next time close(sock); sock = -1; } } else { error("BACKEND: failed to update database backend '%s'", destination); chart_transmission_failures++; // increment the counter we check for data loss failures++; } if(failures > buffer_on_failures) { // too bad! we are going to lose data chart_lost_bytes += buffer_strlen(b); error("BACKEND: reached %d backend failures. Flushing buffers to protect this host - this results in data loss on back-end server '%s'", failures, destination); buffer_flush(b); failures = 0; chart_data_lost_events++; chart_lost_metrics = chart_buffered_metrics; } if(unlikely(netdata_exit)) break; // ------------------------------------------------------------------------ // update the monitoring charts if(likely(chart_ops->counter_done)) rrdset_next(chart_ops); rrddim_set(chart_ops, "read", chart_receptions); rrddim_set(chart_ops, "write", chart_transmission_successes); rrddim_set(chart_ops, "discard", chart_data_lost_events); rrddim_set(chart_ops, "failure", chart_transmission_failures); rrddim_set(chart_ops, "reconnect", chart_backend_reconnects); rrdset_done(chart_ops); if(likely(chart_metrics->counter_done)) rrdset_next(chart_metrics); rrddim_set(chart_metrics, "buffered", chart_buffered_metrics); rrddim_set(chart_metrics, "lost", chart_lost_metrics); rrddim_set(chart_metrics, "sent", chart_sent_metrics); rrdset_done(chart_metrics); if(likely(chart_bytes->counter_done)) rrdset_next(chart_bytes); rrddim_set(chart_bytes, "buffered", chart_buffered_bytes); rrddim_set(chart_bytes, "lost", chart_lost_bytes); rrddim_set(chart_bytes, "sent", chart_sent_bytes); rrddim_set(chart_bytes, "received", chart_received_bytes); rrdset_done(chart_bytes); /* if(likely(chart_latency->counter_done)) rrdset_next(chart_latency); rrddim_set(chart_latency, "latency", chart_backend_latency); rrdset_done(chart_latency); */ getrusage(RUSAGE_THREAD, &thread); if(likely(chart_rusage->counter_done)) rrdset_next(chart_rusage); rrddim_set(chart_rusage, "user", thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec); rrddim_set(chart_rusage, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec); rrdset_done(chart_rusage); if(likely(buffer_strlen(b) == 0)) chart_buffered_metrics = 0; if(unlikely(netdata_exit)) break; } cleanup: if(sock != -1) close(sock); buffer_free(b); buffer_free(response); netdata_thread_cleanup_pop(1); return NULL; }
void charts2json(RRDHOST *host, BUFFER *wb) { static char *custom_dashboard_info_js_filename = NULL; size_t c, dimensions = 0, memory = 0, alarms = 0; RRDSET *st; time_t now = now_realtime_sec(); if(unlikely(!custom_dashboard_info_js_filename)) custom_dashboard_info_js_filename = config_get(CONFIG_SECTION_WEB, "custom dashboard_info.js", ""); buffer_sprintf(wb, "{\n" "\t\"hostname\": \"%s\"" ",\n\t\"version\": \"%s\"" ",\n\t\"os\": \"%s\"" ",\n\t\"timezone\": \"%s\"" ",\n\t\"update_every\": %d" ",\n\t\"history\": %ld" ",\n\t\"custom_info\": \"%s\"" ",\n\t\"charts\": {" , host->hostname , host->program_version , host->os , host->timezone , host->rrd_update_every , host->rrd_history_entries , custom_dashboard_info_js_filename ); c = 0; rrdhost_rdlock(host); rrdset_foreach_read(st, host) { if(rrdset_is_available_for_viewers(st)) { if(c) buffer_strcat(wb, ","); buffer_strcat(wb, "\n\t\t\""); buffer_strcat(wb, st->id); buffer_strcat(wb, "\": "); rrdset2json(st, wb, &dimensions, &memory); c++; st->last_accessed_time = now; } } RRDCALC *rc; for(rc = host->alarms; rc ; rc = rc->next) { if(rc->rrdset) alarms++; } rrdhost_unlock(host); buffer_sprintf(wb , "\n\t}" ",\n\t\"charts_count\": %zu" ",\n\t\"dimensions_count\": %zu" ",\n\t\"alarms_count\": %zu" ",\n\t\"rrd_memory_bytes\": %zu" ",\n\t\"hosts_count\": %zu" ",\n\t\"hosts\": [" , c , dimensions , alarms , memory , rrd_hosts_available ); if(unlikely(rrd_hosts_available > 1)) { rrd_rdlock(); size_t found = 0; RRDHOST *h; rrdhost_foreach_read(h) { if(!rrdhost_should_be_removed(h, host, now)) { buffer_sprintf(wb , "%s\n\t\t{" "\n\t\t\t\"hostname\": \"%s\"" "\n\t\t}" , (found > 0) ? "," : "" , h->hostname ); found++; } } rrd_unlock(); }
void *nfacct_main(void *ptr) { if(ptr) { ; } info("NFACCT thread created with task id %d", gettid()); if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) error("nfacct.plugin: Cannot set pthread cancel type to DEFERRED."); if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) error("nfacct.plugin: Cannot set pthread cancel state to ENABLE."); char buf[MNL_SOCKET_BUFFER_SIZE]; struct mnl_socket *nl = NULL; struct nlmsghdr *nlh = NULL; unsigned int seq = 0, portid = 0; seq = now_realtime_sec() - 1; nl = mnl_socket_open(NETLINK_NETFILTER); if(!nl) { error("nfacct.plugin: mnl_socket_open() failed"); pthread_exit(NULL); return NULL; } if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { mnl_socket_close(nl); error("nfacct.plugin: mnl_socket_bind() failed"); pthread_exit(NULL); return NULL; } portid = mnl_socket_get_portid(nl); // ------------------------------------------------------------------------ struct timeval last, now; usec_t usec = 0, susec = 0; RRDSET *st = NULL; now_realtime_timeval(&last); // ------------------------------------------------------------------------ while(1) { if(unlikely(netdata_exit)) break; seq++; nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_GET, NLM_F_DUMP, seq); if(!nlh) { mnl_socket_close(nl); error("nfacct.plugin: nfacct_nlmsg_build_hdr() failed"); pthread_exit(NULL); return NULL; } if(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { error("nfacct.plugin: mnl_socket_send"); pthread_exit(NULL); return NULL; } if(nfacct_list) nfacct_list->len = 0; int ret; while((ret = mnl_socket_recvfrom(nl, buf, sizeof(buf))) > 0) { if((ret = mnl_cb_run(buf, ret, seq, portid, nfacct_callback, NULL)) <= 0) break; } if (ret == -1) { error("nfacct.plugin: error communicating with kernel."); pthread_exit(NULL); return NULL; } // -------------------------------------------------------------------- now_realtime_timeval(&now); usec = dt_usec(&now, &last) - susec; debug(D_NFACCT_LOOP, "nfacct.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).", usec + susec, usec, susec); if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec; else susec = rrd_update_every * 1000000ULL / 2ULL; // -------------------------------------------------------------------- if(nfacct_list && nfacct_list->len) { int i; st = rrdset_find_bytype("netfilter", "nfacct_packets"); if(!st) { st = rrdset_create("netfilter", "nfacct_packets", NULL, "nfacct", NULL, "Netfilter Accounting Packets", "packets/s", 3206, rrd_update_every, RRDSET_TYPE_STACKED); for(i = 0; i < nfacct_list->len ; i++) rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL); } else rrdset_next(st); for(i = 0; i < nfacct_list->len ; i++) { RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name); if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, rrd_update_every, RRDDIM_INCREMENTAL); if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].pkts); } rrdset_done(st); // ---------------------------------------------------------------- st = rrdset_find_bytype("netfilter", "nfacct_bytes"); if(!st) { st = rrdset_create("netfilter", "nfacct_bytes", NULL, "nfacct", NULL, "Netfilter Accounting Bandwidth", "kilobytes/s", 3207, rrd_update_every, RRDSET_TYPE_STACKED); for(i = 0; i < nfacct_list->len ; i++) rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL); } else rrdset_next(st); for(i = 0; i < nfacct_list->len ; i++) { RRDDIM *rd = rrddim_find(st, nfacct_list->data[i].name); if(!rd) rd = rrddim_add(st, nfacct_list->data[i].name, NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL); if(rd) rrddim_set_by_pointer(st, rd, nfacct_list->data[i].bytes); } rrdset_done(st); } // -------------------------------------------------------------------- usleep(susec); // copy current to last memmove(&last, &now, sizeof(struct timeval)); } mnl_socket_close(nl); pthread_exit(NULL); return NULL; }