9ef39dec9f1b80b9685eae5daba6386d9eb258fe
[public/netxms.git] / src / agent / subagents / ipso / ipso.cpp
1 /* $Id$ */
2
3 /*
4 ** NetXMS subagent for IPSO
5 ** Copyright (C) 2004 Alex Kirhenshtein
6 ** Copyright (C) 2006 Victor Kirhenshtein
7 **
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 **
22 **/
23
24 #include "ipso.h"
25
26
27 //
28 // Handler for System.IPSCTL(*)
29 //
30
31 static LONG H_IPSCTL(char *pszParam, char *pArg, char *pValue)
32 {
33 char szName[256];
34
35 if (!NxGetParameterArg(pszParam, 1, szName, 256))
36 return SYSINFO_RC_UNSUPPORTED;
37
38 return IPSCTLGetString(0, szName, pValue, MAX_RESULT_LENGTH);
39 }
40
41
42 //
43 // Subagent information
44 //
45
46 static NETXMS_SUBAGENT_PARAM m_parameters[] =
47 {
48 { "Disk.Avail(*)", H_DiskInfo, (char *)DISK_AVAIL,
49 DCI_DT_UINT64, DCIDESC_DISK_AVAIL },
50 { "Disk.AvailPerc(*)", H_DiskInfo, (char *)DISK_AVAIL_PERC,
51 DCI_DT_FLOAT, DCIDESC_DISK_AVAILPERC },
52 { "Disk.Free(*)", H_DiskInfo, (char *)DISK_FREE,
53 DCI_DT_UINT64, DCIDESC_DISK_FREE },
54 { "Disk.FreePerc(*)", H_DiskInfo, (char *)DISK_FREE_PERC,
55 DCI_DT_FLOAT, DCIDESC_DISK_FREEPERC },
56 { "Disk.Total(*)", H_DiskInfo, (char *)DISK_TOTAL,
57 DCI_DT_UINT64, DCIDESC_DISK_TOTAL },
58 { "Disk.Used(*)", H_DiskInfo, (char *)DISK_USED,
59 DCI_DT_UINT64, DCIDESC_DISK_USED },
60 { "Disk.UsedPerc(*)", H_DiskInfo, (char *)DISK_USED_PERC,
61 DCI_DT_FLOAT, DCIDESC_DISK_USEDPERC },
62
63 { "Net.IP.Forwarding", H_NetIpForwarding, (char *)4,
64 DCI_DT_INT, DCIDESC_NET_IP_FORWARDING },
65 { "Net.IP6.Forwarding", H_NetIpForwarding, (char *)6,
66 DCI_DT_INT, DCIDESC_NET_IP6_FORWARDING },
67 { "Net.Interface.AdminStatus(*)", H_NetIfStats, (char *)NET_IF_ADMIN_STATUS,
68 DCI_DT_INT, DCIDESC_NET_INTERFACE_ADMINSTATUS },
69 { "Net.Interface.BytesIn(*)", H_NetIfStats, (char *)NET_IF_BYTES_IN,
70 DCI_DT_UINT64, DCIDESC_NET_INTERFACE_BYTESIN },
71 { "Net.Interface.BytesOut(*)", H_NetIfStats, (char *)NET_IF_BYTES_OUT,
72 DCI_DT_UINT64, DCIDESC_NET_INTERFACE_BYTESOUT },
73 { "Net.Interface.Description(*)", H_NetIfStats, (char *)NET_IF_DESCRIPTION,
74 DCI_DT_STRING, DCIDESC_NET_INTERFACE_DESCRIPTION },
75 { "Net.Interface.Link(*)", H_NetIfStats, (char *)NET_IF_LINK,
76 DCI_DT_INT, DCIDESC_NET_INTERFACE_LINK },
77 { "Net.Interface.PacketsIn(*)", H_NetIfStats, (char *)NET_IF_PACKETS_IN,
78 DCI_DT_UINT, DCIDESC_NET_INTERFACE_PACKETSIN },
79 { "Net.Interface.PacketsOut(*)", H_NetIfStats, (char *)NET_IF_PACKETS_OUT,
80 DCI_DT_UINT, DCIDESC_NET_INTERFACE_PACKETSOUT },
81
82 { "Process.Count(*)", H_ProcessCount, (char *)0,
83 DCI_DT_UINT, DCIDESC_PROCESS_COUNT },
84 { "System.ProcessCount", H_ProcessCount, (char *)1,
85 DCI_DT_UINT, DCIDESC_SYSTEM_PROCESSCOUNT },
86
87 { "System.CPU.Count", H_CpuCount, NULL,
88 DCI_DT_INT, DCIDESC_SYSTEM_CPU_COUNT },
89 { "System.CPU.LoadAvg", H_CpuLoad, NULL,
90 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_LOADAVG },
91 { "System.CPU.LoadAvg5", H_CpuLoad, NULL,
92 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_LOADAVG5 },
93 { "System.CPU.LoadAvg15", H_CpuLoad, NULL,
94 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_LOADAVG15 },
95 /* { "System.CPU.Usage", H_CpuUsage, NULL,
96 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_USAGE },
97 { "System.CPU.Usage5", H_CpuUsage, NULL,
98 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_USAGE5 },
99 { "System.CPU.Usage15", H_CpuUsage, NULL,
100 DCI_DT_FLOAT, DCIDESC_SYSTEM_CPU_USAGE15 }, */
101 { "System.IPSCTL(*)", H_IPSCTL, NULL,
102 DCI_DT_STRING, "Value of given ipsctl parameter" },
103 { "System.Hostname", H_Hostname, NULL,
104 DCI_DT_STRING, DCIDESC_SYSTEM_HOSTNAME },
105 { "System.Memory.Physical.Free", H_MemoryInfo, (char *)PHYSICAL_FREE,
106 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_PHYSICAL_FREE },
107 { "System.Memory.Physical.Total", H_MemoryInfo, (char *)PHYSICAL_TOTAL,
108 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_PHYSICAL_TOTAL },
109 { "System.Memory.Physical.Used", H_MemoryInfo, (char *)PHYSICAL_USED,
110 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_PHYSICAL_USED },
111 { "System.Memory.Swap.Free", H_MemoryInfo, (char *)SWAP_FREE,
112 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_SWAP_FREE },
113 { "System.Memory.Swap.Total", H_MemoryInfo, (char *)SWAP_TOTAL,
114 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_SWAP_TOTAL },
115 { "System.Memory.Swap.Used", H_MemoryInfo, (char *)SWAP_USED,
116 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_SWAP_USED },
117 { "System.Memory.Virtual.Free", H_MemoryInfo, (char *)VIRTUAL_FREE,
118 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_VIRTUAL_FREE },
119 { "System.Memory.Virtual.Total", H_MemoryInfo, (char *)VIRTUAL_TOTAL,
120 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_VIRTUAL_TOTAL },
121 { "System.Memory.Virtual.Used", H_MemoryInfo, (char *)VIRTUAL_USED,
122 DCI_DT_UINT64, DCIDESC_SYSTEM_MEMORY_VIRTUAL_USED },
123 { "System.Uname", H_Uname, NULL,
124 DCI_DT_STRING, DCIDESC_SYSTEM_UNAME },
125 { "System.Uptime", H_Uptime, NULL,
126 DCI_DT_UINT, DCIDESC_SYSTEM_UPTIME },
127 };
128
129 static NETXMS_SUBAGENT_ENUM m_enums[] =
130 {
131 { "Net.ArpCache", H_NetArpCache, NULL },
132 { "Net.InterfaceList", H_NetIfList, NULL },
133 { "Net.IP.RoutingTable", H_NetRoutingTable, NULL },
134 { "System.ProcessList", H_ProcessList, NULL },
135 };
136
137 static NETXMS_SUBAGENT_INFO m_info =
138 {
139 NETXMS_SUBAGENT_INFO_MAGIC,
140 "IPSO",
141 NETXMS_VERSION_STRING,
142 NULL,
143 NULL,
144 NULL,
145 sizeof(m_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),
146 m_parameters,
147 sizeof(m_enums) / sizeof(NETXMS_SUBAGENT_ENUM),
148 m_enums,
149 0,
150 NULL
151 };
152
153 //
154 // Entry point for NetXMS agent
155 //
156
157 DECLARE_SUBAGENT_ENTRY_POINT(IPSO)
158 {
159 *ppInfo = &m_info;
160 return TRUE;
161 }
162
163
164 //
165 // Wrappers for ipsctl_get() function
166 //
167
168 LONG IPSCTLGetInt(int nCallerHandle, char *pszName, LONG *pnValue)
169 {
170 int nHandle, nErr;
171 struct ipsctl_value *pValue;
172 LONG nRet = SYSINFO_RC_ERROR;
173
174 nHandle = (nCallerHandle > 0) ? nCallerHandle : ipsctl_open();
175 if (nHandle > 0)
176 {
177 nErr = ipsctl_get(nHandle, pszName, &pValue);
178 if (nErr == 0)
179 {
180 if (pValue->wType == 3)
181 {
182 if (pValue->wSize == 8)
183 *pnValue = (LONG)pValue->data.nInt64;
184 else
185 *pnValue = pValue->data.nInt;
186 nRet = SYSINFO_RC_SUCCESS;
187 }
188 }
189 else
190 {
191 if (nErr == 2) // No such parameter
192 nRet = SYSINFO_RC_UNSUPPORTED;
193 }
194 if (nHandle != nCallerHandle)
195 ipsctl_close(nHandle);
196 }
197 return nRet;
198 }
199
200 LONG IPSCTLGetString(int nCallerHandle, char *pszName,
201 char *pszValue, int nSize)
202 {
203 int nHandle, nErr;
204 struct ipsctl_value *pData;
205 LONG nRet = SYSINFO_RC_ERROR;
206
207 nHandle = (nCallerHandle > 0) ? nCallerHandle : ipsctl_open();
208 if (nHandle > 0)
209 {
210 nErr = ipsctl_get(nHandle, pszName, &pData);
211 if (nErr == 0)
212 {
213 switch(pData->wType)
214 {
215 case 3: // Integer (unsigned?)
216 if (pData->wSize == 8)
217 ret_uint64(pszValue, pData->data.qwUInt64);
218 else
219 ret_uint(pszValue, pData->data.dwUInt);
220 break;
221 case 4: // String
222 nx_strncpy(pszValue, pData->data.szStr, nSize);
223 break;
224 case 5: // IP address
225 IpToStr(ntohl(pData->data.ipAddr.dwAddr), pszValue);
226 if (pData->data.ipAddr.nMaskBits != 32)
227 {
228 sprintf(&pszValue[strlen(pszValue)], "/%d",
229 pData->data.ipAddr.nMaskBits);
230 }
231 break;
232 case 7: // MAC address ?
233 MACToStr((BYTE *)pData->data.szStr, pszValue);
234 break;
235 case 13: // Kbits ?
236 if (pData->wSize == 8)
237 ret_uint64(pszValue, pData->data.qwUInt64 * 1000);
238 else
239 ret_uint(pszValue, pData->data.dwUInt * 1000);
240 break;
241 default: // Unknown data type
242 nx_strncpy(pszValue, "<unknown type>", nSize);
243 break;
244 }
245 nRet = SYSINFO_RC_SUCCESS;
246 }
247 else
248 {
249 if (nErr == 2) // No such parameter
250 nRet = SYSINFO_RC_UNSUPPORTED;
251 }
252 if (nHandle != nCallerHandle)
253 ipsctl_close(nHandle);
254 }
255 return nRet;
256 }
257
258
259 ///////////////////////////////////////////////////////////////////////////////
260 /*
261
262 $Log: not supported by cvs2svn $
263 Revision 1.9 2007/06/07 22:07:11 alk
264 descriptions changed to defines
265
266 Revision 1.8 2006/08/25 22:24:02 victor
267 Implemented Net.InterfaceList and Net.ArpCache
268
269 Revision 1.7 2006/08/18 08:37:46 victor
270 Changed handling of ipsctl data types
271
272 Revision 1.6 2006/08/17 19:22:57 victor
273 Minor changes
274
275 Revision 1.5 2006/08/17 07:38:52 victor
276 Improved handling of data returned by ipsctl_get()
277
278 Revision 1.4 2006/08/16 22:26:09 victor
279 - Most of Net.Interface.XXX functions implemented on IPSO
280 - Added function MACToStr
281
282 Revision 1.3 2006/07/24 06:49:47 victor
283 - Process and physical memory parameters are working
284 - Various other changes
285
286 Revision 1.2 2006/07/21 16:22:44 victor
287 Some parameters are working
288
289 Revision 1.1 2006/07/21 11:48:35 victor
290 Initial commit
291
292 Revision 1.7 2005/09/15 21:47:02 victor
293 Added macro DECLARE_SUBAGENT_INIT to simplify initialization function declaration
294
295 Revision 1.6 2005/08/22 23:00:05 alk
296 Net.IP.RoutingTable added
297
298 Revision 1.5 2005/03/10 19:04:07 alk
299 implemented:
300 Net.Interface.AdminStatus(*)
301 Net.Interface.Link(*)
302
303 Revision 1.4 2005/01/24 19:51:16 alk
304 reurn types/comments added
305 Process.Count(*)/System.ProcessCount fixed
306
307 Revision 1.3 2005/01/23 05:08:06 alk
308 + System.CPU.Count
309 + System.Memory.Physical.*
310 + System.ProcessCount
311 + System.ProcessList
312
313 Revision 1.2 2005/01/17 23:25:47 alk
314 Agent.SourcePackageSupport added
315
316 Revision 1.1 2005/01/17 17:14:32 alk
317 freebsd agent, incomplete (but working)
318
319
320 */