"Fossies" - the Fresh Open Source Software Archive 
Member "netxms-3.8.166/src/server/libnxsrv/hostmib.cpp" (23 Feb 2021, 6233 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "hostmib.cpp" see the
Fossies "Dox" file reference documentation.
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2020 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation; either version 3 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 Lesser 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: hostmib.cpp
20 **/
21
22 #include "libnxsrv.h"
23 #include <nddrv.h>
24
25 #define DEBUG_TAG _T("ndd.hostmib")
26
27 /**
28 * Storage entry helper - get free space in bytes
29 */
30 void HostMibStorageEntry::getFree(TCHAR *buffer, size_t len) const
31 {
32 _sntprintf(buffer, len, UINT64_FMT, static_cast<UINT64>(size - used) * static_cast<UINT64>(unitSize));
33 }
34
35 /**
36 * Storage entry helper - get free space in percents
37 */
38 void HostMibStorageEntry::getFreePerc(TCHAR *buffer, size_t len) const
39 {
40 _sntprintf(buffer, len, _T("%f"), static_cast<double>(size - used) * 100.0 / static_cast<double>(size));
41 }
42
43 /**
44 * Storage entry helper - get total space in bytes
45 */
46 void HostMibStorageEntry::getTotal(TCHAR *buffer, size_t len) const
47 {
48 _sntprintf(buffer, len, UINT64_FMT, static_cast<UINT64>(size) * static_cast<UINT64>(unitSize));
49 }
50
51 /**
52 * Storage entry helper - get used space in bytes
53 */
54 void HostMibStorageEntry::getUsed(TCHAR *buffer, size_t len) const
55 {
56 _sntprintf(buffer, len, UINT64_FMT, static_cast<UINT64>(used) * static_cast<UINT64>(unitSize));
57 }
58
59 /**
60 * Storage entry helper - get used space in percents
61 */
62 void HostMibStorageEntry::getUsedPerc(TCHAR *buffer, size_t len) const
63 {
64 _sntprintf(buffer, len, _T("%f"), static_cast<double>(used) * 100.0 / static_cast<double>(size));
65 }
66
67 /**
68 * Storage entry helper - get metric by name
69 */
70 bool HostMibStorageEntry::getMetric(const TCHAR *name, TCHAR *buffer, size_t len) const
71 {
72 if (!_tcsicmp(name, _T("Free")))
73 getFree(buffer, len);
74 else if (!_tcsicmp(name, _T("FreePerc")))
75 getFreePerc(buffer, len);
76 else if (!_tcsicmp(name, _T("Total")))
77 getTotal(buffer, len);
78 else if (!_tcsicmp(name, _T("Used")))
79 getUsed(buffer, len);
80 else if (!_tcsicmp(name, _T("UsedPerc")))
81 getUsedPerc(buffer, len);
82 else
83 return false;
84 return true;
85 }
86
87 /**
88 * Constructor
89 */
90 HostMibDriverData::HostMibDriverData() : DriverData()
91 {
92 m_storage = new ObjectArray<HostMibStorageEntry>(16, 16, Ownership::True);
93 m_storageCacheTimestamp = 0;
94 m_storageCacheMutex = MutexCreate();
95 }
96
97 /**
98 * Destructor
99 */
100 HostMibDriverData::~HostMibDriverData()
101 {
102 delete m_storage;
103 MutexDestroy(m_storageCacheMutex);
104 }
105
106 /**
107 * Callback for processing storage walk
108 */
109 UINT32 HostMibDriverData::updateStorageCacheCallback(SNMP_Variable *v, SNMP_Transport *snmp)
110 {
111 SNMP_ObjectId oid = v->getName();
112
113 SNMP_PDU request(SNMP_GET_REQUEST, SnmpNewRequestId(), snmp->getSnmpVersion());
114
115 oid.changeElement(10, 2); // hrStorageType
116 request.bindVariable(new SNMP_Variable(oid));
117
118 oid.changeElement(10, 4); // hrStorageAllocationUnits
119 request.bindVariable(new SNMP_Variable(oid));
120
121 oid.changeElement(10, 5); // hrStorageSize
122 request.bindVariable(new SNMP_Variable(oid));
123
124 oid.changeElement(10, 6); // hrStorageUsed
125 request.bindVariable(new SNMP_Variable(oid));
126
127 SNMP_PDU *response;
128 UINT32 rc = snmp->doRequest(&request, &response, SnmpGetDefaultTimeout(), 3);
129 if (rc != SNMP_ERR_SUCCESS)
130 return rc;
131
132 if (response->getNumVariables() == 4)
133 {
134 HostMibStorageEntry *e = new HostMibStorageEntry;
135 v->getValueAsString(e->name, 128);
136 SNMP_ObjectId type = response->getVariable(0)->getValueAsObjectId();
137 if (type.compare(_T(".1.3.6.1.2.1.25.2.1")) == OID_LONGER)
138 e->type = static_cast<HostMibStorageType>(type.getElement(9));
139 else
140 e->type = hrStorageOther;
141 e->unitSize = response->getVariable(1)->getValueAsUInt();
142 e->size = response->getVariable(2)->getValueAsUInt();
143 e->used = response->getVariable(3)->getValueAsUInt();
144 e->lastUpdate = time(NULL);
145 memcpy(e->oid, oid.value(), 12 * sizeof(UINT32));
146 m_storage->add(e);
147 }
148 delete response;
149 return SNMP_ERR_SUCCESS;
150 }
151
152 /**
153 * Update storage entry cache
154 */
155 void HostMibDriverData::updateStorageCache(SNMP_Transport *snmp)
156 {
157 MutexLock(m_storageCacheMutex);
158 m_storage->clear();
159 SnmpWalk(snmp, _T(".1.3.6.1.2.1.25.2.3.1.3"), this, &HostMibDriverData::updateStorageCacheCallback);
160 m_storageCacheTimestamp = time(NULL);
161 nxlog_debug_tag(DEBUG_TAG, 5, _T("Storage cache updated for node %s [%u]:"), m_nodeName, m_nodeId);
162 for(int i = 0; i < m_storage->size(); i++)
163 {
164 HostMibStorageEntry *e = m_storage->get(i);
165 nxlog_debug_tag(DEBUG_TAG, 5, _T(" \"%s\": type=%d, size=%u, used=%u, unit=%u"), e->name, e->type, e->size, e->used, e->unitSize);
166 }
167 MutexUnlock(m_storageCacheMutex);
168 }
169
170 /**
171 * Get storage entry
172 */
173 const HostMibStorageEntry *HostMibDriverData::getStorageEntry(SNMP_Transport *snmp, const TCHAR *name, HostMibStorageType type)
174 {
175 if ((m_storageCacheTimestamp == 0) || (time(NULL) - m_storageCacheTimestamp > 3600))
176 updateStorageCache(snmp);
177
178 MutexLock(m_storageCacheMutex);
179
180 HostMibStorageEntry *entry = NULL;
181 for(int i = 0; i < m_storage->size(); i++)
182 {
183 HostMibStorageEntry *e = m_storage->get(i);
184 if ((e->type == type) && ((name == NULL) || !_tcscmp(name, e->name)))
185 {
186 entry = e;
187 break;
188 }
189 }
190
191 if (entry != NULL)
192 {
193 time_t now = time(NULL);
194 if (entry->lastUpdate + 5 < now)
195 {
196 if (SnmpGetEx(snmp, NULL, entry->oid, 12, &entry->used, sizeof(UINT32), 0, NULL) == SNMP_ERR_SUCCESS)
197 entry->lastUpdate = now;
198 else
199 entry = NULL; // return error
200 }
201 }
202
203 MutexUnlock(m_storageCacheMutex);
204 return entry;
205 }