source: nscp/modules/CheckSystem/PDHCollector.h

Last change on this file was 94bb8cc, checked in by Michael Medin <michael@…>, 6 weeks ago

Fixed issue with commands escaping
Changed a lot of the utf16 into utf8 as well as major API refactoring

  • Property mode set to 100644
File size: 4.9 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 <boost/thread.hpp>
24
25#include <pdh.hpp>
26#include <boost/unordered_map.hpp>
27#include <boost/shared_ptr.hpp>
28
29
30#define PDH_SYSTEM_KEY_CPU "cpu"
31#define PDH_SYSTEM_KEY_MCB "memory commit bytes"
32#define PDH_SYSTEM_KEY_MCL "memory commit limit"
33#define PDH_SYSTEM_KEY_UPT "uptime"
34
35
36/**
37 * @ingroup NSClientCompat
38 * PDH collector thread (gathers performance data and allows for clients to retrieve it)
39 *
40 * @version 1.0
41 * first version
42 *
43 * @date 02-13-2005
44 *
45 * @author mickem
46 *
47 * @par license
48 * This code is absolutely free to use and modify. The code is provided "as is" with
49 * no expressed or implied warranty. The author accepts no liability if it causes
50 * any damage to your computer, causes your pet to fall ill, increases baldness
51 * or makes your car start emitting strange noises when you start it up.
52 * This code has no bugs, just undocumented features!
53 *
54 */
55class PDHCollector {
56public:
57
58        struct system_counter_data {
59
60                struct counter {
61                        typedef enum data_type_struct {
62                                type_int64, type_uint64
63                        };
64                        typedef enum data_format_struct {
65                                format_large
66                        };
67                        typedef enum collection_strategy_struct {
68                                rrd, value
69                        };
70
71                        counter(std::string alias, std::wstring path, data_type_struct data_type, data_format_struct data_format, collection_strategy_struct collection_strategy)
72                                : alias(alias)
73                                , path(path)
74                                , data_type(data_type)
75                                , data_format(data_format)
76                                , collection_strategy(collection_strategy)
77                        {}
78                        counter(std::string alias, std::wstring path, data_type_struct data_type, data_format_struct data_format, collection_strategy_struct collection_strategy, std::string buffer_size)
79                                : alias(alias)
80                                , path(path)
81                                , data_type(data_type)
82                                , data_format(data_format)
83                                , collection_strategy(collection_strategy)
84                                , buffer_size(buffer_size)
85                        {}
86                        data_type_struct data_type;
87                        data_format_struct data_format;
88                        std::string alias;
89                        std::wstring path;
90                        std::string buffer_size;
91                        collection_strategy_struct collection_strategy;
92
93                        boost::shared_ptr<PDHCollectors::PDHCollector> create(int check_intervall);
94                        void set_default_buffer_size(std::string buffer_size_) {
95                                if (buffer_size.empty())
96                                        buffer_size = buffer_size_;
97                        }
98
99                        int get_buffer_length(int check_intervall) {
100                                try {
101                                        unsigned int i = strEx::stoui_as_time(buffer_size, check_intervall*100);
102                                        if (check_intervall == 0)
103                                                return 100; // TODO fix this!
104                                        return i/(check_intervall*100)+10;
105                                } catch (...) {
106                                        return 100; // TODO fix this!
107                                }
108
109                        }
110
111
112                };
113
114                unsigned int check_intervall;
115                std::string buffer_length;
116                std::wstring subsystem;
117
118                std::list<counter> counters;
119        };
120
121private:
122
123        boost::shared_mutex mutex_;
124        HANDLE stop_event_;
125        typedef boost::shared_ptr<PDHCollectors::PDHCollector> collector_ptr;
126        typedef boost::unordered_map<std::string,collector_ptr > counter_map;
127        counter_map counters_;
128        int check_intervall_;
129        boost::shared_ptr<boost::thread> thread_;
130        boost::shared_ptr<system_counter_data> thread_data_;
131
132public:
133        PDHCollector();
134        virtual ~PDHCollector();
135        void thread_proc();
136        void exitThread(void);
137
138        // Retrieve values
139        int getCPUAvrage(std::string time);
140        long long getUptime();
141        unsigned long long getMemCommitLimit();
142        unsigned long long getMemCommit();
143        bool loadCounter(PDH::PDHQuery &pdh);
144
145        __int64 get_int_value(std::string counter);
146        double get_avg_value(std::string counter, unsigned int delta);
147        double get_double(std::string counter);
148        void start(boost::shared_ptr<system_counter_data> data);
149        bool stop();
150};
Note: See TracBrowser for help on using the repository browser.