e0989dcb45308b9802543194baa53947eb7726c6
[public/netxms.git] / src / libnxcl / agentcfg.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005, 2006 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: agentcfg.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Get list of available agent configs
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCGetAgentConfigList(NXC_SESSION hSession, DWORD *pdwNumRecs,
32 NXC_AGENT_CONFIG_INFO **ppList)
33 {
34 CSCPMessage msg, *pResponse;
35 DWORD i, dwRetCode, dwRqId, dwId;
36
37 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
38
39 msg.SetCode(CMD_GET_AGENT_CFG_LIST);
40 msg.SetId(dwRqId);
41 ((NXCL_Session *)hSession)->SendMsg(&msg);
42
43 *pdwNumRecs = 0;
44 *ppList = NULL;
45
46 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
47 if (pResponse != NULL)
48 {
49 dwRetCode = pResponse->GetVariableLong(VID_RCC);
50 if (dwRetCode == RCC_SUCCESS)
51 {
52 *pdwNumRecs = pResponse->GetVariableLong(VID_NUM_RECORDS);
53 *ppList = (NXC_AGENT_CONFIG_INFO *)malloc(sizeof(NXC_AGENT_CONFIG_INFO) * (*pdwNumRecs));
54 for(i = 0, dwId = VID_AGENT_CFG_LIST_BASE; i < *pdwNumRecs; i++, dwId += 7)
55 {
56 (*ppList)[i].dwId = pResponse->GetVariableLong(dwId++);
57 pResponse->GetVariableStr(dwId++, (*ppList)[i].szName, MAX_DB_STRING);
58 (*ppList)[i].dwSequence = pResponse->GetVariableLong(dwId++);
59 }
60 }
61 delete pResponse;
62 }
63 else
64 {
65 dwRetCode = RCC_TIMEOUT;
66 }
67 return dwRetCode;
68 }
69
70
71 //
72 // Open agent's config
73 //
74
75 DWORD LIBNXCL_EXPORTABLE NXCOpenAgentConfig(NXC_SESSION hSession, DWORD dwCfgId,
76 NXC_AGENT_CONFIG *pConfig)
77 {
78 CSCPMessage msg, *pResponse;
79 DWORD dwRetCode, dwRqId;
80
81 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
82
83 msg.SetCode(CMD_OPEN_AGENT_CONFIG);
84 msg.SetId(dwRqId);
85 msg.SetVariable(VID_CONFIG_ID, dwCfgId);
86 ((NXCL_Session *)hSession)->SendMsg(&msg);
87
88 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
89 if (pResponse != NULL)
90 {
91 dwRetCode = pResponse->GetVariableLong(VID_RCC);
92 if (dwRetCode == RCC_SUCCESS)
93 {
94 pConfig->dwId = dwCfgId;
95 pConfig->dwSequence = pResponse->GetVariableLong(VID_SEQUENCE_NUMBER);
96 pConfig->pszFilter = pResponse->GetVariableStr(VID_FILTER);
97 pConfig->pszText = pResponse->GetVariableStr(VID_CONFIG_FILE);
98 pResponse->GetVariableStr(VID_NAME, pConfig->szName, MAX_DB_STRING);
99 }
100 delete pResponse;
101 }
102 else
103 {
104 dwRetCode = RCC_TIMEOUT;
105 }
106 return dwRetCode;
107 }
108
109
110 //
111 // Save agent's config
112 //
113
114 DWORD LIBNXCL_EXPORTABLE NXCSaveAgentConfig(NXC_SESSION hSession, NXC_AGENT_CONFIG *pConfig)
115 {
116 CSCPMessage msg, *pResponse;
117 DWORD dwRetCode, dwRqId;
118
119 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
120
121 msg.SetCode(CMD_SAVE_AGENT_CONFIG);
122 msg.SetId(dwRqId);
123 msg.SetVariable(VID_CONFIG_ID, pConfig->dwId);
124 msg.SetVariable(VID_SEQUENCE_NUMBER, pConfig->dwSequence);
125 msg.SetVariable(VID_NAME, pConfig->szName);
126 msg.SetVariable(VID_CONFIG_FILE, pConfig->pszText);
127 msg.SetVariable(VID_FILTER, pConfig->pszFilter);
128 ((NXCL_Session *)hSession)->SendMsg(&msg);
129
130 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
131 if (pResponse != NULL)
132 {
133 dwRetCode = pResponse->GetVariableLong(VID_RCC);
134 if (dwRetCode == RCC_SUCCESS)
135 {
136 if (pConfig->dwId == 0) // New ID was requested
137 {
138 pConfig->dwId = pResponse->GetVariableLong(VID_CONFIG_ID);
139 pConfig->dwSequence = pResponse->GetVariableLong(VID_SEQUENCE_NUMBER);
140 }
141 }
142 delete pResponse;
143 }
144 else
145 {
146 dwRetCode = RCC_TIMEOUT;
147 }
148 return dwRetCode;
149 }
150
151
152 //
153 // Delete agent config
154 //
155
156 DWORD LIBNXCL_EXPORTABLE NXCDeleteAgentConfig(NXC_SESSION hSession, DWORD dwCfgId)
157 {
158 CSCPMessage msg;
159 DWORD dwRqId;
160
161 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
162
163 msg.SetCode(CMD_DELETE_AGENT_CONFIG);
164 msg.SetId(dwRqId);
165 msg.SetVariable(VID_CONFIG_ID, dwCfgId);
166 ((NXCL_Session *)hSession)->SendMsg(&msg);
167
168 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
169 }
170
171
172 //
173 // Swap sequence numbers of two agent configs
174 //
175
176 DWORD LIBNXCL_EXPORTABLE NXCSwapAgentConfigs(NXC_SESSION hSession, DWORD dwCfgId1, DWORD dwCfgId2)
177 {
178 CSCPMessage msg;
179 DWORD dwRqId;
180
181 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
182
183 msg.SetCode(CMD_SWAP_AGENT_CONFIGS);
184 msg.SetId(dwRqId);
185 msg.SetVariable(VID_CONFIG_ID, dwCfgId1);
186 msg.SetVariable(VID_CONFIG_ID_2, dwCfgId2);
187 ((NXCL_Session *)hSession)->SendMsg(&msg);
188
189 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
190 }