source: nscp/trunk/modules/CheckSystem/PDHCollector.cpp @ 5d8e0b5

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

MAJOR CHANGES
2004-04-19 MickeM

+ Added SSL support
+ Added alot of new options
+ Added documentation

  • Renamed and restructured NSCLientCompat to CheckSystem
  • *ALOT* of fixes all over : We are now starting to get to something that is "stable" : This means that soon you might actually be able to use this.
  • Property mode set to 100644
File size: 4.9 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        NSC_DEBUG_MSG_STD(time + " resolved to: " + strEx::itos(mseconds) + "ms (" + strEx::itos(mseconds / (checkIntervall_*100)) + "items)");
116        MutexLock mutex(mutexHandler);
117        if (!mutex.hasMutex()) {
118                NSC_LOG_ERROR("Failed to get Mutex!");
119                return -1;
120        }
121        return cpu.getAvrage(mseconds / (checkIntervall_*100));
122}
123/**
124 * Get uptime from counter
125 * @bug Do we need to collect this all the time ? (perhaps we can collect this in real time ?)
126 * @return uptime for the system
127 * @bug Are we overflow protected here ? (seem to recall some issues with overflow before ?)
128 */
129long long PDHCollector::getUptime() {
130        MutexLock mutex(mutexHandler);
131        if (!mutex.hasMutex()) {
132                NSC_LOG_ERROR("Failed to get Mutex!");
133                return -1;
134        }
135        return upTime.getValue();
136}
137/**
138 * Memory commit limit (your guess is as good as mine to what this is :)
139 * @return Some form of memory check
140 */
141long long PDHCollector::getMemCommitLimit() {
142        MutexLock mutex(mutexHandler);
143        if (!mutex.hasMutex()) {
144                NSC_LOG_ERROR("Failed to get Mutex!");
145                return -1;
146        }
147        return memCmtLim.getValue();
148}
149/**
150 *
151 * Memory committed bytes (your guess is as good as mine to what this is :)
152 * @return Some form of memory check
153 */
154long long PDHCollector::getMemCommit() {
155        MutexLock mutex(mutexHandler);
156        if (!mutex.hasMutex()) {
157                NSC_LOG_ERROR("Failed to get Mutex!");
158                return -1;
159        }
160        return memCmt.getValue();
161}
Note: See TracBrowser for help on using the repository browser.