| 1 |
/* |
| 2 |
* disk_hw.c |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
|
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#ifdef HAVE_STDLIB_H |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#ifdef HAVE_FCNTL_H |
| 17 |
#include <fcntl.h> |
| 18 |
#endif |
| 19 |
#include <signal.h> |
| 20 |
#ifdef TIME_WITH_SYS_TIME |
| 21 |
#include <sys/time.h> |
| 22 |
#include <time.h> |
| 23 |
#else |
| 24 |
#ifdef HAVE_SYS_TIME_H |
| 25 |
#include <sys/time.h> |
| 26 |
#else |
| 27 |
#include <time.h> |
| 28 |
#endif |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <net-snmp/net-snmp-includes.h> |
| 32 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 33 |
#include <net-snmp/agent/hardware/fsys.h> |
| 34 |
|
| 35 |
#include "struct.h" |
| 36 |
#include "disk_hw.h" |
| 37 |
#include "util_funcs/header_simple_table.h" |
| 38 |
#ifdef USING_UCD_SNMP_ERRORMIB_MODULE |
| 39 |
#include "errormib.h" |
| 40 |
#else |
| 41 |
#define setPerrorstatus(x) snmp_log_perror(x) |
| 42 |
#endif |
| 43 |
|
| 44 |
#define MAX_INT_32 0x7fffffff |
| 45 |
#define MAX_UINT_32 0xffffffff |
| 46 |
|
| 47 |
static int numdisks; |
| 48 |
static int allDisksIncluded; |
| 49 |
static int allDisksMinPercent; |
| 50 |
static int maxdisks; |
| 51 |
static netsnmp_fsys_info **disks; |
| 52 |
|
| 53 |
static const struct variable2 extensible_disk_variables[] = { |
| 54 |
{MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 55 |
var_extensible_disk, 1, {MIBINDEX}}, |
| 56 |
{ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 57 |
var_extensible_disk, 1, {ERRORNAME}}, |
| 58 |
{DISKDEVICE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 59 |
var_extensible_disk, 1, {DISKDEVICE}}, |
| 60 |
{DISKMINIMUM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 61 |
var_extensible_disk, 1, {DISKMINIMUM}}, |
| 62 |
{DISKMINPERCENT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 63 |
var_extensible_disk, 1, {DISKMINPERCENT}}, |
| 64 |
{DISKTOTAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 65 |
var_extensible_disk, 1, {DISKTOTAL}}, |
| 66 |
{DISKAVAIL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 67 |
var_extensible_disk, 1, {DISKAVAIL}}, |
| 68 |
{DISKUSED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 69 |
var_extensible_disk, 1, {DISKUSED}}, |
| 70 |
{DISKPERCENT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 71 |
var_extensible_disk, 1, {DISKPERCENT}}, |
| 72 |
{DISKPERCENTNODE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 73 |
var_extensible_disk, 1, {DISKPERCENTNODE}}, |
| 74 |
{ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 75 |
var_extensible_disk, 1, {ERRORFLAG}}, |
| 76 |
{ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 77 |
var_extensible_disk, 1, {ERRORMSG}}, |
| 78 |
{DISKTOTALLOW, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 79 |
var_extensible_disk, 1, {DISKTOTALLOW}}, |
| 80 |
{DISKTOTALHIGH, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 81 |
var_extensible_disk, 1, {DISKTOTALHIGH}}, |
| 82 |
{DISKAVAILLOW, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 83 |
var_extensible_disk, 1, {DISKAVAILLOW}}, |
| 84 |
{DISKAVAILHIGH, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 85 |
var_extensible_disk, 1, {DISKAVAILHIGH}}, |
| 86 |
{DISKUSEDLOW, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 87 |
var_extensible_disk, 1, {DISKUSEDLOW}}, |
| 88 |
{DISKUSEDHIGH, ASN_UNSIGNED, NETSNMP_OLDAPI_RONLY, |
| 89 |
var_extensible_disk, 1, {DISKUSEDHIGH}}, |
| 90 |
}; |
| 91 |
|
| 92 |
static netsnmp_fsys_info ** |
| 93 |
_expand_disk_array(char *cptr) |
| 94 |
{ |
| 95 |
int prev_max = maxdisks; |
| 96 |
netsnmp_fsys_info **new_disks; |
| 97 |
|
| 98 |
if (maxdisks == 0) |
| 99 |
maxdisks = 50; |
| 100 |
else |
| 101 |
maxdisks *= 2; |
| 102 |
|
| 103 |
new_disks = realloc(disks, maxdisks * sizeof(netsnmp_fsys_info *)); |
| 104 |
if (!new_disks) { |
| 105 |
config_perror("malloc failed for new disk allocation."); |
| 106 |
netsnmp_config_error("\tignoring: %s", cptr); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
disks = new_disks; |
| 111 |
memset(disks + prev_max, 0, (maxdisks - prev_max) * |
| 112 |
sizeof(netsnmp_fsys_info *)); |
| 113 |
|
| 114 |
return disks; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
disk_parse_config(const char *token, char *cptr) |
| 119 |
{ |
| 120 |
char path[STRMAX]; |
| 121 |
int minpercent; |
| 122 |
int minspace; |
| 123 |
netsnmp_fsys_info *entry; |
| 124 |
|
| 125 |
/* |
| 126 |
* Ensure there is space for the new entry |
| 127 |
*/ |
| 128 |
if (numdisks == maxdisks) { |
| 129 |
if (!_expand_disk_array(cptr)) |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* read disk path (eg, /1 or /usr) |
| 135 |
*/ |
| 136 |
copy_nword(cptr, path, sizeof(path)); |
| 137 |
cptr = skip_not_white(cptr); |
| 138 |
cptr = skip_white(cptr); |
| 139 |
|
| 140 |
/* |
| 141 |
* read optional minimum disk usage spec |
| 142 |
*/ |
| 143 |
if (cptr != NULL) { |
| 144 |
if (strchr(cptr, '%') == NULL) { |
| 145 |
minspace = atoi(cptr); |
| 146 |
minpercent = -1; |
| 147 |
} else { |
| 148 |
minspace = -1; |
| 149 |
minpercent = atoi(cptr); |
| 150 |
} |
| 151 |
} else { |
| 152 |
minspace = NETSNMP_DEFDISKMINIMUMSPACE; |
| 153 |
minpercent = -1; |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* check if the disk already exists, if so then modify its |
| 158 |
* parameters. if it does not exist then add it |
| 159 |
*/ |
| 160 |
entry = netsnmp_fsys_by_path(path, NETSNMP_FS_FIND_CREATE); |
| 161 |
if (entry) { |
| 162 |
entry->minspace = minspace; |
| 163 |
entry->minpercent = minpercent; |
| 164 |
entry->flags |= NETSNMP_FS_FLAG_UCD; |
| 165 |
disks[numdisks++] = entry; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
disk_parse_config_all(const char *token, char *cptr) |
| 171 |
{ |
| 172 |
int minpercent = DISKMINPERCENT; |
| 173 |
|
| 174 |
/* |
| 175 |
* read the minimum disk usage percent |
| 176 |
*/ |
| 177 |
if (cptr != NULL) { |
| 178 |
if (strchr(cptr, '%') != NULL) { |
| 179 |
minpercent = atoi(cptr); |
| 180 |
} |
| 181 |
} |
| 182 |
/* |
| 183 |
* if we have already seen the "includeAllDisks" directive |
| 184 |
* then search for the disk in the "disks" array and modify |
| 185 |
* the values. if we havent seen the "includeAllDisks" |
| 186 |
* directive then include this disk |
| 187 |
*/ |
| 188 |
if (allDisksIncluded) { |
| 189 |
config_perror("includeAllDisks already specified."); |
| 190 |
netsnmp_config_error("\tignoring: includeAllDisks %s", cptr); |
| 191 |
} else { |
| 192 |
allDisksIncluded = 1; |
| 193 |
allDisksMinPercent = minpercent; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
disk_free_config(void) |
| 199 |
{ |
| 200 |
netsnmp_fsys_info *entry; |
| 201 |
|
| 202 |
for (entry = netsnmp_fsys_get_first(); |
| 203 |
entry != NULL; entry = netsnmp_fsys_get_next(entry)) { |
| 204 |
|
| 205 |
entry->minspace = -1; |
| 206 |
entry->minpercent = -1; |
| 207 |
entry->flags &= ~NETSNMP_FS_FLAG_UCD; |
| 208 |
} |
| 209 |
if (disks) { |
| 210 |
free(disks); |
| 211 |
disks = NULL; |
| 212 |
maxdisks = numdisks = 0; |
| 213 |
} |
| 214 |
allDisksIncluded = 0; |
| 215 |
allDisksMinPercent = 0; |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
* Define the OID pointer to the top of the mib tree that we're |
| 220 |
* registering underneath |
| 221 |
*/ |
| 222 |
static const oid disk_variables_oid[] = { |
| 223 |
NETSNMP_UCDAVIS_MIB, NETSNMP_DISKMIBNUM, 1 |
| 224 |
}; |
| 225 |
|
| 226 |
void |
| 227 |
init_disk_hw(void) |
| 228 |
{ |
| 229 |
/* |
| 230 |
* register ourselves with the agent to handle our mib tree |
| 231 |
*/ |
| 232 |
REGISTER_MIB("ucd-snmp/disk", extensible_disk_variables, variable2, |
| 233 |
disk_variables_oid); |
| 234 |
|
| 235 |
snmpd_register_config_handler("disk", disk_parse_config, |
| 236 |
disk_free_config, |
| 237 |
"path [ minspace | minpercent% ]"); |
| 238 |
snmpd_register_config_handler("includeAllDisks", disk_parse_config_all, |
| 239 |
disk_free_config, "minpercent%"); |
| 240 |
allDisksIncluded = 0; |
| 241 |
allDisksMinPercent = 0; |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* add new entries to dskTable dynamically |
| 246 |
*/ |
| 247 |
static void |
| 248 |
_refresh_disks(int minpercent) |
| 249 |
{ |
| 250 |
netsnmp_fsys_info *entry; |
| 251 |
|
| 252 |
for (entry = netsnmp_fsys_get_first(); |
| 253 |
entry != NULL; entry = netsnmp_fsys_get_next(entry)) { |
| 254 |
|
| 255 |
if (!(entry->flags & NETSNMP_FS_FLAG_UCD)) { |
| 256 |
/* |
| 257 |
* this is new disk, add it to the table |
| 258 |
*/ |
| 259 |
entry->minspace = -1; |
| 260 |
entry->minpercent = minpercent; |
| 261 |
entry->flags |= NETSNMP_FS_FLAG_UCD; |
| 262 |
/* |
| 263 |
* Ensure there is space for the new entry |
| 264 |
*/ |
| 265 |
if (numdisks == maxdisks) { |
| 266 |
if (!_expand_disk_array(entry->device)) |
| 267 |
return; |
| 268 |
} |
| 269 |
disks[numdisks++] = entry; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static int |
| 275 |
_percent(unsigned long long value, unsigned long long total) |
| 276 |
{ |
| 277 |
float v = value, t = total, pct; |
| 278 |
|
| 279 |
/* |
| 280 |
* avoid division by zero |
| 281 |
*/ |
| 282 |
if (total == 0) |
| 283 |
return 0; |
| 284 |
|
| 285 |
pct = (v * 100) / t; /* Calculate percentage using floating point |
| 286 |
* arithmetic, to avoid overflow errors */ |
| 287 |
pct += 0.5; /* rounding */ |
| 288 |
return (int) pct; |
| 289 |
} |
| 290 |
|
| 291 |
/* |
| 292 |
* var_extensible_disk(... |
| 293 |
* Arguments: |
| 294 |
* vp IN - pointer to variable entry that points here |
| 295 |
* name IN/OUT - IN/name requested, OUT/name found |
| 296 |
* length IN/OUT - length of IN/OUT oid's |
| 297 |
* exact IN - TRUE if an exact match was requested |
| 298 |
* var_len OUT - length of variable or 0 if function returned |
| 299 |
* write_method |
| 300 |
* |
| 301 |
*/ |
| 302 |
u_char * |
| 303 |
var_extensible_disk(struct variable *vp, |
| 304 |
oid * name, |
| 305 |
size_t *length, |
| 306 |
int exact, |
| 307 |
size_t *var_len, WriteMethod ** write_method) |
| 308 |
{ |
| 309 |
int disknum = 0; |
| 310 |
const netsnmp_fsys_info *entry; |
| 311 |
unsigned long long val; |
| 312 |
static long long_ret; |
| 313 |
static char *errmsg; |
| 314 |
static char empty_str[1]; |
| 315 |
netsnmp_cache *cache; |
| 316 |
|
| 317 |
/* |
| 318 |
* Update the fsys H/W module |
| 319 |
*/ |
| 320 |
cache = netsnmp_fsys_get_cache(); |
| 321 |
netsnmp_cache_check_and_reload(cache); |
| 322 |
if (allDisksIncluded) |
| 323 |
_refresh_disks(allDisksMinPercent); |
| 324 |
|
| 325 |
tryAgain: |
| 326 |
if (header_simple_table |
| 327 |
(vp, name, length, exact, var_len, write_method, numdisks)) |
| 328 |
return (NULL); |
| 329 |
disknum = name[*length - 1] - 1; |
| 330 |
entry = disks[disknum]; |
| 331 |
if (!entry) { |
| 332 |
if (exact) |
| 333 |
return NULL; |
| 334 |
goto tryAgain; |
| 335 |
} |
| 336 |
if (!(entry->flags & NETSNMP_FS_FLAG_ACTIVE) |
| 337 |
|| !(entry->flags & NETSNMP_FS_FLAG_UCD)) { |
| 338 |
if (exact) |
| 339 |
return NULL; |
| 340 |
goto tryAgain; |
| 341 |
} |
| 342 |
|
| 343 |
switch (vp->magic) { |
| 344 |
case MIBINDEX: |
| 345 |
long_ret = disknum + 1; |
| 346 |
return ((u_char *) (&long_ret)); |
| 347 |
case ERRORNAME: /* DISKPATH */ |
| 348 |
*var_len = strlen(entry->path); |
| 349 |
return (u_char *)NETSNMP_REMOVE_CONST(char *, entry->path); |
| 350 |
case DISKDEVICE: |
| 351 |
*var_len = strlen(entry->device); |
| 352 |
return (u_char *)NETSNMP_REMOVE_CONST(char *, entry->device); |
| 353 |
case DISKMINIMUM: |
| 354 |
long_ret = entry->minspace; |
| 355 |
return ((u_char *) (&long_ret)); |
| 356 |
case DISKMINPERCENT: |
| 357 |
long_ret = entry->minpercent; |
| 358 |
return ((u_char *) (&long_ret)); |
| 359 |
|
| 360 |
case DISKTOTAL: |
| 361 |
val = netsnmp_fsys_size_ull(entry); |
| 362 |
if (val > MAX_INT_32) |
| 363 |
long_ret = MAX_INT_32; |
| 364 |
else |
| 365 |
long_ret = (long) val; |
| 366 |
return ((u_char *) (&long_ret)); |
| 367 |
case DISKTOTALLOW: |
| 368 |
long_ret = netsnmp_fsys_size_ull(entry) & MAX_UINT_32; |
| 369 |
return ((u_char *) (&long_ret)); |
| 370 |
case DISKTOTALHIGH: |
| 371 |
long_ret = netsnmp_fsys_size_ull(entry) >> 32; |
| 372 |
return ((u_char *) (&long_ret)); |
| 373 |
|
| 374 |
case DISKAVAIL: |
| 375 |
val = netsnmp_fsys_avail_ull(entry); |
| 376 |
if (val > MAX_INT_32) |
| 377 |
long_ret = MAX_INT_32; |
| 378 |
else |
| 379 |
long_ret = (long) val; |
| 380 |
return ((u_char *) (&long_ret)); |
| 381 |
case DISKAVAILLOW: |
| 382 |
long_ret = netsnmp_fsys_avail_ull(entry) & MAX_UINT_32; |
| 383 |
return ((u_char *) (&long_ret)); |
| 384 |
case DISKAVAILHIGH: |
| 385 |
long_ret = netsnmp_fsys_avail_ull(entry) >> 32; |
| 386 |
return ((u_char *) (&long_ret)); |
| 387 |
|
| 388 |
case DISKUSED: |
| 389 |
val = netsnmp_fsys_used_ull(entry); |
| 390 |
if (val > MAX_INT_32) |
| 391 |
long_ret = MAX_INT_32; |
| 392 |
else |
| 393 |
long_ret = (long) val; |
| 394 |
return ((u_char *) (&long_ret)); |
| 395 |
case DISKUSEDLOW: |
| 396 |
long_ret = netsnmp_fsys_used_ull(entry) & MAX_UINT_32; |
| 397 |
return ((u_char *) (&long_ret)); |
| 398 |
case DISKUSEDHIGH: |
| 399 |
long_ret = netsnmp_fsys_used_ull(entry) >> 32; |
| 400 |
return ((u_char *) (&long_ret)); |
| 401 |
|
| 402 |
case DISKPERCENT: |
| 403 |
long_ret = _percent(entry->used, entry->size); |
| 404 |
return ((u_char *) (&long_ret)); |
| 405 |
|
| 406 |
case DISKPERCENTNODE: |
| 407 |
long_ret = |
| 408 |
_percent(entry->inums_total - entry->inums_avail, |
| 409 |
entry->inums_total); |
| 410 |
return ((u_char *) (&long_ret)); |
| 411 |
|
| 412 |
case ERRORFLAG: |
| 413 |
long_ret = 0; |
| 414 |
val = netsnmp_fsys_avail_ull(entry); |
| 415 |
if ((entry->minspace >= 0) && (val < entry->minspace)) |
| 416 |
long_ret = 1; |
| 417 |
else if ((entry->minpercent >= 0) && |
| 418 |
(_percent(entry->avail, entry->size) < entry->minpercent)) |
| 419 |
long_ret = 1; |
| 420 |
return ((u_char *) (&long_ret)); |
| 421 |
|
| 422 |
case ERRORMSG: |
| 423 |
free(errmsg); |
| 424 |
errmsg = NULL; |
| 425 |
*var_len = 0; |
| 426 |
val = netsnmp_fsys_avail_ull(entry); |
| 427 |
if ((entry->minspace >= 0 && val < entry->minspace && |
| 428 |
asprintf(&errmsg, "%s: less than %d free (= %d)", entry->path, |
| 429 |
entry->minspace, (int) val) >= 0) || |
| 430 |
(entry->minpercent >= 0 && |
| 431 |
_percent(entry->avail, entry->size) < entry->minpercent && |
| 432 |
asprintf(&errmsg, "%s: less than %d%% free (= %d%%)", |
| 433 |
entry->path, entry->minpercent, |
| 434 |
_percent(entry->avail, entry->size)) |
| 435 |
>= 0)) { |
| 436 |
*var_len = strlen(errmsg); |
| 437 |
} |
| 438 |
return (u_char *) (errmsg ? errmsg : empty_str); |
| 439 |
} |
| 440 |
return NULL; |
| 441 |
} |