4e0a37a59eb893e9e1cad64610bd813dc5347570
[public/netxms.git] / src / libnxcl / epp.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005, 2006, 2007 Victor Kirhenshtein
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 Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: epp.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Create copy of event policy rule
29 //
30
31 NXC_EPP_RULE LIBNXCL_EXPORTABLE *NXCCopyEventPolicyRule(NXC_EPP_RULE *src)
32 {
33 NXC_EPP_RULE *dst;
34
35 dst = (NXC_EPP_RULE *)nx_memdup(src, sizeof(NXC_EPP_RULE));
36 dst->pszComment = (src->pszComment != NULL) ? _tcsdup(src->pszComment) : NULL;
37 dst->pdwActionList = (DWORD *)nx_memdup(src->pdwActionList, src->dwNumActions * sizeof(DWORD));
38 dst->pdwSourceList = (DWORD *)nx_memdup(src->pdwSourceList, src->dwNumSources * sizeof(DWORD));
39 dst->pdwEventList = (DWORD *)nx_memdup(src->pdwEventList, src->dwNumEvents * sizeof(DWORD));
40 dst->pszScript = (src->pszScript != NULL) ? _tcsdup(src->pszScript) : NULL;
41 return dst;
42 }
43
44
45 //
46 // Create copy of event policy rule in given buffer
47 //
48
49 void LIBNXCL_EXPORTABLE NXCCopyEventPolicyRuleToBuffer(NXC_EPP_RULE *dst, NXC_EPP_RULE *src)
50 {
51 memcpy(dst, src, sizeof(NXC_EPP_RULE));
52 dst->pszComment = (src->pszComment != NULL) ? _tcsdup(src->pszComment) : NULL;
53 dst->pdwActionList = (DWORD *)nx_memdup(src->pdwActionList, src->dwNumActions * sizeof(DWORD));
54 dst->pdwSourceList = (DWORD *)nx_memdup(src->pdwSourceList, src->dwNumSources * sizeof(DWORD));
55 dst->pdwEventList = (DWORD *)nx_memdup(src->pdwEventList, src->dwNumEvents * sizeof(DWORD));
56 dst->pszScript = (src->pszScript != NULL) ? _tcsdup(src->pszScript) : NULL;
57 }
58
59
60 //
61 // Create empty event policy
62 //
63
64 NXC_EPP LIBNXCL_EXPORTABLE *NXCCreateEmptyEventPolicy()
65 {
66 NXC_EPP *epp;
67
68 epp = (NXC_EPP *)malloc(sizeof(NXC_EPP));
69 memset(epp, 0, sizeof(NXC_EPP));
70 return epp;
71 }
72
73
74 //
75 // Delete event policy structure
76 //
77
78 void LIBNXCL_EXPORTABLE NXCDestroyEventPolicy(NXC_EPP *pEventPolicy)
79 {
80 DWORD i;
81
82 if (pEventPolicy == NULL)
83 return;
84
85 for(i = 0; i < pEventPolicy->dwNumRules; i++)
86 {
87 safe_free(pEventPolicy->pRuleList[i].pszComment);
88 safe_free(pEventPolicy->pRuleList[i].pdwActionList);
89 safe_free(pEventPolicy->pRuleList[i].pdwSourceList);
90 safe_free(pEventPolicy->pRuleList[i].pdwEventList);
91 safe_free(pEventPolicy->pRuleList[i].pszScript);
92 }
93 safe_free(pEventPolicy->pRuleList);
94 free(pEventPolicy);
95 }
96
97
98 //
99 // Open and load to client event processing policy
100 //
101
102 DWORD LIBNXCL_EXPORTABLE NXCOpenEventPolicy(NXC_SESSION hSession, NXC_EPP **ppEventPolicy)
103 {
104 CSCPMessage msg, *pResponse;
105 DWORD i, j, dwRqId, dwRetCode, count, id;
106 TCHAR *attr, *value;
107
108 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
109
110 // Prepare message
111 msg.SetCode(CMD_OPEN_EPP);
112 msg.SetId(dwRqId);
113 ((NXCL_Session *)hSession)->SendMsg(&msg);
114
115 // Wait for reply
116 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
117 if (pResponse != NULL)
118 {
119 dwRetCode = pResponse->GetVariableLong(VID_RCC);
120 if (dwRetCode == RCC_SUCCESS)
121 {
122 // Prepare event policy structure
123 *ppEventPolicy = (NXC_EPP *)malloc(sizeof(NXC_EPP));
124 (*ppEventPolicy)->dwNumRules = pResponse->GetVariableLong(VID_NUM_RULES);
125 (*ppEventPolicy)->pRuleList =
126 (NXC_EPP_RULE *)malloc(sizeof(NXC_EPP_RULE) * (*ppEventPolicy)->dwNumRules);
127 memset((*ppEventPolicy)->pRuleList, 0, sizeof(NXC_EPP_RULE) * (*ppEventPolicy)->dwNumRules);
128 delete pResponse;
129
130 // Receive policy rules, each in separate message
131 for(i = 0; i < (*ppEventPolicy)->dwNumRules; i++)
132 {
133 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_EPP_RECORD, dwRqId);
134 if (pResponse != NULL)
135 {
136 (*ppEventPolicy)->pRuleList[i].dwFlags = pResponse->GetVariableLong(VID_FLAGS);
137 (*ppEventPolicy)->pRuleList[i].dwId = pResponse->GetVariableLong(VID_RULE_ID);
138 (*ppEventPolicy)->pRuleList[i].pszComment = pResponse->GetVariableStr(VID_COMMENTS);
139 (*ppEventPolicy)->pRuleList[i].pszScript = pResponse->GetVariableStr(VID_SCRIPT);
140
141 (*ppEventPolicy)->pRuleList[i].dwNumActions =
142 pResponse->GetVariableLong(VID_NUM_ACTIONS);
143 (*ppEventPolicy)->pRuleList[i].pdwActionList =
144 (DWORD *)malloc(sizeof(DWORD) * (*ppEventPolicy)->pRuleList[i].dwNumActions);
145 pResponse->GetVariableInt32Array(VID_RULE_ACTIONS,
146 (*ppEventPolicy)->pRuleList[i].dwNumActions,
147 (*ppEventPolicy)->pRuleList[i].pdwActionList);
148
149 (*ppEventPolicy)->pRuleList[i].dwNumEvents =
150 pResponse->GetVariableLong(VID_NUM_EVENTS);
151 (*ppEventPolicy)->pRuleList[i].pdwEventList =
152 (DWORD *)malloc(sizeof(DWORD) * (*ppEventPolicy)->pRuleList[i].dwNumEvents);
153 pResponse->GetVariableInt32Array(VID_RULE_EVENTS,
154 (*ppEventPolicy)->pRuleList[i].dwNumEvents,
155 (*ppEventPolicy)->pRuleList[i].pdwEventList);
156
157 (*ppEventPolicy)->pRuleList[i].dwNumSources =
158 pResponse->GetVariableLong(VID_NUM_SOURCES);
159 (*ppEventPolicy)->pRuleList[i].pdwSourceList =
160 (DWORD *)malloc(sizeof(DWORD) * (*ppEventPolicy)->pRuleList[i].dwNumSources);
161 pResponse->GetVariableInt32Array(VID_RULE_SOURCES,
162 (*ppEventPolicy)->pRuleList[i].dwNumSources,
163 (*ppEventPolicy)->pRuleList[i].pdwSourceList);
164
165 pResponse->GetVariableStr(VID_ALARM_KEY,
166 (*ppEventPolicy)->pRuleList[i].szAlarmKey,
167 MAX_DB_STRING);
168 pResponse->GetVariableStr(VID_ALARM_MESSAGE,
169 (*ppEventPolicy)->pRuleList[i].szAlarmMessage,
170 MAX_DB_STRING);
171 (*ppEventPolicy)->pRuleList[i].wAlarmSeverity = pResponse->GetVariableShort(VID_ALARM_SEVERITY);
172 (*ppEventPolicy)->pRuleList[i].dwAlarmTimeout = pResponse->GetVariableLong(VID_ALARM_TIMEOUT);
173 (*ppEventPolicy)->pRuleList[i].dwAlarmTimeoutEvent = pResponse->GetVariableLong(VID_ALARM_TIMEOUT_EVENT);
174
175 (*ppEventPolicy)->pRuleList[i].dwSituationId = pResponse->GetVariableLong(VID_SITUATION_ID);
176 pResponse->GetVariableStr(VID_SITUATION_INSTANCE,
177 (*ppEventPolicy)->pRuleList[i].szSituationInstance,
178 MAX_DB_STRING);
179 (*ppEventPolicy)->pRuleList[i].pSituationAttrList = new StringMap;
180 count = pResponse->GetVariableLong(VID_SITUATION_NUM_ATTRS);
181 for(j = 0, id = VID_SITUATION_ATTR_LIST_BASE; j < count; j++)
182 {
183 attr = pResponse->GetVariableStr(id++);
184 value = pResponse->GetVariableStr(id++);
185 (*ppEventPolicy)->pRuleList[i].pSituationAttrList->setPreallocated(attr, value);
186 }
187 delete pResponse;
188 }
189 else
190 {
191 dwRetCode = RCC_TIMEOUT;
192 break;
193 }
194 }
195
196 // Delete allocated policy structure if we have failed somewhere in the middle
197 if (dwRetCode != RCC_SUCCESS)
198 NXCDestroyEventPolicy(*ppEventPolicy);
199 }
200 else
201 {
202 if (dwRetCode == RCC_COMPONENT_LOCKED)
203 {
204 if (pResponse->IsVariableExist(VID_LOCKED_BY))
205 {
206 TCHAR szBuffer[MAX_LOCKINFO_LEN];
207
208 pResponse->GetVariableStr(VID_LOCKED_BY, szBuffer, MAX_LOCKINFO_LEN);
209 ((NXCL_Session *)hSession)->setLastLock(szBuffer);
210 }
211 else
212 {
213 ((NXCL_Session *)hSession)->setLastLock(_T("<unknown>"));
214 }
215 }
216 delete pResponse;
217 }
218 }
219 else
220 {
221 dwRetCode = RCC_TIMEOUT;
222 }
223 return dwRetCode;
224 }
225
226
227 //
228 // Close event policy (without saving)
229 //
230
231 DWORD LIBNXCL_EXPORTABLE NXCCloseEventPolicy(NXC_SESSION hSession)
232 {
233 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_CLOSE_EPP);
234 }
235
236
237 //
238 // Save (and install) new event policy
239 //
240
241 DWORD LIBNXCL_EXPORTABLE NXCSaveEventPolicy(NXC_SESSION hSession, NXC_EPP *pEventPolicy)
242 {
243 CSCPMessage msg;
244 DWORD i, j, id, count, dwRqId, dwRetCode;
245
246 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
247
248 // Prepare message
249 msg.SetCode(CMD_SAVE_EPP);
250 msg.SetId(dwRqId);
251 msg.SetVariable(VID_NUM_RULES, pEventPolicy->dwNumRules);
252
253 // Send message and wait for response
254 ((NXCL_Session *)hSession)->SendMsg(&msg);
255 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
256 if (dwRetCode == RCC_SUCCESS)
257 {
258 // Send all event policy records, one per message
259 msg.SetCode(CMD_EPP_RECORD);
260 for(i = 0; i < pEventPolicy->dwNumRules; i++)
261 {
262 msg.DeleteAllVariables();
263
264 msg.SetVariable(VID_FLAGS, pEventPolicy->pRuleList[i].dwFlags);
265 msg.SetVariable(VID_RULE_ID, pEventPolicy->pRuleList[i].dwId);
266 msg.SetVariable(VID_COMMENTS, (TCHAR *)CHECK_NULL_EX(pEventPolicy->pRuleList[i].pszComment));
267 msg.SetVariable(VID_SCRIPT, (TCHAR *)CHECK_NULL_EX(pEventPolicy->pRuleList[i].pszScript));
268 msg.SetVariable(VID_NUM_ACTIONS, pEventPolicy->pRuleList[i].dwNumActions);
269 msg.SetVariableToInt32Array(VID_RULE_ACTIONS,
270 pEventPolicy->pRuleList[i].dwNumActions,
271 pEventPolicy->pRuleList[i].pdwActionList);
272 msg.SetVariable(VID_NUM_EVENTS, pEventPolicy->pRuleList[i].dwNumEvents);
273 msg.SetVariableToInt32Array(VID_RULE_EVENTS,
274 pEventPolicy->pRuleList[i].dwNumEvents,
275 pEventPolicy->pRuleList[i].pdwEventList);
276 msg.SetVariable(VID_NUM_SOURCES, pEventPolicy->pRuleList[i].dwNumSources);
277 msg.SetVariableToInt32Array(VID_RULE_SOURCES,
278 pEventPolicy->pRuleList[i].dwNumSources,
279 pEventPolicy->pRuleList[i].pdwSourceList);
280 msg.SetVariable(VID_ALARM_KEY, pEventPolicy->pRuleList[i].szAlarmKey);
281 msg.SetVariable(VID_ALARM_MESSAGE, pEventPolicy->pRuleList[i].szAlarmMessage);
282 msg.SetVariable(VID_ALARM_SEVERITY, pEventPolicy->pRuleList[i].wAlarmSeverity);
283 msg.SetVariable(VID_ALARM_TIMEOUT, pEventPolicy->pRuleList[i].dwAlarmTimeout);
284 msg.SetVariable(VID_ALARM_TIMEOUT_EVENT, pEventPolicy->pRuleList[i].dwAlarmTimeoutEvent);
285 msg.SetVariable(VID_SITUATION_ID, pEventPolicy->pRuleList[i].dwSituationId);
286 msg.SetVariable(VID_SITUATION_INSTANCE, pEventPolicy->pRuleList[i].szSituationInstance);
287 count = (pEventPolicy->pRuleList[i].pSituationAttrList != NULL) ? pEventPolicy->pRuleList[i].pSituationAttrList->getSize() : 0;
288 msg.SetVariable(VID_SITUATION_NUM_ATTRS, count);
289 for(j = 0, id = VID_SITUATION_ATTR_LIST_BASE; j < count; j++)
290 {
291 msg.SetVariable(id++, pEventPolicy->pRuleList[i].pSituationAttrList->getKeyByIndex(j));
292 msg.SetVariable(id++, pEventPolicy->pRuleList[i].pSituationAttrList->getValueByIndex(j));
293 }
294
295 ((NXCL_Session *)hSession)->SendMsg(&msg);
296 }
297
298 // Wait for final confirmation if we have sent some rules
299 if (pEventPolicy->dwNumRules > 0)
300 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
301 }
302 return dwRetCode;
303 }
304
305
306 //
307 // Add rule to policy
308 //
309
310 void LIBNXCL_EXPORTABLE NXCAddPolicyRule(NXC_EPP *policy, NXC_EPP_RULE *rule, BOOL dynAllocated)
311 {
312 policy->dwNumRules++;
313 policy->pRuleList = (NXC_EPP_RULE *)realloc(policy->pRuleList, sizeof(NXC_EPP_RULE) * policy->dwNumRules);
314 memcpy(&policy->pRuleList[policy->dwNumRules - 1], rule, sizeof(NXC_EPP_RULE));
315 if (dynAllocated)
316 free(rule);
317 }
318
319
320 //
321 // Delete rule from policy
322 //
323
324 void LIBNXCL_EXPORTABLE NXCDeletePolicyRule(NXC_EPP *pEventPolicy, DWORD dwRule)
325 {
326 if (dwRule < pEventPolicy->dwNumRules)
327 {
328 safe_free(pEventPolicy->pRuleList[dwRule].pdwActionList);
329 safe_free(pEventPolicy->pRuleList[dwRule].pdwEventList);
330 safe_free(pEventPolicy->pRuleList[dwRule].pdwSourceList);
331 safe_free(pEventPolicy->pRuleList[dwRule].pszComment);
332 safe_free(pEventPolicy->pRuleList[dwRule].pszScript);
333 pEventPolicy->dwNumRules--;
334 memmove(&pEventPolicy->pRuleList[dwRule], &pEventPolicy->pRuleList[dwRule + 1],
335 sizeof(NXC_EPP_RULE) * (pEventPolicy->dwNumRules - dwRule));
336 }
337 }