Skip to content

Commit

Permalink
Wire new csv format into aggregation output. #529
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Jun 25, 2024
1 parent e56f7f1 commit 483e780
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
6 changes: 1 addition & 5 deletions src/nfdump/nfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,11 +1168,7 @@ int main(int argc, char **argv) {

if (aggr_fmt) {
// custom aggregation mask overwrites any output format
if (print_format) {
printf("Can not use print format %s to aggregate flows\n", print_format);
exit(EXIT_FAILURE);
}
print_format = ParseAggregateMask(aggr_fmt);
print_format = ParseAggregateMask(print_format, aggr_fmt);
if (!print_format) {
exit(EXIT_FAILURE);
}
Expand Down
34 changes: 24 additions & 10 deletions src/nfdump/nflowcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ static uint32_t maskIndex = 1;

// For automatic output format generation in case of custom aggregation
#define AggrPrependFmt "%ts %td "
#define AggrPrependCvs "%ts,%td"
#define AggrAppendFmt "%pkt %byt %bps %bpp %fl"
#define AggrAppendCvs "%pkt,%byt,%bps,%bpp,%fl"

static struct aggregationElement_s {
char *aggrElement; // name of aggregation parameter
Expand Down Expand Up @@ -1092,7 +1094,7 @@ int SetRecordStat(char *statType, char *optOrder) {
return 1;
} // End of SetRecordStat

char *ParseAggregateMask(char *arg) {
char *ParseAggregateMask(char *print_format, char *arg) {
dbg_printf("Enter %s\n", __func__);
if (bidir_flows) {
LogError("Can not set custom aggregation in bidir mode");
Expand All @@ -1101,8 +1103,18 @@ char *ParseAggregateMask(char *arg) {

int modeCSV = 0;
char *sep = " ";
if (modeCSV) {
sep = ",";
char *prepend = AggrPrependFmt;
char *append = AggrAppendFmt;
if (print_format) {
if (strcmp(print_format, "csv") == 0) {
modeCSV = 1;
sep = ",";
prepend = AggrPrependCvs;
append = AggrAppendCvs;
} else {
printf("Can not use print format %s to aggregate flows\n", print_format);
exit(EXIT_FAILURE);
}
}

uint32_t elementCount = 0;
Expand All @@ -1113,7 +1125,7 @@ char *ParseAggregateMask(char *arg) {

size_t fmt_len = 0;
for (int i = 0; aggregationTable[i].aggrElement != NULL; i++) {
if (HasGeoDB && aggregationTable[i].fmt) {
if (modeCSV == 0 && HasGeoDB && aggregationTable[i].fmt) {
if (strcmp(aggregationTable[i].fmt, "%sa") == 0) aggregationTable[i].fmt = "%gsa";
if (strcmp(aggregationTable[i].fmt, "%da") == 0) aggregationTable[i].fmt = "%gda";
}
Expand All @@ -1122,7 +1134,7 @@ char *ParseAggregateMask(char *arg) {
fmt_len++; // max fmt string len incl. trailing '\0'

// add format prepend and append length
fmt_len += strlen(AggrPrependFmt) + strlen(AggrAppendFmt) + 6; // +6 for 'fmt:', 2 spaces
fmt_len += strlen(prepend) + strlen(append) + 6; // +6 for 'fmt:', 2 spaces

char *aggr_fmt = (char *)malloc(fmt_len);
if (!aggr_fmt) {
Expand All @@ -1131,9 +1143,9 @@ char *ParseAggregateMask(char *arg) {
}
aggr_fmt[0] = '\0';
if (modeCSV)
fmt_len -= snprintf(aggr_fmt, fmt_len, "csv:%s ", AggrPrependFmt);
fmt_len -= snprintf(aggr_fmt, fmt_len, "csv:%s,", prepend);
else
fmt_len -= snprintf(aggr_fmt, fmt_len, "fmt:%s ", AggrPrependFmt);
fmt_len -= snprintf(aggr_fmt, fmt_len, "fmt:%s ", prepend);

uint32_t v4Mask = 0xffffffff;
uint64_t v6Mask[2] = {0xffffffffffffffffLL, 0xffffffffffffffffLL};
Expand Down Expand Up @@ -1263,9 +1275,11 @@ char *ParseAggregateMask(char *arg) {
}

#endif
strncat(aggr_fmt, " ", fmt_len);
fmt_len--;
strncat(aggr_fmt, AggrAppendFmt, fmt_len);
if (modeCSV == 0) {
strncat(aggr_fmt, sep, fmt_len);
fmt_len--;
}
strncat(aggr_fmt, append, fmt_len);
return aggr_fmt;

} // End of ParseAggregateMask
Expand Down
2 changes: 1 addition & 1 deletion src/nfdump/nflowcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void Dispose_FlowTable(void);

int Parse_PrintOrder(char *order);

char *ParseAggregateMask(char *arg);
char *ParseAggregateMask(char *print_format, char *arg);

void ListAggregationHelp(void);

Expand Down

0 comments on commit 483e780

Please sign in to comment.