Rollback from r3608 to r3606
[public/netxms.git] / src / libnxcl / eventdb.cpp
CommitLineData
5039dede
AK
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// Duplicate event template
29//
30
31NXC_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
46void 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
56void 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
66void 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(SYNC_EVENT_DB, RCC_SUCCESS);
89 }
90 }
91}
92
93
94//
95// Load event configuration database
96//
97
98DWORD LIBNXCL_EXPORTABLE NXCLoadEventDB(NXC_SESSION hSession)
99{
100 return ((NXCL_Session *)hSession)->LoadEventDB();
101}
102
103
104//
105// Set event information
106//
107
108DWORD 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
135DWORD 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
157DWORD LIBNXCL_EXPORTABLE NXCGenerateEventCode(NXC_SESSION hSession, DWORD *pdwEventCode)
158{
159 CSCPMessage msg, *pResponse;
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 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
171 if (pResponse != NULL)
172 {
173 dwRetCode = pResponse->GetVariableLong(VID_RCC);
174 if (dwRetCode == RCC_SUCCESS)
175 *pdwEventCode = pResponse->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
189BOOL 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
201const 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
211BOOL 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
222int 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
233BOOL 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 event DB
241//
242
243DWORD LIBNXCL_EXPORTABLE NXCLockEventDB(NXC_SESSION hSession)
244{
245 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_LOCK_EVENT_DB);
246}
247
248
249//
250// Unlock event DB
251//
252
253DWORD LIBNXCL_EXPORTABLE NXCUnlockEventDB(NXC_SESSION hSession)
254{
255 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_UNLOCK_EVENT_DB);
256}