refactoring of AgentConnection::getList
[public/netxms.git] / src / server / core / winperf.cpp
CommitLineData
46ee6286
VK
1/*
2** NetXMS - Network Management System
9b7ff3f5 3** Copyright (C) 2003-2017 Victor Kirhenshtein
46ee6286
VK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: winperf.cpp
20**
21**/
22
23#include "nxcore.h"
24
25/**
26 * Create new empty performance object
27 */
28WinPerfObject::WinPerfObject(const TCHAR *name)
29{
30 m_name = _tcsdup(name);
31 m_instances = new StringList;
32 m_counters = new StringList;
33}
34
35/**
36 * Destructor
37 */
38WinPerfObject::~WinPerfObject()
39{
9b7ff3f5 40 free(m_name);
46ee6286
VK
41 delete m_instances;
42 delete m_counters;
43}
44
45/**
46 * Fill information from agent
47 */
48bool WinPerfObject::readDataFromAgent(AgentConnection *conn)
49{
50 TCHAR param[256];
46ee6286 51 _sntprintf(param, 256, _T("PDH.ObjectCounters(\"%s\")"), m_name);
6a777a32
VK
52
53 StringList *data;
54 if (conn->getList(param, &data) != ERR_SUCCESS)
46ee6286 55 return false;
6a777a32
VK
56 for(int i = 0; i < data->size(); i++)
57 m_counters->add(data->get(i));
58 delete data;
46ee6286
VK
59
60 _sntprintf(param, 256, _T("PDH.ObjectInstances(\"%s\")"), m_name);
6a777a32 61 if (conn->getList(param, &data) != ERR_SUCCESS)
46ee6286 62 return false;
6a777a32
VK
63 for(int i = 0; i < data->size(); i++)
64 m_instances->add(data->get(i));
65 delete data;
46ee6286
VK
66
67 return true;
68}
69
70/**
71 * Fill NXCP message with object's data
72 *
73 * @return next available variable ID
74 */
b368969c 75UINT32 WinPerfObject::fillMessage(NXCPMessage *msg, UINT32 baseId)
46ee6286 76{
b368969c
VK
77 msg->setField(baseId, m_name);
78 msg->setField(baseId + 1, (UINT32)m_counters->size());
79 msg->setField(baseId + 2, (UINT32)m_instances->size());
46ee6286 80
967893bb 81 UINT32 varId = baseId + 3;
a6312bd6 82 for(int i = 0; i < m_counters->size(); i++)
b368969c 83 msg->setField(varId++, m_counters->get(i));
a6312bd6 84 for(int i = 0; i < m_instances->size(); i++)
b368969c 85 msg->setField(varId++, m_instances->get(i));
46ee6286
VK
86 return varId;
87}
88
89/**
90 * Get windows performance objects from node
91 */
92ObjectArray<WinPerfObject> *WinPerfObject::getWinPerfObjectsFromNode(Node *node, AgentConnection *conn)
93{
94 ObjectArray<WinPerfObject> *objects;
6a777a32
VK
95 StringList *data;
96 if (conn->getList(_T("PDH.Objects"), &data) == ERR_SUCCESS)
46ee6286 97 {
6a777a32
VK
98 objects = new ObjectArray<WinPerfObject>(data->size(), 16, true);
99 for(UINT32 i = 0; i < data->size(); i++)
100 objects->add(new WinPerfObject(data->get(i)));
101 delete data;
46ee6286
VK
102
103 for(int i = 0; i < objects->size(); i++)
104 {
105 if (!objects->get(i)->readDataFromAgent(conn))
106 {
c42b4551 107 DbgPrintf(5, _T("WinPerfObject::getWinPerfObjectsFromNode(%s [%d]): cannot read data for object %s"), node->getName(), node->getId(), objects->get(i)->getName());
46ee6286
VK
108 objects->remove(i);
109 i--;
110 }
111 }
112 if (objects->size() == 0)
113 delete_and_null(objects);
114 }
115 else
116 {
c42b4551 117 DbgPrintf(5, _T("WinPerfObject::getWinPerfObjectsFromNode(%s [%d]): cannot read PDH.Objects list"), node->getName(), node->getId());
46ee6286
VK
118 objects = NULL;
119 }
120 return objects;
121}