- Added parameter System.ServiceState(*)
[public/netxms.git] / src / agent / core / getparam.cpp
CommitLineData
e7474978
VK
1/*
2** NetXMS multiplatform core agent
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: getparam.cpp
20**
21**/
22
23#include "nxagentd.h"
24
25
26//
27// Externals
28//
29
30LONG H_AgentStats(char *cmd, char *arg, char *value);
31LONG H_AgentUptime(char *cmd, char *arg, char *value);
32LONG H_CRC32(char *cmd, char *arg, char *value);
33LONG H_FileSize(char *cmd, char *arg, char *value);
34LONG H_MD5Hash(char *cmd, char *arg, char *value);
35LONG H_SHA1Hash(char *cmd, char *arg, char *value);
901c96c7 36LONG H_SubAgentList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
3c774461 37LONG H_ActionList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
dc0e93b4 38
901c96c7
VK
39#ifdef _WIN32
40LONG H_ArpCache(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
41LONG H_InterfaceList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
dc0e93b4
VK
42LONG H_ProcCount(char *cmd, char *arg, char *value);
43LONG H_ProcCountSpecific(char *cmd, char *arg, char *value);
44LONG H_ProcInfo(char *cmd, char *arg, char *value);
45LONG H_DiskInfo(char *cmd, char *arg, char *value);
46LONG H_MemoryInfo(char *cmd, char *arg, char *value);
47LONG H_HostName(char *cmd, char *arg, char *value);
48LONG H_SystemUname(char *cmd, char *arg, char *value);
49LONG H_ThreadCount(char *cmd, char *arg, char *value);
6849d9be
VK
50LONG H_NetIPStats(char *cmd, char *arg, char *value);
51LONG H_NetInterfaceStats(char *cmd, char *arg, char *value);
38f733bb 52LONG H_ServiceState(char *cmd, char *arg, char *value);
901c96c7
VK
53#endif
54
e7474978
VK
55
56//
57// Static data
58//
59
60static AGENT_PARAM *m_pParamList = NULL;
61static int m_iNumParams = 0;
901c96c7
VK
62static NETXMS_SUBAGENT_ENUM *m_pEnumList = NULL;
63static int m_iNumEnums = 0;
e7474978 64static DWORD m_dwTimedOutRequests = 0;
e7474978
VK
65static DWORD m_dwAuthenticationFailures = 0;
66static DWORD m_dwProcessedRequests = 0;
67static DWORD m_dwFailedRequests = 0;
68static DWORD m_dwUnsupportedRequests = 0;
69
70
71//
72// Handler for parameters which always returns string constant
73//
74
75static LONG H_StringConstant(char *cmd, char *arg, char *value)
76{
77 ret_string(value, arg);
78 return SYSINFO_RC_SUCCESS;
79}
80
81
82//
83// Handler for parameters which returns floating point value from specific variable
84//
85
86static LONG H_FloatPtr(char *cmd, char *arg, char *value)
87{
88 ret_double(value, *((double *)arg));
89 return SYSINFO_RC_SUCCESS;
90}
91
92
93//
94// Handler for parameters which returns DWORD value from specific variable
95//
96
97static LONG H_UIntPtr(char *cmd, char *arg, char *value)
98{
99 ret_uint(value, *((DWORD *)arg));
100 return SYSINFO_RC_SUCCESS;
101}
102
103
901c96c7
VK
104//
105// Handler for parameters list
106//
107
108static LONG H_ParamList(char *cmd, char *arg, NETXMS_VALUES_LIST *value)
109{
110 int i;
111
112 for(i = 0; i < m_iNumParams; i++)
113 NxAddResultString(value, m_pParamList[i].name);
114 return SYSINFO_RC_SUCCESS;
115}
116
117
118//
119// Handler for enums list
120//
121
122static LONG H_EnumList(char *cmd, char *arg, NETXMS_VALUES_LIST *value)
123{
124 int i;
125
126 for(i = 0; i < m_iNumEnums; i++)
127 NxAddResultString(value, m_pEnumList[i].szName);
128 return SYSINFO_RC_SUCCESS;
129}
130
131
e7474978
VK
132//
133// Standard agent's parameters
134//
135
136static AGENT_PARAM m_stdParams[] =
137{
dc0e93b4
VK
138#ifdef _WIN32
139 { "Disk.Free(*)", H_DiskInfo, NULL },
140 { "Disk.Total(*)", H_DiskInfo, NULL },
141 { "Disk.Used(*)", H_DiskInfo, NULL },
6849d9be
VK
142 { "Net.Interface.AdminStatus(*)", H_NetInterfaceStats, (char *)NET_IF_ADMIN_STATUS },
143 { "Net.Interface.BytesIn(*)", H_NetInterfaceStats, (char *)NET_IF_BYTES_IN },
144 { "Net.Interface.BytesOut(*)", H_NetInterfaceStats, (char *)NET_IF_BYTES_OUT },
145 { "Net.Interface.Description(*)", H_NetInterfaceStats, (char *)NET_IF_DESCR },
146 { "Net.Interface.InErrors(*)", H_NetInterfaceStats, (char *)NET_IF_IN_ERRORS },
147 { "Net.Interface.Link(*)", H_NetInterfaceStats, (char *)NET_IF_LINK },
148 { "Net.Interface.OutErrors(*)", H_NetInterfaceStats, (char *)NET_IF_OUT_ERRORS },
149 { "Net.Interface.PacketsIn(*)", H_NetInterfaceStats, (char *)NET_IF_PACKETS_IN },
150 { "Net.Interface.PacketsOut(*)", H_NetInterfaceStats, (char *)NET_IF_PACKETS_OUT },
151 { "Net.Interface.Speed(*)", H_NetInterfaceStats, (char *)NET_IF_SPEED },
152 { "Net.IP.Forwarding", H_NetIPStats, (char *)NET_IP_FORWARDING },
dc0e93b4
VK
153 { "Process.Count(*)", H_ProcCountSpecific, NULL },
154 { "Process.GdiObj(*)", H_ProcInfo, (char *)PROCINFO_GDI_OBJ },
155 { "Process.IO.OtherB(*)", H_ProcInfo, (char *)PROCINFO_IO_OTHER_B },
156 { "Process.IO.OtherOp(*)", H_ProcInfo, (char *)PROCINFO_IO_OTHER_OP },
157 { "Process.IO.ReadB(*)", H_ProcInfo, (char *)PROCINFO_IO_READ_B },
158 { "Process.IO.ReadOp(*)", H_ProcInfo, (char *)PROCINFO_IO_READ_OP },
159 { "Process.IO.WriteB(*)", H_ProcInfo, (char *)PROCINFO_IO_WRITE_B },
160 { "Process.IO.WriteOp(*)", H_ProcInfo, (char *)PROCINFO_IO_WRITE_OP },
161 { "Process.KernelTime(*)", H_ProcInfo, (char *)PROCINFO_KTIME },
162 { "Process.PageFaults(*)", H_ProcInfo, (char *)PROCINFO_PF },
163 { "Process.UserObj(*)", H_ProcInfo, (char *)PROCINFO_USER_OBJ },
164 { "Process.UserTime(*)", H_ProcInfo, (char *)PROCINFO_UTIME },
165 { "Process.VMSize(*)", H_ProcInfo, (char *)PROCINFO_VMSIZE },
166 { "Process.WkSet(*)", H_ProcInfo, (char *)PROCINFO_WKSET },
167 { "System.Hostname", H_HostName, NULL },
168 { "System.Memory.Physical.Free", H_MemoryInfo, (char *)MEMINFO_PHYSICAL_FREE },
169 { "System.Memory.Physical.Total", H_MemoryInfo, (char *)MEMINFO_PHYSICAL_TOTAL },
170 { "System.Memory.Physical.Used", H_MemoryInfo, (char *)MEMINFO_PHYSICAL_USED },
171 { "System.Memory.Swap.Free", H_MemoryInfo, (char *)MEMINFO_SWAP_FREE },
172 { "System.Memory.Swap.Total", H_MemoryInfo, (char *)MEMINFO_SWAP_TOTAL },
173 { "System.Memory.Swap.Used", H_MemoryInfo, (char *)MEMINFO_SWAP_USED },
174 { "System.Memory.Virtual.Free", H_MemoryInfo, (char *)MEMINFO_VIRTUAL_FREE },
175 { "System.Memory.Virtual.Total", H_MemoryInfo, (char *)MEMINFO_VIRTUAL_TOTAL },
176 { "System.Memory.Virtual.Used", H_MemoryInfo, (char *)MEMINFO_VIRTUAL_USED },
177 { "System.ProcessCount", H_ProcCount, NULL },
38f733bb 178 { "System.ServiceState(*)", H_ServiceState, NULL },
dc0e93b4
VK
179 { "System.ThreadCount", H_ThreadCount, NULL },
180 { "System.Uname", H_SystemUname, NULL },
181#endif
e9580fef
VK
182 { "Agent.AcceptedConnections", H_UIntPtr, (char *)&g_dwAcceptedConnections },
183 { "Agent.AcceptErrors", H_UIntPtr, (char *)&g_dwAcceptErrors },
e7474978
VK
184 { "Agent.AuthenticationFailures", H_UIntPtr, (char *)&m_dwAuthenticationFailures },
185 { "Agent.FailedRequests", H_UIntPtr, (char *)&m_dwFailedRequests },
186 { "Agent.ProcessedRequests", H_UIntPtr, (char *)&m_dwProcessedRequests },
e9580fef 187 { "Agent.RejectedConnections", H_UIntPtr, (char *)&g_dwRejectedConnections },
e7474978
VK
188 { "Agent.TimedOutRequests", H_UIntPtr, (char *)&m_dwTimedOutRequests },
189 { "Agent.UnsupportedRequests", H_UIntPtr, (char *)&m_dwUnsupportedRequests },
190 { "Agent.Uptime", H_AgentUptime, NULL },
191 { "Agent.Version", H_StringConstant, AGENT_VERSION_STRING },
192 { "File.Hash.CRC32(*)", H_CRC32, NULL },
193 { "File.Hash.MD5(*)", H_MD5Hash, NULL },
194 { "File.Hash.SHA1(*)", H_SHA1Hash, NULL },
195 { "File.Size(*)", H_FileSize, NULL }
196};
197
198
901c96c7
VK
199//
200// Standard agent's enumerations
201//
202
203static NETXMS_SUBAGENT_ENUM m_stdEnums[] =
204{
6849d9be 205 { "Agent.ActionList", H_ActionList, NULL },
901c96c7 206#ifdef _WIN32
6849d9be
VK
207 { "Net.ArpCache", H_ArpCache, NULL },
208 { "Net.InterfaceList", H_InterfaceList, NULL },
901c96c7 209#endif
6849d9be
VK
210 { "Agent.SubAgentList", H_SubAgentList, NULL },
211 { "Agent.SupportedEnums", H_EnumList, NULL },
212 { "Agent.SupportedParameters", H_ParamList, NULL }
901c96c7
VK
213};
214
215
e7474978
VK
216//
217// Initialize dynamic parameters list from default static list
218//
219
220BOOL InitParameterList(void)
221{
901c96c7 222 if ((m_pParamList != NULL) || (m_pEnumList != NULL))
e7474978
VK
223 return FALSE;
224
225 m_iNumParams = sizeof(m_stdParams) / sizeof(AGENT_PARAM);
226 m_pParamList = (AGENT_PARAM *)malloc(sizeof(AGENT_PARAM) * m_iNumParams);
227 if (m_pParamList == NULL)
228 return FALSE;
e7474978 229 memcpy(m_pParamList, m_stdParams, sizeof(AGENT_PARAM) * m_iNumParams);
901c96c7
VK
230
231 m_iNumEnums = sizeof(m_stdEnums) / sizeof(NETXMS_SUBAGENT_ENUM);
232 m_pEnumList = (NETXMS_SUBAGENT_ENUM *)malloc(sizeof(NETXMS_SUBAGENT_ENUM) * m_iNumEnums);
233 if (m_pEnumList == NULL)
234 return FALSE;
235 memcpy(m_pEnumList, m_stdEnums, sizeof(NETXMS_SUBAGENT_ENUM) * m_iNumEnums);
236
e7474978
VK
237 return TRUE;
238}
239
240
241//
242// Add parameter to list
243//
244
245void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg)
246{
247 m_pParamList = (AGENT_PARAM *)realloc(m_pParamList, sizeof(AGENT_PARAM) * (m_iNumParams + 1));
248 strncpy(m_pParamList[m_iNumParams].name, szName, MAX_PARAM_NAME - 1);
249 m_pParamList[m_iNumParams].handler = fpHandler;
250 m_pParamList[m_iNumParams].arg = pArg;
251 m_iNumParams++;
252}
253
254
962b3915
VK
255//
256// Add enum to list
257//
258
259void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg)
260{
261 m_pEnumList = (NETXMS_SUBAGENT_ENUM *)realloc(m_pEnumList, sizeof(NETXMS_SUBAGENT_ENUM) * (m_iNumEnums + 1));
262 strncpy(m_pEnumList[m_iNumEnums].szName, szName, MAX_PARAM_NAME - 1);
263 m_pEnumList[m_iNumEnums].fpHandler = fpHandler;
264 m_pEnumList[m_iNumEnums].pArg = pArg;
265 m_iNumEnums++;
266}
267
268
e7474978
VK
269//
270// Get parameter's value
271//
272
7c205c0c 273DWORD GetParameterValue(char *pszParam, char *pszValue)
e7474978
VK
274{
275 int i, rc;
7c205c0c 276 DWORD dwErrorCode;
e7474978
VK
277
278 for(i = 0; i < m_iNumParams; i++)
7c205c0c 279 if (MatchString(m_pParamList[i].name, pszParam, FALSE))
e7474978 280 {
7c205c0c 281 rc = m_pParamList[i].handler(pszParam, m_pParamList[i].arg, pszValue);
e7474978
VK
282 switch(rc)
283 {
284 case SYSINFO_RC_SUCCESS:
7c205c0c 285 dwErrorCode = ERR_SUCCESS;
e7474978
VK
286 m_dwProcessedRequests++;
287 break;
288 case SYSINFO_RC_ERROR:
7c205c0c 289 dwErrorCode = ERR_INTERNAL_ERROR;
e7474978
VK
290 m_dwFailedRequests++;
291 break;
292 case SYSINFO_RC_UNSUPPORTED:
7c205c0c 293 dwErrorCode = ERR_UNKNOWN_PARAMETER;
e7474978
VK
294 m_dwUnsupportedRequests++;
295 break;
296 default:
7c205c0c
VK
297 WriteLog(MSG_UNEXPECTED_IRC, EVENTLOG_ERROR_TYPE, "ds", rc, pszParam);
298 dwErrorCode = ERR_INTERNAL_ERROR;
e7474978
VK
299 m_dwFailedRequests++;
300 break;
301 }
302 break;
303 }
304 if (i == m_iNumParams)
305 {
7c205c0c 306 dwErrorCode = ERR_UNKNOWN_PARAMETER;
e7474978
VK
307 m_dwUnsupportedRequests++;
308 }
7c205c0c 309 return dwErrorCode;
e7474978 310}
901c96c7
VK
311
312
313//
314// Get parameter's value
315//
316
317DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue)
318{
319 int i, rc;
320 DWORD dwErrorCode;
321
322 for(i = 0; i < m_iNumEnums; i++)
323 if (MatchString(m_pEnumList[i].szName, pszParam, FALSE))
324 {
325 rc = m_pEnumList[i].fpHandler(pszParam, m_pEnumList[i].pArg, pValue);
326 switch(rc)
327 {
328 case SYSINFO_RC_SUCCESS:
329 dwErrorCode = ERR_SUCCESS;
330 m_dwProcessedRequests++;
331 break;
332 case SYSINFO_RC_ERROR:
333 dwErrorCode = ERR_INTERNAL_ERROR;
334 m_dwFailedRequests++;
335 break;
336 case SYSINFO_RC_UNSUPPORTED:
337 dwErrorCode = ERR_UNKNOWN_PARAMETER;
338 m_dwUnsupportedRequests++;
339 break;
340 default:
341 WriteLog(MSG_UNEXPECTED_IRC, EVENTLOG_ERROR_TYPE, "ds", rc, pszParam);
342 dwErrorCode = ERR_INTERNAL_ERROR;
343 m_dwFailedRequests++;
344 break;
345 }
346 break;
347 }
348 if (i == m_iNumEnums)
349 {
350 dwErrorCode = ERR_UNKNOWN_PARAMETER;
351 m_dwUnsupportedRequests++;
352 }
353 return dwErrorCode;
354}