From ad7ee8df8ac73ef7754ccf1d5fb442ec23c879f1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 27 Nov 2008 22:15:00 +0100 Subject: SystemBackendProc zum Modul gemacht --- src/Common/Backends/Makefile.am | 4 +- src/Common/Backends/Makefile.in | 52 +++--------- src/Common/Backends/SystemBackendProc.cpp | 116 -------------------------- src/Common/Backends/SystemBackendProc.h | 59 -------------- src/Makefile.am | 4 +- src/Makefile.in | 4 +- src/mad-core.cpp | 3 +- src/mad.cpp | 3 +- src/modules/Makefile.am | 9 ++- src/modules/Makefile.in | 24 +++--- src/modules/SystemBackendProc.cpp | 130 ++++++++++++++++++++++++++++++ src/modules/SystemBackendProc.h | 67 +++++++++++++++ 12 files changed, 238 insertions(+), 237 deletions(-) delete mode 100644 src/Common/Backends/SystemBackendProc.cpp delete mode 100644 src/Common/Backends/SystemBackendProc.h create mode 100644 src/modules/SystemBackendProc.cpp create mode 100644 src/modules/SystemBackendProc.h (limited to 'src') diff --git a/src/Common/Backends/Makefile.am b/src/Common/Backends/Makefile.am index 83e5b2f..ab20072 100644 --- a/src/Common/Backends/Makefile.am +++ b/src/Common/Backends/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libbackends.la -libbackends_la_SOURCES = SystemBackendProc.cpp +libbackends_la_SOURCES = -noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h +noinst_HEADERS = ConsoleLogger.h FileLogger.h diff --git a/src/Common/Backends/Makefile.in b/src/Common/Backends/Makefile.in index fd44c4d..e57b0e2 100644 --- a/src/Common/Backends/Makefile.in +++ b/src/Common/Backends/Makefile.in @@ -49,19 +49,17 @@ CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libbackends_la_LIBADD = -am_libbackends_la_OBJECTS = SystemBackendProc.lo +am_libbackends_la_OBJECTS = libbackends_la_OBJECTS = $(am_libbackends_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) -depcomp = $(SHELL) $(top_srcdir)/config/depcomp -am__depfiles_maybe = depfiles -CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ - $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -LTCXXCOMPILE = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ - --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ - $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -CXXLD = $(CXX) -CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ - --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ SOURCES = $(libbackends_la_SOURCES) DIST_SOURCES = $(libbackends_la_SOURCES) @@ -207,12 +205,11 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libbackends.la -libbackends_la_SOURCES = SystemBackendProc.cpp -noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h +libbackends_la_SOURCES = +noinst_HEADERS = ConsoleLogger.h FileLogger.h all: all-am .SUFFIXES: -.SUFFIXES: .cpp .lo .o .obj $(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) @for dep in $?; do \ case '$(am__configure_deps)' in \ @@ -252,7 +249,7 @@ clean-noinstLTLIBRARIES: rm -f "$${dir}/so_locations"; \ done libbackends.la: $(libbackends_la_OBJECTS) $(libbackends_la_DEPENDENCIES) - $(CXXLINK) $(libbackends_la_OBJECTS) $(libbackends_la_LIBADD) $(LIBS) + $(LINK) $(libbackends_la_OBJECTS) $(libbackends_la_LIBADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -260,29 +257,6 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackendProc.Plo@am__quote@ - -.cpp.o: -@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< -@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $< - -.cpp.obj: -@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` -@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` - -.cpp.lo: -@am__fastdepCXX_TRUE@ $(LTCXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< -@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $< - mostlyclean-libtool: -rm -f *.lo @@ -396,7 +370,6 @@ clean-am: clean-generic clean-libtool clean-noinstLTLIBRARIES \ mostlyclean-am distclean: distclean-am - -rm -rf ./$(DEPDIR) -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-tags @@ -430,7 +403,6 @@ install-ps: install-ps-am installcheck-am: maintainer-clean: maintainer-clean-am - -rm -rf ./$(DEPDIR) -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp deleted file mode 100644 index 134998f..0000000 --- a/src/Common/Backends/SystemBackendProc.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 - -namespace Mad { -namespace Common { -namespace Backends { - -SystemBackendProc SystemBackendProc::backend; - -bool SystemBackendProc::uptimeInfo(const sigc::slot &callback) { - unsigned long uptime = 0, idleTime = 0; - - uptimeFile.seekg(0, std::ios::beg); - - if(!uptimeFile.good()) - return false; - - float f; - uptimeFile >> f; - if(!uptimeFile.good()) - return false; - - uptime = (unsigned long)f; - - uptimeFile >> f; - if(uptimeFile.good()) - idleTime = (unsigned long)f; - - callback(uptime, idleTime); - - return true; -} - -bool SystemBackendProc::memoryInfo(const sigc::slot &callback) { - unsigned long totalMem = 0, freeMem = 0, totalSwap = 0, freeSwap = 0; - - meminfoFile.seekg(0, std::ios::beg); - - if(!meminfoFile.good()) - return false; - - while(!meminfoFile.eof() && meminfoFile.good()) { - std::string line; - std::getline(meminfoFile, line); - - char *name = new char[line.length()+1]; // is always long enough - unsigned long value; - - if(std::sscanf(line.c_str(), "%s %lu", name, &value) == 2) { - if(std::strcmp(name, "MemTotal:") == 0) - totalMem = value; - else if(std::strcmp(name, "MemFree:") == 0) - freeMem = value; - else if(std::strcmp(name, "SwapTotal:") == 0) - totalSwap = value; - else if(std::strcmp(name, "SwapFree:") == 0) - freeSwap = value; - } - - delete [] name; - - if(totalMem && freeMem && totalSwap && freeSwap) - break; - } - - callback(totalMem, freeMem, totalSwap, freeSwap); - - return true; -} - -bool SystemBackendProc::loadInfo(const sigc::slot &callback) { - unsigned long currentLoad = 0, nProcesses = 0; - float loadAvg1 = 0, loadAvg5 = 0, loadAvg15 = 0; - - loadFile.seekg(0, std::ios::beg); - - if(!loadFile.good()) - return false; - - std::string line; - std::getline(loadFile, line); - - if(line.empty()) - return false; - - std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1, &loadAvg5, &loadAvg15, ¤tLoad, &nProcesses); - - callback(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); - - return true; -} - -} -} -} diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h deleted file mode 100644 index 79ff6c5..0000000 --- a/src/Common/Backends/SystemBackendProc.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SystemBackendProc.h - * - * 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 . - */ - -#ifndef MAD_COMMON_BACKENDS_SYSTEMBACKENDPROC_H_ -#define MAD_COMMON_BACKENDS_SYSTEMBACKENDPROC_H_ - -#include "../SystemBackend.h" - -#include - -namespace Mad { -namespace Common { -namespace Backends { - -class SystemBackendProc : public SystemBackend { - private: - static SystemBackendProc backend; - - std::ifstream uptimeFile; - std::ifstream meminfoFile; - std::ifstream loadFile; - - SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} - - public: - static void registerBackend() { - SystemBackend::registerBackend(&backend); - } - - static void unregisterBackend() { - SystemBackend::unregisterBackend(&backend); - } - - virtual bool uptimeInfo(const sigc::slot &callback); - virtual bool memoryInfo(const sigc::slot &callback); - virtual bool loadInfo(const sigc::slot &callback); -}; - -} -} -} - -#endif /* MAD_COMMON_BACKENDS_SYSTEMBACKENDPROC_H_ */ diff --git a/src/Makefile.am b/src/Makefile.am index 8819a06..36dddfb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,7 +5,7 @@ bin_PROGRAMS = mad madc mad-core mad_SOURCES = mad.cpp mad_LDADD = Daemon/libdaemon.la Common/libcommon.la Net/libnet.la \ $(sigc_LIBS) $(GnuTLS_LIBS) @LIBLTDL@ -mad_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la +mad_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la -dlopen modules/SystemBackendProc.la madc_SOURCES = madc.cpp madc_LDADD = Client/libclient.la Common/libcommon.la Net/libnet.la \ @@ -14,4 +14,4 @@ madc_LDADD = Client/libclient.la Common/libcommon.la Net/libnet.la \ mad_core_SOURCES = mad-core.cpp mad_core_LDADD = Core/libcore.la Common/libcommon.la Net/libnet.la \ $(sigc_LIBS) $(GnuTLS_LIBS) $(GSSAPI_LIBS) @LIBLTDL@ -mad_core_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la +mad_core_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la -dlopen modules/SystemBackendProc.la diff --git a/src/Makefile.in b/src/Makefile.in index 841e70c..22d0c21 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -239,7 +239,7 @@ mad_SOURCES = mad.cpp mad_LDADD = Daemon/libdaemon.la Common/libcommon.la Net/libnet.la \ $(sigc_LIBS) $(GnuTLS_LIBS) @LIBLTDL@ -mad_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la +mad_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la -dlopen modules/SystemBackendProc.la madc_SOURCES = madc.cpp madc_LDADD = Client/libclient.la Common/libcommon.la Net/libnet.la \ $(sigc_LIBS) $(GnuTLS_LIBS) $(READLINE_LIBS) @@ -248,7 +248,7 @@ mad_core_SOURCES = mad-core.cpp mad_core_LDADD = Core/libcore.la Common/libcommon.la Net/libnet.la \ $(sigc_LIBS) $(GnuTLS_LIBS) $(GSSAPI_LIBS) @LIBLTDL@ -mad_core_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la +mad_core_LDFLAGS = -export-dynamic -dlopen modules/SystemBackendPosix.la -dlopen modules/SystemBackendProc.la all: all-recursive .SUFFIXES: diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 96c511f..c5627cb 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -20,7 +20,6 @@ #include "Common/ConfigManager.h" #include "Common/LogManager.h" #include "Common/Logger.h" -#include "Common/Backends/SystemBackendProc.h" #include "Common/ModuleManager.h" #include "Core/ConnectionManager.h" @@ -41,8 +40,8 @@ int main() { Common::ConfigManager::get()->loadFile("mad-core.conf"); Common::ConfigManager::get()->finish(); - Common::Backends::SystemBackendProc::registerBackend(); Common::ModuleManager::get()->loadModule("SystemBackendPosix.la"); + Common::ModuleManager::get()->loadModule("SystemBackendProc.la"); while(true) Core::ConnectionManager::get()->run(); diff --git a/src/mad.cpp b/src/mad.cpp index 6500258..1fed06d 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -20,7 +20,6 @@ #include "Net/ClientConnection.h" #include "Net/FdManager.h" #include "Net/IPAddress.h" -#include "Common/Backends/SystemBackendProc.h" #include "Common/ConfigManager.h" #include "Common/LogManager.h" #include "Common/Logger.h" @@ -52,8 +51,8 @@ int main() { Common::RequestManager::get()->registerPacketType(Net::Packet::COMMAND_REBOOT); Common::RequestManager::get()->registerPacketType(Net::Packet::COMMAND_SHUTDOWN); - Common::Backends::SystemBackendProc::registerBackend(); Common::ModuleManager::get()->loadModule("SystemBackendPosix.la"); + Common::ModuleManager::get()->loadModule("SystemBackendProc.la"); Net::ClientConnection *connection = new Net::ClientConnection; diff --git a/src/modules/Makefile.am b/src/modules/Makefile.am index d94701f..18d8ad6 100644 --- a/src/modules/Makefile.am +++ b/src/modules/Makefile.am @@ -1,9 +1,12 @@ madlibdir = ${libdir}/mad moddir = ${madlibdir}/modules -mod_LTLIBRARIES = SystemBackendPosix.la +mod_LTLIBRARIES = SystemBackendPosix.la SystemBackendProc.la +AM_LDFLAGS = -module -avoid-version -export-dynamic + SystemBackendPosix_la_SOURCES = SystemBackendPosix.cpp SystemBackendPosix_la_LIBADD = $(sigc_LIBS) -SystemBackendPosix_la_LDFLAGS = -module -avoid-version -export-dynamic -noinst_HEADERS = SystemBackendPosix.h +SystemBackendProc_la_SOURCES = SystemBackendProc.cpp + +noinst_HEADERS = SystemBackendPosix.h SystemBackendProc.h diff --git a/src/modules/Makefile.in b/src/modules/Makefile.in index 876828a..538ace6 100644 --- a/src/modules/Makefile.in +++ b/src/modules/Makefile.in @@ -60,9 +60,9 @@ am__DEPENDENCIES_1 = SystemBackendPosix_la_DEPENDENCIES = $(am__DEPENDENCIES_1) am_SystemBackendPosix_la_OBJECTS = SystemBackendPosix.lo SystemBackendPosix_la_OBJECTS = $(am_SystemBackendPosix_la_OBJECTS) -SystemBackendPosix_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \ - $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \ - $(CXXFLAGS) $(SystemBackendPosix_la_LDFLAGS) $(LDFLAGS) -o $@ +SystemBackendProc_la_LIBADD = +am_SystemBackendProc_la_OBJECTS = SystemBackendProc.lo +SystemBackendProc_la_OBJECTS = $(am_SystemBackendProc_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp am__depfiles_maybe = depfiles @@ -75,8 +75,10 @@ CXXLD = $(CXX) CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ -SOURCES = $(SystemBackendPosix_la_SOURCES) -DIST_SOURCES = $(SystemBackendPosix_la_SOURCES) +SOURCES = $(SystemBackendPosix_la_SOURCES) \ + $(SystemBackendProc_la_SOURCES) +DIST_SOURCES = $(SystemBackendPosix_la_SOURCES) \ + $(SystemBackendProc_la_SOURCES) HEADERS = $(noinst_HEADERS) ETAGS = etags CTAGS = ctags @@ -220,11 +222,12 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ madlibdir = ${libdir}/mad moddir = ${madlibdir}/modules -mod_LTLIBRARIES = SystemBackendPosix.la +mod_LTLIBRARIES = SystemBackendPosix.la SystemBackendProc.la +AM_LDFLAGS = -module -avoid-version -export-dynamic SystemBackendPosix_la_SOURCES = SystemBackendPosix.cpp SystemBackendPosix_la_LIBADD = $(sigc_LIBS) -SystemBackendPosix_la_LDFLAGS = -module -avoid-version -export-dynamic -noinst_HEADERS = SystemBackendPosix.h +SystemBackendProc_la_SOURCES = SystemBackendProc.cpp +noinst_HEADERS = SystemBackendPosix.h SystemBackendProc.h all: all-am .SUFFIXES: @@ -286,7 +289,9 @@ clean-modLTLIBRARIES: rm -f "$${dir}/so_locations"; \ done SystemBackendPosix.la: $(SystemBackendPosix_la_OBJECTS) $(SystemBackendPosix_la_DEPENDENCIES) - $(SystemBackendPosix_la_LINK) -rpath $(moddir) $(SystemBackendPosix_la_OBJECTS) $(SystemBackendPosix_la_LIBADD) $(LIBS) + $(CXXLINK) -rpath $(moddir) $(SystemBackendPosix_la_OBJECTS) $(SystemBackendPosix_la_LIBADD) $(LIBS) +SystemBackendProc.la: $(SystemBackendProc_la_OBJECTS) $(SystemBackendProc_la_DEPENDENCIES) + $(CXXLINK) -rpath $(moddir) $(SystemBackendProc_la_OBJECTS) $(SystemBackendProc_la_LIBADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -295,6 +300,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackendPosix.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackendProc.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/modules/SystemBackendProc.cpp b/src/modules/SystemBackendProc.cpp new file mode 100644 index 0000000..1af2f28 --- /dev/null +++ b/src/modules/SystemBackendProc.cpp @@ -0,0 +1,130 @@ +/* + * 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 + +#define init SystemBackendProc_LTX_init +#define deinit SystemBackendProc_LTX_deinit + +namespace Mad { +namespace Modules { + +SystemBackendProc *SystemBackendProc::backend = 0; + +bool SystemBackendProc::uptimeInfo(const sigc::slot &callback) { + unsigned long uptime = 0, idleTime = 0; + + uptimeFile.seekg(0, std::ios::beg); + + if(!uptimeFile.good()) + return false; + + float f; + uptimeFile >> f; + if(!uptimeFile.good()) + return false; + + uptime = (unsigned long)f; + + uptimeFile >> f; + if(uptimeFile.good()) + idleTime = (unsigned long)f; + + callback(uptime, idleTime); + + return true; +} + +bool SystemBackendProc::memoryInfo(const sigc::slot &callback) { + unsigned long totalMem = 0, freeMem = 0, totalSwap = 0, freeSwap = 0; + + meminfoFile.seekg(0, std::ios::beg); + + if(!meminfoFile.good()) + return false; + + while(!meminfoFile.eof() && meminfoFile.good()) { + std::string line; + std::getline(meminfoFile, line); + + char *name = new char[line.length()+1]; // is always long enough + unsigned long value; + + if(std::sscanf(line.c_str(), "%s %lu", name, &value) == 2) { + if(std::strcmp(name, "MemTotal:") == 0) + totalMem = value; + else if(std::strcmp(name, "MemFree:") == 0) + freeMem = value; + else if(std::strcmp(name, "SwapTotal:") == 0) + totalSwap = value; + else if(std::strcmp(name, "SwapFree:") == 0) + freeSwap = value; + } + + delete [] name; + + if(totalMem && freeMem && totalSwap && freeSwap) + break; + } + + callback(totalMem, freeMem, totalSwap, freeSwap); + + return true; +} + +bool SystemBackendProc::loadInfo(const sigc::slot &callback) { + unsigned long currentLoad = 0, nProcesses = 0; + float loadAvg1 = 0, loadAvg5 = 0, loadAvg15 = 0; + + loadFile.seekg(0, std::ios::beg); + + if(!loadFile.good()) + return false; + + std::string line; + std::getline(loadFile, line); + + if(line.empty()) + return false; + + std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1, &loadAvg5, &loadAvg15, ¤tLoad, &nProcesses); + + callback(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); + + return true; +} + +} +} + + +extern "C" { + +void init() { + Mad::Modules::SystemBackendProc::registerBackend(); +} + +void deinit() { + Mad::Modules::SystemBackendProc::unregisterBackend(); +} + +} diff --git a/src/modules/SystemBackendProc.h b/src/modules/SystemBackendProc.h new file mode 100644 index 0000000..db3c743 --- /dev/null +++ b/src/modules/SystemBackendProc.h @@ -0,0 +1,67 @@ +/* + * SystemBackendProc.h + * + * 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 . + */ + +#ifndef MAD_MODULE_SYSTEMBACKENDPROC_H_ +#define MAD_MODULE_SYSTEMBACKENDPROC_H_ + +#include + +#include + +namespace Mad { +namespace Modules { + +class SystemBackendProc : public Common::SystemBackend { + private: + static SystemBackendProc *backend; + + std::ifstream uptimeFile; + std::ifstream meminfoFile; + std::ifstream loadFile; + + SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} + + protected: + virtual bool uptimeInfo(const sigc::slot &callback); + virtual bool memoryInfo(const sigc::slot &callback); + virtual bool loadInfo(const sigc::slot &callback); + + public: + static void registerBackend() { + if(backend) + return; + + backend = new SystemBackendProc(); + SystemBackend::registerBackend(backend); + } + + static void unregisterBackend() { + if(!backend) + return; + + SystemBackend::unregisterBackend(backend); + delete backend; + backend = 0; + } +}; + +} +} + +#endif /* MAD_MODULE_SYSTEMBACKENDPROC_H_ */ -- cgit v1.2.3