summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/Makefile.am9
-rw-r--r--src/modules/Makefile.in24
-rw-r--r--src/modules/SystemBackendProc.cpp130
-rw-r--r--src/modules/SystemBackendProc.h67
4 files changed, 218 insertions, 12 deletions
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 <matthias@gamezock.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "SystemBackendProc.h"
+
+#include <cstdio>
+#include <cstring>
+
+#define init SystemBackendProc_LTX_init
+#define deinit SystemBackendProc_LTX_deinit
+
+namespace Mad {
+namespace Modules {
+
+SystemBackendProc *SystemBackendProc::backend = 0;
+
+bool SystemBackendProc::uptimeInfo(const sigc::slot<void, unsigned long, unsigned long> &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<void, unsigned long, unsigned long, unsigned long, unsigned long> &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<void, unsigned long, unsigned long, float, float, float> &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, &currentLoad, &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 <matthias@gamezock.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_MODULE_SYSTEMBACKENDPROC_H_
+#define MAD_MODULE_SYSTEMBACKENDPROC_H_
+
+#include <Common/SystemBackend.h>
+
+#include <fstream>
+
+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<void, unsigned long, unsigned long> &callback);
+ virtual bool memoryInfo(const sigc::slot<void, unsigned long, unsigned long, unsigned long, unsigned long> &callback);
+ virtual bool loadInfo(const sigc::slot<void, unsigned long, unsigned long, float, float, float> &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_ */