2006-07-11 Esteban Sanchez <estebans@artica.es>

* modules/pandora_module_cpuusage.[cc,h]: Added to repository. Implements a module to get the CPU usage percentage.

        * modules/pandora_module.h, modules/pandora_module_factory.cc, modules/pandora_module_list.cc: Added support to Pandora_Module_Cpuusage.

        * modules/pandora_module_freedisk.cc: Removed unnecessary debug output.

        * modules/pandora_module_service.h: Style correction.

        * windows/pandora_wmi.[cc,h]: Added getCpuUsagePercentage(). Removed unnecessary debug output.

        * PandoraAgent.dev: Added new files to the project.

        * bin/PandoraAgent.exe: Updated to the new commit.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@129 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
esanchezm 2006-07-12 09:15:00 +00:00
parent 2a6ca79ad2
commit 226d6ec3c8
14 changed files with 226 additions and 19 deletions

View File

@ -1,3 +1,24 @@
2006-07-11 Esteban Sanchez <estebans@artica.es>
* modules/pandora_module_cpuusage.[cc,h]: Added to repository.
Implements a module to get the CPU usage percentage.
* modules/pandora_module.h, modules/pandora_module_factory.cc,
modules/pandora_module_list.cc: Added support to
Pandora_Module_Cpuusage.
* modules/pandora_module_freedisk.cc: Removed unnecessary debug
output.
* modules/pandora_module_service.h: Style correction.
* windows/pandora_wmi.[cc,h]: Added getCpuUsagePercentage(). Removed
unnecessary debug output.
* PandoraAgent.dev: Added new files to the project.
* bin/PandoraAgent.exe: Updated to the new commit.
2006-07-11 Esteban Sanchez <estebans@artica.es>
* pandora_windows_service.h: Public methods converted into private.

View File

@ -1,7 +1,7 @@
[Project]
FileName=PandoraAgent.dev
Name=PandoraAgent
UnitCount=61
UnitCount=63
Type=1
Ver=1
ObjFiles=
@ -657,3 +657,23 @@ Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit63]
FileName=modules\pandora_module_cpuusage.cc
CompileCpp=1
Folder=Modules
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit62]
FileName=modules\pandora_module_cpuusage.h
CompileCpp=1
Folder=Modules
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

View File

@ -73,7 +73,7 @@ main (int argc, char *argv[]) {
}
}
service->run ();
delete service;
delete service;
return 0;
}

View File

@ -46,13 +46,15 @@ namespace Pandora_Modules {
MODULE_EXEC,
MODULE_PROC,
MODULE_SERVICE,
MODULE_FREEDISK
MODULE_FREEDISK,
MODULE_CPUUSAGE
};
const string module_exec_str = "module_exec";
const string module_proc_str = "module_proc";
const string module_service_str = "module_service";
const string module_freedisk_str = "module_freedisk";
const string module_cpuusage_str = "module_cpuusage";
class Output_Error : public Pandora::Pandora_Exception { };
class Interval_Error : public Pandora::Pandora_Exception { };

View File

