/* * SystemBackendProc.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "SystemBackendProc.h" #include #include #include namespace Mad { namespace Common { namespace Backends { SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() const { UptimeInfo uptime = {0, 0}; std::ifstream file("/proc/uptime"); if(!file.good()) return uptime; float f; file >> f; if(!file.good()) return uptime; uptime.uptime = (unsigned long)f; file >> f; if(!file.good()) return uptime; uptime.idleTime = (unsigned long)f; return uptime; } SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() const { MemoryInfo memInfo = {0, 0, 0, 0}; std::ifstream file("/proc/meminfo"); if(!file.good()) return memInfo; pcrecpp::RE re("(.+):\\s*(\\d+).*"); while(!file.eof() && file.good()) { std::string line; std::getline(file, line); std::string name; unsigned long value; if(!re.FullMatch(line, &name, &value)) continue; if(name == "MemTotal") memInfo.totalMem = value; else if(name == "MemFree") memInfo.freeMem = value; else if(name == "SwapTotal") memInfo.totalSwap = value; else if(name == "SwapFree") memInfo.freeSwap = value; if(memInfo.totalMem && memInfo.freeMem && memInfo.totalSwap && memInfo.freeSwap) break; } return memInfo; } } } }