02bf92f8c1b2fda24a423ce5a839f88ebd6474cd
[public/netxms.git] / src / libnxcl / events.cpp
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** Client Library
5 ** Copyright (C) 2004, 2005, 2006, 2007, 2008 Victor Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 ** File: events.cpp
22 **
23 **/
24
25 #include "libnxcl.h"
26
27
28 //
29 // Process event log records coming from server
30 //
31
32 void ProcessEventLogRecords(NXCL_Session *pSession, CSCPMessage *pMsg)
33 {
34 DWORD i, dwNumRecords, dwId;
35 NXC_EVENT event;
36 int nOrder;
37
38 dwNumRecords = pMsg->GetVariableLong(VID_NUM_RECORDS);
39 nOrder = (int)pMsg->GetVariableShort(VID_RECORDS_ORDER);
40 DebugPrintf(_T("ProcessEventLogRecords(): %d records in message, in %s order"),
41 dwNumRecords, (nOrder == RECORD_ORDER_NORMAL) ? _T("normal") : _T("reversed"));
42 for(i = 0, dwId = VID_EVENTLOG_MSG_BASE; i < dwNumRecords; i++)
43 {
44 event.qwEventId = pMsg->GetVariableInt64(dwId++);
45 event.dwEventCode = pMsg->GetVariableLong(dwId++);
46 event.dwTimeStamp = pMsg->GetVariableLong(dwId++);
47 event.dwSourceId = pMsg->GetVariableLong(dwId++);
48 event.dwSeverity = pMsg->GetVariableShort(dwId++);
49 pMsg->GetVariableStr(dwId++, event.szMessage, MAX_EVENT_MSG_LENGTH);
50 pMsg->GetVariableStr(dwId++, event.szUserTag, MAX_USERTAG_LENGTH);
51
52 // Skip parameters
53 DWORD count = pMsg->GetVariableLong(dwId++);
54 dwId += count;
55
56 // Call client's callback to handle new record
57 pSession->callEventHandler(NXC_EVENT_NEW_ELOG_RECORD, nOrder, &event);
58 }
59
60 // Notify requestor thread if all messages was received
61 if (pMsg->IsEndOfSequence())
62 pSession->CompleteSync(SYNC_EVENTS, RCC_SUCCESS);
63 }
64
65
66 //
67 // Synchronize event log
68 // This function is NOT REENTRANT
69 //
70
71 DWORD LIBNXCL_EXPORTABLE NXCSyncEvents(NXC_SESSION hSession, DWORD dwMaxRecords)
72 {
73 CSCPMessage msg;
74 DWORD dwRetCode, dwRqId;
75
76 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
77 ((NXCL_Session *)hSession)->PrepareForSync(SYNC_EVENTS);
78
79 msg.SetCode(CMD_GET_EVENTS);
80 msg.SetId(dwRqId);
81 msg.SetVariable(VID_MAX_RECORDS, dwMaxRecords);
82 ((NXCL_Session *)hSession)->SendMsg(&msg);
83
84 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
85 if (dwRetCode == RCC_SUCCESS)
86 dwRetCode = ((NXCL_Session *)hSession)->WaitForSync(SYNC_EVENTS, INFINITE);
87 else
88 ((NXCL_Session *)hSession)->UnlockSyncOp(SYNC_EVENTS);
89
90 return dwRetCode;
91 }
92
93
94 //
95 // Send event to server
96 //
97
98 DWORD LIBNXCL_EXPORTABLE NXCSendEvent(NXC_SESSION hSession, DWORD dwEventCode,
99 DWORD dwObjectId, int iNumArgs, TCHAR **pArgList,
100 TCHAR *pszUserTag)
101 {
102 CSCPMessage msg;
103 DWORD dwRqId;
104 int i;
105
106 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
107
108 msg.SetCode(CMD_TRAP);
109 msg.SetId(dwRqId);
110 msg.SetVariable(VID_EVENT_CODE, dwEventCode);
111 msg.SetVariable(VID_OBJECT_ID, dwObjectId);
112 msg.SetVariable(VID_USER_TAG, CHECK_NULL_EX(pszUserTag));
113 msg.SetVariable(VID_NUM_ARGS, (WORD)iNumArgs);
114 for(i = 0; i < iNumArgs; i++)
115 msg.SetVariable(VID_EVENT_ARG_BASE + i, pArgList[i]);
116 ((NXCL_Session *)hSession)->SendMsg(&msg);
117
118 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
119 }
120
121
122 //
123 // Process syslog records coming from server
124 //
125
126 void ProcessSyslogRecords(NXCL_Session *pSession, CSCPMessage *pMsg)
127 {
128 DWORD i, dwNumRecords, dwId;
129 NXC_SYSLOG_RECORD rec;
130 int nOrder;
131
132 dwNumRecords = pMsg->GetVariableLong(VID_NUM_RECORDS);
133 nOrder = (int)pMsg->GetVariableShort(VID_RECORDS_ORDER);
134 DebugPrintf(_T("ProcessSyslogRecords(): %d records in message, in %s order"),
135 dwNumRecords, (nOrder == RECORD_ORDER_NORMAL) ? _T("normal") : _T("reversed"));
136 for(i = 0, dwId = VID_SYSLOG_MSG_BASE; i < dwNumRecords; i++)
137 {
138 rec.qwMsgId = pMsg->GetVariableInt64(dwId++);
139 rec.dwTimeStamp = pMsg->GetVariableLong(dwId++);
140 rec.wFacility = pMsg->GetVariableShort(dwId++);
141 rec.wSeverity = pMsg->GetVariableShort(dwId++);
142 rec.dwSourceObject = pMsg->GetVariableLong(dwId++);
143 pMsg->GetVariableStr(dwId++, rec.szHost, MAX_SYSLOG_HOSTNAME_LEN);
144 pMsg->GetVariableStr(dwId++, rec.szTag, MAX_SYSLOG_TAG_LEN);
145 rec.pszText = pMsg->GetVariableStr(dwId++);
146
147 // Call client's callback to handle new record
148 pSession->callEventHandler(NXC_EVENT_NEW_SYSLOG_RECORD, nOrder, &rec);
149 free(rec.pszText);
150 }
151
152 // Notify requestor thread if all messages was received
153 if (pMsg->IsEndOfSequence())
154 pSession->CompleteSync(SYNC_SYSLOG, RCC_SUCCESS);
155 }
156
157
158 //
159 // Synchronize syslog
160 // This function is NOT REENTRANT
161 //
162
163 DWORD LIBNXCL_EXPORTABLE NXCSyncSyslog(NXC_SESSION hSession, DWORD dwMaxRecords)
164 {
165 CSCPMessage msg;
166 DWORD dwRetCode, dwRqId;
167
168 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
169 ((NXCL_Session *)hSession)->PrepareForSync(SYNC_SYSLOG);
170
171 msg.SetCode(CMD_GET_SYSLOG);
172 msg.SetId(dwRqId);
173 msg.SetVariable(VID_MAX_RECORDS, dwMaxRecords);
174 ((NXCL_Session *)hSession)->SendMsg(&msg);
175
176 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
177 if (dwRetCode == RCC_SUCCESS)
178 dwRetCode = ((NXCL_Session *)hSession)->WaitForSync(SYNC_SYSLOG, INFINITE);
179 else
180 ((NXCL_Session *)hSession)->UnlockSyncOp(SYNC_SYSLOG);
181
182 return dwRetCode;
183 }