Skip to content

Commit 3a6d1c8

Browse files
ekoopspoiana
authored andcommitted
feat(stats): add host_netinfo networking information stats family
Introduce host_netinfo stats family to hold information regarding host networking. At the moment, it only provides ipv4 and ipv6 addresses list for each interface available on the host. The naming schema for the introduced stats is falco.host_netinfo.interfaces.<ifname>.protocols.<ipv4|ipv6>.addresses. Signed-off-by: Leonardo Di Giovanna <[email protected]>
1 parent 70c10ee commit 3a6d1c8

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

userspace/falco/stats_writer.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,66 @@ stats_writer::collector::collector(const std::shared_ptr<stats_writer>& writer)
309309
{
310310
}
311311

312+
void add_netinfo_metrics_output_fields(
313+
nlohmann::json& output_fields,
314+
const std::shared_ptr<sinsp>& inspector)
315+
{
316+
const auto ipv4_ifinfo = inspector->get_ifaddr_list().get_ipv4_list();
317+
const auto ipv6_ifinfo = inspector->get_ifaddr_list().get_ipv6_list();
318+
319+
// For each interface name, collect the corresponding list of IPv4/IPv6 addresses
320+
std::map<std::string, std::vector<std::string>> ifnames_to_ipv4_addresses;
321+
std::map<std::string, std::vector<std::string>> ifnames_to_ipv6_addresses;
322+
323+
for (const auto& ifinfo : *ipv4_ifinfo)
324+
{
325+
if(ifinfo.m_name == "lo")
326+
{
327+
continue;
328+
}
329+
330+
auto it = ifnames_to_ipv4_addresses.find(ifinfo.m_name);
331+
auto address = ifinfo.addr_to_string();
332+
if (it == ifnames_to_ipv4_addresses.end())
333+
{
334+
ifnames_to_ipv4_addresses.emplace(ifinfo.m_name, std::vector{address});
335+
continue;
336+
}
337+
it->second.emplace_back(address);
338+
}
339+
340+
for (const auto& ifinfo : *ipv6_ifinfo)
341+
{
342+
if(ifinfo.m_name == "lo")
343+
{
344+
continue;
345+
}
346+
347+
auto it = ifnames_to_ipv6_addresses.find(ifinfo.m_name);
348+
auto address = ifinfo.addr_to_string();
349+
if (it == ifnames_to_ipv6_addresses.end())
350+
{
351+
ifnames_to_ipv6_addresses.emplace(ifinfo.m_name, std::vector{address});
352+
continue;
353+
}
354+
it->second.emplace_back(address);
355+
}
356+
357+
for (const auto& item : ifnames_to_ipv4_addresses)
358+
{
359+
auto metric_name = "falco.host_netinfo.interfaces." + item.first + ".protocols.ipv4.addresses";
360+
auto addresses = sinsp_join(item.second.cbegin(), item.second.cend(), ',');
361+
output_fields.emplace(metric_name, addresses);
362+
}
363+
364+
for (const auto& item : ifnames_to_ipv6_addresses)
365+
{
366+
auto metric_name = "falco.host_netinfo.interfaces." + item.first + ".protocols.ipv6.addresses";
367+
auto addresses = sinsp_join(item.second.cbegin(), item.second.cend(), ',');
368+
output_fields.emplace(metric_name, addresses);
369+
}
370+
}
371+
312372
void stats_writer::collector::get_metrics_output_fields_wrapper(
313373
nlohmann::json& output_fields,
314374
const std::shared_ptr<sinsp>& inspector,
@@ -358,6 +418,8 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
358418
output_fields[metric_name_file_sha256] = item.second;
359419
}
360420

421+
add_netinfo_metrics_output_fields(output_fields, inspector);
422+
361423
#endif
362424
output_fields["evt.source"] = src;
363425
for (size_t i = 0; i < sizeof(all_driver_engines) / sizeof(const char*); i++)

0 commit comments

Comments
 (0)