diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index ded6495250..78e8e3f8b8 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,24 @@ +2006-07-11 Esteban Sanchez + + * 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 * pandora_windows_service.h: Public methods converted into private. diff --git a/pandora_agents/win32/PandoraAgent.dev b/pandora_agents/win32/PandoraAgent.dev index f681fc581c..56646718cb 100644 --- a/pandora_agents/win32/PandoraAgent.dev +++ b/pandora_agents/win32/PandoraAgent.dev @@ -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= + diff --git a/pandora_agents/win32/bin/PandoraAgent.exe b/pandora_agents/win32/bin/PandoraAgent.exe index fcd39c3ea5..f3163648a2 100755 Binary files a/pandora_agents/win32/bin/PandoraAgent.exe and b/pandora_agents/win32/bin/PandoraAgent.exe differ diff --git a/pandora_agents/win32/main.cc b/pandora_agents/win32/main.cc index b8ee375f78..0b0f6ee54c 100644 --- a/pandora_agents/win32/main.cc +++ b/pandora_agents/win32/main.cc @@ -73,7 +73,7 @@ main (int argc, char *argv[]) { } } service->run (); - - delete service; + + delete service; return 0; } diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index 401bbe16d5..81523fca66 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -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 { }; diff --git a/pandora_agents/win32/modules/pandora_module_cpuusage.cc b/pandora_agents/win32/modules/pandora_module_cpuusage.cc new file mode 100644 index 0000000000..1b0191a24f --- /dev/null +++ b/pandora_agents/win32/modules/pandora_module_cpuusage.cc @@ -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; + } +} diff --git a/pandora_agents/win32/modules/pandora_module_cpuusage.h b/pandora_agents/win32/modules/pandora_module_cpuusage.h new file mode 100644 index 0000000000..f4c54c4be4 --- /dev/null +++ b/pandora_agents/win32/modules/pandora_module_cpuusage.h @@ -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 diff --git a/pandora_agents/win32/modules/pandora_module_factory.cc b/pandora_agents/win32/modules/pandora_module_factory.cc index 2ef56accd7..d9d4d20727 100644 --- a/pandora_agents/win32/modules/pandora_module_factory.cc +++ b/pandora_agents/win32/modules/pandora_module_factory.cc @@ -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 @@ -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; } diff --git a/pandora_agents/win32/modules/pandora_module_freedisk.cc b/pandora_agents/win32/modules/pandora_module_freedisk.cc index 33dfe37608..4badf4cb38 100644 --- a/pandora_agents/win32/modules/pandora_module_freedisk.cc +++ b/pandora_agents/win32/modules/pandora_module_freedisk.cc @@ -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; } diff --git a/pandora_agents/win32/modules/pandora_module_list.cc b/pandora_agents/win32/modules/pandora_module_list.cc index 497f037c4b..7fd3cac5f7 100644 --- a/pandora_agents/win32/modules/pandora_module_list.cc +++ b/pandora_agents/win32/modules/pandora_module_list.cc @@ -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 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; diff --git a/pandora_agents/win32/modules/pandora_module_service.h b/pandora_agents/win32/modules/pandora_module_service.h index d1a8a47fb3..47ccb97c56 100644 --- a/pandora_agents/win32/modules/pandora_module_service.h +++ b/pandora_agents/win32/modules/pandora_module_service.h @@ -31,7 +31,7 @@ namespace Pandora_Modules { public: Pandora_Module_Service (string name, string service_name); - void run (); + void run (); }; } diff --git a/pandora_agents/win32/pandora_windows_service.h b/pandora_agents/win32/pandora_windows_service.h index 7ac534fd73..a2e167bfb9 100644 --- a/pandora_agents/win32/pandora_windows_service.h +++ b/pandora_agents/win32/pandora_windows_service.h @@ -39,7 +39,7 @@ private: string agent_name; TiXmlElement * getXmlHeader (); - +public: void pandora_run (); void pandora_init (); public: diff --git a/pandora_agents/win32/windows/pandora_wmi.cc b/pandora_agents/win32/windows/pandora_wmi.cc index 70d5862117..ba908cafec 100644 --- a/pandora_agents/win32/windows/pandora_wmi.cc +++ b/pandora_agents/win32/windows/pandora_wmi.cc @@ -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; diff --git a/pandora_agents/win32/windows/pandora_wmi.h b/pandora_agents/win32/windows/pandora_wmi.h index a5a693dbfd..384f7d5cf9 100644 --- a/pandora_agents/win32/windows/pandora_wmi.h +++ b/pandora_agents/win32/windows/pandora_wmi.h @@ -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