Permalink
Browse files

CpuLoad plugin required libstatgrab.

  • Loading branch information...
1 parent 4b39347 commit de5b9ecae61f05773a5cb8cf7e100393dcf4cb2b @hronus hronus committed Feb 18, 2012
@@ -112,16 +112,14 @@ if (HELLOWORLD_PLUGIN)
add_subdirectory(plugin-helloworld)
endif (HELLOWORLD_PLUGIN)
-IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
- if (CPULOAD_PLUGIN)
+if (CPULOAD_PLUGIN)
+ FIND_LIBRARY(STATGRAB_LIB statgrab)
+
+ IF ( NOT(${STATGRAB_LIB} MATCHES "NOTFOUND" ))
set(ENABLED_PLUGINS ${ENABLED_PLUGINS} "CpuLoad")
add_subdirectory(plugin-cpuload)
- endif (CPULOAD_PLUGIN)
-
- SET(OperatingSystem "Linux")
-ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
-
-
+ endif ( NOT(${STATGRAB_LIB} MATCHES "NOTFOUND" ))
+endif (CPULOAD_PLUGIN)
message(STATUS "**************** The following plugins will be built ****************")
foreach (PLUGIN_STR ${ENABLED_PLUGINS})
@@ -4,20 +4,21 @@ set(PLUGIN "cpuload")
set(HEADERS
- razorcpuload.h
+ razorcpuload.h
)
set(SOURCES
- razorcpuload.cpp
+ razorcpuload.cpp
)
set(MOCS
- razorcpuload.h
+ razorcpuload.h
)
set(UIS
)
+LINK_LIBRARIES(${PLUGIN} ${STATGRAB_LIB})
#*******************************************
include ("../BuildPlugin.cmake")
@@ -30,6 +30,11 @@
#include <QPainter>
#include <QLinearGradient>
+extern "C" {
+#include <statgrab.h>
+}
+
+
EXPORT_RAZOR_PANEL_PLUGIN_CPP(RazorCpuLoad)
RazorCpuLoad::RazorCpuLoad(const RazorPanelPluginStartInfo* startInfo, QWidget* parent):
@@ -38,6 +43,14 @@ RazorCpuLoad::RazorCpuLoad(const RazorPanelPluginStartInfo* startInfo, QWidget*
setObjectName("CpuLoad");
addWidget(&m_stuff);
+ /* Initialise statgrab */
+ sg_init();
+
+ /* Drop setuid/setgid privileges. */
+ if (sg_drop_privileges() != 0) {
+ perror("Error. Failed to drop privileges");
+ }
+
getLoadCpu();
startTimer(500);
@@ -63,45 +76,14 @@ void RazorCpuLoad::resizeEvent(QResizeEvent *)
void RazorCpuLoad::getLoadCpu()
{
- QFile f("/proc/stat");
- f.open(QIODevice::ReadOnly);
- QTextStream st(&f);
- QStringList lines = st.readAll().split("\n");
-
- for( int i = 0 ; i< lines.count(); i++)
- {
- const QString& line = lines[i];
- if( line.startsWith("cpu "))
- {
- QStringList params = line.split(QRegExp("[ \t]+"));
- quint64 oldUser = currentUser;
- quint64 oldNice = currentNice;
- quint64 oldSystem = currentSystem;
- quint64 oldIdle = currentIdle;
-
- currentUser = params[1].toInt();
- currentNice = params[2].toInt();
- currentSystem= params[3].toInt();
- currentIdle = params[4].toInt();
-
- quint64 deltaUser = currentUser - oldUser;
- quint64 deltaNice = currentNice - oldNice;
- quint64 deltaSystem= currentSystem - oldSystem;
- quint64 deltaIdle = currentIdle - oldIdle;
-
- quint64 total = deltaUser+deltaNice+deltaSystem+deltaIdle;
- m_avg = 100 * (deltaUser+deltaNice+deltaSystem)/total;
-
- setToolTip(tr("Cpu load %1%").arg(m_avg));
-
- break;
- }
- }
+ sg_cpu_percents* cur = sg_get_cpu_percents();
+ m_avg = (cur->user + cur->kernel + cur->nice);
}
void RazorCpuLoad::timerEvent(QTimerEvent *event)
{
getLoadCpu();
+ setToolTip(tr("Cpu load %1%").arg(m_avg));
update();
}
@@ -48,11 +48,6 @@ class RazorCpuLoad: public RazorPanelPlugin
void getLoadCpu();
QWidget m_stuff;
- quint64 currentUser;
- quint64 currentNice;
- quint64 currentSystem;
- quint64 currentIdle;
-
//! average load
int m_avg;
};

0 comments on commit de5b9ec

Please sign in to comment.