source: nscp/trunk/modules/CheckSystem/PDHCollector.cpp @ ae192e3

Last change on this file since ae192e3 was ae192e3, checked in by Michael Medin <michael@…>, 8 years ago

+ Added multitasking to socket listsner (it can now handle multiple connections)

  • Fixed bug in NSClientListener now "seqv" in check_nt shouldn't happen. + Added COUNTER support to NSClient and CheckSystem
  • Property mode set to 100644
File size: 4.8 KB
Line 
1//////////////////////////////////////////////////////////////////////////
2// PDH Collector
3//
4// Functions from this file collects data from the PDH subsystem and stores
5// it for later use
6// *NOTICE* that this is done in a separate thread so threading issues has
7// to be handled. I handle threading issues in the CounterListener's get/
8// set accessors.
9//
10// Copyright (c) 2004 MySolutions NORDIC (http://www.medin.nu)
11//
12// Date: 2004-03-13
13// Author: Michael Medin (mickem@medin.nu)
14//
15// This software is provided "AS IS", without a warranty of any kind.
16// You are free to use/modify this code but leave this header intact.
17//
18//////////////////////////////////////////////////////////////////////////
19
20#include "stdafx.h"
21#include "PDHCollector.h"
22
23
24PDHCollector::PDHCollector() : hStopEvent_(NULL) {
25        checkIntervall_ = NSCModuleHelper::getSettingsInt(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CHECK_RESOLUTION, C_SYSTEM_CHECK_RESOLUTION_DEFAULT);
26        std::string s = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU_BUFFER_TIME, C_SYSTEM_CPU_BUFFER_TIME_DEFAULT);
27        unsigned int i = strEx::stoui_as_time(s, checkIntervall_*100);
28        cpu.resize(i/(checkIntervall_*100));
29}
30
31PDHCollector::~PDHCollector()
32{
33        if (hStopEvent_)
34                CloseHandle(hStopEvent_);
35}
36
37/**
38 * Thread that collects the data every "CHECK_INTERVAL" seconds.
39 *
40 * @param lpParameter Not used
41 * @return thread exit status
42 *
43 * @author mickem
44 *
45 * @date 03-13-2004
46 *
47 * @bug If we have "custom named" counters ?
48 * @bug This whole concept needs work I think.
49 *
50 */
51DWORD PDHCollector::threadProc(LPVOID lpParameter) {
52        PDH::PDHQuery pdh;
53        pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT), &memCmtLim);
54        pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT), &memCmt);
55        pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT), &upTime);
56        pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT), &cpu);
57
58        try {
59                pdh.open();
60        } catch (const PDH::PDHException &e) {
61                NSC_LOG_ERROR_STD("Failed to open performance counters: " + e.str_);
62                return 0;
63        }
64
65        hStopEvent_ = CreateEvent(NULL, TRUE, FALSE, NULL);
66        if (!hStopEvent_) {
67                NSC_LOG_ERROR_STD("Create StopEvent failed: " + strEx::itos(GetLastError()));
68                return 0;
69        }
70
71        do {
72                MutexLock mutex(mutexHandler);
73                if (!mutex.hasMutex())
74                        NSC_LOG_ERROR("Failed to get Mutex!");
75                else {
76                        try {
77                                pdh.collect();
78                        } catch (const PDH::PDHException &e) {
79                                NSC_LOG_ERROR_STD("Failed to query performance counters: " + e.str_);
80                        }
81                }
82        }while (!(WaitForSingleObject(hStopEvent_, checkIntervall_*100) == WAIT_OBJECT_0));
83
84        if (!CloseHandle(hStopEvent_))
85                NSC_LOG_ERROR_STD("Failed to close stopEvent handle: " + strEx::itos(GetLastError()));
86        else
87                hStopEvent_ = NULL;
88        try {
89                pdh.close();
90        } catch (const PDH::PDHException &e) {
91                NSC_LOG_ERROR_STD("Failed to close performance counters: " + e.str_);
92        }
93        return 0;
94}
95
96
97/**
98 * Request termination of the thread (waiting for thread termination is not handled)
99 */
100void PDHCollector::exitThread(void) {
101        if (hStopEvent_ == NULL)
102                NSC_LOG_ERROR("Failed to get Mutex!");
103        else
104                if (!SetEvent(hStopEvent_)) {
105                        NSC_LOG_ERROR_STD("SetStopEvent failed");
106        }
107}
108/**
109 * Get the average CPU usage for "time"
110 * @param time Time to check
111 * @return average CPU usage
112 */
113int PDHCollector::getCPUAvrage(std::string time) {
114        unsigned int mseconds = strEx::stoui_as_time(time, checkIntervall_*100);
115        MutexLock mutex(mutexHandler);
116        if (!mutex.hasMutex()) {
117                NSC_LOG_ERROR("Failed to get Mutex!");
118                return -1;
119        }
120        return cpu.getAvrage(mseconds / (checkIntervall_*100));
121}
122/**
123 * Get uptime from counter
124 * @bug Do we need to collect this all the time ? (perhaps we can collect this in real time ?)
125 * @return uptime for the system
126 * @bug Are we overflow protected here ? (seem to recall some issues with overflow before ?)
127 */
128long long PDHCollector::getUptime() {
129        MutexLock mutex(mutexHandler);
130        if (!mutex.hasMutex()) {
131                NSC_LOG_ERROR("Failed to get Mutex!");
132                return -1;
133        }
134        return upTime.getValue();
135}
136/**
137 * Memory commit limit (your guess is as good as mine to what this is :)
138 * @return Some form of memory check
139 */
140long long PDHCollector::getMemCommitLimit() {
141        MutexLock mutex(mutexHandler);
142        if (!mutex.hasMutex()) {
143                NSC_LOG_ERROR("Failed to get Mutex!");
144                return -1;
145        }
146        return memCmtLim.getValue();
147}
148/**
149 *
150 * Memory committed bytes (your guess is as good as mine to what this is :)
151 * @return Some form of memory check
152 */
153long long PDHCollector::getMemCommit() {
154        MutexLock mutex(mutexHandler);
155        if (!mutex.hasMutex()) {
156                NSC_LOG_ERROR("Failed to get Mutex!");
157                return -1;
158        }
159        return memCmt.getValue();
160}
Note: See TracBrowser for help on using the repository browser.