2
* Host Resources MIB - Device group implementation - hr_device.c
5
/* Portions of this file are subject to the following copyright(s). See
6
* the Net-SNMP's COPYING file for more details and other copyrights
10
* Portions of this file are copyrighted by:
11
* Copyright ļæ½ 2003 Sun Microsystems, Inc. All rights reserved.
12
* Use is subject to license terms specified in the COPYING file
13
* distributed with the Net-SNMP package.
16
#include <net-snmp/net-snmp-config.h>
24
#include "hr_device.h"
26
/*********************
28
* Kernel & interface information,
29
* and internal forward declarations
31
*********************/
33
int Get_Next_Device(void);
35
PFV init_device[HRDEV_TYPE_MAX];
36
PFIV next_device[HRDEV_TYPE_MAX];
37
PFV save_device[HRDEV_TYPE_MAX];
38
int dev_idx_inc[HRDEV_TYPE_MAX];
40
PFS device_descr[HRDEV_TYPE_MAX];
41
PFO device_prodid[HRDEV_TYPE_MAX];
42
PFI device_status[HRDEV_TYPE_MAX];
43
PFI device_errors[HRDEV_TYPE_MAX];
47
void Init_Device(void);
48
int header_hrdevice(struct variable *, oid *, size_t *, int,
49
size_t *, WriteMethod **);
52
/*********************
54
* Initialisation & common implementation functions
56
*********************/
62
#define HRDEV_STATUS 5
63
#define HRDEV_ERRORS 6
65
struct variable4 hrdevice_variables[] = {
66
{HRDEV_INDEX, ASN_INTEGER, RONLY, var_hrdevice, 2, {1, 1}},
67
{HRDEV_TYPE, ASN_OBJECT_ID, RONLY, var_hrdevice, 2, {1, 2}},
68
{HRDEV_DESCR, ASN_OCTET_STR, RONLY, var_hrdevice, 2, {1, 3}},
69
{HRDEV_ID, ASN_OBJECT_ID, RONLY, var_hrdevice, 2, {1, 4}},
70
{HRDEV_STATUS, ASN_INTEGER, RONLY, var_hrdevice, 2, {1, 5}},
71
{HRDEV_ERRORS, ASN_COUNTER, RONLY, var_hrdevice, 2, {1, 6}}
73
oid hrdevice_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 2 };
82
* Initially assume no devices
83
* Insert pointers to initialisation/get_next routines
84
* for particular device types as they are implemented
85
* (set up in the appropriate 'init_*()' routine )
88
for (i = 0; i < HRDEV_TYPE_MAX; ++i) {
89
init_device[i] = NULL;
90
next_device[i] = NULL;
91
save_device[i] = NULL;
92
dev_idx_inc[i] = 0; /* Assume random indices */
94
device_descr[i] = NULL;
95
device_prodid[i] = NULL;
96
device_status[i] = NULL;
97
device_errors[i] = NULL;
100
REGISTER_MIB("host/hr_device", hrdevice_variables, variable4,
101
hrdevice_variables_oid);
106
* header_hrdevice(...
108
* vp IN - pointer to variable entry that points here
109
* name IN/OUT - IN/name requested, OUT/name found
110
* length IN/OUT - length of IN/OUT oid's
111
* exact IN - TRUE if an exact match was requested
112
* var_len OUT - length of variable or 0 if function returned
118
header_hrdevice(struct variable *vp,
121
int exact, size_t * var_len, WriteMethod ** write_method)
123
#define HRDEV_ENTRY_NAME_LENGTH 11
124
oid newname[MAX_OID_LEN];
125
int dev_idx, LowIndex = -1, LowType = -1;
128
DEBUGMSGTL(("host/hr_device", "var_hrdevice: "));
129
DEBUGMSGOID(("host/hr_device", name, *length));
130
DEBUGMSG(("host/hr_device", " %d\n", exact));
132
memcpy((char *) newname, (char *) vp->name,
133
(int) vp->namelen * sizeof(oid));
137
* Find the "next" device entry.
138
* If we're in the middle of the table, then there's
139
* no point in examining earlier types of devices,
140
* so set the starting type to that of the variable
142
* If we've moved from one column of the table to another,
143
* then we need to start at the beginning again.
144
* (i.e. the 'compare' fails to match)
145
* Similarly if we're at the start of the table
146
* (i.e. *length is too short to be a full instance)
149
if ((snmp_oid_compare(vp->name, vp->namelen, name, vp->namelen) == 0)
150
&& (*length > HRDEV_ENTRY_NAME_LENGTH))
151
current_type = (name[HRDEV_ENTRY_NAME_LENGTH] >> HRDEV_TYPE_SHIFT);
157
dev_idx = Get_Next_Device();
158
DEBUGMSG(("host/hr_device", "(index %d ....", dev_idx));
161
if (LowType != -1 && LowType < (dev_idx >> HRDEV_TYPE_SHIFT))
163
newname[HRDEV_ENTRY_NAME_LENGTH] = dev_idx;
164
DEBUGMSGOID(("host/hr_device", newname, *length));
165
DEBUGMSG(("host/hr_device", "\n"));
166
result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
167
if (exact && (result == 0)) {
168
if (save_device[current_type] != NULL)
169
(*save_device[current_type]) ();
173
if ((!exact && (result < 0)) &&
174
(LowIndex == -1 || dev_idx < LowIndex)) {
175
if (save_device[current_type] != NULL)
176
(*save_device[current_type]) ();
178
LowType = (dev_idx >> HRDEV_TYPE_SHIFT);
179
if (dev_idx_inc[LowType]) /* Increasing indices => now done */
185
if (LowIndex == -1) {
186
DEBUGMSGTL(("host/hr_device", "... index out of range\n"));
187
return (MATCH_FAILED);
190
newname[HRDEV_ENTRY_NAME_LENGTH] = LowIndex;
191
memcpy((char *) name, (char *) newname,
192
((int) vp->namelen + 1) * sizeof(oid));
193
*length = vp->namelen + 1;
195
*var_len = sizeof(long); /* default to 'long' results */
197
DEBUGMSGTL(("host/hr_device", "... get device stats "));
198
DEBUGMSGOID(("host/hr_device", name, *length));
199
DEBUGMSG(("host/hr_device", "\n"));
205
oid device_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 3, 1, 99 }; /* hrDeviceType99 */
206
int device_type_len =
207
sizeof(device_type_id) / sizeof(device_type_id[0]);
210
/*********************
212
* System specific implementation functions
214
*********************/
218
var_hrdevice(struct variable *vp,
221
int exact, size_t * var_len, WriteMethod ** write_method)
225
static char string[1024];
228
header_hrdevice(vp, name, length, exact, var_len, write_method);
229
if (dev_idx == MATCH_FAILED)
232
type = (dev_idx >> HRDEV_TYPE_SHIFT);
236
long_return = dev_idx;
237
return (u_char *) & long_return;
239
device_type_id[device_type_len - 1] = type;
240
*var_len = sizeof(device_type_id);
241
return (u_char *) device_type_id;
243
if (device_descr[type] != NULL) {
244
strncpy(string, ((*device_descr[type]) (dev_idx)),
246
string[ sizeof(string)-1] = 0;
251
sprintf(string, "a black box of some sort");
253
*var_len = strlen(string);
254
return (u_char *) string;
256
if (device_prodid[type] != NULL)
257
oid_p = ((*device_prodid[type]) (dev_idx, var_len));
260
*var_len = nullOidLen;
262
return (u_char *) oid_p;
264
if (device_status[type] != NULL)
265
long_return = ((*device_status[type]) (dev_idx));
270
long_return = 2; /* Assume running */
272
return (u_char *) & long_return;
274
if (device_errors[type] != NULL)
275
long_return = (*device_errors[type]) (dev_idx);
280
long_return = 0; /* Assume OK */
282
return (u_char *) & long_return;
284
DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrdevice\n",
291
/*********************
293
* Internal implementation functions
295
*********************/
302
* Find the first non-NULL initialisation function
305
while (current_type < HRDEV_TYPE_MAX &&
306
init_device[current_type] == NULL)
307
if (++current_type >= HRDEV_TYPE_MAX)
309
/* Check current_type, if >= MAX first time into loop, would fail below */
310
if (current_type < HRDEV_TYPE_MAX)
311
(*init_device[current_type]) ();
315
Get_Next_Device(void)
320
* Call the 'next device' function for the current
323
* TODO: save the necessary information about that device
325
if (current_type < HRDEV_TYPE_MAX && next_device[current_type] != NULL)
326
result = (*next_device[current_type]) ();
329
* No more devices of the current type.
330
* Try the next type (if any)
333
if (++current_type >= HRDEV_TYPE_MAX) {
338
return Get_Next_Device();