-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryRunner.cpp
218 lines (169 loc) · 5.8 KB
/
QueryRunner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
////////////////////////////////////////////////////////////////////////////////
//! \file QueryRunner.cpp
//! \brief The QueryRunner class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "QueryRunner.hpp"
#include <Core/StringUtils.hpp>
#include <WMI/Connection.hpp>
#include <WMI/DateTime.hpp>
#include "Formats.hpp"
#include <WMI/ObjectIterator.hpp>
#include <WMI/Exception.hpp>
////////////////////////////////////////////////////////////////////////////////
//! Execute a single query against a host.
tstring QueryRunner::run(WMI::Connection& connection, const ConstQueryPtr& query)
{
tstring formattedValue;
try
{
WMI::ObjectIterator end;
tstring text = Core::fmt(TXT("SELECT * FROM %s"), query->m_wmiClass.c_str());
if (!query->m_filterProperty.empty())
{
text += Core::fmt(TXT(" WHERE %s = '%s'"), query->m_filterProperty.c_str(),
query->m_filterValue.c_str());
}
WMI::ObjectIterator it = connection.execQuery(text.c_str());
if (it != end)
{
if (it->hasProperty(query->m_wmiProperty))
{
WCL::Variant value;
it->getProperty(query->m_wmiProperty, value);
formattedValue = formatValue(value, query->m_format);
}
else
{
formattedValue = TXT("#N/A");
}
}
}
catch (const WMI::Exception& /*e*/)
{
formattedValue = TXT("#ERR");
}
return formattedValue;
}
////////////////////////////////////////////////////////////////////////////////
//! Execute the queries for a host.
QueryRunner::Results QueryRunner::run(WMI::Connection& connection, const ConstQueryPtr* begin, const ConstQueryPtr* end)
{
std::vector<ConstQueryPtr> queries(begin, end);
return run(connection, queries.begin(), queries.end());
}
////////////////////////////////////////////////////////////////////////////////
//! Execute the queries for a host.
QueryRunner::Results QueryRunner::run(WMI::Connection& connection, const_iterator begin, const_iterator end)
{
Results results;
for (const_iterator it = begin; it != end; ++it)
results.push_back(run(connection, *it));
return results;
}
////////////////////////////////////////////////////////////////////////////////
//! Get the string used to separate groups of digits in a number.
static tstring getGroupSeparator()
{
int count = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, nullptr, 0);
tchar* buffer = static_cast<tchar*>(_alloca(Core::numBytes<tchar>(count+1)));
buffer[0] = TXT('\0');
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHOUSANDSEP, buffer, count+1) == 0)
return TXT(",");
return buffer;
}
////////////////////////////////////////////////////////////////////////////////
//! Format an integer value.
static tstring formatIntegerValue(uint64 value)
{
typedef tstring::reverse_iterator rev_iter;
typedef tstring::const_reverse_iterator c_rev_iter;
const tstring rawResult = Core::format<uint64>(value);
const size_t numDigits = (rawResult[0] != TXT('-')) ? rawResult.length() : (rawResult.length()-1);
const tstring separator = getGroupSeparator();
const size_t numSeps = (numDigits-1) / 3;
const size_t total = rawResult.length() + (numSeps * separator.length());
tstring result = tstring(total, TXT(' '));
c_rev_iter it = rawResult.rbegin();
c_rev_iter end = rawResult.rend();
size_t digits = 0;
size_t seps = 0;
rev_iter output = result.rbegin();
while (it != end)
{
*output++ = *it++;
if ( ((++digits % 3) == 0) && (seps++ != numSeps) )
output = std::copy(separator.rbegin(), separator.rend(), output);
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
//! Try and convert a string into a datetime. The format of a WMI datetime is:-
//! YYYYMMDDHHMMSS.FFFFFF+TZO e.g. 20101008181758.546000+060
bool tryConvertDateTime(const tstring& value, tstring& datetime)
{
CDateTime parsed;
tstring offset;
if (!WMI::tryParseDateTime(value, parsed, offset))
return false;
datetime = Core::fmt(TXT("%s %s"), parsed.ToString().c_str(), offset.c_str());
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Format the value according to the custom format string.
tstring QueryRunner::formatValue(const WCL::Variant& value, const tstring& format)
{
try
{
tstring formattedValue = TXT("<unknown format>");
if (format == Formats::STRING)
{
formattedValue = value.format();
}
else if (format == Formats::DATETIME)
{
if (!tryConvertDateTime(WCL::getValue<tstring>(value), formattedValue))
formattedValue = TXT("<non-datetime value>");
}
else if (format == Formats::BYTES)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value)) + TXT(" B");
}
else if (format == Formats::K_BYTES)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / 1024u) + TXT(" KB");
}
else if (format == Formats::M_BYTES)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / (1024u*1024u)) + TXT(" MB");
}
else if (format == Formats::G_BYTES)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / (1024u*1024u*1024u)) + TXT(" GB");
}
else if (format == Formats::KB_AS_MB)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / 1024u) + TXT(" MB");
}
else if (format == Formats::KB_AS_GB)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / (1024u*1024u)) + TXT(" GB");
}
else if (format == Formats::MB_AS_GB)
{
WCL::Variant uint64Value(value, VT_UI8);
formattedValue = formatIntegerValue(V_UI8(&uint64Value) / 1024u) + TXT(" GB");
}
return formattedValue;
}
catch (const WCL::ComException& /*e*/)
{
return TXT("#VAL");
}
}