Skip to content

Commit 18e335c

Browse files
committed
new(metrics): add host_ifinfo metric
Signed-off-by: Melissa Kilby <[email protected]>
1 parent 3e91a27 commit 18e335c

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

unit_tests/engine/test_falco_utils.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,28 @@ TEST(FalcoUtils, matches_wildcard)
101101
ASSERT_FALSE(falco::utils::matches_wildcard("*hello*world", "come on hello this world yes"));
102102
ASSERT_FALSE(falco::utils::matches_wildcard("*hello*world*", "come on hello this yes"));
103103
}
104+
105+
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
106+
TEST(FalcoUtils, ipv4addr_to_string)
107+
{
108+
ASSERT_EQ(falco::utils::network::ipv4addr_to_string(0x0101A8C0), "192.168.1.1");
109+
ASSERT_EQ(falco::utils::network::ipv4addr_to_string(0x0100007F), "127.0.0.1");
110+
ASSERT_EQ(falco::utils::network::ipv4addr_to_string(0xFFFFFFFF), "255.255.255.255");
111+
ASSERT_EQ(falco::utils::network::ipv4addr_to_string(0x00000000), "0.0.0.0");
112+
}
113+
114+
TEST(FalcoUtils, ipv6addr_to_string)
115+
{
116+
ipv6addr addr1("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
117+
ASSERT_EQ(falco::utils::network::ipv6addr_to_string(addr1), "2001:db8:85a3:0:0:8a2e:370:7334");
118+
119+
ipv6addr addr2("fe80:0:0:0:2aa:ff:fe9a:4ca3");
120+
ASSERT_EQ(falco::utils::network::ipv6addr_to_string(addr2), "fe80:0:0:0:2aa:ff:fe9a:4ca3");
121+
122+
ipv6addr addr3("0:0:0:0:0:0:0:0");
123+
ASSERT_EQ(falco::utils::network::ipv6addr_to_string(addr3), "0:0:0:0:0:0:0:0");
124+
125+
ipv6addr addr4("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
126+
ASSERT_EQ(falco::utils::network::ipv6addr_to_string(addr4), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
127+
}
128+
#endif

userspace/engine/falco_utils.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,38 @@ bool is_unix_scheme(const std::string& url)
257257
{
258258
return sinsp_utils::startswith(url, UNIX_SCHEME);
259259
}
260+
261+
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
262+
std::string ipv4addr_to_string(uint32_t addr)
263+
{
264+
char dest[16];
265+
snprintf(
266+
dest,
267+
sizeof(dest),
268+
"%d.%d.%d.%d",
269+
(addr & 0xFF),
270+
((addr & 0xFF00) >> 8),
271+
((addr & 0xFF0000) >> 16),
272+
((addr & 0xFF000000) >> 24));
273+
return std::string(dest);
274+
}
275+
276+
std::string ipv6addr_to_string(const ipv6addr& addr)
277+
{
278+
std::ostringstream oss;
279+
const uint16_t* words = reinterpret_cast<const uint16_t*>(addr.m_b);
280+
for (int i = 0; i < 8; ++i)
281+
{
282+
if (i != 0)
283+
{
284+
oss << ':';
285+
}
286+
oss << std::hex << ntohs(words[i]);
287+
}
288+
return oss.str();
289+
}
290+
#endif
291+
260292
} // namespace network
261293
} // namespace utils
262294
} // namespace falco

userspace/engine/falco_utils.h

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ limitations under the License.
2222

2323
#include <cstdint>
2424
#include <string>
25+
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
26+
#include <arpa/inet.h>
27+
#include <libsinsp/tuples.h>
28+
#endif
2529

2630
namespace falco::utils
2731
{
@@ -45,5 +49,11 @@ namespace network
4549
{
4650
static const std::string UNIX_SCHEME("unix://");
4751
bool is_unix_scheme(const std::string& url);
52+
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
53+
// todo: consider extending libs and expose API for ipv4 and ipv6 to string conversion
54+
std::string ipv4addr_to_string(uint32_t addr);
55+
std::string ipv6addr_to_string(const ipv6addr& addr);
56+
#endif
57+
4858
} // namespace network
4959
} // namespace falco::utils

