Blame agent/mibgroup/hardware/fsys/fsys_mntctl.c

Packit fcad23
#include <net-snmp/net-snmp-config.h>
Packit fcad23
#include <net-snmp/net-snmp-includes.h>
Packit fcad23
#include <net-snmp/agent/net-snmp-agent-includes.h>
Packit fcad23
#include <net-snmp/agent/hardware/fsys.h>
Packit fcad23
Packit fcad23
#include <stdio.h>
Packit fcad23
#if HAVE_SYS_MNTCTL_H
Packit fcad23
#include <sys/mntctl.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_VMOUNT_H
Packit fcad23
#include <sys/vmount.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_STATFS_H
Packit fcad23
#include <sys/statfs.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_STATVFS_H
Packit fcad23
#include <sys/statvfs.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
Packit fcad23
static int
Packit fcad23
_fsys_remote( char *device, int type, char *host )
Packit fcad23
{
Packit fcad23
    if (( type == NETSNMP_FS_TYPE_NFS) ||
Packit fcad23
        ( type == NETSNMP_FS_TYPE_AFS))
Packit fcad23
        return 1;
Packit fcad23
    else
Packit fcad23
        return 0;
Packit fcad23
}
Packit fcad23
Packit fcad23
static int
Packit fcad23
_fsys_type( int type)
Packit fcad23
{
Packit fcad23
    DEBUGMSGTL(("fsys:type", "Classifying %d\n", type));
Packit fcad23
Packit fcad23
    switch ( type ) {
Packit fcad23
        case  MNT_AIX:
Packit fcad23
        case  MNT_JFS:
Packit fcad23
            return NETSNMP_FS_TYPE_BERKELEY;
Packit fcad23
Packit fcad23
        case  MNT_CDROM:
Packit fcad23
            return NETSNMP_FS_TYPE_ISO9660;
Packit fcad23
Packit fcad23
        case  MNT_NFS:
Packit fcad23
        case  MNT_NFS3:
Packit fcad23
            return NETSNMP_FS_TYPE_NFS;
Packit 8e45bd
        case  MNT_AUTOFS:
Packit 8e45bd
            return NETSNMP_FS_TYPE_AUTOFS;
Packit fcad23
Packit fcad23
    /*
Packit fcad23
     *  The following code covers selected filesystems
Packit fcad23
     *    which are not covered by the HR-TYPES enumerations,
Packit fcad23
     *    but should still be monitored.
Packit fcad23
     *  These are all mapped into type "other"
Packit fcad23
     *
Packit fcad23
     */    
Packit fcad23
#ifdef MNT_NAMEFS
Packit fcad23
        case MNT_NAMEFS:
Packit fcad23
#endif
Packit fcad23
#ifdef MNT_PROCFS
Packit fcad23
        case MNT_PROCFS:
Packit fcad23
#endif
Packit fcad23
#ifdef MNT_ACFS
Packit fcad23
        case MNT_ACFS:
Packit fcad23
#endif
Packit fcad23
        case MNT_SFS:
Packit fcad23
        case MNT_CACHEFS:
Packit fcad23
            return NETSNMP_FS_TYPE_OTHER;
Packit fcad23
Packit fcad23
    /*    
Packit fcad23
     *  All other types are silently skipped
Packit fcad23
     */
Packit fcad23
        default:
Packit fcad23
            return NETSNMP_FS_TYPE_IGNORE;
Packit fcad23
    }
Packit fcad23
    return NETSNMP_FS_TYPE_IGNORE;  /* Not reached */
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
netsnmp_fsys_arch_init( void )
Packit fcad23
{
Packit fcad23
    return;
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
netsnmp_fsys_arch_load( void )
Packit fcad23
{
Packit fcad23
    int  ret  = 0, i = 0;
Packit fcad23
    uint size = 0;
Packit fcad23
Packit fcad23
    struct vmount *aixmnt, *aixcurr;
Packit fcad23
    char          *path;
Packit fcad23
    struct statfs  stat_buf;
Packit fcad23
    netsnmp_fsys_info *entry;
Packit fcad23
    char               tmpbuf[1024];
Packit fcad23
Packit fcad23
    /*
Packit fcad23
     * Retrieve information about the currently mounted filesystems...
Packit fcad23
     */
Packit fcad23
    ret = mntctl(MCTL_QUERY, sizeof(uint), (void *) &size);
Packit fcad23
    if ( ret != 0 || size<=0 ) {
Packit fcad23
        snmp_log_perror( "initial mntctl failed" );
Packit fcad23
        return;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    aixmnt = (struct vmount *)malloc( size );
Packit fcad23
    if ( aixmnt == NULL ) {
Packit fcad23
        snmp_log_perror( "cannot allocate memory for mntctl data" );
Packit fcad23
        return;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    ret = mntctl(MCTL_QUERY, size, (void *) aixmnt);
Packit fcad23
    if ( ret <= 0 ) {
Packit fcad23
        free(aixmnt);
Packit fcad23
        snmp_log_perror( "main mntctl failed" );
Packit fcad23
        return;
Packit fcad23
    }
Packit fcad23
    aixcurr = aixmnt;
Packit fcad23
Packit fcad23
Packit fcad23
    /*
Packit fcad23
     * ... and insert this into the filesystem container.
Packit fcad23
     */
Packit fcad23
Packit fcad23
    for (i = 0;
Packit fcad23
         i < ret;
Packit fcad23
         i++, aixcurr = (struct vmount *) ((char*)aixcurr + aixcurr->vmt_length) ) {
Packit fcad23
Packit fcad23
        path = vmt2dataptr( aixcurr, VMT_STUB );
Packit fcad23
        entry = netsnmp_fsys_by_path( path, NETSNMP_FS_FIND_CREATE );
Packit fcad23
        if (!entry) {
Packit fcad23
            continue;
Packit fcad23
        }
Packit fcad23
Packit fcad23
        strlcpy(entry->path, path, sizeof(entry->path));
Packit fcad23
        strlcpy(entry->device, vmt2dataptr(aixcurr, VMT_OBJECT),
Packit fcad23
                sizeof(entry->device));
Packit fcad23
        entry->type   = _fsys_type( aixcurr->vmt_gfstype );
Packit fcad23
Packit fcad23
        if (!(entry->type & _NETSNMP_FS_TYPE_SKIP_BIT))
Packit fcad23
            entry->flags |= NETSNMP_FS_FLAG_ACTIVE;
Packit fcad23
Packit fcad23
        if ( _fsys_remote( entry->device, entry->type, vmt2dataptr( aixcurr, VMT_HOST) ))
Packit fcad23
            entry->flags |= NETSNMP_FS_FLAG_REMOTE;
Packit fcad23
        if ( aixcurr->vmt_flags & MNT_READONLY )
Packit fcad23
            entry->flags |= NETSNMP_FS_FLAG_RONLY;
Packit fcad23
        /*
Packit fcad23
         *  The root device is presumably bootable.
Packit fcad23
         *  Other partitions probably aren't!
Packit fcad23
         */
Packit fcad23
        if ((entry->path[0] == '/') && (entry->path[1] == '\0'))
Packit fcad23
            entry->flags |= NETSNMP_FS_FLAG_BOOTABLE;
Packit fcad23
Packit fcad23
        /*
Packit fcad23
         *  XXX - identify removeable disks
Packit fcad23
         */
Packit fcad23
Packit fcad23
        /*
Packit fcad23
         *  Optionally skip retrieving statistics for remote mounts
Packit 8e45bd
         *  AUTOFS is skipped by default
Packit fcad23
         */
Packit 8e45bd
        if ( ((entry->flags & NETSNMP_FS_FLAG_REMOTE) &&
Packit fcad23
            netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
Packit 8e45bd
                                   NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES)) ||
Packit 8e45bd
              entry->type == (NETSNMP_FS_TYPE_AUTOFS))
Packit fcad23
            continue;
Packit fcad23
Packit fcad23
        if ( statfs( entry->path, &stat_buf ) < 0 ) {
Packit fcad23
            snprintf( tmpbuf, sizeof(tmpbuf), "Cannot statfs %s", entry->path );
Packit fcad23
            snmp_log_perror( tmpbuf );
Packit fcad23
            continue;
Packit fcad23
        }
Packit fcad23
        entry->units =  stat_buf.f_bsize;
Packit fcad23
        entry->size  =  stat_buf.f_blocks;
Packit fcad23
        entry->used  = (stat_buf.f_blocks - stat_buf.f_bfree);
Packit fcad23
        entry->avail =  stat_buf.f_bavail;
Packit fcad23
        entry->inums_total = stat_buf.f_files;
Packit fcad23
        entry->inums_avail = stat_buf.f_ffree;
Packit fcad23
        netsnmp_fsys_calculate32(entry);
Packit fcad23
    }
Packit fcad23
    free(aixmnt);
Packit fcad23
    aixmnt  = NULL;
Packit fcad23
    aixcurr = NULL;
Packit fcad23
}
Packit fcad23