@ -0,0 +1,56 @@
/* Pandora cpuusage module. These modules check if a cpuusage is running in the
system.
Copyright (C) 2006 Artica ST.
Written by Esteban Sanchez.
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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "pandora_module_cpuusage.h"
#include "../windows/pandora_wmi.h"
#include "../pandora_strutils.h"
using namespace Pandora;
using namespace Pandora_Modules;
using namespace Pandora_Strutils;
Pandora_Module_Cpuusage::Pandora_Module_Cpuusage (string name, int cpu_id)
: Pandora_Module (name) {
this->cpu_id = cpu_id;
this->module_kind_str = module_cpuusage_str;
this->module_kind = MODULE_CPUUSAGE;
}
void
Pandora_Module_Cpuusage::run () {
int res;
try {
Pandora_Module::run ();
} catch (Interval_Not_Fulfilled e) {
return;
}
try {
res = Pandora_Wmi::getCpuUsagePercentage (this->cpu_id);
output = inttostr (res);
} catch (Pandora_Wmi::Pandora_Wmi_Error e) {
this->has_output = false;
}
}

View File

@ -0,0 +1,38 @@
/* Pandora CPU usage module. These modules check the usage percentage
of a CPU.
Copyright (C) 2006 Artica ST.
Written by Esteban Sanchez.
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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __PANDORA_MODULE_CPUUSAGE_H__
#define __PANDORA_MODULE_CPUUSAGE_H__
#include "pandora_module.h"
namespace Pandora_Modules {
class Pandora_Module_Cpuusage : public Pandora_Module {
private:
int cpu_id;
public:
Pandora_Module_Cpuusage (string name, int cpu_id);
void run ();
};
}
#endif

View File

@ -24,6 +24,7 @@
#include "pandora_module_proc.h"
#include "pandora_module_service.h"
#include "pandora_module_freedisk.h"
#include "pandora_module_cpuusage.h"
#include "../pandora_strutils.h"
#include <list>
@ -38,6 +39,7 @@ using namespace Pandora_Strutils;
#define TOKEN_PROC ("module_proc ")
#define TOKEN_SERVICE ("module_service ")
#define TOKEN_FREEDISK ("module_freedisk ")
#define TOKEN_CPUUSAGE ("module_cpuusage ")
#define TOKEN_MAX ("module_max ")
#define TOKEN_MIN ("module_min ")
#define TOKEN_DESCRIPTION ("module_description ")
@ -61,7 +63,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
string module_name, module_type, module_exec;
string module_min, module_max, module_description;
string module_interval, module_proc, module_service;
string module_freedisk;
string module_freedisk, module_cpuusage;
Pandora_Module *module;
bool numeric;
@ -106,6 +108,9 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
if (module_freedisk == "") {
module_freedisk = parseLine (line, TOKEN_FREEDISK);
}
if (module_cpuusage == "") {
module_cpuusage = parseLine (line, TOKEN_CPUUSAGE);
}
if (module_max == "") {
module_max = parseLine (line, TOKEN_MAX);
}
@ -132,7 +137,22 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) {
module = new Pandora_Module_Freedisk (module_name,
module_freedisk);
} else {
} else if (module_cpuusage != "") {
int cpu_id;
try {
cpu_id = strtoint (module_cpuusage);
} catch (Invalid_Conversion e) {
pandoraLog ("Invalid CPU id '%s' on module_cpuusage %s",
module_name.c_str ());
return NULL;
}
module = new Pandora_Module_Cpuusage (module_name,
cpu_id);
}
else {
return NULL;
}

View File

@ -55,7 +55,6 @@ Pandora_Module_Freedisk::run () {
res = Pandora_Wmi::getDiskFreeSpace (this->disk_id);
output = longtostr (res);
pandoraDebug ("%s", output.c_str ());
} catch (Pandora_Wmi::Pandora_Wmi_Error e) {
this->has_output = false;
}

View File

@ -24,6 +24,7 @@
#include "pandora_module_proc.h"
#include "pandora_module_service.h"
#include "pandora_module_freedisk.h"
#include "pandora_module_cpuusage.h"
#include <fstream>
using namespace std;
@ -97,6 +98,7 @@ Pandora_Module_List::parseModuleDefinition (string definition) {
Pandora_Module_Proc *module_proc;
Pandora_Module_Service *module_service;
Pandora_Module_Freedisk *module_freedisk;
Pandora_Module_Cpuusage *module_cpuusage;
module = Pandora_Module_Factory::getModuleFromDefinition (definition);
@ -123,6 +125,11 @@ Pandora_Module_List::parseModuleDefinition (string definition) {
module_freedisk = (Pandora_Module_Freedisk *) module;
modules->push_back (module_freedisk);
break;
case MODULE_CPUUSAGE:
module_cpuusage = (Pandora_Module_Cpuusage *) module;
modules->push_back (module_cpuusage);
break;
default:
break;

View File

@ -31,7 +31,7 @@ namespace Pandora_Modules {
public:
Pandora_Module_Service (string name, string service_name);
void run ();
void run ();
};
}

View File

@ -39,7 +39,7 @@ private:
string agent_name;
TiXmlElement * getXmlHeader ();
public:
void pandora_run ();
void pandora_init ();
public:

View File

@ -159,12 +159,10 @@ Pandora_Wmi::getDiskFreeSpace (string disk_id) {
id = fix.id;
pandoraDebug ("%s %s", disk_id.c_str (), id.c_str ());
if (disk_id == id) {
dhGetValue (L"%d", &fix.free_space, quickfix,
L".FreeSpace");
pandoraDebug ("%d Bytes", fix.free_space);
return fix.free_space;
}
@ -176,6 +174,51 @@ Pandora_Wmi::getDiskFreeSpace (string disk_id) {
throw Pandora_Wmi_Error ();
}
int
Pandora_Wmi::getCpuUsagePercentage (int cpu_id) {
CDhInitialize init;
CDispPtr wmi_svc, quickfixes;
string id, cpu_id_str;
dhToggleExceptions (TRUE);
cpu_id_str = "CPU";
cpu_id_str += Pandora_Strutils::inttostr (cpu_id);
struct QFix {
CDhStringA id;
long load_percentage;
};
try {
dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc));
dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc,
L".ExecQuery(%S)",
L"SELECT * FROM Win32_Processor "));
FOR_EACH (quickfix, quickfixes, NULL) {
QFix fix = { 0 };
dhGetValue (L"%s", &fix.id, quickfix,
L".DeviceID");
id = fix.id;
if (cpu_id_str == id) {
dhGetValue (L"%d", &fix.load_percentage, quickfix,
L".LoadPercentage");
return fix.load_percentage;
}
} NEXT_THROW (quickfix);
} catch (string errstr) {
pandoraLog ("getOSName error. %s", errstr.c_str ());
}
throw Pandora_Wmi_Error ();
}
string
Pandora_Wmi::getOSName () {
CDhInitialize init;

View File

@ -32,13 +32,14 @@ using namespace std;
namespace Pandora_Wmi {
class Pandora_Wmi_Error : public Pandora_Exception { };
int isProcessRunning (string process_name);
int isServiceRunning (string service_name);
long getDiskFreeSpace (string disk_id);
string getOSName ();
string getOSVersion ();
string getOSBuild ();
string getSystemName ();
int isProcessRunning (string process_name);
int isServiceRunning (string service_name);
long getDiskFreeSpace (string disk_id);
int getCpuUsagePercentage (int cpu_id);
string getOSName ();
string getOSVersion ();
string getOSBuild ();
string getSystemName ();
};
#endif