logswan

Fast Web log analyzer using probabilistic data structures
Log | Files | Refs | README | LICENSE

commit c1639bff071db7215085ab2bb050b5675f0641cb
parent 4509efd812f46824a5184cc9ad1b04dae8e1a98d
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date:   Sat, 19 Dec 2015 00:14:03 +0100

Process GeoIP continent information

Diffstat:
MTODO | 1-
Msrc/definitions.c | 20+++++++++++++++++++-
Msrc/definitions.h | 4+++-
Msrc/logswan.c | 10+++++++++-
Msrc/logswan.h | 3++-
Msrc/output.c | 10+++++++++-
Msrc/results.h | 3++-
7 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/TODO b/TODO @@ -1,4 +1,3 @@ - Use strtok_r instead of strtok to tokenize lines? -- Add GeoIP continent information - Companion tool to split logs by day - Create a manual page diff --git a/src/definitions.c b/src/definitions.c @@ -4,13 +4,31 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/11/02 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ /* */ /*****************************************************************************/ +char *continentsId[] = { + "AF", + "AS", + "EU", + "NA", + "OC", + "SA" +}; + +char *continentsNames[] = { + "Africa", + "Asia", + "Europe", + "North America", + "Oceania", + "South America" +}; + char *methods[] = { "OPTIONS", "GET", diff --git a/src/definitions.h b/src/definitions.h @@ -4,7 +4,7 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/11/02 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ @@ -14,6 +14,8 @@ #ifndef DEFINITIONS_H #define DEFINITIONS_H +extern char *continentsId[]; +extern char *continentsNames[]; extern char *methods[]; extern char *protocols[]; diff --git a/src/logswan.c b/src/logswan.c @@ -4,7 +4,7 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/12/14 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ @@ -138,6 +138,14 @@ int main (int argc, char *argv[]) { results.countries[countryId]++; + /* Increment continents array */ + for (int loop = 0; loop<CONTINENTS; loop++) { + if (!strcmp(continentsId[loop], GeoIP_continent_by_id(countryId))) { + results.continents[loop] ++; + break; + } + } + /* Unique visitors */ if (isIPv4) { hll_add(&uniqueIPv4, parsedLine.remoteHost, strlen(parsedLine.remoteHost)); diff --git a/src/logswan.h b/src/logswan.h @@ -4,7 +4,7 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/12/15 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ @@ -21,6 +21,7 @@ enum { LINE_MAX_LENGTH = 65536, STATUS_CODE_MAX = 512, + CONTINENTS = 6, METHODS = 9, PROTOCOLS = 2 }; diff --git a/src/output.c b/src/output.c @@ -4,7 +4,7 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/12/05 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ @@ -22,12 +22,19 @@ char *output(Results results) { json_t *jsonObject = json_object(); json_t *hitsObject = json_object(); json_t *visitsObject = json_object(); + json_t *continentsArray = json_array(); json_t *countriesArray = json_array(); json_t *hoursArray = json_array(); json_t *httpStatusArray = json_array(); json_t *methodsArray = json_array(); json_t *protocolsArray = json_array(); + for (int loop=0; loop<CONTINENTS; loop++) { + if (results.continents[loop]) { + json_array_append_new(continentsArray, json_pack("{s:s, s:s, s:i}", "data", continentsId[loop], "name", continentsNames[loop], "hits", results.continents[loop])); + } + } + for (int loop=0; loop<255; loop++) { if (results.countries[loop]) { json_array_append_new(countriesArray, json_pack("{s:s, s:s, s:i}", "data", GeoIP_code_by_id(loop), "name", GeoIP_name_by_id(loop), "hits", results.countries[loop])); @@ -76,6 +83,7 @@ char *output(Results results) { json_object_set_new(jsonObject, "runtime", json_real(results.runtime)); json_object_set_new(jsonObject, "hits", hitsObject); json_object_set_new(jsonObject, "visits", visitsObject); + json_object_set_new(jsonObject, "continents", continentsArray); json_object_set_new(jsonObject, "countries", countriesArray); json_object_set_new(jsonObject, "hours", hoursArray); json_object_set_new(jsonObject, "methods", methodsArray); diff --git a/src/results.h b/src/results.h @@ -4,7 +4,7 @@ /* https://github.com/fcambus/logswan */ /* */ /* Created: 2015/05/31 */ -/* Last Updated: 2015/12/05 */ +/* Last Updated: 2015/12/19 */ /* */ /* Logswan is released under the BSD 3-Clause license. */ /* See LICENSE file for details. */ @@ -26,6 +26,7 @@ struct results { uint64_t visits; uint64_t visitsIPv4; uint64_t visitsIPv6; + uint64_t continents[CONTINENTS]; uint64_t countries[255]; uint64_t hours[24]; uint64_t status[STATUS_CODE_MAX];