47 |
#include <fcntl.h> |
#include <fcntl.h> |
48 |
#endif |
#endif |
49 |
|
|
|
#ifdef HAVE_SYS_VFS_H |
|
|
# include <sys/vfs.h> |
|
|
#endif |
|
|
|
|
|
#ifdef HAVE_SYS_STATVFS_H |
|
|
# include <sys/statvfs.h> |
|
|
#endif |
|
|
|
|
|
#ifdef HAVE_MNTENT_H |
|
|
#include <mntent.h> |
|
|
#endif |
|
|
|
|
|
#ifdef HAVE_SYS_MNTTAB_H |
|
|
# include <sys/mnttab.h> |
|
|
#endif |
|
|
|
|
|
|
|
50 |
#include "monitor.h" |
#include "monitor.h" |
51 |
|
|
|
|
|
|
#if defined HAVE_STATVFS |
|
|
#define statfs statvfs |
|
|
#endif |
|
|
|
|
|
#if defined HAVE_SYS_PARAM_H |
|
|
#include <sys/param.h> |
|
|
#endif |
|
|
|
|
|
#if defined HAVE_SYS_MOUNT_H |
|
|
#include <sys/mount.h> |
|
|
#endif |
|
|
|
|
52 |
/** |
/** |
53 |
* Utilities used for managing files used by monit. |
* Utilities used for managing files used by monit. |
54 |
* |
* |
368 |
|
|
369 |
return TRUE; |
return TRUE; |
370 |
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* This function validates if given object is valid for filesystem |
|
|
* informations statistics. Valid objects are file or directory that |
|
|
* is part of requested filesystem or block special device. In the |
|
|
* case of file or directory the result is original object, in the |
|
|
* case of block special device mountpoint is returned. In any case, |
|
|
* filesystem must be mounted. |
|
|
* @param path String to store result |
|
|
* @param path_length Defines max. length of the resulting string |
|
|
* @param object Identifies appropriate device object |
|
|
* @return NULL in the case of failure otherwise pointer to |
|
|
* resulting path |
|
|
*/ |
|
|
char *get_device(char *path, unsigned path_length, char *object) { |
|
|
|
|
|
struct stat buf; |
|
|
|
|
|
ASSERT(path && object); |
|
|
|
|
|
if(stat(object, &buf) != 0) { |
|
|
log("%s: Cannot stat '%s' -- %s\n", prog, object, STRERROR); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
memset(path, '\0', path_length); |
|
|
|
|
|
if(S_ISREG(buf.st_mode) || S_ISDIR(buf.st_mode)) { |
|
|
|
|
|
return strncpy(path, object, path_length-1); |
|
|
|
|
|
} else if(S_ISBLK(buf.st_mode)) { |
|
|
|
|
|
FILE *mntfd; |
|
|
|
|
|
#ifdef HAVE_MTAB |
|
|
|
|
|
struct mntent *mnt; |
|
|
|
|
|
if((mntfd= setmntent("/etc/mtab", "r")) == NULL) { |
|
|
log("%s: Cannot open /etc/mtab file", prog); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/* Last match is significant */ |
|
|
while((mnt= getmntent(mntfd)) != NULL) { |
|
|
|
|
|
if(IS(object, mnt->mnt_fsname)) |
|
|
strncpy(path, mnt->mnt_dir, path_length-1); |
|
|
|
|
|
} |
|
|
|
|
|
endmntent(mntfd); |
|
|
|
|
|
#elif defined HAVE_MNTTAB |
|
|
|
|
|
struct mnttab mnt; |
|
|
|
|
|
if((mntfd= fopen("/etc/mnttab", "r")) == NULL) { |
|
|
log("%s: Cannot open /etc/mnttab file", prog); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/* Last match is significant */ |
|
|
while(getmntent(mntfd, &mnt) == 0) { |
|
|
|
|
|
if(IS(object, mnt.mnt_special)) |
|
|
strncpy(path, mnt.mnt_mountp, path_length-1); |
|
|
|
|
|
} |
|
|
|
|
|
fclose(mntfd); |
|
|
|
|
|
#else |
|
|
|
|
|
log("%s: Unsupported mounted filesystem information method", prog); |
|
|
return NULL; |
|
|
|
|
|
#endif |
|
|
|
|
|
return path; |
|
|
|
|
|
} |
|
|
|
|
|
log("%s: Not file, directory or block special device: '%s'", prog, object); |
|
|
|
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Filesystem usage statistics |
|
|
* @param object Identifies requested device |
|
|
* @param buf Information structure where resulting data will be stored |
|
|
* @return TRUE if informations were succesfully read otherwise FALSE |
|
|
*/ |
|
|
int get_fsusage(char *object, DeviceInfo_T buf) { |
|
|
|
|
|
#if (defined HAVE_STATFS) || (defined HAVE_STATVFS) |
|
|
|
|
|
struct statfs usage; |
|
|
|
|
|
#else |
|
|
|
|
|
log("%s: Unsupported filesystem informations gathering method\n", prog); |
|
|
|
|
|
return FALSE; |
|
|
|
|
|
#endif |
|
|
|
|
|
char path[STRLEN]; |
|
|
|
|
|
ASSERT(object); |
|
|
ASSERT(buf); |
|
|
|
|
|
if(!get_device(path, STRLEN, object)) { |
|
|
log("%s: Error looking for filesystem '%s'\n", prog, object); |
|
|
return FALSE; |
|
|
} |
|
|
|
|
|
if(statfs(path, &usage) != 0) { |
|
|
log("%s: Error getting usage statistics for device '%s' -- %s\n", |
|
|
prog, object, STRERROR); |
|
|
return FALSE; |
|
|
} |
|
|
|
|
|
buf->f_bsize= usage.f_bsize; |
|
|
buf->f_blocks= usage.f_blocks; |
|
|
buf->f_blocksfree= usage.f_bavail; |
|
|
buf->f_blocksfreetotal= usage.f_bfree; |
|
|
buf->f_files= usage.f_files; |
|
|
buf->f_filesfree= usage.f_ffree; |
|
|
|
|
|
return TRUE; |
|
|
|
|
371 |
} |
} |
372 |
|
|