e53b5b9613792d044412b1204896e7fae1e47169
[public/netxms.git] / src / libnxcl / alarms.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: alarms.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Fill alarm record from message
29 //
30
31 static void AlarmFromMsg(CSCPMessage *pMsg, NXC_ALARM *pAlarm)
32 {
33 pAlarm->dwAckByUser = pMsg->GetVariableLong(VID_ACK_BY_USER);
34 pAlarm->dwTermByUser = pMsg->GetVariableLong(VID_TERMINATED_BY_USER);
35 pAlarm->qwSourceEventId = pMsg->GetVariableInt64(VID_EVENT_ID);
36 pAlarm->dwSourceEventCode = pMsg->GetVariableLong(VID_EVENT_CODE);
37 pAlarm->dwSourceObject = pMsg->GetVariableLong(VID_OBJECT_ID);
38 pAlarm->dwCreationTime = pMsg->GetVariableLong(VID_CREATION_TIME);
39 pAlarm->dwLastChangeTime = pMsg->GetVariableLong(VID_LAST_CHANGE_TIME);
40 pMsg->GetVariableStr(VID_ALARM_KEY, pAlarm->szKey, MAX_DB_STRING);
41 pMsg->GetVariableStr(VID_ALARM_MESSAGE, pAlarm->szMessage, MAX_DB_STRING);
42 pAlarm->nState = (BYTE)pMsg->GetVariableShort(VID_STATE);
43 pAlarm->nCurrentSeverity = (BYTE)pMsg->GetVariableShort(VID_CURRENT_SEVERITY);
44 pAlarm->nOriginalSeverity = (BYTE)pMsg->GetVariableShort(VID_ORIGINAL_SEVERITY);
45 pAlarm->dwRepeatCount = pMsg->GetVariableLong(VID_REPEAT_COUNT);
46 pAlarm->nHelpDeskState = (BYTE)pMsg->GetVariableShort(VID_HELPDESK_STATE);
47 pMsg->GetVariableStr(VID_HELPDESK_REF, pAlarm->szHelpDeskRef, MAX_HELPDESK_REF_LEN);
48 pAlarm->dwTimeout = pMsg->GetVariableLong(VID_ALARM_TIMEOUT);
49 pAlarm->dwTimeoutEvent = pMsg->GetVariableLong(VID_ALARM_TIMEOUT_EVENT);
50 pAlarm->pUserData = NULL;
51 }
52
53
54 //
55 // Process CMD_ALARM_UPDATE message
56 //
57
58 void ProcessAlarmUpdate(NXCL_Session *pSession, CSCPMessage *pMsg)
59 {
60 NXC_ALARM alarm;
61 DWORD dwCode;
62
63 dwCode = pMsg->GetVariableLong(VID_NOTIFICATION_CODE);
64 alarm.dwAlarmId = pMsg->GetVariableLong(VID_ALARM_ID);
65 AlarmFromMsg(pMsg, &alarm);
66 pSession->callEventHandler(NXC_EVENT_NOTIFICATION, dwCode, &alarm);
67 }
68
69
70 //
71 // Load all alarms from server
72 //
73
74 DWORD LIBNXCL_EXPORTABLE NXCLoadAllAlarms(NXC_SESSION hSession, BOOL bIncludeAck,
75 DWORD *pdwNumAlarms, NXC_ALARM **ppAlarmList)
76 {
77 CSCPMessage msg, *pResponse;
78 DWORD dwRqId, dwRetCode = RCC_SUCCESS, dwNumAlarms = 0, dwAlarmId = 0;
79 NXC_ALARM *pList = NULL;
80
81 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
82
83 msg.SetCode(CMD_GET_ALL_ALARMS);
84 msg.SetId(dwRqId);
85 msg.SetVariable(VID_IS_ACK, (WORD)bIncludeAck);
86 ((NXCL_Session *)hSession)->SendMsg(&msg);
87
88 do
89 {
90 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_ALARM_DATA, dwRqId);
91 if (pResponse != NULL)
92 {
93 dwAlarmId = pResponse->GetVariableLong(VID_ALARM_ID);
94 if (dwAlarmId != 0) // 0 is end of list indicator
95 {
96 pList = (NXC_ALARM *)realloc(pList, sizeof(NXC_ALARM) * (dwNumAlarms + 1));
97 pList[dwNumAlarms].dwAlarmId = dwAlarmId;
98 AlarmFromMsg(pResponse, &pList[dwNumAlarms]);
99 dwNumAlarms++;
100 }
101 delete pResponse;
102 }
103 else
104 {
105 dwRetCode = RCC_TIMEOUT;
106 dwAlarmId = 0;
107 }
108 }
109 while(dwAlarmId != 0);
110
111 // Destroy results on failure or save on success
112 if (dwRetCode == RCC_SUCCESS)
113 {
114 *ppAlarmList = pList;
115 *pdwNumAlarms = dwNumAlarms;
116 }
117 else
118 {
119 safe_free(pList);
120 *ppAlarmList = NULL;
121 *pdwNumAlarms = 0;
122 }
123
124 return dwRetCode;
125 }
126
127
128 //
129 // Acknowledge alarm by ID
130 //
131
132 DWORD LIBNXCL_EXPORTABLE NXCAcknowledgeAlarm(NXC_SESSION hSession, DWORD dwAlarmId)
133 {
134 CSCPMessage msg;
135 DWORD dwRqId;
136
137 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
138
139 msg.SetCode(CMD_ACK_ALARM);
140 msg.SetId(dwRqId);
141 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
142 ((NXCL_Session *)hSession)->SendMsg(&msg);
143
144 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
145 }
146
147
148 //
149 // Terminate alarm by ID
150 //
151
152 DWORD LIBNXCL_EXPORTABLE NXCTerminateAlarm(NXC_SESSION hSession, DWORD dwAlarmId)
153 {
154 CSCPMessage msg;
155 DWORD dwRqId;
156
157 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
158
159 msg.SetCode(CMD_TERMINATE_ALARM);
160 msg.SetId(dwRqId);
161 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
162 ((NXCL_Session *)hSession)->SendMsg(&msg);
163
164 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
165 }
166
167
168 //
169 // Delete alarm by ID
170 //
171
172 DWORD LIBNXCL_EXPORTABLE NXCDeleteAlarm(NXC_SESSION hSession, DWORD dwAlarmId)
173 {
174 CSCPMessage msg;
175 DWORD dwRqId;
176
177 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
178
179 msg.SetCode(CMD_DELETE_ALARM);
180 msg.SetId(dwRqId);
181 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
182 ((NXCL_Session *)hSession)->SendMsg(&msg);
183
184 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
185 }
186
187
188 //
189 // Set helpdesk state to "Open"
190 //
191
192 DWORD LIBNXCL_EXPORTABLE NXCOpenAlarm(NXC_SESSION hSession, DWORD dwAlarmId, TCHAR *pszHelpdeskRef)
193 {
194 CSCPMessage msg;
195 DWORD dwRqId;
196
197 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
198
199 msg.SetCode(CMD_SET_ALARM_HD_STATE);
200 msg.SetId(dwRqId);
201 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
202 msg.SetVariable(VID_HELPDESK_STATE, (WORD)ALARM_HELPDESK_OPEN);
203 msg.SetVariable(VID_HELPDESK_REF, CHECK_NULL_EX(pszHelpdeskRef));
204 ((NXCL_Session *)hSession)->SendMsg(&msg);
205
206 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
207 }
208
209
210 //
211 // Set helpdesk state to "Closed"
212 //
213
214 DWORD LIBNXCL_EXPORTABLE NXCCloseAlarm(NXC_SESSION hSession, DWORD dwAlarmId)
215 {
216 CSCPMessage msg;
217 DWORD dwRqId;
218
219 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
220
221 msg.SetCode(CMD_SET_ALARM_HD_STATE);
222 msg.SetId(dwRqId);
223 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
224 msg.SetVariable(VID_HELPDESK_STATE, (WORD)ALARM_HELPDESK_CLOSED);
225 ((NXCL_Session *)hSession)->SendMsg(&msg);
226
227 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
228 }
229
230
231 //
232 // Format text from alarm data
233 //
234
235 TCHAR LIBNXCL_EXPORTABLE *NXCFormatAlarmText(NXC_SESSION session, NXC_ALARM *alarm, TCHAR *format)
236 {
237 String out;
238 TCHAR *prev, *curr;
239 static const TCHAR *helpdeskState[] = { _T("IGNORED"), _T("OPEN"), _T("CLOSED") };
240 static const TCHAR *severityText[] = { _T("NORMAL"), _T("WARNING"), _T("MINOR"), _T("MAJOR"), _T("CRITICAL") };
241
242 for(prev = format; *prev != 0; prev = curr)
243 {
244 curr = _tcschr(prev, _T('%'));
245 if (curr == NULL)
246 {
247 out += prev;
248 break;
249 }
250 out.addString(prev, (DWORD)(curr - prev));
251 curr++;
252 switch(*curr)
253 {
254 case '%':
255 out += _T("%");
256 break;
257 case 'e':
258 out.addFormattedString(_T("%u"), alarm->dwSourceEventCode);
259 break;
260 case 'E':
261 out += NXCGetEventName(session, alarm->dwSourceEventCode);
262 break;
263 case 'h':
264 out.addFormattedString(_T("%d"), alarm->nHelpDeskState);
265 break;
266 case 'H':
267 out += helpdeskState[alarm->nHelpDeskState];
268 break;
269 case 'I':
270 out.addFormattedString(_T("%u"), alarm->dwAlarmId);
271 break;
272 case 'm':
273 out += alarm->szMessage;
274 break;
275 case 's':
276 out.addFormattedString(_T("%d"), alarm->nCurrentSeverity);
277 break;
278 case 'S':
279 out += severityText[alarm->nCurrentSeverity];
280 break;
281 case 0:
282 curr--;
283 break;
284 default:
285 break;
286 }
287 curr++;
288 }
289 return _tcsdup((const TCHAR *)out);
290 }