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