Skip to content

Commit

Permalink
Added ability to get Windows marketing name using --real-os.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Feb 5, 2014
1 parent fc72bd2 commit eee4093
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 45 deletions.
48 changes: 43 additions & 5 deletions gdashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,18 +1191,51 @@ add_host_node (GHolder * h, int hits, char *data, unsigned long long bw,
h->idx++;
}

static void
add_os_node (GHolder * h, GOpeSys * opesys, char *data, unsigned long long bw)
{
GSubList *sub_list;
int type_idx = -1;

type_idx = get_item_idx_in_holder (h, opesys->os_type);
if (type_idx == -1) {
h->items[h->idx].bw += bw;
h->items[h->idx].hits += opesys->hits;
h->items[h->idx].data = xstrdup (opesys->os_type);

/* data (child) */
sub_list = new_gsublist ();
add_sub_item_back (sub_list, h->module, data, opesys->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, opesys->hits, bw);

h->items[type_idx].sub_list = sub_list;
h->items[type_idx].bw += bw;
h->items[type_idx].hits += opesys->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)
{
char *opsys = NULL, *type = NULL;
char optype[OPESYS_TYPE_LEN];
GSubList *sub_list;
char *type = NULL;
int type_idx = -1;

if (h->module == OS)
type = verify_os (data, OPESYS_TYPE);
else
if (h->module == OS) {
opsys = verify_os (data, optype);
if (opsys == NULL)
return;
type = optype;
} else {
type = verify_browser (data, BROWSER_TYPE);
}

type_idx = get_item_idx_in_holder (h, type);
if (type_idx == -1) {
Expand All @@ -1224,7 +1257,10 @@ add_os_browser_node (GHolder * h, int hits, char *data, unsigned long long bw)
h->items[type_idx].hits += hits;
h->sub_items_size++;
}
free (type);
if (h->module == BROWSERS)
free (type);
if (h->module == OS)
free (opsys);
}

/* add request items (e.g., method, protocol, request) to holder */
Expand Down Expand Up @@ -1584,6 +1620,8 @@ load_data_to_holder (GRawData * raw_data, GHolder * h, GModule module,
add_request_node (h, raw_data->items[i].value, data, bw);
break;
case OS:
add_os_node (h, raw_data->items[i].value, data, 0);
break;
case BROWSERS:
hits = GPOINTER_TO_INT (raw_data->items[i].value);
add_os_browser_node (h, hits, data, bw);
Expand Down
10 changes: 10 additions & 0 deletions goaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ free_countries (GO_UNUSED gpointer old_key, gpointer old_value,
}
#endif

static void
free_os (GO_UNUSED gpointer old_key, gpointer old_value,
GO_UNUSED gpointer user_data)
{
GOpeSys *opesys = old_value;
free (opesys);
}

static void
free_requests (GO_UNUSED gpointer old_key, gpointer old_value,
GO_UNUSED gpointer user_data)
Expand Down Expand Up @@ -239,6 +247,8 @@ house_keeping (void)
g_hash_table_foreach (ht_requests_static, free_requests, NULL);
g_hash_table_foreach (ht_not_found_requests, free_requests, NULL);

g_hash_table_foreach (ht_os, free_os, NULL);

free (logger);

/* realpath() uses malloc */
Expand Down
29 changes: 27 additions & 2 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,30 @@ process_request_meta (GHashTable * ht, char *key, unsigned long long size)
return 0;
}

static int
process_opesys (GHashTable * ht, const char *key, const char *os_type)
{
GOpeSys *opesys;
if (ht == NULL)
return (EINVAL);

opesys = g_hash_table_lookup (ht, key);
if (opesys != NULL) {
opesys->hits++;
} else {
opesys = xcalloc (1, sizeof (GOpeSys));

strncpy (opesys->os_type, os_type, OPESYS_TYPE_LEN);
opesys->os_type[OPESYS_TYPE_LEN - 1] = '\0';
opesys->hits = 1;
}

/* replace the entry. old key will be freed by "free_os" */
g_hash_table_replace (ht, g_strdup (key), opesys);

return 0;
}

static int
process_request (GHashTable * ht, const char *key, const GLogItem * log)
{
Expand Down Expand Up @@ -546,6 +570,7 @@ process_unique_data (char *host, char *date, char *agent)
char *browser_key = NULL, *browser = NULL;
char *opsys = NULL, *os_key = NULL;
char visitor_key[UKEY_BUFFER];
char optype[OPESYS_TYPE_LEN];

a = deblank (xstrdup (agent));
snprintf (visitor_key, sizeof (visitor_key), "%s|%s|%s", host, date, a);
Expand All @@ -562,13 +587,13 @@ process_unique_data (char *host, char *date, char *agent)

/* extract browser & OS from agent */
browser = verify_browser (browser_key, BROWSER);
opsys = verify_os (os_key, OPESYS);

if (browser != NULL)
process_generic_data (ht_browsers, browser);

opsys = verify_os (os_key, optype);
if (opsys != NULL)
process_generic_data (ht_os, opsys);
process_opesys (ht_os, opsys, optype);

if ((date = strchr (visitor_key, '|')) != NULL) {
char *tmp_date = NULL;
Expand Down
73 changes: 53 additions & 20 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,27 @@ get_continent_name_and_code (const char *continentid)
}
#endif

/* get Windows marketing name */
static char *
get_real_win (const char *win)
{
if (strstr (win, "6.3"))
return alloc_string ("Windows 8.1");
else if (strstr (win, "6.2"))
return alloc_string ("Windows 8");
else if (strstr (win, "6.1"))
return alloc_string ("Windows 7");
else if (strstr (win, "6.0"))
return alloc_string ("Windows Vista");
else if (strstr (win, "5.2"))
return alloc_string ("Windows XP x64");
else if (strstr (win, "5.1"))
return alloc_string ("Windows XP");
else if (strstr (win, "5.0"))
return alloc_string ("Windows 2000");
return NULL;
}

/* get Mac OS X Codename */
static char *
get_real_mac_osx (const char *osx)
Expand Down Expand Up @@ -727,30 +748,38 @@ file_size (const char *filename)
}

char *
verify_os (const char *str, GOpeSysStr o_type)
verify_os (const char *str, char *os_type)
{
char *a, *b, *p;
const char *lookfor;
int space = 0;
size_t i;

if (str == NULL || *str == '\0')
return (NULL);
return NULL;

for (i = 0; i < ARRAY_SIZE (os); i++) {
if ((a = strstr (str, os[i][0])) == NULL)
continue;

/* windows */
if ((lookfor = "Windows", strstr (str, lookfor)) != NULL) {
strncpy (os_type, os[i][1], OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return conf.real_os &&
(b = get_real_win (a)) ? b : alloc_string (os[i][0]);
}

/* agents w/ space in between */
if ((lookfor = "Windows", strstr (str, lookfor)) != NULL ||
(lookfor = "iPhone OS", strstr (str, lookfor)) != NULL ||
if ((lookfor = "iPhone OS", strstr (str, lookfor)) != NULL ||
(lookfor = "Red Hat", strstr (str, lookfor)) != NULL ||
(lookfor = "Win", strstr (str, lookfor)) != NULL) {

if (o_type == OPESYS)
return alloc_string (os[i][0]);
else
return alloc_string (os[i][1]);
strncpy (os_type, os[i][1], OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return alloc_string (os[i][0]);
}

/* parse mac os x */
Expand All @@ -767,10 +796,10 @@ verify_os (const char *str, GOpeSysStr o_type)
}
*p = 0;

if (o_type == OPESYS)
return conf.real_os ? get_real_mac_osx (a) : alloc_string (a);
else
return alloc_string (os[i][1]);
strncpy (os_type, os[i][1], OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return conf.real_os ? get_real_mac_osx (a) : alloc_string (a);
}

/* parse android */
Expand All @@ -781,14 +810,14 @@ verify_os (const char *str, GOpeSysStr o_type)
p++;
*p = 0;

if (o_type == OPESYS)
return alloc_string (a);
else
return alloc_string (os[i][1]);
strncpy (os_type, os[i][1], OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return alloc_string (a);
}

if (!(b = a))
return (NULL);
return NULL;

/* all others */
for (p = a; *p; p++) {
Expand All @@ -800,11 +829,15 @@ verify_os (const char *str, GOpeSysStr o_type)
}
*p = 0;

if (o_type == OPESYS)
return alloc_string (b);
return alloc_string (os[i][1]);
strncpy (os_type, os[i][1], OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return alloc_string (b);
}

strncpy (os_type, "Unknown", OPESYS_TYPE_LEN);
os_type[OPESYS_TYPE_LEN - 1] = '\0';

return alloc_string ("Unknown");
}

Expand Down
38 changes: 20 additions & 18 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

#define COUNTRY_LEN 48
#define CONTINENT_LEN 48
#define REGEX_ERROR 100
#define DATE_LEN 12 /* date length */
#define KB 1024
#define MB (KB * 1024)
#define GB (MB * 1024)
#define OPESYS_TYPE_LEN 12
#define BROWER_TYPE_LEN 12
#define COUNTRY_LEN 48
#define CONTINENT_LEN 48
#define REGEX_ERROR 100
#define DATE_LEN 12 /* date length */
#define KB 1024
#define MB (KB * 1024)
#define GB (MB * 1024)

#define MILS 1000ULL
#define SECS 1000000ULL
#define MINS 60000000ULL
#define HOUR 3600000000ULL
#define MILS 1000ULL
#define SECS 1000000ULL
#define MINS 60000000ULL
#define HOUR 3600000000ULL

typedef enum
{
BROWSER,
BROWSER_TYPE
} GBrowserStr;

typedef enum
typedef struct GOpeSys_
{
OPESYS,
OPESYS_TYPE
} GOpeSysStr;
char os_type[OPESYS_TYPE_LEN];
int hits;
} GOpeSys;

typedef struct GLocation_
{
Expand All @@ -62,18 +64,18 @@ char *clean_month (char *s);
char *convert_date (char *result, char *data, const char *from, const char *to,
int size);
char *deblank (char *str);
char *escape_str (const char *src);
char *filesize_str (unsigned long long log_size);
char *int_to_str (int d);
char *replace_str (const char *str, const char *old, const char *new);
char *reverse_ip (char *str);
char *secs_to_str (int secs);
char *substring (const char *str, int begin, int len);
char *trim_str (char *str);
char *unescape_str (const char *src);
char *usecs_to_str (unsigned long long usec);
char *verify_browser (const char *str, GBrowserStr b_type);
char *verify_os (const char *str, GOpeSysStr o_type);
char *unescape_str (const char *src);
char *escape_str (const char *src);
char *verify_os (const char *str, char *os_type);
const char *get_continent_name_and_code (const char *continentid);
const char *verify_status_code (char *str);
const char *verify_status_code_type (const char *str);
Expand Down

0 comments on commit eee4093

Please sign in to comment.