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

0.4.00.4.10.4.2
Last change on this file since f7a074d was 1ecd26f, checked in by Michael Medin <michael@…>, 2 years ago

syncronized streams between 0.4.x and 0.3.x as well as improed the CMAke build *alot*

  • 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#include <boost/shared_ptr.hpp>
30
31namespace PDH {
32        class PDHQuery : public PDH::PDHImplSubscriber {
33        private:
34                typedef boost::shared_ptr<PDHCounter> counter_ptr;
35                typedef boost::shared_ptr<PDHCounterListener> listener_ptr;
36                typedef std::list<counter_ptr> CounterList;
37                CounterList counters_;
38                PDH::PDH_HQUERY hQuery_;
39        public:
40                PDHQuery() : hQuery_(NULL) {
41                }
42                virtual ~PDHQuery(void) {
43                        removeAllCounters();
44                }
45
46                counter_ptr addCounter(std::wstring name, listener_ptr listener) {
47                        counter_ptr counter = counter_ptr(new PDHCounter(name, listener));
48                        counters_.push_back(counter);
49                        return counter;
50                }
51                counter_ptr addCounter(std::wstring name) {
52                        counter_ptr counter = counter_ptr(new PDHCounter(name));
53                        counters_.push_back(counter);
54                        return counter;
55                }
56                void removeAllCounters() {
57                        if (hQuery_)
58                                close();
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                        counters_.clear();
97                }
98
99                void gatherData() {
100                        collect();
101                        for (CounterList::iterator it = counters_.begin(); it != counters_.end(); it++) {
102                                (*it)->collect();
103                        }
104                }
105                inline void collect() {
106                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhCollectQueryData(hQuery_);
107                        if (status.is_error())
108                                throw PDHException(_T("PdhCollectQueryData failed: "), status);
109                }
110
111                PDH::PDH_HQUERY getQueryHandle() const {
112                        return hQuery_;
113                }
114        };
115}
Note: See TracBrowser for help on using the repository browser.