Skip to content

Commit 35ee179

Browse files
committed
Merge remote-tracking branch 'origin/fds_output' into devel
2 parents 21897b8 + 1e8d2c4 commit 35ee179

File tree

13 files changed

+1253
-2
lines changed

13 files changed

+1253
-2
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(SUB_HEADERS
1616
ipfixcol2/message_session.h
1717
ipfixcol2/plugins.h
1818
ipfixcol2/session.h
19+
ipfixcol2/utils.h
1920
ipfixcol2/verbose.h
2021
"${PROJECT_BINARY_DIR}/include/ipfixcol2/api.h"
2122
)

include/ipfixcol2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
#include <ipfixcol2/plugins.h>
6565
#include <ipfixcol2/session.h>
66+
#include <ipfixcol2/utils.h>
6667
#include <ipfixcol2/verbose.h>
6768

6869

include/ipfixcol2/utils.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @file include/ipfixcol2/utils.h
3+
* @author Lukas Hutak <[email protected]>
4+
* @brief Auxiliary utilities for plugins (header file)
5+
* @date June 2019
6+
*
7+
* Copyright(c) 2019 CESNET z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#ifndef IPX_UTILS_H
12+
#define IPX_UTILS_H
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#include <ipfixcol2/api.h>
19+
#include <sys/stat.h> // mkdir file permissions
20+
21+
/**
22+
* \defgroup ipxSource Transport session identification
23+
* \ingroup publicAPIs
24+
* \brief Transport session interface
25+
*
26+
* Data types and API functions for identification and management of Transport
27+
* session identification. The Exporting Process uses the Transport Session to
28+
* send messages from multiple _independent_ Observation Domains to the
29+
* Collecting Process. Moreover, in case of SCTP session messages are also send
30+
* over _independent_ streams.
31+
*
32+
* Following structures represents Transport session between Exporting process
33+
* and Collecting Process. However, proper processing of flows also requires
34+
* distinguishing Observation Domain IDs and Stream identifications out of
35+
* scope of these structures.
36+
*
37+
* @{
38+
*/
39+
40+
/**
41+
* @brief Default file permission of newly created directories
42+
* @note Read/write/execute for a user and his group, read/execute for others.
43+
*/
44+
#define IPX_UTILS_MKDIR_DEF (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
45+
46+
/**
47+
* @brief Create recursively a directory
48+
*
49+
* @note
50+
* File permission @p mode only affects newly created directories. In other
51+
* words, if a directory (or subdirectory) already exists, file permission
52+
* bits @p mode are not applied.
53+
* @note
54+
* The function is implemented as "recursive" wrapper over standard mkdir
55+
* function. See man 3 mkdir for more information.
56+
* @param[in] path Full directory path to create
57+
* @param[in] mode The file permission bits of the new directories
58+
* (see default value #IPX_UTILS_MKDIR_DEF)
59+
* @return #IPX_OK on success
60+
* @return #IPX_ERR_DENIED otherwise and errno is set appropriately.
61+
*/
62+
IPX_API int
63+
ipx_utils_mkdir(const char *path, mode_t mode);
64+
65+
/**@}*/
66+
67+
#ifdef __cplusplus
68+
}
69+
#endif
70+
#endif // IPX_UTILS_H

src/core/utils.c

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@
4343
#undef _GNU_SOURCE
4444
#define _POSIX_C_SOURCE 200809L
4545

46+
47+
#include <errno.h>
48+
#include <limits.h>
49+
#include <stdlib.h>
4650
#include <string.h>
47-
#include <stdio.h>
51+
4852
#include "utils.h"
4953

