NetUsage (Linux): new module (WIP)

This commit is contained in:
李通洲
2023-09-26 10:32:21 +08:00
parent 50c8809d51
commit 57bd741d96
5 changed files with 90 additions and 69 deletions
+1
View File
@@ -387,6 +387,7 @@ if(LINUX)
src/detection/media/media_linux.c
src/detection/memory/memory_linux.c
src/detection/monitor/monitor_linux.c
src/detection/netusage/netusage_linux.c
src/detection/opengl/opengl_linux.c
src/detection/os/os_linux.c
src/detection/packages/packages_linux.c
-14
View File
@@ -5,18 +5,6 @@
#include "fastfetch.h"
typedef struct FFLocalIpIoCounters
{
uint64_t txBytes;
uint64_t rxBytes;
uint64_t txPackets;
uint64_t rxPackets;
uint64_t rxErrors;
uint64_t txErrors;
uint64_t rxDrops;
uint64_t txDrops;
} FFLocalIpIoCounters;
typedef struct FFLocalIpResult
{
FFstrbuf name;
@@ -25,8 +13,6 @@ typedef struct FFLocalIpResult
FFstrbuf mac;
bool defaultRoute;
FFLocalIpIoCounters ioCounters;
#ifdef _WIN32
uint32_t ifIndex;
#endif
-55
View File
@@ -228,61 +228,6 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
#ifdef __linux__
{
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/class/net/");
uint32_t baseLen = path.length;
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
FF_LIST_FOR_EACH(FFLocalIpResult, ip, *results)
{
ffStrbufAppend(&path, &ip->name);
ffStrbufAppendS(&path, "/statistics/");
uint32_t statLen = path.length;
ffStrbufAppendS(&path, "rx_bytes");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.rxBytes = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_bytes");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.txBytes = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_packets");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.rxPackets = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_packets");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.txPackets = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_errors");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.rxErrors = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_errors");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.txErrors = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_dropped");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.rxDrops = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_dropped");
if (ffReadFileBuffer(path.chars, &buffer))
ip->ioCounters.txDrops = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, baseLen);
}
}
#endif
char iface[16 /*IF_NAMESIZE*/ + 1];
if (getDefaultRoute(iface))
{
+16
View File
@@ -0,0 +1,16 @@
#include "fastfetch.h"
typedef struct FFNetUsageIoCounters
{
FFstrbuf name;
uint64_t txBytes;
uint64_t rxBytes;
uint64_t txPackets;
uint64_t rxPackets;
uint64_t rxErrors;
uint64_t txErrors;
uint64_t rxDrops;
uint64_t txDrops;
} FFNetUsageIoCounters;
const char* ffGetNetIoCounter(FFlist* result /* list of FFNetUsageIoCounters */);
+73
View File
@@ -0,0 +1,73 @@
#include "netusage.h"
#include "common/io/io.h"
#include <net/if.h>
const char* ffGetNetIoCounter(FFlist* result)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net");
if (!dirp) return "opendir(\"/sys/class/net\") == NULL";
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateA(64);
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
struct dirent* entry;
while((entry = readdir(dirp)) != NULL)
{
if(entry->d_name[0] == '.')
continue;
ffStrbufSetF(&path, "/sys/class/net/%s/operstate", entry->d_name);
if(!ffReadFileBuffer(path.chars, &buffer) || !ffStrbufEqualS(&buffer, "up"))
continue;
FFNetUsageIoCounters* counters = (FFNetUsageIoCounters*) ffListAdd(result);
ffStrbufInitS(&counters->name, entry->d_name);
ffStrbufSetF(&path, "/sys/class/net/%s/statistics/");
uint32_t statLen = path.length;
ffStrbufAppendS(&path, "rx_bytes");
if (ffReadFileBuffer(path.chars, &buffer))
counters->rxBytes = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_bytes");
if (ffReadFileBuffer(path.chars, &buffer))
counters->txBytes = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_packets");
if (ffReadFileBuffer(path.chars, &buffer))
counters->rxPackets = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_packets");
if (ffReadFileBuffer(path.chars, &buffer))
counters->txPackets = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_errors");
if (ffReadFileBuffer(path.chars, &buffer))
counters->rxErrors = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_errors");
if (ffReadFileBuffer(path.chars, &buffer))
counters->txErrors = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "rx_dropped");
if (ffReadFileBuffer(path.chars, &buffer))
counters->rxDrops = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
ffStrbufAppendS(&path, "tx_dropped");
if (ffReadFileBuffer(path.chars, &buffer))
counters->txDrops = ffStrbufToUInt(&buffer, 0);
ffStrbufSubstrBefore(&path, statLen);
}
return NULL;
}