2 * Host Resources MIB - proc processor group implementation - hr_proc.c
6 #include <net-snmp/net-snmp-config.h>
18 #include <net-snmp/agent/auto_nlist.h>
19 #include <net-snmp/agent/agent_read_config.h>
20 #include "ucd-snmp/loadave.h"
22 #define HRPROC_MONOTONICALLY_INCREASING
24 /*********************
26 * Kernel & interface information,
27 * and internal forward declarations
29 *********************/
31 extern void Init_HR_Proc(void);
32 extern int Get_Next_HR_Proc(void);
33 int header_hrproc(struct variable *, oid *, size_t *, int,
34 size_t *, WriteMethod **);
36 /*********************
38 * Initialisation & common implementation functions
40 *********************/
46 struct variable4 hrproc_variables[] = {
47 {HRPROC_ID, ASN_OBJECT_ID, RONLY, var_hrproc, 2, {1, 1}},
48 {HRPROC_LOAD, ASN_INTEGER, RONLY, var_hrproc, 2, {1, 2}}
50 oid hrproc_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 3 };
56 init_device[HRDEV_PROC] = Init_HR_Proc;
57 next_device[HRDEV_PROC] = Get_Next_HR_Proc;
58 #ifdef HRPROC_MONOTONICALLY_INCREASING
59 dev_idx_inc[HRDEV_PROC] = 1;
62 REGISTER_MIB("host/hr_proc", hrproc_variables, variable4,
63 hrproc_variables_oid);
69 * vp IN - pointer to variable entry that points here
70 * name IN/OUT - IN/name requested, OUT/name found
71 * length IN/OUT - length of IN/OUT oid's
72 * exact IN - TRUE if an exact match was requested
73 * var_len OUT - length of variable or 0 if function returned
79 header_hrproc(struct variable *vp,
82 int exact, size_t * var_len, WriteMethod ** write_method)
84 #define HRPROC_ENTRY_NAME_LENGTH 11
85 oid newname[MAX_OID_LEN];
86 int proc_idx, LowIndex = -1;
89 DEBUGMSGTL(("host/hr_proc", "var_hrproc: "));
90 DEBUGMSGOID(("host/hr_proc", name, *length));
91 DEBUGMSG(("host/hr_proc", " %d\n", exact));
93 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
95 * Find "next" proc entry
100 proc_idx = Get_Next_HR_Proc();
103 newname[HRPROC_ENTRY_NAME_LENGTH] = proc_idx;
104 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
105 if (exact && (result == 0)) {
108 * Save processor status information
112 if ((!exact && (result < 0)) &&
113 (LowIndex == -1 || proc_idx < LowIndex)) {
116 * Save processor status information
118 #ifdef HRPROC_MONOTONICALLY_INCREASING
124 if (LowIndex == -1) {
125 DEBUGMSGTL(("host/hr_proc", "... index out of range\n"));
126 return (MATCH_FAILED);
129 memcpy((char *) name, (char *) newname,
130 (vp->namelen + 1) * sizeof(oid));
131 *length = vp->namelen + 1;
133 *var_len = sizeof(long); /* default to 'long' results */
135 DEBUGMSGTL(("host/hr_proc", "... get proc stats "));
136 DEBUGMSGOID(("host/hr_proc", name, *length));
137 DEBUGMSG(("host/hr_proc", "\n"));
142 /*********************
144 * System specific implementation functions
146 *********************/
150 var_hrproc(struct variable * vp,
153 int exact, size_t * var_len, WriteMethod ** write_method)
159 header_hrproc(vp, name, length, exact, var_len, write_method);
160 if (proc_idx == MATCH_FAILED)
162 if (try_getloadavg(&avenrun[0], sizeof(avenrun) / sizeof(avenrun[0]))
168 *var_len = nullOidLen;
169 return (u_char *) nullOid;
174 long_return = avenrun[0] * 100; /* 1 minute average */
175 if (long_return > 100)
177 return (u_char *) & long_return;
179 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrproc\n",
186 /*********************
188 * Internal implementation functions
190 *********************/
192 static int HRP_index;
201 Get_Next_HR_Proc(void)
204 * Silly question time:
205 * How do you detect processors?
206 * Assume we've just got one.
210 return (HRDEV_PROC << HRDEV_TYPE_SHIFT) + HRP_index++;