75806614787a19d089ef439eee725a6fdc475a04
[public/netxms.git] / src / libnxcl / eventdb.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: eventdb.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Fill event template structure from NXCP message
29 //
30
31 static void EventTemplateFromMsg(CSCPMessage *pMsg, NXC_EVENT_TEMPLATE *pEventTemplate)
32 {
33 pEventTemplate->dwCode = pMsg->GetVariableLong(VID_EVENT_CODE);
34 pEventTemplate->dwSeverity = pMsg->GetVariableLong(VID_SEVERITY);
35 pEventTemplate->dwFlags = pMsg->GetVariableLong(VID_FLAGS);
36 pMsg->GetVariableStr(VID_NAME, pEventTemplate->szName, MAX_EVENT_NAME);
37 pEventTemplate->pszMessage = pMsg->GetVariableStr(VID_MESSAGE, NULL, 0);
38 pEventTemplate->pszDescription = pMsg->GetVariableStr(VID_DESCRIPTION, NULL, 0);
39 }
40
41
42 //
43 // Process CMD_EVENT_DB_UPDATE message
44 //
45
46 void ProcessEventDBUpdate(NXCL_Session *pSession, CSCPMessage *pMsg)
47 {
48 NXC_EVENT_TEMPLATE et;
49 DWORD dwCode;
50
51 dwCode = pMsg->GetVariableShort(VID_NOTIFICATION_CODE);
52 et.dwCode = pMsg->GetVariableLong(VID_EVENT_CODE);
53 if (dwCode != NX_NOTIFY_ETMPL_DELETED)
54 EventTemplateFromMsg(pMsg, &et);
55 pSession->callEventHandler(NXC_EVENT_NOTIFICATION, dwCode, &et);
56 }
57
58
59 //
60 // Duplicate event template
61 //
62
63 NXC_EVENT_TEMPLATE *DuplicateEventTemplate(NXC_EVENT_TEMPLATE *pSrc)
64 {
65 NXC_EVENT_TEMPLATE *pDst;
66
67 pDst = (NXC_EVENT_TEMPLATE *)nx_memdup(pSrc, sizeof(NXC_EVENT_TEMPLATE));
68 pDst->pszDescription = _tcsdup(pSrc->pszDescription);
69 pDst->pszMessage = _tcsdup(pSrc->pszMessage);
70 return pDst;
71 }
72
73
74 //
75 // Add template to list
76 //
77
78 void LIBNXCL_EXPORTABLE NXCAddEventTemplate(NXC_SESSION hSession, NXC_EVENT_TEMPLATE *pEventTemplate)
79 {
80 ((NXCL_Session *)hSession)->AddEventTemplate(pEventTemplate, TRUE);
81 }
82
83
84 //
85 // Delete record from list
86 //
87
88 void LIBNXCL_EXPORTABLE NXCDeleteEDBRecord(NXC_SESSION hSession, DWORD dwEventCode)
89 {
90 ((NXCL_Session *)hSession)->DeleteEDBRecord(dwEventCode);
91 }
92
93
94 //
95 // Process record from network
96 //
97
98 void ProcessEventDBRecord(NXCL_Session *pSession, CSCPMessage *pMsg)
99 {
100 NXC_EVENT_TEMPLATE *pEventTemplate;
101 DWORD dwEventCode;
102
103 if (pMsg->GetCode() == CMD_EVENT_DB_RECORD)
104 {
105 dwEventCode = pMsg->GetVariableLong(VID_EVENT_CODE);
106 if (!pMsg->IsEndOfSequence() && (dwEventCode != 0))
107 {
108 // Allocate new event template structure and fill it with values from message
109 pEventTemplate = (NXC_EVENT_TEMPLATE *)malloc(sizeof(NXC_EVENT_TEMPLATE));
110 EventTemplateFromMsg(pMsg, pEventTemplate);
111 pSession->AddEventTemplate(pEventTemplate, FALSE);
112 }
113 else
114 {
115 pSession->CompleteSync(SYNC_EVENT_DB, RCC_SUCCESS);
116 }
117 }
118 }
119
120
121 //
122 // Load event configuration database
123 //
124
125 DWORD LIBNXCL_EXPORTABLE NXCLoadEventDB(NXC_SESSION hSession)
126 {
127 return ((NXCL_Session *)hSession)->LoadEventDB();
128 }
129
130
131 //
132 // Set event information
133 //
134
135 DWORD LIBNXCL_EXPORTABLE NXCModifyEventTemplate(NXC_SESSION hSession, NXC_EVENT_TEMPLATE *pArg)
136 {
137 CSCPMessage msg;
138 DWORD dwRqId;
139
140 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
141
142 // Prepare message
143 msg.SetCode(CMD_SET_EVENT_INFO);
144 msg.SetId(dwRqId);
145 msg.SetVariable(VID_EVENT_CODE, pArg->dwCode);
146 msg.SetVariable(VID_SEVERITY, pArg->dwSeverity);
147 msg.SetVariable(VID_FLAGS, pArg->dwFlags);
148 msg.SetVariable(VID_NAME, pArg->szName);
149 msg.SetVariable(VID_MESSAGE, pArg->pszMessage);
150 msg.SetVariable(VID_DESCRIPTION, pArg->pszDescription);
151 ((NXCL_Session *)hSession)->SendMsg(&msg);
152
153 // Wait for reply
154 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
155 }
156
157
158 //
159 // Delete event template
160 //
161
162 DWORD LIBNXCL_EXPORTABLE NXCDeleteEventTemplate(NXC_SESSION hSession, DWORD dwEventCode)
163 {
164 CSCPMessage msg;
165 DWORD dwRqId;
166
167 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
168
169 // Prepare message
170 msg.SetCode(CMD_DELETE_EVENT_TEMPLATE);
171 msg.SetId(dwRqId);
172 msg.SetVariable(VID_EVENT_CODE, dwEventCode);
173 ((NXCL_Session *)hSession)->SendMsg(&msg);
174
175 // Wait for reply
176 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
177 }
178
179
180 //
181 // Generate ID for new event
182 //
183
184 DWORD LIBNXCL_EXPORTABLE NXCGenerateEventCode(NXC_SESSION hSession, DWORD *pdwEventCode)
185 {
186 CSCPMessage msg, *pResponse;
187 DWORD dwRqId, dwRetCode;
188
189 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
190
191 // Prepare message
192 msg.SetCode(CMD_GENERATE_EVENT_CODE);
193 msg.SetId(dwRqId);
194 ((NXCL_Session *)hSession)->SendMsg(&msg);
195
196 // Wait for reply
197 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
198 if (pResponse != NULL)
199 {
200 dwRetCode = pResponse->GetVariableLong(VID_RCC);
201 if (dwRetCode == RCC_SUCCESS)
202 *pdwEventCode = pResponse->GetVariableLong(VID_EVENT_CODE);
203 }
204 else
205 {
206 dwRetCode = RCC_TIMEOUT;
207 }
208 return dwRetCode;
209 }
210
211
212 //
213 // Get pointer to event templates list
214 //
215
216 BOOL LIBNXCL_EXPORTABLE NXCGetEventDB(NXC_SESSION hSession,
217 NXC_EVENT_TEMPLATE ***pppTemplateList,
218 DWORD *pdwNumRecords)
219 {
220 return ((NXCL_Session *)hSession)->GetEventDB(pppTemplateList, pdwNumRecords);
221 }
222
223
224 //
225 // Resolve event id to name
226 //
227
228 const TCHAR LIBNXCL_EXPORTABLE *NXCGetEventName(NXC_SESSION hSession, DWORD dwId)
229 {
230 return ((NXCL_Session *)hSession)->GetEventName(dwId);
231 }
232
233
234 //
235 // Resolve event id to name using application-provided buffer
236 //
237
238 BOOL LIBNXCL_EXPORTABLE NXCGetEventNameEx(NXC_SESSION hSession, DWORD dwId,
239 TCHAR *pszBuffer, DWORD dwBufSize)
240 {
241 return ((NXCL_Session *)hSession)->GetEventNameEx(dwId, pszBuffer, dwBufSize);
242 }
243
244
245 //
246 // Get severity for given event id. Will return -1 for unknown id.
247 //
248
249 int LIBNXCL_EXPORTABLE NXCGetEventSeverity(NXC_SESSION hSession, DWORD dwId)
250 {
251 return ((NXCL_Session *)hSession)->GetEventSeverity(dwId);
252 }
253
254
255 //
256 // Get text of event template with given ID.
257 // If there are no template with such ID, empty string will be returned.
258 //
259
260 BOOL LIBNXCL_EXPORTABLE NXCGetEventText(NXC_SESSION hSession, DWORD dwId, TCHAR *pszBuffer, DWORD dwBufSize)
261 {
262 return ((NXCL_Session *)hSession)->GetEventText(dwId, pszBuffer, dwBufSize);
263 }