fixed problems with IPSO build
[public/netxms.git] / src / agent / subagents / ipso / system.cpp
1 /* $Id$ */
2
3 /*
4 ** NetXMS subagent for FreeBSD
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 #include <sys/sysctl.h>
26 #include <sys/utsname.h>
27 #include <vm/vm_param.h>
28 #include <sys/vmmeter.h>
29 #include <kvm.h>
30
31
32 //
33 // Defines for getting data from process information structure
34 //
35
36 #define KP_PID(x) (*((pid_t *)(kp + 48)))
37 #define KP_PNAME(x) (x + 0xCB)
38
39
40 //
41 // Handler for System.Uptime
42 //
43
44 LONG H_Uptime(char *pszParam, char *pArg, char *pValue)
45 {
46 static int mib[2] = { CTL_KERN, KERN_BOOTTIME };
47 time_t nNow;
48 size_t nSize;
49 struct timeval bootTime;
50 time_t nUptime = 0;
51
52 nSize = sizeof(bootTime);
53
54 time(&nNow);
55 if (sysctl(mib, 2, &bootTime, &nSize, NULL, 0) != (-1))
56 {
57 nUptime = (time_t)(nNow - bootTime.tv_sec);
58 }
59 else
60 {
61 perror("uptime()");
62 }
63
64 if (nUptime > 0)
65 {
66 ret_uint(pValue, nUptime);
67 }
68
69 return nUptime > 0 ? SYSINFO_RC_SUCCESS : SYSINFO_RC_ERROR;
70 }
71
72
73 //
74 // Handler for System.Uname
75 //
76
77 LONG H_Uname(char *pszParam, char *pArg, char *pValue)
78 {
79 struct utsname utsName;
80 int nRet = SYSINFO_RC_ERROR;
81
82 if (uname(&utsName) == 0)
83 {
84 char szBuff[1024];
85
86 snprintf(szBuff, sizeof(szBuff), "%s %s %s %s %s", utsName.sysname,
87 utsName.nodename, utsName.release, utsName.version,
88 utsName.machine);
89 // TODO: processor & platform
90
91 ret_string(pValue, szBuff);
92
93 nRet = SYSINFO_RC_SUCCESS;
94 }
95
96 return nRet;
97 }
98
99 LONG H_Hostname(char *pszParam, char *pArg, char *pValue)
100 {
101 int nRet = SYSINFO_RC_ERROR;
102 char szBuff[128];
103
104 if (gethostname(szBuff, sizeof(szBuff)) == 0)
105 {
106 ret_string(pValue, szBuff);
107 nRet = SYSINFO_RC_SUCCESS;
108 }
109
110 return nRet;
111 }
112
113 LONG H_CpuLoad(char *pszParam, char *pArg, char *pValue)
114 {
115 int nRet = SYSINFO_RC_ERROR;
116 char szArg[128] = {0};
117 FILE *hFile;
118 double dLoad[3];
119
120 // get processor
121 //AgentGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
122
123 if (getloadavg(dLoad, 3) == 3)
124 {
125 switch (pszParam[19])
126 {
127 case '1': // 15 min
128 ret_double(pValue, dLoad[2]);
129 break;
130 case '5': // 5 min
131 ret_double(pValue, dLoad[1]);
132 break;
133 default: // 1 min
134 ret_double(pValue, dLoad[0]);
135 break;
136 }
137 nRet = SYSINFO_RC_SUCCESS;
138 }
139
140 return nRet;
141 }
142
143 LONG H_CpuCount(char *pszParam, char *pArg, char *pValue)
144 {
145 int nRet = SYSINFO_RC_ERROR;
146 static int mib[2] = { CTL_HW, HW_NCPU };
147 size_t nSize = sizeof(mib), nValSize;
148 int nVal;
149
150 nValSize = sizeof(nVal);
151 if (sysctl(mib, nSize, &nVal, &nValSize, NULL, 0) == 0)
152 {
153 ret_int(pValue, nVal);
154 nRet = SYSINFO_RC_SUCCESS;
155 }
156
157 return nRet;
158 }
159
160
161 //
162 // Handler for System.ProcessCount and Process.Count(*) parameters
163 //
164
165 LONG H_ProcessCount(char *pszParam, char *pArg, char *pValue)
166 {
167 char *kp, szArg[128] = "";
168 int i, nCount, nResult = -1;
169 kvm_t *kd;
170 LONG nRet = SYSINFO_RC_ERROR;
171
172 AgentGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
173
174 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
175 if (kd != NULL)
176 {
177 kp = (char *)kvm_getprocs(kd, KERN_PROC_ALL, 0, &nCount);
178
179 if (kp != NULL)
180 {
181 if (szArg[0] != 0)
182 {
183 nResult = 0;
184 for(i = 0; i < nCount; i++, kp += 648)
185 {
186 if (!stricmp(KP_PNAME(kp), szArg))
187 {
188 nResult++;
189 }
190 }
191 }
192 else
193 {
194 nResult = nCount;
195 }
196 }
197 kvm_close(kd);
198 }
199
200 if (nResult >= 0)
201 {
202 ret_int(pValue, nResult);
203 nRet = SYSINFO_RC_SUCCESS;
204 }
205 return nRet;
206 }
207
208
209 //
210 // Handler for System.Memory.* parameters
211 //
212
213 LONG H_MemoryInfo(char *pszParam, char *pArg, char *pValue)
214 {
215 struct vmtotal vmStat;
216 DWORD dwPageSize, dwPhysMem;
217 size_t nSize;
218 int mib[2] = { CTL_HW, HW_PAGESIZE };
219 int nRet = SYSINFO_RC_SUCCESS;
220
221 nSize = sizeof(int);
222 if (sysctl(mib, 2, &dwPageSize, &nSize, NULL, 0) == -1)
223 {
224 return SYSINFO_RC_ERROR;
225 }
226
227 nSize = sizeof(int);
228 mib[1] = HW_PHYSMEM;
229 if (sysctl(mib, 2, &dwPhysMem, &nSize, NULL, 0) == -1)
230 {
231 return SYSINFO_RC_ERROR;
232 }
233
234 nSize = sizeof(struct vmtotal);
235 mib[0] = CTL_VM;
236 mib[1] = VM_METER;
237 if (sysctl(mib, 2, &vmStat, &nSize, NULL, 0) != -1)
238 {
239 /*
240 #define XX(a) a,a*dwPageSize,(a*dwPageSize)/1024
241 printf("t_avm = %d %d %dK\n",XX(vmStat.t_avm));
242 printf("t_rm = %d %d %dK\n",XX(vmStat.t_rm));
243 printf("t_arm = %d %d %dK\n",XX(vmStat.t_arm));
244 printf("t_vmshr = %d %d %dK\n",XX(vmStat.t_vmshr));
245 printf("t_avmshr = %d %d %dK\n",XX(vmStat.t_avmshr));
246 printf("t_rmshr = %d %d %dK\n",XX(vmStat.t_rmshr));
247 printf("t_armshr = %d %d %dK\n",XX(vmStat.t_armshr));
248 printf("t_free = %d %d %dK\n",XX(vmStat.t_free));
249 printf("PageSize = %d\n",dwPageSize);
250 */
251 switch((int)pArg)
252 {
253 case PHYSICAL_FREE:
254 ret_uint64(pValue, (QWORD)vmStat.t_free * dwPageSize);
255 break;
256 case PHYSICAL_TOTAL:
257 ret_uint64(pValue, (QWORD)dwPhysMem);
258 break;
259 case PHYSICAL_USED:
260 ret_uint64(pValue, (QWORD)dwPhysMem - (QWORD)vmStat.t_arm * dwPageSize);
261 break;
262 default:
263 nRet = SYSINFO_RC_UNSUPPORTED;
264 break;
265 }
266 }
267 else
268 {
269 nRet = SYSINFO_RC_ERROR;
270 }
271 return nRet;
272 }
273
274
275 //
276 // Handler for System.ProcessList enum
277 //
278
279 LONG H_ProcessList(char *pszParam, char *pArg, StringList *pValue)
280 {
281 int i, nCount = -1;
282 char *kp;
283 kvm_t *kd;
284 LONG nRet = SYSINFO_RC_ERROR;
285
286 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
287 if (kd != 0)
288 {
289 kp = (char *)kvm_getprocs(kd, KERN_PROC_ALL, 0, &nCount);
290 if (kp != NULL)
291 {
292 for (i = 0; i < nCount; i++, kp += 648)
293 {
294 char szBuff[128];
295
296 snprintf(szBuff, sizeof(szBuff), "%d %s",
297 KP_PID(kp), KP_PNAME(kp));
298 pValue->add(szBuff);
299 }
300 }
301 kvm_close(kd);
302 }
303
304 if (nCount >= 0)
305 {
306 nRet = SYSINFO_RC_SUCCESS;
307 }
308 return nRet;
309 }
310
311
312 ///////////////////////////////////////////////////////////////////////////////
313 /*
314
315 $Log: not supported by cvs2svn $
316 Revision 1.4 2006/08/16 22:26:09 victor
317 - Most of Net.Interface.XXX functions implemented on IPSO
318 - Added function MACToStr
319
320 Revision 1.3 2006/07/24 06:49:48 victor
321 - Process and physical memory parameters are working
322 - Various other changes
323
324 Revision 1.2 2006/07/21 16:22:44 victor
325 Some parameters are working
326
327 Revision 1.1 2006/07/21 11:48:35 victor
328 Initial commit
329
330 */