- Added parameter System.ServiceState(*)
[public/netxms.git] / src / agent / core / getparam.cpp
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
30 LONG H_AgentStats(char *cmd, char *arg, char *value);
31 LONG H_AgentUptime(char *cmd, char *arg, char *value);
32 LONG H_CRC32(char *cmd, char *arg, char *value);
33 LONG H_FileSize(char *cmd, char *arg, char *value);
34 LONG H_MD5Hash(char *cmd, char *arg, char *value);
35 LONG H_SHA1Hash(char *cmd, char *arg, char *value);
36 LONG H_SubAgentList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
37 LONG H_ActionList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
38
39 #ifdef _WIN32
40 LONG H_ArpCache(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
41 LONG H_InterfaceList(char *cmd, char *arg, NETXMS_VALUES_LIST *value);
42 LONG H_ProcCount(char *cmd, char *arg, char *value);
43 LONG H_ProcCountSpecific(char *cmd, char *arg, char *value);
44 LONG H_ProcInfo(char *cmd, char *arg, char *value);
45 LONG H_DiskInfo(char *cmd, char *arg, char *value);
46 LONG H_MemoryInfo(char *cmd, char *arg, char *value);
47 LONG H_HostName(char *cmd, char *arg, char *value);
48 LONG H_SystemUname(char *cmd, char *arg, char *value);
49 LONG H_ThreadCount(char *cmd, char *arg, char *value);
50 LONG H_NetIPStats(char *cmd, char *arg, char *value);
51 LONG H_NetInterfaceStats(char *cmd, char *arg, char *value);
52 LONG H_ServiceState(char *cmd, char *arg, char *value);
53 #endif
54
55
56 //
57 // Static data
58 //
59
60 static AGENT_PARAM *m_pParamList = NULL;
61 static int m_iNumParams = 0;
62 static NETXMS_SUBAGENT_ENUM *m_pEnumList = NULL;
63 static int m_iNumEnums = 0;
64 static DWORD m_dwTimedOutRequests = 0;
65 static DWORD m_dwAuthenticationFailures = 0;
66 static DWORD m_dwProcessedRequests = 0;
67 static DWORD m_dwFailedRequests = 0;
68 static DWORD m_dwUnsupportedRequests = 0;
69
70
71 //
72 // Handler for parameters which always returns string constant
73 //
74
75 static 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
86 static 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
97 static LONG H_UIntPtr(char *cmd, char *arg, char *value)
98 {
99 ret_uint(value, *((DWORD *)arg));
100 return SYSINFO_RC_SUCCESS;
101 }
102
103
104 //
105 // Handler for parameters list
106 //
107
108 static 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
122 static 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
132 //
133 // Standard agent's parameters
134 //
135
136 static AGENT_PARAM m_stdParams[] =
137 {
138 #ifdef _WIN32
139 { "Disk.Free(*)", H_DiskInfo, NULL },
140 { "Disk.Total(*)", H_DiskInfo, NULL },
141 { "Disk.Used(*)", H_DiskInfo, NULL },
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 },
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 },
178 { "System.ServiceState(*)", H_ServiceState, NULL },
179 { "System.ThreadCount", H_ThreadCount, NULL },
180 { "System.Uname", H_SystemUname, NULL },
181 #endif
182 { "Agent.AcceptedConnections", H_UIntPtr, (char *)&g_dwAcceptedConnections },
183 { "Agent.AcceptErrors", H_UIntPtr, (char *)&g_dwAcceptErrors },
184 { "Agent.AuthenticationFailures", H_UIntPtr, (char *)&m_dwAuthenticationFailures },
185 { "Agent.FailedRequests", H_UIntPtr, (char *)&m_dwFailedRequests },
186 { "Agent.ProcessedRequests", H_UIntPtr, (char *)&m_dwProcessedRequests },
187 { "Agent.RejectedConnections", H_UIntPtr, (char *)&g_dwRejectedConnections },
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
199 //
200 // Standard agent's enumerations
201 //
202
203 static NETXMS_SUBAGENT_ENUM m_stdEnums[] =
204 {
205 { "Agent.ActionList", H_ActionList, NULL },
206 #ifdef _WIN32
207 { "Net.ArpCache", H_ArpCache, NULL },
208 { "Net.InterfaceList", H_InterfaceList, NULL },
209 #endif
210 { "Agent.SubAgentList", H_SubAgentList, NULL },
211 { "Agent.SupportedEnums", H_EnumList, NULL },
212 { "Agent.SupportedParameters", H_ParamList, NULL }
213 };
214
215
216 //
217 // Initialize dynamic parameters list from default static list
218 //
219
220 BOOL InitParameterList(void)
221 {
222 if ((m_pParamList != NULL) || (m_pEnumList != NULL))
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;
229 memcpy(m_pParamList, m_stdParams, sizeof(AGENT_PARAM) * m_iNumParams);
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
237 return TRUE;
238 }
239
240
241 //
242 // Add parameter to list
243 //
244
245 void 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
255 //
256 // Add enum to list
257 //
258
259 void 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
269 //
270 // Get parameter's value
271 //
272
273 DWORD GetParameterValue(char *pszParam, char *pszValue)
274 {
275 int i, rc;
276 DWORD dwErrorCode;
277
278 for(i = 0; i < m_iNumParams; i++)
279 if (MatchString(m_pParamList[i].name, pszParam, FALSE))
280 {
281 rc = m_pParamList[i].handler(pszParam, m_pParamList[i].arg, pszValue);
282 switch(rc)
283 {
284 case SYSINFO_RC_SUCCESS:
285 dwErrorCode = ERR_SUCCESS;
286 m_dwProcessedRequests++;
287 break;
288 case SYSINFO_RC_ERROR:
289 dwErrorCode = ERR_INTERNAL_ERROR;
290 m_dwFailedRequests++;
291 break;
292 case SYSINFO_RC_UNSUPPORTED:
293 dwErrorCode = ERR_UNKNOWN_PARAMETER;
294 m_dwUnsupportedRequests++;
295 break;
296 default:
297 WriteLog(MSG_UNEXPECTED_IRC, EVENTLOG_ERROR_TYPE, "ds", rc, pszParam);
298 dwErrorCode = ERR_INTERNAL_ERROR;
299 m_dwFailedRequests++;
300 break;
301 }
302 break;
303 }
304 if (i == m_iNumParams)
305 {
306 dwErrorCode = ERR_UNKNOWN_PARAMETER;
307 m_dwUnsupportedRequests++;
308 }
309 return dwErrorCode;
310 }
311
312
313 //
314 // Get parameter's value
315 //
316
317 DWORD 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 }