54+
5055
int
5156
ipx_strerror_fn(int errnum, char *buffer, size_t buffer_size)
5257
{
@@ -57,4 +62,86 @@ ipx_strerror_fn(int errnum, char *buffer, size_t buffer_size)
5762

5863
snprintf(buffer, buffer_size, "strerror_r() failed: Unable process error code %d!", errnum);
5964
return IPX_ERR_ARG;
60-
}
65+
}
66+
67+
int
68+
ipx_utils_mkdir(const char *path, mode_t mode)
69+
{
70+
const char ch_slash = '/';
71+
bool add_slash = false;
72+
73+
// Check the parameter
74+
size_t len = strlen(path);
75+
if (path[len - 1] != ch_slash) {
76+
len++; // We have to add another slash
77+
add_slash = true;
78+
}
79+
80+
if (len > PATH_MAX - 1) {
81+
errno = ENAMETOOLONG;
82+
return IPX_ERR_DENIED;
83+
}
84+
85+
// Make a copy
86+
char *path_cpy = malloc((len + 1) * sizeof(char)); // +1 for '\0'
87+
if (!path_cpy) {
88+
errno = ENOMEM;
89+
return IPX_ERR_DENIED;
90+
}
91+
92+
strcpy(path_cpy, path);
93+
if (add_slash) {
94+
path_cpy[len - 1] = ch_slash;
95+
path_cpy[len] = '\0';
96+
}
97+
98+
struct stat info;
99+
char *pos;
100+
101+
// Create directories from the beginning
102+
for (pos = path_cpy + 1; *pos; pos++) {
103+
// Find a slash
104+
if (*pos != ch_slash) {
105+
continue;
106+
}
107+
108+
*pos = '\0'; // Temporarily truncate pathname
109+
110+
// Check if a subdirectory exists
111+
if (stat(path_cpy, &info) == 0) {
112+
// Check if the "info" is about directory
113+
if (!S_ISDIR(info.st_mode)) {
114+
free(path_cpy);
115+
errno = ENOTDIR;
116+
return IPX_ERR_DENIED;
117+
}
118+
119+
// Fix the pathname and continue with the next subdirectory
120+
*pos = ch_slash;
121+
continue;
122+
}
123+
124+
// Errno is filled by stat()
125+
if (errno != ENOENT) {
126+
int errno_cpy = errno;
127+
free(path_cpy);
128+
errno = errno_cpy;
129+
return IPX_ERR_DENIED;
130+
}
131+
132+
// Required directory doesn't exist -> create new one
133+
if (mkdir(path_cpy, mode) != 0 && errno != EEXIST) {
134+
// Failed (by the way, EEXIST because of race condition i.e.
135+
// multiple applications creating the same folder)
136+
int errno_cpy = errno;
137+
free(path_cpy);
138+
errno = errno_cpy;
139+
return IPX_ERR_DENIED;
140+
}
141+
142+
*pos = ch_slash;
143+
}
144+
145+
free(path_cpy);
146+
return IPX_OK;
147+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Create a linkable module
2+
add_library(fds-output MODULE
3+
src/Config.cpp
4+
src/Config.hpp
5+
src/Exception.hpp
6+
src/fds.cpp
7+
src/Storage.cpp
8+
src/Storage.hpp
9+
)
10+
11+
install(
12+
TARGETS fds-output
13+
LIBRARY DESTINATION "${INSTALL_DIR_LIB}/ipfixcol2/"
14+
)
15+
16+
if (ENABLE_DOC_MANPAGE)
17+
# Build a manual page
18+
set(SRC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/doc/ipfixcol2-fds-output.7.rst")
19+
set(DST_FILE "${CMAKE_CURRENT_BINARY_DIR}/ipfixcol2-fds-output.7")
20+
21+
add_custom_command(TARGET fds-output PRE_BUILD
22+
COMMAND ${RST2MAN_EXECUTABLE} --syntax-highlight=none ${SRC_FILE} ${DST_FILE}
23+
DEPENDS ${SRC_FILE}
24+
VERBATIM
25+
)
26+
27+
install(
28+
FILES "${DST_FILE}"
29+
DESTINATION "${INSTALL_DIR_MAN}/man7"
30+
)
31+
endif()

src/plugins/output/fds/README.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Flow Data Storage (output plugin)
2+
=================================
3+
4+
The plugin converts and stores IPFIX Data Records into FDS file format. The file
5+
is based on IPFIX, therefore, it provides highly-effective way for long-term
6+
storage and stores complete flow records (including all Enterprise-specific
7+
fields, biflow, etc.) together with identification of the flow exporters who
8+
exported these records.
9+
10+
All data are stored into flat files, which are automatically rotated and renamed
11+
every N minutes (by default 5 minutes).
12+
13+
Example configuration
14+
---------------------
15+
16+
.. code-block:: xml
17+
18+
<output>
19+
<name>FDS output</name>
20+
<plugin>fds</plugin>
21+
<params>
22+
<storagePath>/tmp/ipfixcol2/fds/</storagePath>
23+
<compression>none</compression>
24+
<dumpInterval>
25+
<timeWindow>300</timeWindow>
26+
<align>yes</align>
27+
</dumpInterval>
28+
</params>
29+
</output>
30+
31+
Parameters
32+
----------
33+
34+
:``storagePath``:
35+
The path element specifies the storage directory for data files. Keep on
36+
mind that the path must exist in your system. Otherwise, no files are stored.
37+
All files will be stored based on the configuration using the following
38+
template: ``<storagePath>/YYYY/MM/DD/flows.<ts>.fds`` where ``YYYY/MM/DD``
39+
means year/month/day and ``<ts>`` represents a UTC timestamp in
40+
format ``YYMMDDhhmmss``.
41+
42+
:``compression``:
43+
Data compression helps to significantly reduce size of output files.
44+
Following compression algorithms are available:
45+
46+
:``none``: Compression disabled [default]
47+
:``lz4``: LZ4 compression (very fast, slightly worse compression ration)
48+
:``zstd``: ZSTD compression (slightly slower, good compression ration)
49+
50+
:``dumpInterval``:
51+
Configuration of output files rotation.
52+
53+
:``timeWindow``:
54+
Specifies time interval in seconds to rotate files i.e. close the current
55+
file and create a new one. [default: 300]
56+
57+
:``align``:
58+
Align file rotation with next N minute interval. For example, if enabled
59+
and window size is 5 minutes long, files will be created at 0, 5, 10, etc.
60+
[values: yes/no, default: yes]
61+
62+
:``asyncIO``:
63+
Allows to use asynchronous I/O for writing to the file. Usually when parts
64+
of the file are being written, the process is blocked on synchronous I/O
65+
and waits for the operation to complete. However, asynchronous I/O allows
66+
the plugin to simultaneously write to file and process flow records, which
67+
significantly improves overall performance. (Note: a pool of service
68+
threads shared among instances of FDS plugin might be created).
69+
[values: true/false, default: true]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
======================
2+
ipfixcol2-fds-output
3+
======================
4+
5+
---------------------------------
6+
Flow Data Storage (output plugin)
7+
---------------------------------
8+
9+
:Author: Lukáš Huták ([email protected])
10+
:Date: 2019-07-01
11+
:Copyright: Copyright © 2019 CESNET, z.s.p.o.
12+
:Version: 2.0
13+
:Manual section: 7
14+
:Manual group: IPFIXcol collector
15+
16+
Description
17+
-----------
18+
19+
.. include:: ../README.rst
20+
:start-line: 3

0 commit comments

Comments
 (0)