Solaris subagent: implemented System.Memory.Physical.* parameters
[public/netxms.git] / src / agent / subagents / sunos / system.cpp
CommitLineData
1b3c0ef0
VK
1/*
2** NetXMS subagent for SunOS/Solaris
3** Copyright (C) 2004, 2005 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: system.cpp
20**
21**/
22
23#include "sunos_subagent.h"
24#include <sys/systeminfo.h>
25
26
27//
28// Handler for System.Uname parameter
29//
30
31LONG H_Uname(char *pszParam, char *pArg, char *pValue)
32{
33 char szSysStr[7][64];
34 int i;
35 LONG nRet = SYSINFO_RC_SUCCESS;
36 static int nSysCode[7] =
37 {
38 SI_SYSNAME,
39 SI_HOSTNAME,
40 SI_RELEASE,
41 SI_VERSION,
42 SI_MACHINE,
43 SI_ARCHITECTURE,
44 SI_PLATFORM
45 };
46
47 for(i = 0; i < 7; i++)
48 if (sysinfo(nSysCode[i], szSysStr[i], 64) == -1)
49 {
50 nRet = SYSINFO_RC_ERROR;
51 break;
52 }
53
54 if (nRet == SYSINFO_RC_SUCCESS)
55 {
56 snprintf(pValue, MAX_RESULT_LENGTH, "%s %s %s %s %s %s %s",
57 szSysStr[0], szSysStr[1], szSysStr[2], szSysStr[3],
58 szSysStr[4], szSysStr[5], szSysStr[6]);
59 }
60
61 return nRet;
62}
63
64
65//
66// Handler for System.Uptime parameter
67//
68
69LONG H_Uptime(char *pszParam, char *pArg, char *pValue)
70{
71 kstat_ctl_t *kc;
72 kstat_t *kp;
73 kstat_named_t *kn;
74 DWORD hz, secs;
75 LONG nRet = SYSINFO_RC_ERROR;
76
77 hz = sysconf(_SC_CLK_TCK);
78
79 // Open kstat
80 kc = kstat_open();
81 if (kc != NULL)
82 {
83 // read uptime counter
84 kp = kstat_lookup(kc, "unix", 0, "system_misc");
85 if (kp != NULL)
86 {
87 if(kstat_read(kc, kp, 0) != -1)
88 {
89 kn = (kstat_named_t *)kstat_data_lookup(kp, "clk_intr");
3babc9bf
VK
90 if (kn != NULL)
91 {
92 secs = kn->value.ul / hz;
93 ret_uint(pValue, secs);
94 nRet = SYSINFO_RC_SUCCESS;
95 }
1b3c0ef0
VK
96 }
97 }
98 kstat_close(kc);
99 }
100
101 return nRet;
102}
103
5e55b48b
VK
104//
105// Handler for System.Hostname parameter
106//
107
108LONG H_Hostname(char *pszParam, char *pArg, char *pValue)
109{
110 return (sysinfo(SI_HOSTNAME, pValue, MAX_RESULT_LENGTH) == -1) ?
3babc9bf
VK
111 SYSINFO_RC_ERROR : SYSINFO_RC_SUCCESS;
112}
113
114
115//
116// Handler for System.CPU.LoadAvg
117//
118
119LONG H_LoadAvg(char *pszParam, char *pArg, char *pValue)
120{
121 kstat_ctl_t *kc;
122 kstat_t *kp;
123 kstat_named_t *kn;
124 LONG nRet = SYSINFO_RC_ERROR;
125 static char *szParam[] = { "avenrun_1min", "avenrun_5min", "avenrun_15min" };
126
127 // Open kstat
128 kc = kstat_open();
129 if (kc != NULL)
130 {
131 kp = kstat_lookup(kc, "unix", 0, "system_misc");
132 if (kp != NULL)
133 {
134 if(kstat_read(kc, kp, 0) != -1)
135 {
136 kn = (kstat_named_t *)kstat_data_lookup(kp, szParam[(int)pArg]);
137 if (kn != NULL)
138 {
139 sprintf(pValue, "%.2f0000", (double)kn->value.ul / 256.0);
140 nRet = SYSINFO_RC_SUCCESS;
141 }
142 }
143 }
144 kstat_close(kc);
145 }
146
147 return nRet;
5e55b48b 148}
7b1d9d90
VK
149
150
151//
152// Handler for System.KStat(*)
153//
154
155LONG H_KStat(char *pszParam, char *pArg, char *pValue)
156{
157 char *eptr, szModule[128], szName[128], szInstance[16], szStat[128];
158 LONG nInstance;
159
160 // Read parameters
161 if ((!NxGetParameterArg(pszParam, 1, szModule, 128)) ||
162 (!NxGetParameterArg(pszParam, 2, szInstance, 16)) ||
163 (!NxGetParameterArg(pszParam, 3, szName, 128)) ||
164 (!NxGetParameterArg(pszParam, 4, szStat, 128)))
165 return SYSINFO_RC_UNSUPPORTED;
166
167 if (szInstance[0] != 0)
168 {
169 nInstance = strtol(szInstance, &eptr, 0);
170 if (*eptr != 0)
171 return SYSINFO_RC_UNSUPPORTED;
172 }
173 else
174 {
175 nInstance = 0;
176 }
177
29b8518f 178 return ReadKStatValue(szModule, nInstance, szName, szStat, pValue, NULL);
7b1d9d90
VK
179}
180
181
182//
183// Handler for System.CPU.Count
184//
185
186LONG H_CPUCount(char *pszParam, char *pArg, char *pValue)
187{
29b8518f 188 return ReadKStatValue("unix", 0, "system_misc", "ncpus", pValue, NULL);
7b1d9d90
VK
189}
190
191
192//
193// Handler for generic kstat parameter
194//
195
7a5229b4 196LONG ReadKStatValue(char *pszModule, LONG nInstance, char *pszName,
29b8518f 197 char *pszStat, char *pValue, kstat_named_t *pRawValue)
7b1d9d90
VK
198{
199 kstat_ctl_t *kc;
200 kstat_t *kp;
201 kstat_named_t *kn;
202 LONG nRet = SYSINFO_RC_ERROR;
203
204 // Open kstat
205 kc = kstat_open();
206 if (kc != NULL)
207 {
208 kp = kstat_lookup(kc, pszModule, nInstance, pszName);
209 if (kp != NULL)
210 {
211 if(kstat_read(kc, kp, 0) != -1)
212 {
213 kn = (kstat_named_t *)kstat_data_lookup(kp, pszStat);
214 if (kn != NULL)
215 {
29b8518f
VK
216 if (pValue != NULL)
217 {
218 switch(kn->data_type)
219 {
220 case KSTAT_DATA_CHAR:
221 ret_string(pValue, kn->value.c);
222 break;
223 case KSTAT_DATA_INT32:
224 ret_int(pValue, kn->value.i32);
225 break;
226 case KSTAT_DATA_UINT32:
227 ret_uint(pValue, kn->value.ui32);
228 break;
229 case KSTAT_DATA_INT64:
230 ret_int64(pValue, kn->value.i64);
231 break;
232 case KSTAT_DATA_UINT64:
233 ret_uint64(pValue, kn->value.ui64);
234 break;
235 case KSTAT_DATA_FLOAT:
236 ret_double(pValue, kn->value.f);
237 break;
238 case KSTAT_DATA_DOUBLE:
239 ret_double(pValue, kn->value.d);
240 break;
241 default:
242 ret_int(pValue, 0);
243 break;
244 }
7b1d9d90 245 }
29b8518f
VK
246
247 if (pRawValue != NULL)
248 {
249 memcpy(pRawValue, kn, sizeof(kstat_named_t));
250 }
251
7b1d9d90
VK
252 nRet = SYSINFO_RC_SUCCESS;
253 }
254 }
255 }
256 kstat_close(kc);
257 }
258
259 return nRet;
260}
7a5229b4
VK
261
262
263//
264// Handler for System.CPU.Count
265//
266
267LONG H_MemoryInfo(char *pszParam, char *pArg, char *pValue)
268{
269 LONG nRet = SYSINFO_RC_SUCCESS;
29b8518f
VK
270 kstat_named_t kn;
271 QWORD qwPageSize;
272
273 qwPageSize = sysconf(_SC_PAGESIZE);
7a5229b4
VK
274
275 switch((int)pArg)
276 {
277 case MEMINFO_PHYSICAL_TOTAL:
29b8518f
VK
278 ret_uint64(pValue, (QWORD)sysconf(_SC_PHYS_PAGES) * qwPageSize);
279 break;
280 case MEMINFO_PHYSICAL_FREE:
281 nRet = ReadKStatValue("unix", 0, "system_pages", "freemem", NULL, &kn);
282 if (nRet == SYSINFO_RC_SUCCESS)
283 {
284 ret_uint64(pValue, (QWORD)kn.value.ul * qwPageSize);
285 }
286 break;
287 case MEMINFO_PHYSICAL_USED:
288 nRet = ReadKStatValue("unix", 0, "system_pages", "freemem", NULL, &kn);
289 if (nRet == SYSINFO_RC_SUCCESS)
290 {
291 ret_uint64(pValue, (QWORD)(sysconf(_SC_PHYS_PAGES) - kn.value.ul) * qwPageSize);
292 }
7a5229b4
VK
293 break;
294 default:
295 nRet = SYSINFO_RC_UNSUPPORTED;
296 break;
297 }
298
299 return nRet;
300}