2 * Host Resources MIB - system group implementation - hr_system.c
6 #include <net-snmp/net-snmp-config.h>
13 #include <net-snmp/net-snmp-includes.h>
14 #include <net-snmp/agent/net-snmp-agent-includes.h>
18 #include "hr_system.h"
19 #include <net-snmp/agent/auto_nlist.h>
21 #ifdef HAVE_SYS_PROC_H
22 #include <sys/param.h>
32 #ifdef HAVE_LINUX_TASKS_H
33 #include <linux/tasks.h>
36 * If this file doesn't exist, then there is no hard limit on the number
37 * of processes, so return 0 for hrSystemMaxProcesses.
43 #if defined(hpux10) || defined(hpux11)
44 #include <sys/pstat.h>
47 #ifdef HAVE_SYS_SYSCTL_H
48 #include <sys/sysctl.h>
51 #if !defined(UTMP_FILE) && defined(_PATH_UTMP)
52 #define UTMP_FILE _PATH_UTMP
55 #if defined(UTMP_FILE) && !HAVE_UTMPX_H
58 struct utmp *getutent(void);
59 #endif /* UTMP_FILE */
62 /*********************
64 * Kernel & interface information,
65 * and internal forward declarations
67 *********************/
69 static int get_load_dev(void);
70 static int count_users(void);
71 extern int count_processes(void);
74 /*********************
76 * Initialisation & common implementation functions
78 *********************/
80 #define HRSYS_UPTIME 1
82 #define HRSYS_LOAD_DEV 3
83 #define HRSYS_LOAD_PARAM 4
86 #define HRSYS_MAXPROCS 7
88 struct variable2 hrsystem_variables[] = {
89 {HRSYS_UPTIME, ASN_TIMETICKS, RONLY, var_hrsys, 1, {1}},
90 {HRSYS_DATE, ASN_OCTET_STR, RONLY, var_hrsys, 1, {2}},
91 {HRSYS_LOAD_DEV, ASN_INTEGER, RONLY, var_hrsys, 1, {3}},
92 {HRSYS_LOAD_PARAM, ASN_OCTET_STR, RONLY, var_hrsys, 1, {4}},
93 {HRSYS_USERS, ASN_GAUGE, RONLY, var_hrsys, 1, {5}},
94 {HRSYS_PROCS, ASN_GAUGE, RONLY, var_hrsys, 1, {6}},
95 {HRSYS_MAXPROCS, ASN_INTEGER, RONLY, var_hrsys, 1, {7}}
97 oid hrsystem_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 1 };
104 auto_nlist(NPROC_SYMBOL, 0, 0);
107 REGISTER_MIB("host/hr_system", hrsystem_variables, variable2,
108 hrsystem_variables_oid);
114 * vp IN - pointer to variable entry that points here
115 * name IN/OUT - IN/name requested, OUT/name found
116 * length IN/OUT - length of IN/OUT oid's
117 * exact IN - TRUE if an exact match was requested
118 * var_len OUT - length of variable or 0 if function returned
123 header_hrsys(struct variable *vp,
126 int exact, size_t * var_len, WriteMethod ** write_method)
128 #define HRSYS_NAME_LENGTH 9
129 oid newname[MAX_OID_LEN];
132 DEBUGMSGTL(("host/hr_system", "var_hrsys: "));
133 DEBUGMSGOID(("host/hr_system", name, *length));
134 DEBUGMSG(("host/hr_system", " %d\n", exact));
136 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
137 newname[HRSYS_NAME_LENGTH] = 0;
138 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
139 if ((exact && (result != 0)) || (!exact && (result >= 0)))
140 return (MATCH_FAILED);
141 memcpy((char *) name, (char *) newname,
142 (vp->namelen + 1) * sizeof(oid));
143 *length = vp->namelen + 1;
146 *var_len = sizeof(long); /* default to 'long' results */
147 return (MATCH_SUCCEEDED);
151 /*********************
153 * System specific implementation functions
155 *********************/
158 var_hrsys(struct variable * vp,
161 int exact, size_t * var_len, WriteMethod ** write_method)
163 static char string[100];
171 #if CAN_USE_SYSCTL && defined(CTL_KERN) && defined(KERN_MAXPROC)
172 static int maxproc_mib[] = { CTL_KERN, KERN_MAXPROC };
175 #if defined(hpux10) || defined(hpux11)
176 struct pst_static pst_buf;
179 if (header_hrsys(vp, name, length, exact, var_len, write_method) ==
185 long_return = get_uptime();
186 return (u_char *) & long_return;
189 return (u_char *) date_n_time(&now, var_len);
191 long_return = get_load_dev();
192 return (u_char *) & long_return;
193 case HRSYS_LOAD_PARAM:
195 fp = fopen("/proc/cmdline", "r");
196 fgets(string, sizeof(string), fp);
202 sprintf(string, "ask Dave"); /* XXX */
204 *var_len = strlen(string);
205 return (u_char *) string;
207 long_return = count_users();
208 return (u_char *) & long_return;
210 #if USING_HOST_HR_SWRUN_MODULE
211 long_return = count_processes();
218 return (u_char *) & long_return;
220 #if defined(NR_TASKS)
221 long_return = NR_TASKS; /* <linux/tasks.h> */
222 #elif CAN_USE_SYSCTL && defined(CTL_KERN) && defined(KERN_MAXPROC)
223 buf_size = sizeof(nproc);
224 if (sysctl(maxproc_mib, 2, &nproc, &buf_size, NULL, 0) < 0)
227 #elif defined(hpux10) || defined(hpux11)
228 pstat_getstatic(&pst_buf, sizeof(struct pst_static), 1, 0);
229 long_return = pst_buf.max_proc;
230 #elif defined(NPROC_SYMBOL)
231 auto_nlist(NPROC_SYMBOL, (char *) &nproc, sizeof(int));
239 return (u_char *) & long_return;
241 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrsys\n",
248 /*********************
250 * Internal implementation functions
252 *********************/
255 * Return the DeviceIndex corresponding
261 return (HRDEV_DISK << HRDEV_TYPE_SHIFT); /* XXX */
269 #define setutent setutxent
270 #define getutent getutxent
271 #define endutent endutxent
272 struct utmpx *utmp_p;
278 while ((utmp_p = getutent()) != NULL) {
279 #ifndef UTMP_HAS_NO_TYPE
280 if (utmp_p->ut_type == USER_PROCESS)
288 #if defined(UTMP_FILE) && !HAVE_UTMPX_H
290 static FILE *utmp_file;
291 static struct utmp utmp_rec;
298 utmp_file = fopen(UTMP_FILE, "r");
315 while (fread(&utmp_rec, sizeof(utmp_rec), 1, utmp_file) == 1)
316 if (*utmp_rec.ut_name && *utmp_rec.ut_line)
321 #endif /* UTMP_FILE */