userspace/falco/falco_metrics.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@ std::string falco_metrics::to_text(const falco::app::state& state)
111111
metric_name_file_sha256 = "falco_sha256_config_file_" + falco::utils::sanitize_metric_name(metric_name_file_sha256);
112112
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
113113
}
114+
115+
static bool is_first_call = true;
116+
static std::string ifinfo_json_escaped;
117+
if (is_first_call)
118+
{
119+
is_first_call = false;
120+
auto ipv4list = inspector->get_ifaddr_list().get_ipv4_list();
121+
auto ipv6list = inspector->get_ifaddr_list().get_ipv6_list();
122+
nlohmann::json ipv4_json;
123+
nlohmann::json ipv6_json;
124+
if(ipv4list)
125+
{
126+
for (const auto& item : *ipv4list)
127+
{
128+
if(item.m_name == "lo")
129+
{
130+
continue;
131+
}
132+
ipv4_json[item.m_name] = falco::utils::network::ipv4addr_to_string(item.m_addr);
133+
}
134+
}
135+
136+
if(ipv6list)
137+
{
138+
for (const auto& item : *ipv6list)
139+
{
140+
if(item.m_name == "lo")
141+
{
142+
continue;
143+
}
144+
ipv6_json[item.m_name] = falco::utils::network::ipv6addr_to_string(item.m_net);
145+
}
146+
}
147+
nlohmann::json ifinfo_json;
148+
ifinfo_json["ipv4"] = ipv4_json;
149+
ifinfo_json["ipv6"] = ipv6_json;
150+
ifinfo_json_escaped = ifinfo_json.dump();
151+
}
152+
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus("host_ifinfo", "falcosecurity", "falco", {{"host_ifinfo", ifinfo_json_escaped}});
114153
#endif
115154

116155
for (const std::string& source: inspector->event_sources())

userspace/falco/stats_writer.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,46 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
357357
metric_name_file_sha256 = "falco.sha256_config_file." + falco::utils::sanitize_metric_name(metric_name_file_sha256);
358358
output_fields[metric_name_file_sha256] = item.second;
359359
}
360+
361+
static bool is_first_call = true;
362+
static std::string ifinfo_json_escaped;
363+
if (is_first_call)
364+
{
365+
is_first_call = false;
366+
auto ipv4list = inspector->get_ifaddr_list().get_ipv4_list();
367+
auto ipv6list = inspector->get_ifaddr_list().get_ipv6_list();
368+
nlohmann::json ipv4_json;
369+
nlohmann::json ipv6_json;
370+
if(ipv4list)
371+
{
372+
for (const auto& item : *ipv4list)
373+
{
374+
if(item.m_name == "lo")
375+
{
376+
continue;
377+
}
378+
ipv4_json[item.m_name] = falco::utils::network::ipv4addr_to_string(item.m_addr);
379+
}
380+
}
381+
382+
if(ipv6list)
383+
{
384+
for (const auto& item : *ipv6list)
385+
{
386+
if(item.m_name == "lo")
387+
{
388+
continue;
389+
}
390+
ipv6_json[item.m_name] = falco::utils::network::ipv6addr_to_string(item.m_net);
391+
}
392+
}
393+
nlohmann::json ifinfo_json;
394+
ifinfo_json["ipv4"] = ipv4_json;
395+
ifinfo_json["ipv6"] = ipv6_json;
396+
ifinfo_json_escaped = ifinfo_json.dump();
397+
}
398+
output_fields["falco.host_ifinfo"] = ifinfo_json_escaped;
399+
360400
#endif
361401
output_fields["evt.source"] = src;
362402
for (size_t i = 0; i < sizeof(all_driver_engines) / sizeof(const char*); i++)

0 commit comments

Comments
 (0)