source: nscp/include/pdh/query.hpp @ f0e6036

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

Reintegrated stable branch and removed some project files (no longer needed) (part 1 of many)

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <list>
24#include <pdh.h>
25#include <pdhmsg.h>
26#include <sstream>
27#include <error.hpp>
28#include <pdh/resolver.hpp>
29
30namespace PDH {
31        class PDHQuery : public PDH::PDHImplSubscriber {
32        private:
33                typedef std::list<PDHCounter*> CounterList;
34                CounterList counters_;
35                PDH::PDH_HQUERY hQuery_;
36        public:
37                PDHQuery() : hQuery_(NULL) {
38                }
39                virtual ~PDHQuery(void) {
40                        removeAllCounters();
41                }
42
43                PDHCounter* addCounter(std::wstring name, PDHCounterListener *listener) {
44                        PDHCounter *counter = new PDHCounter(name, listener);
45                        counters_.push_back(counter);
46                        return counter;
47                }
48                PDHCounter* addCounter(std::wstring name) {
49                        PDHCounter *counter = new PDHCounter(name);
50                        counters_.push_back(counter);
51                        return counter;
52                }
53                void removeAllCounters() {
54                        if (hQuery_)
55                                close();
56                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
57                                delete (*it);
58                        }
59                        counters_.clear();
60                }
61
62                virtual void on_unload() {
63                        if (hQuery_ == NULL)
64                                return;
65                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
66                                (*it)->remove();
67                        }
68                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhCloseQuery(hQuery_);
69                        if (status.is_error())
70                                throw PDHException(_T("PdhCloseQuery failed"), status);
71                        hQuery_ = NULL;
72                }
73                virtual void on_reload() {
74                        if (hQuery_ != NULL)
75                                return;
76                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhOpenQuery( NULL, 0, &hQuery_ );
77                        if (status.is_error())
78                                throw PDHException(_T("PdhOpenQuery failed"), status);
79                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
80                                (*it)->addToQuery(getQueryHandle());
81                        }
82                }
83
84                void open() {
85                        if (hQuery_ != NULL)
86                                throw PDHException(_T("query is not null!"));
87                        PDH::PDHFactory::get_impl()->add_listener(this);
88                        on_reload();
89                }
90
91                void close() {
92                        if (hQuery_ == NULL)
93                                throw PDHException(_T("query is null!"));
94                        PDH::PDHFactory::get_impl()->remove_listener(this);
95                        on_unload();
96                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
97                                delete (*it);
98                        }
99                        counters_.clear();
100                }
101
102                void gatherData() {
103                        collect();
104                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
105                                (*it)->collect();
106                        }
107                }
108                inline void collect() {
109                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhCollectQueryData(hQuery_);
110                        if (status.is_error())
111                                throw PDHException(_T("PdhCollectQueryData failed: "), status);
112                }
113
114                PDH::PDH_HQUERY getQueryHandle() const {
115                        return hQuery_;
116                }
117        };
118}
Note: See TracBrowser for help on using the repository browser.