license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxcl / objtools.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 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 Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: objtools.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Get list of object tools
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCGetObjectTools(NXC_SESSION hSession, DWORD *pdwNumTools,
32 NXC_OBJECT_TOOL **ppToolList)
33 {
34 CSCPMessage msg, *pResponse;
35 DWORD i, dwResult, dwRqId, dwId;
36
37 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
38
39 msg.SetCode(CMD_GET_OBJECT_TOOLS);
40 msg.SetId(dwRqId);
41 ((NXCL_Session *)hSession)->SendMsg(&msg);
42
43 *pdwNumTools = 0;
44 *ppToolList = NULL;
45
46 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
47 if (pResponse != NULL)
48 {
49 dwResult = pResponse->GetVariableLong(VID_RCC);
50 if (dwResult == RCC_SUCCESS)
51 {
52 *pdwNumTools = pResponse->GetVariableLong(VID_NUM_TOOLS);
53 *ppToolList = (NXC_OBJECT_TOOL *)malloc(sizeof(NXC_OBJECT_TOOL) * (*pdwNumTools));
54 memset(*ppToolList, 0, sizeof(NXC_OBJECT_TOOL) * (*pdwNumTools));
55 for(i = 0, dwId = VID_OBJECT_TOOLS_BASE; i < *pdwNumTools; i++, dwId += 10)
56 {
57 (*ppToolList)[i].dwId = pResponse->GetVariableLong(dwId);
58 pResponse->GetVariableStr(dwId + 1, (*ppToolList)[i].szName, MAX_DB_STRING);
59 (*ppToolList)[i].wType = pResponse->GetVariableShort(dwId + 2);
60 (*ppToolList)[i].pszData = pResponse->GetVariableStr(dwId + 3);
61 (*ppToolList)[i].dwFlags = pResponse->GetVariableLong(dwId + 4);
62 pResponse->GetVariableStr(dwId + 5, (*ppToolList)[i].szDescription, MAX_DB_STRING);
63 (*ppToolList)[i].pszMatchingOID = pResponse->GetVariableStr(dwId + 6);
64 (*ppToolList)[i].pszConfirmationText = pResponse->GetVariableStr(dwId + 7);
65 }
66 }
67 delete pResponse;
68 }
69 else
70 {
71 dwResult = RCC_TIMEOUT;
72 }
73 return dwResult;
74 }
75
76
77 //
78 // Destroy list of object tools previously created by NXCGetObjectTools
79 //
80
81 void LIBNXCL_EXPORTABLE NXCDestroyObjectToolList(DWORD dwNumTools, NXC_OBJECT_TOOL *pList)
82 {
83 DWORD i;
84
85 if (pList != NULL)
86 {
87 for(i = 0; i < dwNumTools; i++)
88 {
89 safe_free(pList[i].pszData);
90 safe_free(pList[i].pszMatchingOID);
91 safe_free(pList[i].pszConfirmationText);
92 }
93 free(pList);
94 }
95 }
96
97
98 //
99 // Execute table tool
100 //
101
102 DWORD LIBNXCL_EXPORTABLE NXCExecuteTableTool(NXC_SESSION hSession, DWORD dwNodeId,
103 DWORD dwToolId, Table **ppData)
104 {
105 CSCPMessage msg, *pResponse;
106 DWORD dwResult, dwRqId;
107
108 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
109
110 msg.SetCode(CMD_EXEC_TABLE_TOOL);
111 msg.SetId(dwRqId);
112 msg.SetVariable(VID_OBJECT_ID, dwNodeId);
113 msg.SetVariable(VID_TOOL_ID, dwToolId);
114 ((NXCL_Session *)hSession)->SendMsg(&msg);
115
116 *ppData = NULL;
117
118 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
119 if (dwResult == RCC_SUCCESS)
120 {
121 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_TABLE_DATA, dwRqId, 180000);
122 if (pResponse != NULL)
123 {
124 dwResult = pResponse->GetVariableLong(VID_RCC);
125 if (dwResult == RCC_SUCCESS)
126 {
127 *ppData = new Table(pResponse);
128 }
129 delete pResponse;
130 }
131 else
132 {
133 dwResult = RCC_TIMEOUT;
134 }
135 }
136
137 return dwResult;
138 }
139
140
141 //
142 // Delete object tool
143 //
144
145 DWORD LIBNXCL_EXPORTABLE NXCDeleteObjectTool(NXC_SESSION hSession, DWORD dwToolId)
146 {
147 CSCPMessage msg;
148 DWORD dwRqId;
149
150 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
151
152 msg.SetCode(CMD_DELETE_OBJECT_TOOL);
153 msg.SetId(dwRqId);
154 msg.SetVariable(VID_TOOL_ID, dwToolId);
155 ((NXCL_Session *)hSession)->SendMsg(&msg);
156
157 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
158 }
159
160
161 //
162 // Get object tool details
163 //
164
165 DWORD LIBNXCL_EXPORTABLE NXCGetObjectToolDetails(NXC_SESSION hSession, DWORD dwToolId,
166 NXC_OBJECT_TOOL_DETAILS **ppData)
167 {
168 CSCPMessage msg, *pResponse;
169 DWORD dwRqId, dwResult;
170
171 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
172
173 msg.SetCode(CMD_GET_OBJECT_TOOL_DETAILS);
174 msg.SetId(dwRqId);
175 msg.SetVariable(VID_TOOL_ID, dwToolId);
176 ((NXCL_Session *)hSession)->SendMsg(&msg);
177
178 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
179 if (pResponse != NULL)
180 {
181 dwResult = pResponse->GetVariableLong(VID_RCC);
182 if (dwResult == RCC_SUCCESS)
183 {
184 *ppData = (NXC_OBJECT_TOOL_DETAILS *)malloc(sizeof(NXC_OBJECT_TOOL_DETAILS));
185 memset(*ppData, 0, sizeof(NXC_OBJECT_TOOL_DETAILS));
186 (*ppData)->dwId = dwToolId;
187 (*ppData)->dwFlags = pResponse->GetVariableLong(VID_FLAGS);
188 (*ppData)->wType = pResponse->GetVariableShort(VID_TOOL_TYPE);
189 (*ppData)->pszData = pResponse->GetVariableStr(VID_TOOL_DATA);
190 (*ppData)->pszConfirmationText = pResponse->GetVariableStr(VID_CONFIRMATION_TEXT);
191 pResponse->GetVariableStr(VID_NAME, (*ppData)->szName, MAX_DB_STRING);
192 pResponse->GetVariableStr(VID_DESCRIPTION, (*ppData)->szDescription, MAX_DB_STRING);
193 (*ppData)->pszMatchingOID = pResponse->GetVariableStr(VID_TOOL_OID);
194 (*ppData)->dwACLSize = pResponse->GetVariableLong(VID_ACL_SIZE);
195 (*ppData)->pdwACL = (DWORD *)malloc(sizeof(DWORD) * (*ppData)->dwACLSize);
196 pResponse->GetVariableInt32Array(VID_ACL, (*ppData)->dwACLSize, (*ppData)->pdwACL);
197 if (((*ppData)->wType == TOOL_TYPE_TABLE_SNMP) ||
198 ((*ppData)->wType == TOOL_TYPE_TABLE_AGENT))
199 {
200 DWORD i, dwId;
201
202 (*ppData)->wNumColumns = pResponse->GetVariableShort(VID_NUM_COLUMNS);
203 (*ppData)->pColList = (NXC_OBJECT_TOOL_COLUMN *)malloc(sizeof(NXC_OBJECT_TOOL_COLUMN) * (*ppData)->wNumColumns);
204 for(i = 0, dwId = VID_COLUMN_INFO_BASE; i < (DWORD)(*ppData)->wNumColumns; i++)
205 {
206 pResponse->GetVariableStr(dwId++, (*ppData)->pColList[i].szName, MAX_DB_STRING);
207 pResponse->GetVariableStr(dwId++, (*ppData)->pColList[i].szOID, MAX_DB_STRING);
208 (*ppData)->pColList[i].nFormat = (int)pResponse->GetVariableShort(dwId++);
209 (*ppData)->pColList[i].nSubstr = (int)pResponse->GetVariableShort(dwId++);
210 }
211 }
212 }
213 delete pResponse;
214 }
215 else
216 {
217 dwResult = RCC_TIMEOUT;
218 }
219 return dwResult;
220 }
221
222
223 //
224 // Destroy object tool detailed info structure
225 //
226
227 void LIBNXCL_EXPORTABLE NXCDestroyObjectToolDetails(NXC_OBJECT_TOOL_DETAILS *pData)
228 {
229 if (pData != NULL)
230 {
231 safe_free(pData->pszData);
232 safe_free(pData->pszMatchingOID);
233 safe_free(pData->pColList);
234 safe_free(pData->pdwACL);
235 free(pData);
236 }
237 }
238
239
240 //
241 // Generate ID for new object tool
242 //
243
244 DWORD LIBNXCL_EXPORTABLE NXCGenerateObjectToolId(NXC_SESSION hSession, DWORD *pdwToolId)
245 {
246 CSCPMessage msg, *pResponse;
247 DWORD dwRqId, dwRetCode;
248
249 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
250
251 // Prepare message
252 msg.SetCode(CMD_GENERATE_OBJECT_TOOL_ID);
253 msg.SetId(dwRqId);
254 ((NXCL_Session *)hSession)->SendMsg(&msg);
255
256 // Wait for reply
257 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
258 if (pResponse != NULL)
259 {
260 dwRetCode = pResponse->GetVariableLong(VID_RCC);
261 if (dwRetCode == RCC_SUCCESS)
262 *pdwToolId = pResponse->GetVariableLong(VID_TOOL_ID);
263 }
264 else
265 {
266 dwRetCode = RCC_TIMEOUT;
267 }
268 return dwRetCode;
269 }
270
271
272 //
273 // Update object tool configuration
274 //
275
276 DWORD LIBNXCL_EXPORTABLE NXCUpdateObjectTool(NXC_SESSION hSession,
277 NXC_OBJECT_TOOL_DETAILS *pData)
278 {
279 CSCPMessage msg;
280 DWORD dwRqId;
281
282 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
283
284 // Prepare message
285 msg.SetCode(CMD_UPDATE_OBJECT_TOOL);
286 msg.SetId(dwRqId);
287 msg.SetVariable(VID_TOOL_ID, pData->dwId);
288 msg.SetVariable(VID_NAME, pData->szName);
289 msg.SetVariable(VID_TOOL_TYPE, pData->wType);
290 msg.SetVariable(VID_FLAGS, pData->dwFlags);
291 msg.SetVariable(VID_DESCRIPTION, pData->szDescription);
292 msg.SetVariable(VID_TOOL_DATA, pData->pszData);
293 msg.SetVariable(VID_CONFIRMATION_TEXT, CHECK_NULL_EX(pData->pszConfirmationText));
294 msg.SetVariable(VID_ACL_SIZE, pData->dwACLSize);
295 msg.SetVariable(VID_TOOL_OID, CHECK_NULL_EX(pData->pszMatchingOID));
296 msg.SetVariableToInt32Array(VID_ACL, pData->dwACLSize, pData->pdwACL);
297 if ((pData->wType == TOOL_TYPE_TABLE_SNMP) ||
298 (pData->wType == TOOL_TYPE_TABLE_AGENT))
299 {
300 int i;
301 DWORD dwId;
302
303 msg.SetVariable(VID_NUM_COLUMNS, pData->wNumColumns);
304 for(i = 0, dwId = VID_COLUMN_INFO_BASE; i < (int)pData->wNumColumns; i++)
305 {
306 msg.SetVariable(dwId++, pData->pColList[i].szName);
307 msg.SetVariable(dwId++, pData->pColList[i].szOID);
308 msg.SetVariable(dwId++, (WORD)pData->pColList[i].nFormat);
309 msg.SetVariable(dwId++, (WORD)pData->pColList[i].nSubstr);
310 }
311 }
312
313 ((NXCL_Session *)hSession)->SendMsg(&msg);
314
315 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
316 }
317
318
319 //
320 // Check if given object tool is appropriate for given node
321 //
322
323 BOOL LIBNXCL_EXPORTABLE NXCIsAppropriateTool(NXC_OBJECT_TOOL *pTool, NXC_OBJECT *pObject)
324 {
325 BOOL bResult;
326
327 if (pObject == NULL)
328 return TRUE;
329
330 if (pObject->iClass == OBJECT_NODE)
331 {
332 bResult = TRUE;
333
334 if ((pTool->dwFlags & TF_REQUIRES_SNMP) && (!(pObject->node.dwFlags & NF_IS_SNMP)))
335 bResult = FALSE;
336 if ((pTool->dwFlags & TF_REQUIRES_AGENT) &&
337 (!(pObject->node.dwFlags & NF_IS_NATIVE_AGENT)))
338 bResult = FALSE;
339 if (pTool->dwFlags & TF_REQUIRES_OID_MATCH)
340 {
341 const TCHAR *pszPattern;
342
343 pszPattern = CHECK_NULL_EX(pTool->pszMatchingOID);
344 if (*pszPattern == 0)
345 pszPattern = _T("*");
346 if (!MatchString(pszPattern, CHECK_NULL_EX(pObject->node.pszSnmpObjectId), TRUE))
347 bResult = FALSE;
348 }
349 }
350 else
351 {
352 bResult = FALSE;
353 }
354 return bResult;
355 }