source: nscp/modules/NSClientCompat/PDHCollector.cpp @ 5fda37e

0.4.00.4.10.4.2stable
Last change on this file since 5fda37e was 452fd41, checked in by Michael Medin <michael@…>, 8 years ago
  • Changed the Thread class a bit (mutex -> signal, and CreatThread? does not return the instance)
  • Moved settings "keys" fro NRPE to config.h
  • Changed build options (added Distribution) which builds a zip file under ./dist (requires 7z installed)
  • Minor tweaks to error/debug logging and small fixes "here and there"
  • Property mode set to 100644
File size: 4.2 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() : cpu(BACK_INTERVAL*60/CHECK_INTERVAL), hStopEvent_(NULL) {
25}
26PDHCollector::~PDHCollector()
27{
28        if (hStopEvent_)
29                CloseHandle(hStopEvent_);
30}
31
32/**
33 * Thread that collects the data every "CHECK_INTERVAL" seconds.
34 *
35 * @param lpParameter Not used
36 * @return thread exit status
37 *
38 * @author mickem
39 *
40 * @date 03-13-2004
41 *
42 * @bug As this is now we have to wait for CHECK_INTERVAL seconds before the service terminates (should probably be changed into some form of event)
43 * @bug If we have "custom named" counters ?
44 * @bug This whole concept needs work I think.
45 *
46 */
47DWORD PDHCollector::threadProc(LPVOID lpParameter) {
48        PDH::PDHQuery pdh;
49        pdh.addCounter("\\\\.\\Memory\\Commit Limit", &memCmtLim);
50        pdh.addCounter("\\\\.\\Memory\\Committed Bytes", &memCmt);
51        pdh.addCounter("\\\\.\\System\\System Up Time", &upTime);
52        pdh.addCounter("\\\\.\\Processor(_total)\\% Processor Time", &cpu);
53
54        try {
55                pdh.open();
56        } catch (const PDH::PDHException &e) {
57                NSC_LOG_ERROR_STD("Failed to open performance counters: " + e.str_);
58                return 0;
59        }
60
61        hStopEvent_ = CreateEvent(NULL, TRUE, FALSE, NULL);
62        if (!hStopEvent_) {
63                NSC_LOG_ERROR_STD("Create StopEvent failed: " + strEx::itos(GetLastError()));
64                return 0;
65        }
66
67        do {
68                MutexLock mutex(mutexHandler);
69                if (!mutex.hasMutex())
70                        NSC_LOG_ERROR("Failed to get Mutex!");
71                else {
72                        try {
73                                pdh.collect();
74                        } catch (const PDH::PDHException &e) {
75                                NSC_LOG_ERROR_STD("Failed to query performance counters: " + e.str_);
76                        }
77                }
78        }while (!(WaitForSingleObject(hStopEvent_, CHECK_INTERVAL*1000) == WAIT_OBJECT_0));
79
80        if (!CloseHandle(hStopEvent_))
81                NSC_LOG_ERROR_STD("Failed to close stopEvent handle: " + strEx::itos(GetLastError()));
82        else
83                hStopEvent_ = NULL;
84        try {
85                pdh.close();
86        } catch (const PDH::PDHException &e) {
87                NSC_LOG_ERROR_STD("Failed to close performance counters: " + e.str_);
88        }
89        return 0;
90}
91
92
93/**
94 * Request termination of the thread (waiting for thread termination is not handled)
95 */
96void PDHCollector::exitThread(void) {
97        if (hStopEvent_ == NULL)
98                NSC_LOG_ERROR("Failed to get Mutex!");
99        else
100                if (!SetEvent(hStopEvent_)) {
101                        NSC_LOG_ERROR_STD("SetStopEvent failed");
102        }
103}
104/**
105 * Get the average CPU usage for "time"
106 * @param backItems
107 * @return average CPU usage
108 */
109int PDHCollector::getCPUAvrage(unsigned int backItems) {
110        MutexLock mutex(mutexHandler);
111        if (!mutex.hasMutex()) {
112                NSC_LOG_ERROR("Failed to get Mutex!");
113                return -1;
114        }
115        return cpu.getAvrage(backItems);
116}
117/**
118 * Get uptime from counter
119 * @bug Do we need to collect this all the time ? (perhaps we can collect this in real time ?)
120 * @return uptime for the system
121 * @bug Are we overflow protected here ? (seem to recall some issues with overflow before ?)
122 */
123long long PDHCollector::getUptime() {
124        MutexLock mutex(mutexHandler);
125        if (!mutex.hasMutex()) {
126                NSC_LOG_ERROR("Failed to get Mutex!");
127                return -1;
128        }
129        return upTime.getValue();
130}
131/**
132 * Memory commit limit (your guess is as good as mine to what this is :)
133 * @return Some form of memory check
134 */
135long long PDHCollector::getMemCommitLimit() {
136        MutexLock mutex(mutexHandler);
137        if (!mutex.hasMutex()) {
138                NSC_LOG_ERROR("Failed to get Mutex!");
139                return -1;
140        }
141        return memCmtLim.getValue();
142}
143/**
144 *
145 * Memory committed bytes (your guess is as good as mine to what this is :)
146 * @return Some form of memory check
147 */
148long long PDHCollector::getMemCommit() {
149        MutexLock mutex(mutexHandler);
150        if (!mutex.hasMutex()) {
151                NSC_LOG_ERROR("Failed to get Mutex!");
152                return -1;
153        }
154        return memCmt.getValue();
155}
Note: See TracBrowser for help on using the repository browser.