fcba05482ba5e63db2c95fbd3a7afdfced375dc7
[public/netxms.git] / src / libnxcl / server.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005, 2006, 2007, 2008 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: server.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Get list of configuration variables
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCGetServerVariables(NXC_SESSION hSession,
32 NXC_SERVER_VARIABLE **ppVarList,
33 DWORD *pdwNumVars)
34 {
35 CSCPMessage msg, *pResponse;
36 DWORD i, dwId, dwRqId, dwRetCode;
37
38 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
39
40 *pdwNumVars = 0;
41 *ppVarList = NULL;
42
43 // Build request message
44 msg.SetCode(CMD_GET_CONFIG_VARLIST);
45 msg.SetId(dwRqId);
46
47 // Send request
48 ((NXCL_Session *)hSession)->SendMsg(&msg);
49
50 // Wait for response
51 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId, 30000);
52 if (pResponse != NULL)
53 {
54 dwRetCode = pResponse->GetVariableLong(VID_RCC);
55 if (dwRetCode == RCC_SUCCESS)
56 {
57 *pdwNumVars = pResponse->GetVariableLong(VID_NUM_VARIABLES);
58 *ppVarList = (NXC_SERVER_VARIABLE *)malloc(sizeof(NXC_SERVER_VARIABLE) * (*pdwNumVars));
59
60 for(i = 0, dwId = VID_VARLIST_BASE; i < *pdwNumVars; i++)
61 {
62 pResponse->GetVariableStr(dwId++, (*ppVarList)[i].szName, MAX_OBJECT_NAME);
63 pResponse->GetVariableStr(dwId++, (*ppVarList)[i].szValue, MAX_DB_STRING);
64 (*ppVarList)[i].bNeedRestart = pResponse->GetVariableShort(dwId++) ? TRUE : FALSE;
65 }
66 }
67 delete pResponse;
68 }
69 else
70 {
71 dwRetCode = RCC_TIMEOUT;
72 }
73
74 return dwRetCode;
75 }
76
77
78 //
79 // Set value of server's variable
80 //
81
82 DWORD LIBNXCL_EXPORTABLE NXCSetServerVariable(NXC_SESSION hSession, const TCHAR *pszVarName,
83 const TCHAR *pszValue)
84 {
85 CSCPMessage msg;
86 DWORD dwRqId;
87
88 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
89
90 msg.SetCode(CMD_SET_CONFIG_VARIABLE);
91 msg.SetId(dwRqId);
92 msg.SetVariable(VID_NAME, pszVarName);
93 msg.SetVariable(VID_VALUE, pszValue);
94 ((NXCL_Session *)hSession)->SendMsg(&msg);
95
96 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
97 }
98
99
100 //
101 // Delete server's variable
102 //
103
104 DWORD LIBNXCL_EXPORTABLE NXCDeleteServerVariable(NXC_SESSION hSession, const TCHAR *pszVarName)
105 {
106 CSCPMessage msg;
107 DWORD dwRqId;
108
109 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
110
111 msg.SetCode(CMD_DELETE_CONFIG_VARIABLE);
112 msg.SetId(dwRqId);
113 msg.SetVariable(VID_NAME, pszVarName);
114 ((NXCL_Session *)hSession)->SendMsg(&msg);
115
116 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
117 }
118
119
120 //
121 // Get server statistics
122 //
123
124 DWORD LIBNXCL_EXPORTABLE NXCGetServerStats(NXC_SESSION hSession, NXC_SERVER_STATS *pStats)
125 {
126 CSCPMessage msg, *pResponse;
127 DWORD dwRqId, dwRetCode;
128
129 memset(pStats, 0, sizeof(NXC_SERVER_STATS));
130 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
131
132 msg.SetCode(CMD_GET_SERVER_STATS);
133 msg.SetId(dwRqId);
134 ((NXCL_Session *)hSession)->SendMsg(&msg);
135
136 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
137 if (pResponse != NULL)
138 {
139 dwRetCode = pResponse->GetVariableLong(VID_RCC);
140 if (dwRetCode == RCC_SUCCESS)
141 {
142 pStats->dwNumAlarms = pResponse->GetVariableLong(VID_NUM_ALARMS);
143 pResponse->GetVariableInt32Array(VID_ALARMS_BY_SEVERITY, 5, pStats->dwAlarmsBySeverity);
144 pStats->dwNumClientSessions = pResponse->GetVariableLong(VID_NUM_SESSIONS);
145 pStats->dwNumDCI = pResponse->GetVariableLong(VID_NUM_ITEMS);
146 pStats->dwNumNodes = pResponse->GetVariableLong(VID_NUM_NODES);
147 pStats->dwNumObjects = pResponse->GetVariableLong(VID_NUM_OBJECTS);
148 pStats->dwServerProcessVMSize = pResponse->GetVariableLong(VID_NETXMSD_PROCESS_VMSIZE);
149 pStats->dwServerProcessWorkSet = pResponse->GetVariableLong(VID_NETXMSD_PROCESS_WKSET);
150 pStats->dwServerUptime = pResponse->GetVariableLong(VID_SERVER_UPTIME);
151 pResponse->GetVariableStr(VID_SERVER_VERSION, pStats->szServerVersion, MAX_DB_STRING);
152 pStats->dwQSizeConditionPoller = pResponse->GetVariableLong(VID_QSIZE_CONDITION_POLLER);
153 pStats->dwQSizeConfPoller = pResponse->GetVariableLong(VID_QSIZE_CONF_POLLER);
154 pStats->dwQSizeDBWriter = pResponse->GetVariableLong(VID_QSIZE_DBWRITER);
155 pStats->dwQSizeDCIPoller = pResponse->GetVariableLong(VID_QSIZE_DCI_POLLER);
156 pStats->dwQSizeDiscovery = pResponse->GetVariableLong(VID_QSIZE_DISCOVERY);
157 pStats->dwQSizeEvents = pResponse->GetVariableLong(VID_QSIZE_EVENT);
158 pStats->dwQSizeNodePoller = pResponse->GetVariableLong(VID_QSIZE_NODE_POLLER);
159 pStats->dwQSizeRoutePoller = pResponse->GetVariableLong(VID_QSIZE_ROUTE_POLLER);
160 pStats->dwQSizeStatusPoller = pResponse->GetVariableLong(VID_QSIZE_STATUS_POLLER);
161 }
162 delete pResponse;
163 }
164 else
165 {
166 dwRetCode = RCC_TIMEOUT;
167 }
168 return dwRetCode;
169 }
170
171
172 //
173 // Get address list
174 //
175
176 DWORD LIBNXCL_EXPORTABLE NXCGetAddrList(NXC_SESSION hSession, DWORD dwListType,
177 DWORD *pdwAddrCount, NXC_ADDR_ENTRY **ppAddrList)
178 {
179 DWORD i, dwRetCode, dwRqId, dwId;
180 CSCPMessage msg, *pResponse;
181
182 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
183
184 msg.SetCode(CMD_GET_ADDR_LIST);
185 msg.SetId(dwRqId);
186 msg.SetVariable(VID_ADDR_LIST_TYPE, dwListType);
187 ((NXCL_Session *)hSession)->SendMsg(&msg);
188
189 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
190 if (pResponse != NULL)
191 {
192 dwRetCode = pResponse->GetVariableLong(VID_RCC);
193 if (dwRetCode == RCC_SUCCESS)
194 {
195 *pdwAddrCount = pResponse->GetVariableLong(VID_NUM_RECORDS);
196 *ppAddrList = (NXC_ADDR_ENTRY *)malloc(sizeof(NXC_ADDR_ENTRY) * (*pdwAddrCount));
197 for(i = 0, dwId = VID_ADDR_LIST_BASE; i < *pdwAddrCount; i++, dwId += 7)
198 {
199 (*ppAddrList)[i].dwType = pResponse->GetVariableLong(dwId++);
200 (*ppAddrList)[i].dwAddr1 = pResponse->GetVariableLong(dwId++);
201 (*ppAddrList)[i].dwAddr2 = pResponse->GetVariableLong(dwId++);
202 }
203 }
204 delete pResponse;
205 }
206 else
207 {
208 dwRetCode = RCC_TIMEOUT;
209 }
210 return dwRetCode;
211 }
212
213
214 //
215 // Set address list
216 //
217
218 DWORD LIBNXCL_EXPORTABLE NXCSetAddrList(NXC_SESSION hSession, DWORD dwListType,
219 DWORD dwAddrCount, NXC_ADDR_ENTRY *pAddrList)
220 {
221 DWORD i, dwRqId, dwId;
222 CSCPMessage msg;
223
224 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
225
226 msg.SetCode(CMD_SET_ADDR_LIST);
227 msg.SetId(dwRqId);
228 msg.SetVariable(VID_ADDR_LIST_TYPE, dwListType);
229 msg.SetVariable(VID_NUM_RECORDS, dwAddrCount);
230 for(i = 0, dwId = VID_ADDR_LIST_BASE; i < dwAddrCount; i++, dwId += 7)
231 {
232 msg.SetVariable(dwId++, pAddrList[i].dwType);
233 msg.SetVariable(dwId++, pAddrList[i].dwAddr1);
234 msg.SetVariable(dwId++, pAddrList[i].dwAddr2);
235 }
236 ((NXCL_Session *)hSession)->SendMsg(&msg);
237
238 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
239 }
240
241
242 //
243 // Reset server's component
244 //
245
246 DWORD LIBNXCL_EXPORTABLE NXCResetServerComponent(NXC_SESSION hSession, DWORD dwComponent)
247 {
248 DWORD dwRqId;
249 CSCPMessage msg;
250
251 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
252
253 msg.SetCode(CMD_RESET_COMPONENT);
254 msg.SetId(dwRqId);
255 msg.SetVariable(VID_COMPONENT_ID, dwComponent);
256 ((NXCL_Session *)hSession)->SendMsg(&msg);
257
258 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
259 }
260
261
262 //
263 // Get value of CLOB config variable
264 //
265
266 DWORD LIBNXCL_EXPORTABLE NXCGetServerConfigCLOB(NXC_SESSION hSession, const TCHAR *name, TCHAR **value)
267 {
268 CSCPMessage msg, *pResponse;
269 DWORD dwRqId, dwRetCode;
270
271 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
272 *value = NULL;
273
274 // Build request message
275 msg.SetCode(CMD_CONFIG_GET_CLOB);
276 msg.SetId(dwRqId);
277 msg.SetVariable(VID_NAME, name);
278
279 // Send request
280 ((NXCL_Session *)hSession)->SendMsg(&msg);
281
282 // Wait for response
283 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
284 if (pResponse != NULL)
285 {
286 dwRetCode = pResponse->GetVariableLong(VID_RCC);
287 if (dwRetCode == RCC_SUCCESS)
288 {
289 *value = pResponse->GetVariableStr(VID_VALUE);
290 }
291 delete pResponse;
292 }
293 else
294 {
295 dwRetCode = RCC_TIMEOUT;
296 }
297
298 return dwRetCode;
299 }
300
301
302 //
303 // Set value of CLOB config variable
304 //
305
306 DWORD LIBNXCL_EXPORTABLE NXCSetServerConfigCLOB(NXC_SESSION hSession, const TCHAR *name, const TCHAR *value)
307 {
308 CSCPMessage msg;
309 DWORD dwRqId;
310
311 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
312
313 // Build request message
314 msg.SetCode(CMD_CONFIG_SET_CLOB);
315 msg.SetId(dwRqId);
316 msg.SetVariable(VID_NAME, name);
317 msg.SetVariable(VID_VALUE, value);
318
319 // Send request
320 ((NXCL_Session *)hSession)->SendMsg(&msg);
321
322 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
323 }