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

0.4.00.4.10.4.2
Last change on this file since f7a074d 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.6 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/counters.hpp>
29
30namespace PDH {
31
32        class PDHResolver {
33        public:
34#define PDH_INDEX_BUF_LEN 2048
35                static std::wstring PdhLookupPerfNameByIndex(LPCTSTR szMachineName, DWORD dwNameIndex) {
36                        TCHAR *buffer = new TCHAR[PDH_INDEX_BUF_LEN+1];
37                        DWORD bufLen = PDH_INDEX_BUF_LEN;
38                       
39                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhLookupPerfNameByIndex(szMachineName,dwNameIndex,buffer,&bufLen);
40                        if (status.is_error()) {
41                                delete [] buffer;
42                                throw PDHException(_T("RESOLVER"), _T("PdhLookupPerfNameByIndex: Could not find index: ") + strEx::itos(dwNameIndex), status);
43                        }
44                        std::wstring ret = buffer;
45                        delete [] buffer;
46                        return ret;
47                }
48                static std::list<std::wstring> PdhExpandCounterPath(std::wstring szWildCardPath, DWORD buffSize = PDH_INDEX_BUF_LEN) {
49                        TCHAR *buffer = new TCHAR[buffSize+1];
50                        DWORD bufLen = buffSize;
51                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhExpandCounterPath(szWildCardPath.c_str(),buffer,&bufLen);
52                        if (status.is_error()) {
53                                delete [] buffer;
54                                if (buffSize == PDH_INDEX_BUF_LEN && bufLen > buffSize)
55                                        return PdhExpandCounterPath(szWildCardPath, bufLen+10);
56                                throw PDHException(_T("RESOLVER"), _T("PdhExpandCounterPath: Could not find index: ") + szWildCardPath, status);
57                        }
58                        std::list<std::wstring> ret = PDHHelpers::build_list(buffer, bufLen);
59                        delete [] buffer;
60                        return ret;
61                }
62
63                static DWORD PdhLookupPerfIndexByName(LPCTSTR szMachineName, LPCTSTR indexName) {
64                        DWORD ret;
65                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhLookupPerfIndexByName(szMachineName,indexName, &ret);
66                        if (status.is_error()) {
67                                throw PDHException(_T("RESOLVER"), std::wstring(_T("PdhLookupPerfNameByIndex: Could not find index: ")) + indexName, status);
68                        }
69                        return ret;
70                }
71
72                static bool validate(std::wstring counter, std::wstring &error, bool force_reload) {
73                        PDH::PDHError status = PDH::PDHFactory::get_impl()->PdhValidatePath(counter.c_str(), force_reload);
74                        if (status.is_error())
75                                error = status.to_wstring();
76                        return status.is_ok();
77                }
78
79                static bool PDHResolver::expand_index(std::wstring &counter) {
80                        std::wstring::size_type pos = 0;
81                        do {
82                                std::wstring::size_type p1 = counter.find_first_of(_T("0123456789"), pos);
83                                if (p1 == std::wstring::npos)
84                                        return true;
85                                std::wstring::size_type p2 = counter.find_first_not_of(_T("0123456789"), p1);
86                                if (p2 == std::wstring::npos)
87                                        p2 = counter.size();
88                                if (p2 <= p1)
89                                        return false;
90                                unsigned int index = strEx::stoi(counter.substr(p1, p2-p1));
91                                std::wstring sindex = PDH::PDHResolver::lookupIndex(index);
92                                counter.replace(p1, p2-p1, sindex);
93                                pos = p1 + sindex.size();
94                        } while (true);
95                }
96
97                static std::wstring lookupIndex(DWORD index) {
98                        return PDHResolver::PdhLookupPerfNameByIndex(NULL, index);
99                }
100                static DWORD lookupIndex(std::wstring name) {
101                        return PDHResolver::PdhLookupPerfIndexByName(NULL, name.c_str());
102                }
103
104        };
105}
Note: See TracBrowser for help on using the repository browser.