Smallcase folders part 1
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
#include "Metrics/Collector.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <sys/statvfs.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
||||
namespace metrics
|
||||
{
|
||||
|
||||
Collector::Collector(const std::vector<std::string> &disks) : disks(disks)
|
||||
{
|
||||
}
|
||||
|
||||
Host Collector::collect()
|
||||
{
|
||||
Host host;
|
||||
|
||||
auto curCpu = readCpuTimes();
|
||||
|
||||
if (prevCpu.second.empty())
|
||||
prevCpu = curCpu;
|
||||
|
||||
host.cpu.coreLoads.resize(curCpu.second.size());
|
||||
|
||||
host.cpu.totalLoad = cpuLoad(prevCpu.first, curCpu.first);
|
||||
for (size_t i = 0; i < curCpu.second.size(); ++i)
|
||||
host.cpu.coreLoads[i] = cpuLoad(prevCpu.second[i], curCpu.second[i]);
|
||||
|
||||
prevCpu = curCpu;
|
||||
|
||||
host.memory = readMemory();
|
||||
for (const std::string &disk : disks)
|
||||
host.disks.push_back(readDisk(disk.c_str()));
|
||||
readLoad(host.load1, host.load5, host.load15);
|
||||
host.uptime = readUptime();
|
||||
host.hostname = readHostname();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
std::pair<Collector::CpuTimes, std::vector<Collector::CpuTimes>> Collector::readCpuTimes()
|
||||
{
|
||||
std::ifstream f("/proc/stat");
|
||||
std::pair<CpuTimes, std::vector<CpuTimes>> out;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(f, line))
|
||||
{
|
||||
if (!line.starts_with("cpu"))
|
||||
break;
|
||||
|
||||
std::istringstream ss(line);
|
||||
std::string cpu;
|
||||
CpuTimes t;
|
||||
|
||||
ss >> cpu >> t.user >> t.nice >> t.system >> t.idle >> t.iowait >> t.irq >> t.softirq >> t.steal;
|
||||
|
||||
if (cpu == "cpu")
|
||||
{
|
||||
out.first = t;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.second.push_back(t);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
float Collector::cpuLoad(const Collector::CpuTimes &prev,
|
||||
const Collector::CpuTimes &cur)
|
||||
{
|
||||
uint64_t prevIdle = prev.idle + prev.iowait;
|
||||
uint64_t curIdle = cur.idle + cur.iowait;
|
||||
|
||||
uint64_t prevTotal =
|
||||
prev.user + prev.nice + prev.system +
|
||||
prevIdle + prev.irq + prev.softirq + prev.steal;
|
||||
|
||||
uint64_t curTotal =
|
||||
cur.user + cur.nice + cur.system +
|
||||
curIdle + cur.irq + cur.softirq + cur.steal;
|
||||
|
||||
uint64_t totalDiff = curTotal - prevTotal;
|
||||
uint64_t idleDiff = curIdle - prevIdle;
|
||||
|
||||
if (totalDiff == 0)
|
||||
return 0.f;
|
||||
|
||||
return 100.f * (totalDiff - idleDiff) / totalDiff;
|
||||
}
|
||||
|
||||
Memory Collector::readMemory()
|
||||
{
|
||||
std::ifstream f("/proc/meminfo");
|
||||
std::string key;
|
||||
uint64_t value;
|
||||
std::string unit;
|
||||
|
||||
uint64_t mem_total = 0, mem_available = 0, swap_total = 0, swap_available = 0;
|
||||
|
||||
while (f >> key >> value >> unit)
|
||||
{
|
||||
if (key == "MemTotal:")
|
||||
mem_total = value * 1024;
|
||||
else if (key == "MemAvailable:")
|
||||
mem_available = value * 1024;
|
||||
else if (key == "SwapTotal:")
|
||||
swap_total = value * 1024;
|
||||
else if (key == "SwapFree:")
|
||||
swap_available = value * 1024;
|
||||
}
|
||||
|
||||
Memory memory;
|
||||
memory.mem_total = mem_total;
|
||||
memory.mem_available = mem_available;
|
||||
memory.mem_used = mem_total - mem_available;
|
||||
memory.swap_total = swap_total;
|
||||
memory.swap_available = swap_available;
|
||||
memory.swap_used = swap_total - swap_available;
|
||||
return memory;
|
||||
}
|
||||
|
||||
Disk Collector::readDisk(const char *path)
|
||||
{
|
||||
struct statvfs vfs;
|
||||
if (statvfs(path, &vfs) != 0)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string("statvfs failed for ") + path + ": " +
|
||||
std::strerror(errno));
|
||||
}
|
||||
|
||||
uint64_t total = static_cast<uint64_t>(vfs.f_blocks) * vfs.f_frsize;
|
||||
uint64_t free = static_cast<uint64_t>(vfs.f_bavail) * vfs.f_frsize;
|
||||
uint64_t used = total - free;
|
||||
|
||||
Disk d;
|
||||
d.name = path;
|
||||
d.total = total;
|
||||
d.free = free;
|
||||
d.used = used;
|
||||
return d;
|
||||
}
|
||||
|
||||
void Collector::readLoad(float &l1, float &l5, float &l15)
|
||||
{
|
||||
std::ifstream f("/proc/loadavg");
|
||||
f >> l1 >> l5 >> l15;
|
||||
}
|
||||
|
||||
uint64_t Collector::readUptime()
|
||||
{
|
||||
std::ifstream f("/proc/uptime");
|
||||
double up;
|
||||
f >> up;
|
||||
return static_cast<uint64_t>(up);
|
||||
}
|
||||
|
||||
std::string Collector::readHostname()
|
||||
{
|
||||
char buf[HOST_NAME_MAX + 1] = {};
|
||||
if (gethostname(buf, sizeof(buf)) == 0)
|
||||
return std::string(buf);
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user