Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Expose network received/sent bytes metric.

kishorenc13591fc01706
diff --git a/include/system_metrics.h b/include/system_metrics.h
index f179874c..9c640e68 100644
--- a/include/system_metrics.h
+++ b/include/system_metrics.h
@@ -6,6 +6,7 @@
#include "json.hpp"
const int NUM_CPU_STATES = 10;
+const int NUM_NETWORK_STATS = 16;
struct cpu_data_t {
std::string cpu;
@@ -145,6 +146,8 @@ public:
non_proc_mem_bytes = memory_used_bytes - get_memory_active_bytes();
}
+ static void linux_get_network_data(const std::string & stat_path, uint64_t& received_bytes, uint64_t& sent_bytes);
+
void get(const std::string & data_dir_path, nlohmann::json& result);
static float used_memory_ratio();
diff --git a/src/system_metrics.cpp b/src/system_metrics.cpp
index 43c7133e..9afeca4d 100644
--- a/src/system_metrics.cpp
+++ b/src/system_metrics.cpp
@@ -14,6 +14,7 @@
#include <mach/mach_host.h>
#endif
+#include "string_utils.h"
#include "jemalloc.h"
#if __APPLE__
@@ -67,7 +68,7 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
result["system_memory_total_bytes"] = std::to_string(get_memory_total_bytes());
result["system_memory_used_bytes"] = std::to_string(get_memory_used_bytes());
- // CPU METRICS
+ // CPU and Network metrics
#if __linux__
const std::vector<cpu_stat_t>& cpu_stats = get_cpu_stats();
@@ -75,6 +76,11 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
std::string cpu_id = (i == 0) ? "" : std::to_string(i);
result["system_cpu" + cpu_id + "_active_percentage"] = cpu_stats[i].active;
}
+
+ uint64_t received_bytes, sent_bytes;
+ linux_get_network_data("/proc/net/dev", received_bytes, sent_bytes);
+ result["system_network_received_bytes"] = std::to_string(received_bytes);
+ result["system_network_sent_bytes"] = std::to_string(sent_bytes);
#endif
}
@@ -171,3 +177,51 @@ uint64_t SystemMetrics::get_memory_non_proc_bytes() {
return non_proc_mem_bytes;
}
+
+void SystemMetrics::linux_get_network_data(const std::string & stat_path,
+ uint64_t &received_bytes, uint64_t &sent_bytes) {
+ //std::ifstream stat_file("/proc/net/dev");
+ std::ifstream stat_file(stat_path);
+ std::string line;
+
+ // TODO: this probably needs to be handled better!
+ const std::string STR_ENS5("ens5");
+ const std::string STR_ETH0("eth0");
+
+ /*
+ Inter-| Receive | Transmit
+ face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
+ ens5: 324278716 897631 0 0 0 0 0 0 93933882 575535 0 0 0 0 0 0
+ */
+
+ received_bytes = 0;
+ sent_bytes = 0;
+
+ while (std::getline(stat_file, line)) {
+ StringUtils::trim(line);
+ if (line.rfind(STR_ENS5, 0) == 0 || line.rfind(STR_ETH0, 0) == 0) {
+ std::istringstream ss(line);
+ std::string throwaway;
+
+ // read interface label
+ ss >> throwaway;
+
+ uint64_t stat_value;
+
+ // read stats
+ for (int i = 0; i < NUM_NETWORK_STATS; i++) {
+ ss >> stat_value;
+
+ if(i == 0) {
+ received_bytes = stat_value;
+ }
+
+ if(i == 8) {
+ sent_bytes = stat_value;
+ }
+ }
+
+ break;
+ }
+ }
+}
diff --git a/test/resources/proc_net_dev.txt b/test/resources/proc_net_dev.txt
new file mode 100644
index 00000000..e8ae14ad
--- /dev/null
+++ b/test/resources/proc_net_dev.txt
@@ -0,0 +1,4 @@
+Inter-| Receive | Transmit
+ face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
+ ens5: 324278716 897631 0 0 0 0 0 0 93933882 575535 0 0 0 0 0 0
+ lo: 1308386 5312 0 0 0 0 0 0 1308386 5312 0 0 0 0 0 0
\ No newline at end of file
diff --git a/test/system_metrics_test.cpp b/test/system_metrics_test.cpp
new file mode 100644
index 00000000..0d0e95e5
--- /dev/null
+++ b/test/system_metrics_test.cpp
@@ -0,0 +1,10 @@
+#include <gtest/gtest.h>
+#include "system_metrics.h"
+
+TEST(SystemMetricsTest, ParsingNetworkStats) {
+ std::string proc_net_dev_path = std::string(ROOT_DIR)+"test/resources/proc_net_dev.txt";
+ uint64_t received_bytes, sent_bytes;
+ SystemMetrics::linux_get_network_data(proc_net_dev_path, received_bytes, sent_bytes);
+ ASSERT_EQ(324278716, received_bytes);
+ ASSERT_EQ(93933882, sent_bytes);
+}
\ No newline at end of file