4 ** NetXMS subagent for FreeBSD
5 ** Copyright (C) 2004 Alex Kirhenshtein
6 ** Copyright (C) 2006 Victor Kirhenshtein
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.
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.
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.
25 #include <sys/sysctl.h>
26 #include <sys/utsname.h>
27 #include <vm/vm_param.h>
28 #include <sys/vmmeter.h>
33 // Defines for getting data from process information structure
36 #define KP_PID(x) (*((pid_t *)(kp + 48)))
37 #define KP_PNAME(x) (x + 0xCB)
41 // Handler for System.Uptime
44 LONG
H_Uptime(char *pszParam
, char *pArg
, char *pValue
)
46 static int mib
[2] = { CTL_KERN
, KERN_BOOTTIME
};
49 struct timeval bootTime
;
52 nSize
= sizeof(bootTime
);
55 if (sysctl(mib
, 2, &bootTime
, &nSize
, NULL
, 0) != (-1))
57 nUptime
= (time_t)(nNow
- bootTime
.tv_sec
);
66 ret_uint(pValue
, nUptime
);
69 return nUptime
> 0 ? SYSINFO_RC_SUCCESS
: SYSINFO_RC_ERROR
;
74 // Handler for System.Uname
77 LONG
H_Uname(char *pszParam
, char *pArg
, char *pValue
)
79 struct utsname utsName
;
80 int nRet
= SYSINFO_RC_ERROR
;
82 if (uname(&utsName
) == 0)
86 snprintf(szBuff
, sizeof(szBuff
), "%s %s %s %s %s", utsName
.sysname
,
87 utsName
.nodename
, utsName
.release
, utsName
.version
,
89 // TODO: processor & platform
91 ret_string(pValue
, szBuff
);
93 nRet
= SYSINFO_RC_SUCCESS
;
99 LONG
H_Hostname(char *pszParam
, char *pArg
, char *pValue
)
101 int nRet
= SYSINFO_RC_ERROR
;
104 if (gethostname(szBuff
, sizeof(szBuff
)) == 0)
106 ret_string(pValue
, szBuff
);
107 nRet
= SYSINFO_RC_SUCCESS
;
113 LONG
H_CpuLoad(char *pszParam
, char *pArg
, char *pValue
)
115 int nRet
= SYSINFO_RC_ERROR
;
116 char szArg
[128] = {0};
121 //AgentGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
123 if (getloadavg(dLoad
, 3) == 3)
125 switch (pszParam
[19])
128 ret_double(pValue
, dLoad
[2]);
131 ret_double(pValue
, dLoad
[1]);
134 ret_double(pValue
, dLoad
[0]);
137 nRet
= SYSINFO_RC_SUCCESS
;
143 LONG
H_CpuCount(char *pszParam
, char *pArg
, char *pValue
)
145 int nRet
= SYSINFO_RC_ERROR
;
146 static int mib
[2] = { CTL_HW
, HW_NCPU
};
147 size_t nSize
= sizeof(mib
), nValSize
;
150 nValSize
= sizeof(nVal
);
151 if (sysctl(mib
, nSize
, &nVal
, &nValSize
, NULL
, 0) == 0)
153 ret_int(pValue
, nVal
);
154 nRet
= SYSINFO_RC_SUCCESS
;
162 // Handler for System.ProcessCount and Process.Count(*) parameters
165 LONG
H_ProcessCount(char *pszParam
, char *pArg
, char *pValue
)
167 char *kp
, szArg
[128] = "";
168 int i
, nCount
, nResult
= -1;
170 LONG nRet
= SYSINFO_RC_ERROR
;
172 AgentGetParameterArg(pszParam
, 1, szArg
, sizeof(szArg
));
174 kd
= kvm_openfiles(NULL
, NULL
, NULL
, O_RDONLY
, NULL
);
177 kp
= (char *)kvm_getprocs(kd
, KERN_PROC_ALL
, 0, &nCount
);
184 for(i
= 0; i
< nCount
; i
++, kp
+= 648)
186 if (!stricmp(KP_PNAME(kp
), szArg
))
202 ret_int(pValue
, nResult
);
203 nRet
= SYSINFO_RC_SUCCESS
;
210 // Handler for System.Memory.* parameters
213 LONG
H_MemoryInfo(char *pszParam
, char *pArg
, char *pValue
)
215 struct vmtotal vmStat
;
216 DWORD dwPageSize
, dwPhysMem
;
218 int mib
[2] = { CTL_HW
, HW_PAGESIZE
};
219 int nRet
= SYSINFO_RC_SUCCESS
;
222 if (sysctl(mib
, 2, &dwPageSize
, &nSize
, NULL
, 0) == -1)
224 return SYSINFO_RC_ERROR
;
229 if (sysctl(mib
, 2, &dwPhysMem
, &nSize
, NULL
, 0) == -1)
231 return SYSINFO_RC_ERROR
;
234 nSize
= sizeof(struct vmtotal
);
237 if (sysctl(mib
, 2, &vmStat
, &nSize
, NULL
, 0) != -1)
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);
254 ret_uint64(pValue
, (QWORD
)vmStat
.t_free
* dwPageSize
);
257 ret_uint64(pValue
, (QWORD
)dwPhysMem
);
260 ret_uint64(pValue
, (QWORD
)dwPhysMem
- (QWORD
)vmStat
.t_arm
* dwPageSize
);
263 nRet
= SYSINFO_RC_UNSUPPORTED
;
269 nRet
= SYSINFO_RC_ERROR
;
276 // Handler for System.ProcessList enum
279 LONG
H_ProcessList(char *pszParam
, char *pArg
, StringList
*pValue
)
284 LONG nRet
= SYSINFO_RC_ERROR
;
286 kd
= kvm_openfiles(NULL
, NULL
, NULL
, O_RDONLY
, NULL
);
289 kp
= (char *)kvm_getprocs(kd
, KERN_PROC_ALL
, 0, &nCount
);
292 for (i
= 0; i
< nCount
; i
++, kp
+= 648)
296 snprintf(szBuff
, sizeof(szBuff
), "%d %s",
297 KP_PID(kp
), KP_PNAME(kp
));
306 nRet
= SYSINFO_RC_SUCCESS
;
312 ///////////////////////////////////////////////////////////////////////////////
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
320 Revision 1.3 2006/07/24 06:49:48 victor
321 - Process and physical memory parameters are working
322 - Various other changes
324 Revision 1.2 2006/07/21 16:22:44 victor
325 Some parameters are working
327 Revision 1.1 2006/07/21 11:48:35 victor