and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / host / hr_proc.c
1 /*
2  *  Host Resources MIB - proc processor group implementation - hr_proc.c
3  *
4  */
5
6 #include <net-snmp/net-snmp-config.h>
7 #if HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #if HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15
16 #include "host_res.h"
17 #include "hr_proc.h"
18 #include <net-snmp/agent/auto_nlist.h>
19 #include <net-snmp/agent/agent_read_config.h>
20 #include "ucd-snmp/loadave.h"
21
22 #define HRPROC_MONOTONICALLY_INCREASING
23
24         /*********************
25          *
26          *  Kernel & interface information,
27          *   and internal forward declarations
28          *
29          *********************/
30
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 **);
35
36         /*********************
37          *
38          *  Initialisation & common implementation functions
39          *
40          *********************/
41
42
43 #define HRPROC_ID               1
44 #define HRPROC_LOAD             2
45
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}}
49 };
50 oid             hrproc_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 3 };
51
52
53 void
54 init_hr_proc(void)
55 {
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;
60 #endif
61
62     REGISTER_MIB("host/hr_proc", hrproc_variables, variable4,
63                  hrproc_variables_oid);
64 }
65
66 /*
67  * header_hrproc(...
68  * Arguments:
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
74  * write_method
75  * 
76  */
77
78 int
79 header_hrproc(struct variable *vp,
80               oid * name,
81               size_t * length,
82               int exact, size_t * var_len, WriteMethod ** write_method)
83 {
84 #define HRPROC_ENTRY_NAME_LENGTH        11
85     oid             newname[MAX_OID_LEN];
86     int             proc_idx, LowIndex = -1;
87     int             result;
88
89     DEBUGMSGTL(("host/hr_proc", "var_hrproc: "));
90     DEBUGMSGOID(("host/hr_proc", name, *length));
91     DEBUGMSG(("host/hr_proc", " %d\n", exact));
92
93     memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
94     /*
95      * Find "next" proc entry 
96      */
97
98     Init_HR_Proc();
99     for (;;) {
100         proc_idx = Get_Next_HR_Proc();
101         if (proc_idx == -1)
102             break;
103         newname[HRPROC_ENTRY_NAME_LENGTH] = proc_idx;
104         result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
105         if (exact && (result == 0)) {
106             LowIndex = proc_idx;
107             /*
108              * Save processor status information 
109              */
110             break;
111         }
112         if ((!exact && (result < 0)) &&
113             (LowIndex == -1 || proc_idx < LowIndex)) {
114             LowIndex = proc_idx;
115             /*
116              * Save processor status information 
117              */
118 #ifdef HRPROC_MONOTONICALLY_INCREASING
119             break;
120 #endif
121         }
122     }
123
124     if (LowIndex == -1) {
125         DEBUGMSGTL(("host/hr_proc", "... index out of range\n"));
126         return (MATCH_FAILED);
127     }
128
129     memcpy((char *) name, (char *) newname,
130            (vp->namelen + 1) * sizeof(oid));
131     *length = vp->namelen + 1;
132     *write_method = 0;
133     *var_len = sizeof(long);    /* default to 'long' results */
134
135     DEBUGMSGTL(("host/hr_proc", "... get proc stats "));
136     DEBUGMSGOID(("host/hr_proc", name, *length));
137     DEBUGMSG(("host/hr_proc", "\n"));
138     return LowIndex;
139 }
140
141
142         /*********************
143          *
144          *  System specific implementation functions
145          *
146          *********************/
147
148
149 u_char         *
150 var_hrproc(struct variable * vp,
151            oid * name,
152            size_t * length,
153            int exact, size_t * var_len, WriteMethod ** write_method)
154 {
155     int             proc_idx;
156     double          avenrun[3];
157
158     proc_idx =
159         header_hrproc(vp, name, length, exact, var_len, write_method);
160     if (proc_idx == MATCH_FAILED)
161         return NULL;
162     if (try_getloadavg(&avenrun[0], sizeof(avenrun) / sizeof(avenrun[0]))
163         == -1)
164         return NULL;
165
166     switch (vp->magic) {
167     case HRPROC_ID:
168         *var_len = nullOidLen;
169         return (u_char *) nullOid;
170     case HRPROC_LOAD:
171 #if NO_DUMMY_VALUES
172         return NULL;
173 #endif
174         long_return = avenrun[0] * 100; /* 1 minute average */
175         if (long_return > 100)
176             long_return = 100;
177         return (u_char *) & long_return;
178     default:
179         DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrproc\n",
180                     vp->magic));
181     }
182     return NULL;
183 }
184
185
186         /*********************
187          *
188          *  Internal implementation functions
189          *
190          *********************/
191
192 static int      HRP_index;
193
194 void
195 Init_HR_Proc(void)
196 {
197     HRP_index = 1;
198 }
199
200 int
201 Get_Next_HR_Proc(void)
202 {
203     /*
204      * Silly question time:
205      *   How do you detect processors?
206      *   Assume we've just got one.
207      */
208
209     if (HRP_index < 2)
210         return (HRDEV_PROC << HRDEV_TYPE_SHIFT) + HRP_index++;
211     else
212         return -1;
213 }