logswan

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

commit 521ef8e3adda3301db3e44bf7a25ce73c822ca65
parent 48631c7c9406b1831ef19306852a18b6b5481b41
Author: Frederic Cambus <fred@statdns.com>
Date:   Sun, 14 Feb 2021 23:39:00 +0100

Rename variables to get rid of all camelCase occurences.

Diffstat:
Msrc/config.c | 6+++---
Msrc/config.h | 6+++---
Msrc/continents.c | 6+++---
Msrc/continents.h | 6+++---
Msrc/countries.c | 6+++---
Msrc/countries.h | 6+++---
Msrc/logswan.c | 132++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/output.c | 32++++++++++++++++----------------
Msrc/output.h | 20++++++++++----------
Msrc/parse.c | 36++++++++++++++++++------------------
Msrc/parse.h | 12++++++------
11 files changed, 134 insertions(+), 134 deletions(-)

diff --git a/src/config.c b/src/config.c @@ -4,13 +4,13 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-10-17 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. */ -const char *methodsNames[] = { +const char *methods_names[] = { "GET", "POST", "HEAD", @@ -22,7 +22,7 @@ const char *methodsNames[] = { "PATCH" }; -const char *protocolsNames[] = { +const char *protocols_names[] = { "HTTP/1.1", "HTTP/1.0", "HTTP/2.0", diff --git a/src/config.h b/src/config.h @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2020-07-02 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -26,7 +26,7 @@ enum { PROTOCOLS = 4 }; -extern char *methodsNames[]; -extern char *protocolsNames[]; +extern char *methods_names[]; +extern char *protocols_names[]; #endif /* CONFIG_H */ diff --git a/src/continents.c b/src/continents.c @@ -4,13 +4,13 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. */ -const char *continentsId[] = { +const char *continents_id[] = { "AF", "AN", "AS", @@ -20,7 +20,7 @@ const char *continentsId[] = { "SA" }; -const char *continentsNames[] = { +const char *continents_names[] = { "Africa", "Antarctica", "Asia", diff --git a/src/continents.h b/src/continents.h @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -13,7 +13,7 @@ #ifndef CONTINENTS_H #define CONTINENTS_H -extern char *continentsId[]; -extern char *continentsNames[]; +extern char *continents_id[]; +extern char *continents_names[]; #endif /* CONTINENTS */ diff --git a/src/countries.c b/src/countries.c @@ -4,13 +4,13 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2020-07-02 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. */ -const char *countriesId[] = { +const char *countries_id[] = { "AD", "AE", "AF", @@ -263,7 +263,7 @@ const char *countriesId[] = { "ZW" }; -const char *countriesNames[] = { +const char *countries_names[] = { "Andorra", "United Arab Emirates", "Afghanistan", diff --git a/src/countries.h b/src/countries.h @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -13,8 +13,8 @@ #ifndef COUNTRIES_H #define COUNTRIES_H -extern char *countriesId[]; -extern char *countriesNames[]; +extern char *countries_id[]; +extern char *countries_names[]; #endif /* COUNTRIES */ diff --git a/src/logswan.c b/src/logswan.c @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2021-02-14 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -46,30 +46,30 @@ MMDB_s geoip2; struct timespec begin, end, elapsed; -char lineBuffer[LINE_LENGTH_MAX]; +char linebuffer[LINE_LENGTH_MAX]; struct results results; -struct date parsedDate; -struct logLine parsedLine; -struct request parsedRequest; +struct date parsed_date; +struct logline parsed_line; +struct request parsed_request; struct sockaddr_in ipv4; struct sockaddr_in6 ipv6; -bool isIPv4, isIPv6; +bool is_ipv4, is_ipv6; uint64_t bandwidth; -uint32_t statusCode; +uint32_t status_code; uint32_t hour; -FILE *logFile; -struct stat logFileStat; +FILE *logfile; +struct stat logfile_stat; const char *errstr; -int8_t getoptFlag; +int8_t opt; -struct HLL uniqueIPv4, uniqueIPv6; -char *intputFile; +struct HLL unique_ipv4, unique_ipv6; +char *input; char *db = NULL; static void @@ -105,11 +105,11 @@ main(int argc, char *argv[]) } #endif - hll_init(&uniqueIPv4, HLL_BITS); - hll_init(&uniqueIPv6, HLL_BITS); + hll_init(&unique_ipv4, HLL_BITS); + hll_init(&unique_ipv6, HLL_BITS); - while ((getoptFlag = getopt(argc, argv, "d:ghv")) != -1) { - switch (getoptFlag) { + while ((opt = getopt(argc, argv, "d:ghv")) != -1) { + switch (opt) { case 'd': db = optarg; break; @@ -129,7 +129,7 @@ main(int argc, char *argv[]) } if (optind < argc) { - intputFile = argv[optind]; + input = argv[optind]; } else { displayUsage(); return EXIT_SUCCESS; @@ -148,76 +148,76 @@ main(int argc, char *argv[]) } /* Open log file */ - if (!strcmp(intputFile, "-")) { + if (!strcmp(input, "-")) { /* Read from standard input */ - logFile = stdin; + logfile = stdin; } else { /* Attempt to read from file */ - if (!(logFile = fopen(intputFile, "r"))) { + if (!(logfile = fopen(input, "r"))) { perror("Can't open log file"); return EXIT_FAILURE; } } /* Get log file size */ - if (fstat(fileno(logFile), &logFileStat)) { + if (fstat(fileno(logfile), &logfile_stat)) { perror("Can't stat log file"); return EXIT_FAILURE; } - results.fileName = intputFile; - results.fileSize = logFileStat.st_size; + results.file_name = input; + results.file_size = logfile_stat.st_size; - while (fgets(lineBuffer, LINE_LENGTH_MAX, logFile)) { + while (fgets(linebuffer, LINE_LENGTH_MAX, logfile)) { /* Parse and tokenize line */ - parseLine(&parsedLine, lineBuffer); + parseLine(&parsed_line, linebuffer); /* Detect if remote host is IPv4 or IPv6 */ - if (parsedLine.remoteHost) { /* Do not feed NULL tokens to inet_pton */ - if ((isIPv4 = inet_pton(AF_INET, parsedLine.remoteHost, &ipv4.sin_addr))) { - isIPv6 = false; + if (parsed_line.remote_host) { /* Do not feed NULL tokens to inet_pton */ + if ((is_ipv4 = inet_pton(AF_INET, parsed_line.remote_host, &ipv4.sin_addr))) { + is_ipv6 = false; } else { - isIPv6 = inet_pton(AF_INET6, parsedLine.remoteHost, &ipv6.sin6_addr); + is_ipv6 = inet_pton(AF_INET6, parsed_line.remote_host, &ipv6.sin6_addr); - if (!isIPv6) { - results.invalidLines++; + if (!is_ipv6) { + results.invalid_lines++; continue; } } } else { /* Invalid line */ - results.invalidLines++; + results.invalid_lines++; continue; } - if (isIPv4) { + if (is_ipv4) { /* Increment hits counter */ - results.hitsIPv4++; + results.hits_ipv4++; /* Unique visitors */ - hll_add(&uniqueIPv4, parsedLine.remoteHost, strlen(parsedLine.remoteHost)); + hll_add(&unique_ipv4, parsed_line.remote_host, strlen(parsed_line.remote_host)); } - if (isIPv6) { + if (is_ipv6) { /* Increment hits counter */ - results.hitsIPv6++; + results.hits_ipv6++; /* Unique visitors */ - hll_add(&uniqueIPv6, parsedLine.remoteHost, strlen(parsedLine.remoteHost)); + hll_add(&unique_ipv6, parsed_line.remote_host, strlen(parsed_line.remote_host)); } if (geoip) { MMDB_entry_data_s entry_data; memset(&entry_data, 0, sizeof(MMDB_entry_data_s)); - lookup = MMDB_lookup_string(&geoip2, parsedLine.remoteHost, &gai_error, &mmdb_error); + lookup = MMDB_lookup_string(&geoip2, parsed_line.remote_host, &gai_error, &mmdb_error); MMDB_get_value(&lookup.entry, &entry_data, "country", "iso_code", NULL); if (entry_data.has_data) { /* Increment countries array */ for (size_t loop = 0; loop < COUNTRIES; loop++) { - if (!strncmp(countriesId[loop], entry_data.utf8_string, 2)) { + if (!strncmp(countries_id[loop], entry_data.utf8_string, 2)) { results.countries[loop]++; break; } @@ -229,7 +229,7 @@ main(int argc, char *argv[]) if (entry_data.has_data) { /* Increment continents array */ for (size_t loop = 0; loop < CONTINENTS; loop++) { - if (!strncmp(continentsId[loop], entry_data.utf8_string, 2)) { + if (!strncmp(continents_id[loop], entry_data.utf8_string, 2)) { results.continents[loop]++; break; } @@ -238,11 +238,11 @@ main(int argc, char *argv[]) } /* Hourly distribution */ - if (parsedLine.date) { - parseDate(&parsedDate, parsedLine.date); + if (parsed_line.date) { + parseDate(&parsed_date, parsed_line.date); - if (parsedDate.hour) { - hour = strtonum(parsedDate.hour, 0, 23, &errstr); + if (parsed_date.hour) { + hour = strtonum(parsed_date.hour, 0, 23, &errstr); if (!errstr) { results.hours[hour]++; @@ -251,21 +251,21 @@ main(int argc, char *argv[]) } /* Parse request */ - if (parsedLine.request) { - parseRequest(&parsedRequest, parsedLine.request); + if (parsed_line.request) { + parseRequest(&parsed_request, parsed_line.request); - if (parsedRequest.method) { + if (parsed_request.method) { for (size_t loop = 0; loop < METHODS; loop++) { - if (!strcmp(methodsNames[loop], parsedRequest.method)) { + if (!strcmp(methods_names[loop], parsed_request.method)) { results.methods[loop]++; break; } } } - if (parsedRequest.protocol) { + if (parsed_request.protocol) { for (size_t loop = 0; loop < PROTOCOLS; loop++) { - if (!strcmp(protocolsNames[loop], parsedRequest.protocol)) { + if (!strcmp(protocols_names[loop], parsed_request.protocol)) { results.protocols[loop]++; break; } @@ -274,17 +274,17 @@ main(int argc, char *argv[]) } /* Count HTTP status codes occurrences */ - if (parsedLine.statusCode) { - statusCode = strtonum(parsedLine.statusCode, 0, STATUS_CODE_MAX-1, &errstr); + if (parsed_line.status_code) { + status_code = strtonum(parsed_line.status_code, 0, STATUS_CODE_MAX-1, &errstr); if (!errstr) { - results.status[statusCode]++; + results.status[status_code]++; } } /* Increment bandwidth usage */ - if (parsedLine.objectSize) { - bandwidth = strtonum(parsedLine.objectSize, 0, INT64_MAX, &errstr); + if (parsed_line.object_size) { + bandwidth = strtonum(parsed_line.object_size, 0, INT64_MAX, &errstr); if (!errstr) { results.bandwidth += bandwidth; @@ -293,13 +293,13 @@ main(int argc, char *argv[]) } /* Counting hits and processed lines */ - results.hits = results.hitsIPv4 + results.hitsIPv6; - results.processedLines = results.hits + results.invalidLines; + results.hits = results.hits_ipv4 + results.hits_ipv6; + results.processed_lines = results.hits + results.invalid_lines; /* Counting unique visitors */ - results.visitsIPv4 = hll_count(&uniqueIPv4); - results.visitsIPv6 = hll_count(&uniqueIPv6); - results.visits = results.visitsIPv4 + results.visitsIPv6; + results.visits_ipv4 = hll_count(&unique_ipv4); + results.visits_ipv6 = hll_count(&unique_ipv6); + results.visits = results.visits_ipv4 + results.visits_ipv6; /* Stopping timer */ clock_gettime(CLOCK_MONOTONIC, &end); @@ -309,19 +309,19 @@ main(int argc, char *argv[]) /* Generate timestamp */ time_t now = time(NULL); - strftime(results.timeStamp, 20, "%Y-%m-%d %H:%M:%S", localtime(&now)); + strftime(results.timestamp, 20, "%Y-%m-%d %H:%M:%S", localtime(&now)); /* Printing results */ fprintf(stdout, "%s\n", output(&results)); - fprintf(stderr, "Processed %" PRIu64 " lines in %f seconds.\n", results.processedLines, results.runtime); + fprintf(stderr, "Processed %" PRIu64 " lines in %f seconds.\n", results.processed_lines, results.runtime); /* Clean up */ - fclose(logFile); + fclose(logfile); MMDB_close(&geoip2); - hll_destroy(&uniqueIPv4); - hll_destroy(&uniqueIPv6); + hll_destroy(&unique_ipv4); + hll_destroy(&unique_ipv6); return EXIT_SUCCESS; } diff --git a/src/output.c b/src/output.c @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -38,8 +38,8 @@ char json_array_append_new( continents, json_pack("{s:s, s:s, s:i}", - "data", continentsId[loop], - "name", continentsNames[loop], + "data", continents_id[loop], + "name", continents_names[loop], "hits", results->continents[loop])); } } @@ -49,8 +49,8 @@ char json_array_append_new( countries, json_pack("{s:s, s:s, s:i}", - "data", countriesId[loop], - "name", countriesNames[loop], + "data", countries_id[loop], + "name", countries_names[loop], "hits", results->countries[loop])); } } @@ -80,7 +80,7 @@ char json_array_append_new( methods, json_pack("{s:s, s:i}", - "data", methodsNames[loop], + "data", methods_names[loop], "hits", results->methods[loop])); } } @@ -90,25 +90,25 @@ char json_array_append_new( protocols, json_pack("{s:s, s:i}", - "data", protocolsNames[loop], + "data", protocols_names[loop], "hits", results->protocols[loop])); } } - json_object_set_new(hits, "ipv4", json_integer(results->hitsIPv4)); - json_object_set_new(hits, "ipv6", json_integer(results->hitsIPv6)); + json_object_set_new(hits, "ipv4", json_integer(results->hits_ipv4)); + json_object_set_new(hits, "ipv6", json_integer(results->hits_ipv6)); json_object_set_new(hits, "total", json_integer(results->hits)); - json_object_set_new(visits, "ipv4", json_integer(results->visitsIPv4)); - json_object_set_new(visits, "ipv6", json_integer(results->visitsIPv6)); + json_object_set_new(visits, "ipv4", json_integer(results->visits_ipv4)); + json_object_set_new(visits, "ipv6", json_integer(results->visits_ipv6)); json_object_set_new(visits, "total", json_integer(results->visits)); - json_object_set_new(output, "date", json_string(results->timeStamp)); + json_object_set_new(output, "date", json_string(results->timestamp)); json_object_set_new(output, "generator", json_string(VERSION)); - json_object_set_new(output, "file_name", json_string(results->fileName)); - json_object_set_new(output, "file_size", json_integer(results->fileSize)); - json_object_set_new(output, "processed_lines", json_integer(results->processedLines)); - json_object_set_new(output, "invalid_lines", json_integer(results->invalidLines)); + json_object_set_new(output, "file_name", json_string(results->file_name)); + json_object_set_new(output, "file_size", json_integer(results->file_size)); + json_object_set_new(output, "processed_lines", json_integer(results->processed_lines)); + json_object_set_new(output, "invalid_lines", json_integer(results->invalid_lines)); json_object_set_new(output, "bandwidth", json_integer(results->bandwidth)); json_object_set_new(output, "runtime", json_real(results->runtime)); json_object_set_new(output, "hits", hits); diff --git a/src/output.h b/src/output.h @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -19,17 +19,17 @@ #include "config.h" struct results { - char *fileName; - off_t fileSize; - uint64_t invalidLines; - uint64_t processedLines; + char *file_name; + off_t file_size; + uint64_t invalid_lines; + uint64_t processed_lines; uint64_t bandwidth; uint64_t hits; - uint64_t hitsIPv4; - uint64_t hitsIPv6; + uint64_t hits_ipv4; + uint64_t hits_ipv6; uint64_t visits; - uint64_t visitsIPv4; - uint64_t visitsIPv6; + uint64_t visits_ipv4; + uint64_t visits_ipv6; uint64_t continents[CONTINENTS]; uint64_t countries[COUNTRIES]; uint64_t hours[24]; @@ -37,7 +37,7 @@ struct results { uint64_t protocols[PROTOCOLS]; uint64_t status[STATUS_CODE_MAX]; double runtime; - char timeStamp[20]; + char timestamp[20]; }; char *output(struct results *); diff --git a/src/parse.c b/src/parse.c @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -15,22 +15,22 @@ #include "parse.h" void -parseDate(struct date *parsedDate, char *date) +parseDate(struct date *parsed_date, char *date) { - parsedDate->day = strtok(date, "/"); - parsedDate->month = strtok(NULL, "/"); - parsedDate->year = strtok(NULL, ":"); - parsedDate->hour = strtok(NULL, ":"); - parsedDate->minute = strtok(NULL, ":"); - parsedDate->second = strtok(NULL, " "); + parsed_date->day = strtok(date, "/"); + parsed_date->month = strtok(NULL, "/"); + parsed_date->year = strtok(NULL, ":"); + parsed_date->hour = strtok(NULL, ":"); + parsed_date->minute = strtok(NULL, ":"); + parsed_date->second = strtok(NULL, " "); } void -parseLine(struct logLine *parsedLine, char *lineBuffer) +parseLine(struct logline *parsed_line, char *linebuffer) { - if (*lineBuffer) { + if (*linebuffer) { /* Remote host */ - parsedLine->remoteHost = strtok(lineBuffer, " "); + parsed_line->remote_host = strtok(linebuffer, " "); /* User-identifier */ strtok(NULL, " "); @@ -39,27 +39,27 @@ parseLine(struct logLine *parsedLine, char *lineBuffer) strtok(NULL, "["); /* Date */ - parsedLine->date = strtok(NULL, "]"); + parsed_line->date = strtok(NULL, "]"); /* Requested resource */ strtok(NULL, "\""); - parsedLine->request = strtok(NULL, "\""); + parsed_line->request = strtok(NULL, "\""); /* HTTP status codes */ - parsedLine->statusCode = strtok(NULL, " "); + parsed_line->status_code = strtok(NULL, " "); /* Returned object size */ - parsedLine->objectSize = strtok(NULL, " \""); + parsed_line->object_size = strtok(NULL, " \""); } } void -parseRequest(struct request *parsedRequest, char *request) +parseRequest(struct request *parsed_request, char *request) { char *pch = strrchr(request, ' '); if (pch) { - parsedRequest->protocol = pch + 1; - parsedRequest->method = strtok(request, " "); + parsed_request->protocol = pch + 1; + parsed_request->method = strtok(request, " "); } } diff --git a/src/parse.h b/src/parse.h @@ -4,7 +4,7 @@ * https://www.logswan.org * * Created: 2015-05-31 - * Last Updated: 2019-01-19 + * Last Updated: 2021-02-15 * * Logswan is released under the BSD 2-Clause license. * See LICENSE file for details. @@ -22,12 +22,12 @@ struct date { char *second; }; -struct logLine { - char *remoteHost; +struct logline { + char *remote_host; char *date; char *request; - char *statusCode; - char *objectSize; + char *status_code; + char *object_size; }; struct request { @@ -37,7 +37,7 @@ struct request { }; void parseDate(struct date *, char *); -void parseLine(struct logLine *, char *); +void parseLine(struct logline *, char *); void parseRequest(struct request *, char *); #endif /* PARSE_H */