3 * Host Resources MIB - partition device group implementation - hr_partition.c
7 #include <net-snmp/net-snmp-config.h>
20 #include "hr_partition.h"
21 #include "hr_filesys.h"
26 #define HRP_MONOTONICALLY_INCREASING
28 /*********************
30 * Kernel & interface information,
31 * and internal forward declarations
33 *********************/
35 static int HRP_savedDiskIndex;
36 static int HRP_savedPartIndex;
37 static char HRP_savedName[100];
39 static int HRP_DiskIndex;
41 static void Save_HR_Partition(int, int);
44 /*********************
46 * Initialisation & common implementation functions
48 *********************/
50 static void Init_HR_Partition(void);
51 static int Get_Next_HR_Partition(void);
52 int header_hrpartition(struct variable *, oid *, size_t *, int,
53 size_t *, WriteMethod **);
56 #define HRPART_INDEX 1
57 #define HRPART_LABEL 2
60 #define HRPART_FSIDX 5
62 struct variable4 hrpartition_variables[] = {
63 {HRPART_INDEX, ASN_INTEGER, RONLY, var_hrpartition, 2, {1, 1}},
64 {HRPART_LABEL, ASN_OCTET_STR, RONLY, var_hrpartition, 2, {1, 2}},
65 {HRPART_ID, ASN_OCTET_STR, RONLY, var_hrpartition, 2, {1, 3}},
66 {HRPART_SIZE, ASN_INTEGER, RONLY, var_hrpartition, 2, {1, 4}},
67 {HRPART_FSIDX, ASN_INTEGER, RONLY, var_hrpartition, 2, {1, 5}}
69 oid hrpartition_variables_oid[] =
70 { 1, 3, 6, 1, 2, 1, 25, 3, 7 };
74 init_hr_partition(void)
76 REGISTER_MIB("host/hr_partition", hrpartition_variables, variable4,
77 hrpartition_variables_oid);
82 * header_hrpartition(...
84 * vp IN - pointer to variable entry that points here
85 * name IN/OUT - IN/name requested, OUT/name found
86 * length IN/OUT - length of IN/OUT oid's
87 * exact IN - TRUE if an exact match was requested
88 * var_len OUT - length of variable or 0 if function returned
94 header_hrpartition(struct variable *vp,
98 size_t * var_len, WriteMethod ** write_method)
100 #define HRPART_DISK_NAME_LENGTH 11
101 #define HRPART_ENTRY_NAME_LENGTH 12
102 oid newname[MAX_OID_LEN];
103 int part_idx, LowDiskIndex = -1, LowPartIndex = -1;
106 DEBUGMSGTL(("host/hr_partition", "var_hrpartition: "));
107 DEBUGMSGOID(("host/hr_partition", name, *length));
108 DEBUGMSG(("host/hr_partition", " %d\n", exact));
110 memcpy((char *) newname, (char *) vp->name,
111 (int) vp->namelen * sizeof(oid));
113 * Find "next" partition entry
120 * Find the "next" disk and partition entries.
121 * If we're in the middle of the table, then there's
122 * no point in examining earlier disks, so set the
123 * starting disk to that of the variable being queried.
125 * If we've moved from one column of the table to another,
126 * then we need to start at the beginning again.
127 * (i.e. the 'compare' fails to match)
128 * Similarly if we're at the start of the table
129 * (i.e. *length is too short to be a full instance)
132 if ((snmp_oid_compare(vp->name, vp->namelen, name, vp->namelen) == 0)
133 && (*length > HRPART_DISK_NAME_LENGTH)) {
135 (name[HRPART_DISK_NAME_LENGTH] &
136 ((1 << HRDEV_TYPE_SHIFT) - 1));
138 while (HRP_DiskIndex < LowDiskIndex) {
139 Init_HR_Partition(); /* moves to next disk */
140 if (HRP_DiskIndex == -1)
141 return (MATCH_FAILED);
146 part_idx = Get_Next_HR_Partition();
149 newname[HRPART_DISK_NAME_LENGTH] =
150 (HRDEV_DISK << HRDEV_TYPE_SHIFT) + HRP_DiskIndex;
151 newname[HRPART_ENTRY_NAME_LENGTH] = part_idx;
152 result = snmp_oid_compare(name, *length, newname, vp->namelen + 2);
153 if (exact && (result == 0)) {
154 Save_HR_Partition(HRP_DiskIndex, part_idx);
155 LowDiskIndex = HRP_DiskIndex;
156 LowPartIndex = part_idx;
159 if (!exact && (result < 0)) {
160 if (LowPartIndex == -1) {
161 Save_HR_Partition(HRP_DiskIndex, part_idx);
162 LowDiskIndex = HRP_DiskIndex;
163 LowPartIndex = part_idx;
164 } else if (LowDiskIndex < HRP_DiskIndex)
166 else if (part_idx < LowPartIndex) {
167 Save_HR_Partition(HRP_DiskIndex, part_idx);
168 LowDiskIndex = HRP_DiskIndex;
169 LowPartIndex = part_idx;
171 #ifdef HRP_MONOTONICALLY_INCREASING
177 if (LowPartIndex == -1) {
178 DEBUGMSGTL(("host/hr_partition", "... index out of range\n"));
179 return (MATCH_FAILED);
182 newname[HRPART_DISK_NAME_LENGTH] =
183 (HRDEV_DISK << HRDEV_TYPE_SHIFT) + LowDiskIndex;
184 newname[HRPART_ENTRY_NAME_LENGTH] = LowPartIndex;
185 memcpy((char *) name, (char *) newname,
186 ((int) vp->namelen + 2) * sizeof(oid));
187 *length = vp->namelen + 2;
189 *var_len = sizeof(long); /* default to 'long' results */
191 DEBUGMSGTL(("host/hr_partition", "... get partition stats "));
192 DEBUGMSGOID(("host/hr_partition", name, *length));
193 DEBUGMSG(("host/hr_partition", "\n"));
198 /*********************
200 * System specific implementation functions
202 *********************/
206 var_hrpartition(struct variable * vp,
209 int exact, size_t * var_len, WriteMethod ** write_method)
212 static char string[100];
213 struct stat stat_buf;
216 header_hrpartition(vp, name, length, exact, var_len, write_method);
217 if (part_idx == MATCH_FAILED)
220 if (stat(HRP_savedName, &stat_buf) == -1)
225 long_return = part_idx;
226 return (u_char *) & long_return;
228 *var_len = strlen(HRP_savedName);
229 return (u_char *) HRP_savedName;
230 case HRPART_ID: /* Use the device number */
231 sprintf(string, "0x%x", (int) stat_buf.st_rdev);
232 *var_len = strlen(string);
233 return (u_char *) string;
236 * XXX - based on single partition assumption
238 long_return = Get_FSSize(HRP_savedName);
239 return (u_char *) & long_return;
241 long_return = Get_FSIndex(HRP_savedName);
242 return (u_char *) & long_return;
244 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrpartition\n",
251 /*********************
253 * Internal implementation functions
255 *********************/
257 static int HRP_index;
260 Init_HR_Partition(void)
262 HRP_DiskIndex = Get_Next_HR_Disk();
263 if (HRP_DiskIndex != -1)
264 HRP_DiskIndex &= ((1 << HRDEV_TYPE_SHIFT) - 1);
270 Get_Next_HR_Partition(void)
275 if (HRP_DiskIndex == -1) {
280 while (Get_Next_HR_Disk_Partition(string, HRP_index) != -1) {
281 DEBUGMSGTL(("host/hr_partition",
282 "Get_Next_HR_Partition: %s (:%d)\n",
285 fd = open(string, O_RDONLY);
288 return HRP_index + 1;
289 } else if (errno == EBUSY) {
290 return HRP_index + 1;
296 * Finished with this disk, try the next
299 return (Get_Next_HR_Partition());
303 Save_HR_Partition(int disk_idx, int part_idx)
305 HRP_savedDiskIndex = disk_idx;
306 HRP_savedPartIndex = part_idx;
307 (void) Get_Next_HR_Disk_Partition(HRP_savedName, HRP_index);