/* add a status code item to holder */ static void add_status_code_node (GHolder * h, int hits, char *data, unsigned long long bw) { GSubList *sub_list; char *type = NULL, *status = NULL; int type_idx = -1; type = verify_status_code_type (data); status = verify_status_code (data); type_idx = get_item_idx_in_holder (h, type); if (type_idx == -1) { h->items[h->idx].bw += bw; h->items[h->idx].hits += hits; h->items[h->idx].data = xstrdup (type); /* data (child) */ sub_list = new_gsublist (); add_sub_item_back (sub_list, h->module, status, hits, bw); h->items[h->idx++].sub_list = sub_list; h->sub_items_size++; } else { sub_list = h->items[type_idx].sub_list; add_sub_item_back (sub_list, h->module, status, hits, bw); h->items[type_idx].sub_list = sub_list; h->items[type_idx].bw += bw; h->items[type_idx].hits += hits; h->sub_items_size++; } }
/* add a browser item to holder */ static void add_os_browser_node (GHolder * h, int hits, char *data, unsigned long long bw) { GSubList *sub_list; char *type = NULL; int type_idx = -1; if (h->module == OS) type = verify_os (data, OPESYS_TYPE); else type = verify_browser (data, BROWSER_TYPE); type_idx = get_item_idx_in_holder (h, type); if (type_idx == -1) { h->items[h->idx].bw += bw; h->items[h->idx].hits += hits; h->items[h->idx].data = xstrdup (type); /* data (child) */ sub_list = new_gsublist (); add_sub_item_back (sub_list, h->module, data, hits, bw); h->items[h->idx++].sub_list = sub_list; h->sub_items_size++; } else { sub_list = h->items[type_idx].sub_list; add_sub_item_back (sub_list, h->module, data, hits, bw); h->items[type_idx].sub_list = sub_list; h->items[type_idx].bw += bw; h->items[type_idx].hits += hits; h->sub_items_size++; } free (type); }
static void add_host_child_to_holder (GHolder * h) { GMetrics *nmetrics; GSubList *sub_list = new_gsublist (); char *host = h->items[h->idx].metrics->data; char *hostname = NULL; int n = h->sub_items_size; /* add child nodes */ set_host_sub_list (h, sub_list); pthread_mutex_lock (&gdns_thread.mutex); hostname = get_hostname (host); pthread_mutex_unlock (&gdns_thread.mutex); /* hostname */ if (!hostname) { dns_resolver (host); } else if (hostname) { set_host_child_metrics (hostname, MTRC_ID_HOSTNAME, &nmetrics); add_sub_item_back (sub_list, h->module, nmetrics); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; free (hostname); } /* did not add any items */ if (n == h->sub_items_size) free (sub_list); }
/* should be faster than sorting the list */ void sort_sub_list (GHolder * h, GSort sort) { int i, j, k; GHolderItem *arr_items; for (i = 0; i < h->idx; i++) { GSubList *sub_list = h->items[i].sub_list; GSubItem *iter; arr_items = new_gholder_item (sub_list->size); /* copy items from the linked-list into an rray */ for (j = 0, iter = sub_list->head; iter; iter = iter->next) { arr_items[j].data = (char *) iter->data; arr_items[j++].hits = iter->hits; } sort_holder_items (arr_items, j, sort); delete_sub_list (sub_list); sub_list = new_gsublist (); for (k = 0; k < j; k++) { if (k > 0) sub_list = h->items[i].sub_list; add_sub_item_back (sub_list, h->module, arr_items[k].data, arr_items[k].hits, 0); h->items[i].sub_list = sub_list; } free (arr_items); } }
/* add a host item to holder */ static void add_host_node (GHolder * h, int hits, char *data, unsigned long long bw, unsigned long long usecs) { GSubList *sub_list = new_gsublist (); char *ip = xstrdup (data); gpointer value_ptr; gboolean found; #ifdef HAVE_LIBGEOIP const char *addr = data; const char *location = NULL; #endif h->items[h->idx].bw += bw; h->items[h->idx].hits += hits; h->items[h->idx].data = xstrdup (data); if (conf.serve_usecs) h->items[h->idx].usecs = usecs; h->items[h->idx].sub_list = sub_list; #ifdef HAVE_LIBGEOIP location = get_geoip_data (addr); add_sub_item_back (sub_list, h->module, location, hits, bw); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; #endif pthread_mutex_lock (&gdns_thread.mutex); found = g_hash_table_lookup_extended (ht_hostnames, ip, NULL, &value_ptr); pthread_mutex_unlock (&gdns_thread.mutex); if (!found) { dns_resolver (ip); } else if (value_ptr) { add_sub_item_back (sub_list, h->module, (char *) value_ptr, hits, bw); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; } free (ip); h->idx++; }
static void set_host_sub_list (GHolder * h, GSubList * sub_list) { GMetrics *nmetrics; #ifdef HAVE_LIBGEOIP char city[CITY_LEN] = ""; char continent[CONTINENT_LEN] = ""; char country[COUNTRY_LEN] = ""; #endif char *host = h->items[h->idx].metrics->data, *hostname = NULL; #ifdef HAVE_LIBGEOIP /* add geolocation child nodes */ set_geolocation (host, continent, country, city); /* country */ if (country[0] != '\0') { set_host_child_metrics (country, MTRC_ID_COUNTRY, &nmetrics); add_sub_item_back (sub_list, h->module, nmetrics); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; } /* city */ if (city[0] != '\0') { set_host_child_metrics (city, MTRC_ID_CITY, &nmetrics); add_sub_item_back (sub_list, h->module, nmetrics); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; } #endif /* hostname */ if (conf.enable_html_resolver && conf.output_html) { hostname = reverse_ip (host); set_host_child_metrics (hostname, MTRC_ID_HOSTNAME, &nmetrics); add_sub_item_back (sub_list, h->module, nmetrics); h->items[h->idx].sub_list = sub_list; h->sub_items_size++; free (hostname); } }
static void add_root_to_holder (GRawDataItem item, GHolder * h, GO_UNUSED const GPanel * panel) { GDataMap *map; GSubList *sub_list; GMetrics *metrics, *nmetrics; char *root = NULL; int data_nkey = 0, root_idx = KEY_NOT_FOUND, idx = 0; data_nkey = (*(int *) item.key); map = (GDataMap *) item.value; if (map == NULL) return; if (set_root_metrics (data_nkey, map, h->module, &nmetrics) == 1) return; if (!(root = (get_root_from_key (map->root, h->module)))) return; /* add data as a child node into holder */ if (KEY_NOT_FOUND == (root_idx = get_item_idx_in_holder (h, root))) { idx = h->idx; sub_list = new_gsublist (); metrics = new_gmetrics (); h->items[idx].metrics = metrics; h->items[idx].metrics->data = root; h->idx++; } else { sub_list = h->items[root_idx].sub_list; metrics = h->items[root_idx].metrics; idx = root_idx; free (root); } add_sub_item_back (sub_list, h->module, nmetrics); h->items[idx].sub_list = sub_list; h->items[idx].metrics = metrics; h->items[idx].metrics->avgts.nts += nmetrics->avgts.nts; h->items[idx].metrics->cumts.nts += nmetrics->cumts.nts; h->items[idx].metrics->maxts.nts += nmetrics->maxts.nts; h->items[idx].metrics->bw.nbw += nmetrics->bw.nbw; h->items[idx].metrics->hits += nmetrics->hits; h->items[idx].metrics->visitors += nmetrics->visitors; h->sub_items_size++; }
/* Copy linked-list items to an array, sort, and move them back * to the list. Should be faster than sorting the list */ static void sort_sub_list (GHolder * h, GSort sort) { GHolderItem *arr; GSubItem *iter; GSubList *sub_list; int i, j, k; /* iterate over root-level nodes */ for (i = 0; i < h->idx; i++) { sub_list = h->items[i].sub_list; if (sub_list == NULL) continue; arr = new_gholder_item (sub_list->size); /* copy items from the linked-list into an array */ for (j = 0, iter = sub_list->head; iter; iter = iter->next, j++) { arr[j].metrics = new_gmetrics (); arr[j].metrics->bw.nbw = iter->metrics->bw.nbw; arr[j].metrics->data = xstrdup (iter->metrics->data); arr[j].metrics->hits = iter->metrics->hits; arr[j].metrics->id = iter->metrics->id; arr[j].metrics->visitors = iter->metrics->visitors; if (conf.serve_usecs) { arr[j].metrics->avgts.nts = iter->metrics->avgts.nts; arr[j].metrics->cumts.nts = iter->metrics->cumts.nts; arr[j].metrics->maxts.nts = iter->metrics->maxts.nts; } } sort_holder_items (arr, j, sort); delete_sub_list (sub_list); sub_list = new_gsublist (); for (k = 0; k < j; k++) { if (k > 0) sub_list = h->items[i].sub_list; add_sub_item_back (sub_list, h->module, arr[k].metrics); h->items[i].sub_list = sub_list; } free (arr); } }