b0afd660c536a74b553a3a534063263d52ed7855
[public/netxms.git] / src / agent / subagents / freebsd / system.cpp
1 /* $Id: system.cpp,v 1.4 2005-01-23 05:14:49 alk Exp $ */
2
3 /*
4 ** NetXMS subagent for FreeBSD
5 ** Copyright (C) 2004 Alex Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 **/
22
23 #include <nms_common.h>
24 #include <nms_agent.h>
25
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
29 #include <sys/utsname.h>
30 #include <sys/param.h>
31 #include <sys/user.h>
32
33 #include "system.h"
34
35 LONG H_Uptime(char *pszParam, char *pArg, char *pValue)
36 {
37 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
38 time_t nNow;
39 size_t nSize;
40 struct timeval bootTime;
41 time_t nUptime = 0;
42
43 nSize = sizeof(bootTime);
44
45 time(&nNow);
46 if (sysctl(mib, 2, &bootTime, &nSize, NULL, 0) != (-1))
47 {
48 nUptime = (time_t)(nNow - bootTime.tv_sec);
49 }
50 else
51 {
52 perror("uptime()");
53 }
54
55 if (nUptime > 0)
56 {
57 ret_uint(pValue, nUptime);
58 }
59
60 return nUptime > 0 ? SYSINFO_RC_SUCCESS : SYSINFO_RC_ERROR;
61 }
62
63 LONG H_Uname(char *pszParam, char *pArg, char *pValue)
64 {
65 struct utsname utsName;
66 int nRet = SYSINFO_RC_ERROR;
67
68 if (uname(&utsName) == 0)
69 {
70 char szBuff[1024];
71
72 snprintf(szBuff, sizeof(szBuff), "%s %s %s %s %s", utsName.sysname,
73 utsName.nodename, utsName.release, utsName.version,
74 utsName.machine);
75 // TODO: processor & platform
76
77 ret_string(pValue, szBuff);
78
79 nRet = SYSINFO_RC_SUCCESS;
80 }
81
82 return nRet;
83 }
84
85 LONG H_Hostname(char *pszParam, char *pArg, char *pValue)
86 {
87 int nRet = SYSINFO_RC_ERROR;
88 char szBuff[128];
89
90 if (gethostname(szBuff, sizeof(szBuff)) == 0)
91 {
92 ret_string(pValue, szBuff);
93 nRet = SYSINFO_RC_SUCCESS;
94 }
95
96 return nRet;
97 }
98
99 LONG H_CpuLoad(char *pszParam, char *pArg, char *pValue)
100 {
101 int nRet = SYSINFO_RC_ERROR;
102 char szArg[128] = {0};
103 FILE *hFile;
104 double dLoad[3];
105
106 // get processor
107 //NxGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
108
109 if (getloadavg(dLoad, 3) == 3)
110 {
111 switch (pszParam[19])
112 {
113 case '1': // 15 min
114 ret_double(pValue, dLoad[2]);
115 break;
116 case '5': // 5 min
117 ret_double(pValue, dLoad[1]);
118 break;
119 default: // 1 min
120 ret_double(pValue, dLoad[0]);
121 break;
122 }
123 nRet = SYSINFO_RC_SUCCESS;
124 }
125
126 return nRet;
127 }
128
129 LONG H_CpuUsage(char *pszParam, char *pArg, char *pValue)
130 {
131 return SYSINFO_RC_UNSUPPORTED;
132 }
133
134 LONG H_CpuCount(char *pszParam, char *pArg, char *pValue)
135 {
136 int nRet = SYSINFO_RC_ERROR;
137 int mib[2];
138 size_t nSize = sizeof(mib), nValSize;
139 int nVal;
140
141 if (sysctlnametomib("hw.ncpu", mib, &nSize) != 0)
142 {
143 return SYSINFO_RC_ERROR;
144 }
145
146 nValSize = sizeof(nVal);
147 if (sysctl(mib, nSize, &nVal, &nValSize, NULL, 0) == 0)
148 {
149 ret_int(pValue, nVal);
150 nRet = SYSINFO_RC_SUCCESS;
151 }
152
153 return nRet;
154 }
155
156 LONG H_ProcessCount(char *pszParam, char *pArg, char *pValue)
157 {
158 int nRet = SYSINFO_RC_ERROR;
159 // struct statvfs s;
160 char szArg[128] = {0};
161 int nCount = -1;
162 struct kinfo_proc *pKInfo;
163 int mib[3];
164 size_t nSize;
165 int i;
166
167 NxGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
168
169 nSize = sizeof(mib);
170 if (sysctlnametomib("kern.proc.all", mib, &nSize) == 0)
171 {
172 if (sysctl(mib, 3, NULL, &nSize, NULL, 0) == 0)
173 {
174 {
175 pKInfo = (struct kinfo_proc *)malloc(nSize);
176 if (pKInfo != NULL)
177 {
178 if (sysctl(mib, 3, pKInfo, &nSize, NULL, 0) == 0)
179 {
180 nCount = 0;
181 for (i = 0; i < (nSize / sizeof(struct kinfo_proc)); i++)
182 {
183 if (szArg[0] == 0 ||
184 strcasecmp(pKInfo[i].kp_proc.p_comm, szArg) == 0)
185 {
186 nCount++;
187 }
188 }
189 }
190 free(pKInfo);
191 }
192 }
193 }
194 }
195
196 if (nCount >= 0)
197 {
198 ret_int(pValue, nCount);
199 nRet = SYSINFO_RC_SUCCESS;
200 }
201
202 return nRet;
203 }
204
205 LONG H_MemoryInfo(char *pszParam, char *pArg, char *pValue)
206 {
207 int nRet = SYSINFO_RC_ERROR;
208 FILE *hFile;
209 int nPageCount, nFreeCount;
210 char *pTag;
211 int mib[4];
212 size_t nSize;
213 int i;
214 char *pOid;
215 int nPageSize;
216 char szArg[16] = {0};
217
218 nPageCount = nFreeCount = 0;
219
220 NxGetParameterArg(pszParam, 1, szArg, sizeof(szArg));
221
222 #define DOIT(x, y) { \
223 nSize = sizeof(mib); \
224 if (sysctlnametomib(x, mib, &nSize) == 0) { \
225 size_t __nTmp = sizeof(y); \
226 if (sysctl(mib, nSize, &y, &__nTmp, NULL, 0) == 0) { \
227 nRet = SYSINFO_RC_SUCCESS; \
228 } else { \
229 perror(x ": sysctl()"); \
230 } \
231 } else { \
232 perror(x ": sysctlnametomib()"); \
233 } \
234 }
235
236 nPageSize = getpagesize();
237
238 DOIT("vm.stats.vm.v_page_count", nPageCount);
239
240 if (nRet == SYSINFO_RC_SUCCESS)
241 {
242 nRet = SYSINFO_RC_ERROR;
243 DOIT("vm.stats.vm.v_free_count", nFreeCount);
244 }
245
246 #undef DOIT
247
248 if (nRet == SYSINFO_RC_SUCCESS)
249 {
250 switch((int)pArg)
251 {
252 case PHYSICAL_FREE: // ph-free
253 ret_uint64(pValue, ((int64_t)nFreeCount) * nPageSize);
254 break;
255 case PHYSICAL_TOTAL: // ph-total
256 ret_uint64(pValue, ((int64_t)nPageCount) * nPageSize);
257 break;
258 case PHYSICAL_USED: // ph-used
259 ret_uint64(pValue, ((int64_t)(nPageCount - nFreeCount)) * nPageSize);
260 break;
261 case SWAP_FREE: // sw-free
262 ret_uint64(pValue, 0);
263 break;
264 case SWAP_TOTAL: // sw-total
265 ret_uint64(pValue, 0);
266 break;
267 case SWAP_USED: // sw-used
268 ret_uint64(pValue, 0);
269 break;
270 case VIRTUAL_FREE: // vi-free
271 ret_uint64(pValue, 0);
272 break;
273 case VIRTUAL_TOTAL: // vi-total
274 ret_uint64(pValue, 0);
275 break;
276 case VIRTUAL_USED: // vi-used
277 ret_uint64(pValue, 0);
278 break;
279 default: // error
280 nRet = SYSINFO_RC_ERROR;
281 break;
282 }
283 }
284
285 return nRet;
286 }
287
288 LONG H_ProcessList(char *pszParam, char *pArg, NETXMS_VALUES_LIST *pValue)
289 {
290 int nRet = SYSINFO_RC_ERROR;
291 int nCount = -1;
292 struct kinfo_proc *pKInfo;
293 int mib[3];
294 size_t nSize;
295 int i;
296
297 nSize = sizeof(mib);
298 if (sysctlnametomib("kern.proc.all", mib, &nSize) == 0)
299 {
300 if (sysctl(mib, 3, NULL, &nSize, NULL, 0) == 0)
301 {
302 {
303 pKInfo = (struct kinfo_proc *)malloc(nSize);
304 if (pKInfo != NULL)
305 {
306 if (sysctl(mib, 3, pKInfo, &nSize, NULL, 0) == 0)
307 {
308 nCount = 0;
309 for (i = 0; i < (nSize / sizeof(struct kinfo_proc)); i++)
310 {
311 char szBuff[128];
312
313 snprintf(szBuff, sizeof(szBuff), "%d %s",
314 pKInfo[i].kp_proc.p_pid, pKInfo[i].kp_proc.p_comm);
315 NxAddResultString(pValue, szBuff);
316
317 nCount++;
318 }
319 }
320 free(pKInfo);
321 }
322 }
323 }
324 }
325
326 if (nCount >= 0)
327 {
328 nRet = SYSINFO_RC_SUCCESS;
329 }
330
331 return nRet;
332 }
333
334 //
335 // stub
336 //
337 LONG H_SourcePkgSupport(char *pszParam, char *pArg, char *pValue)
338 {
339 ret_int(pValue, 1);
340 return SYSINFO_RC_SUCCESS;
341 }
342
343
344 ///////////////////////////////////////////////////////////////////////////////
345 /*
346
347 $Log: not supported by cvs2svn $
348 Revision 1.3 2005/01/23 05:08:06 alk
349 + System.CPU.Count
350 + System.Memory.Physical.*
351 + System.ProcessCount
352 + System.ProcessList
353
354 Revision 1.2 2005/01/17 23:25:47 alk
355 Agent.SourcePackageSupport added
356
357 Revision 1.1 2005/01/17 17:14:32 alk
358 freebsd agent, incomplete (but working)
359
360 Revision 1.1 2004/10/22 22:08:35 alk
361 source restructured;
362 implemented:
363 Net.IP.Forwarding
364 Net.IP6.Forwarding
365 Process.Count(*)
366 Net.ArpCache
367 Net.InterfaceList (if-type not implemented yet)
368 System.ProcessList
369
370
371 */