Many improvements in data collection configuration
[public/netxms.git] / src / libnxcl / alarms.cpp
CommitLineData
03482383
VK
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: alarms.cpp
21**
22**/
23
24#include "libnxcl.h"
25
26
eb484131
VK
27//
28// Fill alarm record from message
29//
30
31static void AlarmFromMsg(CSCPMessage *pMsg, NXC_ALARM *pAlarm)
32{
33 pAlarm->dwAckByUser = pMsg->GetVariableLong(VID_ACK_BY_USER);
34 pAlarm->dwSourceEvent = pMsg->GetVariableLong(VID_EVENT_ID);
35 pAlarm->dwSourceObject = pMsg->GetVariableLong(VID_OBJECT_ID);
36 pAlarm->dwTimeStamp = pMsg->GetVariableLong(VID_TIMESTAMP);
37 pMsg->GetVariableStr(VID_ALARM_KEY, pAlarm->szKey, MAX_DB_STRING);
38 pMsg->GetVariableStr(VID_ALARM_MESSAGE, pAlarm->szMessage, MAX_DB_STRING);
39 pAlarm->wIsAck = pMsg->GetVariableShort(VID_IS_ACK);
40 pAlarm->wSeverity = pMsg->GetVariableShort(VID_SEVERITY);
41}
42
43
44//
45// Process CMD_ALARM_UPDATE message
46//
47
48void ProcessAlarmUpdate(CSCPMessage *pMsg)
49{
50 NXC_ALARM alarm;
51 DWORD dwCode;
52
53 dwCode = pMsg->GetVariableLong(VID_NOTIFICATION_CODE);
54 alarm.dwAlarmId = pMsg->GetVariableLong(VID_ALARM_ID);
b900a78b 55 AlarmFromMsg(pMsg, &alarm);
eb484131
VK
56 CallEventHandler(NXC_EVENT_NOTIFICATION, dwCode, &alarm);
57}
58
59
03482383
VK
60//
61// Load all alarms from server
62//
63
ee20e615
VK
64DWORD LIBNXCL_EXPORTABLE NXCLoadAllAlarms(BOOL bIncludeAck, DWORD *pdwNumAlarms,
65 NXC_ALARM **ppAlarmList)
03482383 66{
ee20e615 67 CSCPMessage msg, *pResponce;
ece89362 68 DWORD dwRqId, dwRetCode = RCC_SUCCESS, dwNumAlarms = 0, dwAlarmId = 0;
ee20e615
VK
69 NXC_ALARM *pList = NULL;
70
71 dwRqId = g_dwMsgId++;
72
73 msg.SetCode(CMD_GET_ALL_ALARMS);
74 msg.SetId(dwRqId);
75 msg.SetVariable(VID_IS_ACK, (WORD)bIncludeAck);
76 SendMsg(&msg);
77
78 do
79 {
80 pResponce = WaitForMessage(CMD_ALARM_DATA, dwRqId, 2000);
81 if (pResponce != NULL)
82 {
83 dwAlarmId = pResponce->GetVariableLong(VID_ALARM_ID);
84 if (dwAlarmId != 0) // 0 is end of list indicator
85 {
9d72bde1 86 pList = (NXC_ALARM *)realloc(pList, sizeof(NXC_ALARM) * (dwNumAlarms + 1));
ee20e615 87 pList[dwNumAlarms].dwAlarmId = dwAlarmId;
eb484131 88 AlarmFromMsg(pResponce, &pList[dwNumAlarms]);
ee20e615
VK
89 dwNumAlarms++;
90 }
91 delete pResponce;
92 }
93 else
94 {
95 dwRetCode = RCC_TIMEOUT;
96 dwAlarmId = 0;
97 }
98 }
99 while(dwAlarmId != 0);
100
101 // Destroy results on failure or save on success
102 if (dwRetCode == RCC_SUCCESS)
103 {
104 *ppAlarmList = pList;
105 *pdwNumAlarms = dwNumAlarms;
106 }
107 else
108 {
9d72bde1 109 safe_free(pList);
ee20e615
VK
110 *ppAlarmList = NULL;
111 *pdwNumAlarms = 0;
112 }
113
114 return dwRetCode;
115}
116
117
118//
119// Acknowlege alarm by ID
120//
121
122DWORD LIBNXCL_EXPORTABLE NXCAcknowlegeAlarm(DWORD dwAlarmId)
123{
124 CSCPMessage msg;
125 DWORD dwRqId;
126
127 dwRqId = g_dwMsgId++;
128
129 msg.SetCode(CMD_ACK_ALARM);
130 msg.SetId(dwRqId);
131 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
132 SendMsg(&msg);
133
134 return WaitForRCC(dwRqId);
135}
136
137
138//
139// Delete alarm by ID
140//
141
142DWORD LIBNXCL_EXPORTABLE NXCDeleteAlarm(DWORD dwAlarmId)
143{
144 CSCPMessage msg;
145 DWORD dwRqId;
146
147 dwRqId = g_dwMsgId++;
148
149 msg.SetCode(CMD_DELETE_ALARM);
150 msg.SetId(dwRqId);
151 msg.SetVariable(VID_ALARM_ID, dwAlarmId);
152 SendMsg(&msg);
153
154 return WaitForRCC(dwRqId);
03482383 155}