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