/* process data based on a unique key, this includes the following * modules, VISITORS, BROWSERS, OS */ static void process_unique_data (char *host, char *date, char *agent) { #ifdef HAVE_LIBGEOIP char city[CITY_LEN] = ""; char continent[CONTINENT_LEN] = ""; char country[COUNTRY_LEN] = ""; #endif char *a = NULL, *browser_key = NULL, *browser = NULL; char *os_key = NULL, *opsys = NULL; char visitor_key[UKEY_BUFFER]; char os_type[OPESYS_TYPE_LEN], browser_type[BROWSER_TYPE_LEN]; a = deblank (xstrdup (agent)); snprintf (visitor_key, sizeof (visitor_key), "%s|%s|%s", host, date, a); (visitor_key)[sizeof (visitor_key) - 1] = '\0'; free (a); /* Check if the unique visitor key exists, if not, * process hit as unique visitor. Includes, BROWSERS, OSs, VISITORS. */ if (process_generic_data (ht_unique_visitors, visitor_key) == -1) { process_generic_data (ht_unique_vis, date); browser_key = xstrdup (agent); os_key = xstrdup (agent); /* extract browser & OS from agent */ browser = verify_browser (browser_key, browser_type); if (browser != NULL) process_browser (ht_browsers, browser, browser_type); opsys = verify_os (os_key, os_type); if (opsys != NULL) process_opesys (ht_os, opsys, os_type); #ifdef HAVE_LIBGEOIP if (geo_location_data != NULL) { if (conf.geoip_city_data) geoip_get_city (host, city); geoip_get_country (host, country); geoip_get_continent (host, continent); process_geolocation (ht_countries, country, continent, city); } #endif } if (browser != NULL) free (browser); if (browser_key != NULL) free (browser_key); if (os_key != NULL) free (os_key); if (opsys != NULL) free (opsys); }
/* Entry point to set GeoIP location into the corresponding buffers, * (continent, country, city). * * On error, 1 is returned * On success, buffers are set and 0 is returned */ int set_geolocation (char *host, char *continent, char *country, char *city) { int type_ip = 0; if (geo_location_data == NULL) return 1; if (invalid_ipaddr (host, &type_ip)) return 1; geoip_get_country (host, country, type_ip); geoip_get_continent (host, continent, type_ip); if (conf.geoip_database) geoip_get_city (host, city, type_ip); return 0; }
/** * Generate JSON on complete fields for the following modules: * REQUESTS, REQUESTS_STATIC, NOT_FOUND, HOSTS */ static void print_json_complete (FILE * fp, GHolder * holder, int process) { #ifdef HAVE_LIBGEOIP char country[COUNTRY_LEN] = ""; char city[CITY_LEN] = ""; #endif char *data, *host, *method = NULL, *protocol = NULL; float percent; GHolder *h = holder; int i, j, hits; unsigned long long bw, usecs; for (i = 0; i < 4; i++) { switch (i) { case 0: h = holder + REQUESTS; fprintf (fp, "\t\"%s\": [\n", REQUE_ID); break; case 1: h = holder + REQUESTS_STATIC; fprintf (fp, "\t\"%s\": [\n", STATI_ID); break; case 2: h = holder + NOT_FOUND; fprintf (fp, "\t\"%s\": [\n", FOUND_ID); break; case 3: h = holder + HOSTS; fprintf (fp, "\t\"%s\": [\n", HOSTS_ID); break; } for (j = 0; j < h->idx; j++) { hits = h->items[j].hits; data = h->items[j].data; percent = get_percentage (process, hits); percent = percent < 0 ? 0 : percent; bw = h->items[j].bw; usecs = h->items[j].usecs; method = h->items[j].method; protocol = h->items[j].protocol; fprintf (fp, "\t\t{\n"); fprintf (fp, "\t\t\t\"hits\": \"%d\",\n", hits); fprintf (fp, "\t\t\t\"percent\": \"%4.2f%%\",\n", percent); fprintf (fp, "\t\t\t\"data\": \""); escape_json_output (fp, data); fprintf (fp, "\",\n"); fprintf (fp, "\t\t\t\"bytes\": \"%lld\"", bw); if (h->module == HOSTS) { if (conf.enable_html_resolver) { host = reverse_ip (data); fprintf (fp, ",\n\t\t\t\"host\": \""); escape_json_output (fp, host); fprintf (fp, "\""); free (host); } #ifdef HAVE_LIBGEOIP geoip_get_country (data, country); fprintf (fp, ",\n\t\t\t\"country\": \""); escape_json_output (fp, country); fprintf (fp, "\""); if (conf.geoip_city_data) { geoip_get_city (data, city); fprintf (fp, ",\n\t\t\t\"city\": \""); escape_json_output (fp, city); fprintf (fp, "\""); } #endif } if (conf.serve_usecs) fprintf (fp, ",\n\t\t\t\"time_served\": \"%lld\"", usecs); if (conf.append_protocol && protocol) fprintf (fp, ",\n\t\t\t\"protocol\": \"%s\"", protocol); if (conf.append_method && method) fprintf (fp, ",\n\t\t\t\"method\": \"%s\"", method); fprintf (fp, "\n\t\t}"); if (j != h->idx - 1) fprintf (fp, ",\n"); else fprintf (fp, "\n"); } if (i != 3) fprintf (fp, "\t],\n"); else fprintf (fp, "\t]\n"); } }