31 |
#include <strings.h> |
#include <strings.h> |
32 |
#endif |
#endif |
33 |
|
|
|
#ifdef HAVE_UNISTD_H |
|
|
#include <unistd.h> |
|
|
#endif |
|
|
|
|
34 |
#ifdef HAVE_SYS_TYPES_H |
#ifdef HAVE_SYS_TYPES_H |
35 |
#include <sys/types.h> |
#include <sys/types.h> |
36 |
#endif |
#endif |
39 |
#include <sys/stat.h> |
#include <sys/stat.h> |
40 |
#endif |
#endif |
41 |
|
|
42 |
|
#ifdef HAVE_UNISTD_H |
43 |
|
#include <unistd.h> |
44 |
|
#endif |
45 |
|
|
46 |
|
#ifdef HAVE_FCNTL_H |
47 |
|
#include <fcntl.h> |
48 |
|
#endif |
49 |
|
|
50 |
|
#ifdef HAVE_SYS_VFS_H |
51 |
|
# include <sys/vfs.h> |
52 |
|
#endif |
53 |
|
|
54 |
|
#ifdef HAVE_SYS_STATVFS_H |
55 |
|
# include <sys/statvfs.h> |
56 |
|
#endif |
57 |
|
|
58 |
|
#ifdef HAVE_MNTENT_H |
59 |
|
#include <mntent.h> |
60 |
|
#endif |
61 |
|
|
62 |
|
#ifdef HAVE_SYS_MNTTAB_H |
63 |
|
# include <sys/mnttab.h> |
64 |
|
#endif |
65 |
|
|
66 |
|
|
67 |
#include "monitor.h" |
#include "monitor.h" |
68 |
|
|
69 |
|
|
70 |
|
#if defined HAVE_STATVFS |
71 |
|
#define statfs statvfs |
72 |
|
#endif |
73 |
|
|
74 |
|
|
75 |
/** |
/** |
76 |
* Utilities used for managing files used by monit. |
* Utilities used for managing files used by monit. |
77 |
* |
* |
394 |
|
|
395 |
return TRUE; |
return TRUE; |
396 |
|
|
397 |
|
} |
398 |
|
|
399 |
|
|
400 |
|
/** |
401 |
|
* This function validates if given object is valid for filesystem |
402 |
|
* informations statistics. Valid objects are file or directory that |
403 |
|
* is part of requested filesystem or block special device. In the |
404 |
|
* case of file or directory the result is original object, in the |
405 |
|
* case of block special device mountpoint is returned. In any case, |
406 |
|
* filesystem must be mounted. |
407 |
|
* @param object Identifies appropriate device object |
408 |
|
* @param path String to store result |
409 |
|
* @param path_length Defines max. length of the resulting string |
410 |
|
* @return NULL in the case of failure otherwise pointer to |
411 |
|
* resulting path |
412 |
|
*/ |
413 |
|
char *get_device(char *path, unsigned path_length, char *object) { |
414 |
|
|
415 |
|
struct stat buf; |
416 |
|
|
417 |
|
ASSERT(path && object); |
418 |
|
|
419 |
|
if(stat(object, &buf) != 0) { |
420 |
|
log("%s: Cannot stat '%s' -- %s\n", prog, object, STRERROR); |
421 |
|
return NULL; |
422 |
|
} |
423 |
|
|
424 |
|
memset(path, 0, path_length); |
425 |
|
|
426 |
|
if(S_ISREG(buf.st_mode) || S_ISDIR(buf.st_mode)) { |
427 |
|
|
428 |
|
return strncpy(path, object, path_length-1); |
429 |
|
|
430 |
|
} else if(S_ISBLK(buf.st_mode)) { |
431 |
|
|
432 |
|
FILE *mntfd; |
433 |
|
|
434 |
|
#ifdef HAVE_MTAB |
435 |
|
|
436 |
|
struct mntent *mnt; |
437 |
|
|
438 |
|
if((mntfd= setmntent("/etc/mtab", "r")) == NULL) { |
439 |
|
log("%s: Cannot open /etc/mtab file", prog); |
440 |
|
return NULL; |
441 |
|
} |
442 |
|
|
443 |
|
/* Last match is significant */ |
444 |
|
while((mnt= getmntent(mntfd)) != NULL) { |
445 |
|
|
446 |
|
if(is(object, mnt->mnt_fsname)) |
447 |
|
strncpy(path, mnt->mnt_dir, path_length-1); |
448 |
|
|
449 |
|
} |
450 |
|
|
451 |
|
endmntent(mntfd); |
452 |
|
|
453 |
|
#elif defined HAVE_MNTTAB |
454 |
|
|
455 |
|
struct mnttab mnt; |
456 |
|
|
457 |
|
if((mntfd= fopen("/etc/mnttab", "r")) == NULL) { |
458 |
|
log("%s: Cannot open /etc/mnttab file", prog); |
459 |
|
return NULL; |
460 |
|
} |
461 |
|
|
462 |
|
/* Last match is significant */ |
463 |
|
while(getmntent(mntfd, &mnt) == 0) { |
464 |
|
|
465 |
|
if(is(object, mnt.mnt_special)) |
466 |
|
strncpy(path, mnt.mnt_mountp, path_length-1); |
467 |
|
|
468 |
|
} |
469 |
|
|
470 |
|
fclose(mntfd); |
471 |
|
|
472 |
|
#else |
473 |
|
|
474 |
|
log("%s: Unsupported mounted filesystem information method", prog); |
475 |
|
return NULL; |
476 |
|
|
477 |
|
#endif |
478 |
|
|
479 |
|
return path; |
480 |
|
|
481 |
|
} |
482 |
|
|
483 |
|
log("%s: Not file, directory or block special device: '%s'", prog, object); |
484 |
|
|
485 |
|
return NULL; |
486 |
|
|
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
|
/** |
491 |
|
* Filesystem usage statistics |
492 |
|
* @param object Identifies requested device |
493 |
|
* @param buf Information structure where resulting data will be stored |
494 |
|
* @return TRUE if informations were succesfully read otherwise FALSE |
495 |
|
*/ |
496 |
|
int get_fsusage(char *object, Device_T buf) { |
497 |
|
|
498 |
|
#if (defined HAVE_STATFS) || (defined HAVE_STATVFS) |
499 |
|
|
500 |
|
struct statfs usage; |
501 |
|
|
502 |
|
#else |
503 |
|
|
504 |
|
log("%s: Unsupported filesystem informations gathering method\n", prog); |
505 |
|
|
506 |
|
return FALSE; |
507 |
|
|
508 |
|
#endif |
509 |
|
|
510 |
|
char path[STRLEN]; |
511 |
|
|
512 |
|
ASSERT(object); |
513 |
|
|
514 |
|
if(!get_device(path, STRLEN, object)) { |
515 |
|
log("%s: Error looking for filesystem '%s'\n", prog, object); |
516 |
|
return FALSE; |
517 |
|
} |
518 |
|
|
519 |
|
if(statfs(path, &usage) != 0) { |
520 |
|
log("%s: Error getting usage statistics for device '%s' -- %s\n", prog, object, STRERROR); |
521 |
|
return FALSE; |
522 |
|
} |
523 |
|
|
524 |
|
buf->f_bsize= usage.f_bsize; |
525 |
|
buf->f_blocks= usage.f_blocks; |
526 |
|
buf->f_blocksfree= usage.f_bavail; |
527 |
|
buf->f_blocksfreetotal= usage.f_bfree; |
528 |
|
buf->f_files= usage.f_files; |
529 |
|
buf->f_filesfree= usage.f_ffree; |
530 |
|
|
531 |
|
return TRUE; |
532 |
|
|
533 |
} |
} |
534 |
|
|