disk.patch   [plain text]


diff -I '\$Id: ' -u -r -b -w -p -d --new-file --exclude-from=/Users/rstory/.rcfiles/diff-ignore SVN/agent/mibgroup/host/hr_disk.c APPLE/agent/mibgroup/host/hr_disk.c
--- SVN/agent/mibgroup/host/hr_disk.c
+++ APPLE/agent/mibgroup/host/hr_disk.c
@@ -65,6 +65,19 @@
 #include <limits.h>
 #endif
 
+#ifdef darwin
+#include <paths.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/storage/IOBlockStorageDriver.h>
+#include <IOKit/storage/IOMedia.h>
+#include <IOKit/storage/IOBDMedia.h>
+#include <IOKit/storage/IOStorageProtocolCharacteristics.h>
+#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
+#include <IOKit/IOBSD.h>
+#include <DiskArbitration/DADisk.h>
+#endif
+
 #ifdef linux
 /*
  * define BLKGETSIZE from <linux/fs.h>:
@@ -79,6 +92,27 @@
 
 #define HRD_MONOTONICALLY_INCREASING
 
+/*************************************************************
+ * constants for enums for the MIB node
+ * hrDiskStorageAccess (INTEGER / ASN_INTEGER)
+ */
+#define HRDISKSTORAGEACCESS_READWRITE  1
+#define HRDISKSTORAGEACCESS_READONLY  2
+
+
+/*************************************************************
+ * constants for enums for the MIB node
+ * hrDiskStorageMedia (INTEGER / ASN_INTEGER)
+ */
+#define HRDISKSTORAGEMEDIA_OTHER  1
+#define HRDISKSTORAGEMEDIA_UNKNOWN  2
+#define HRDISKSTORAGEMEDIA_HARDDISK  3
+#define HRDISKSTORAGEMEDIA_FLOPPYDISK  4
+#define HRDISKSTORAGEMEDIA_OPTICALDISKROM  5
+#define HRDISKSTORAGEMEDIA_OPTICALDISKWORM  6
+#define HRDISKSTORAGEMEDIA_OPTICALDISKRW  7
+#define HRDISKSTORAGEMEDIA_RAMDISK  8
+
         /*********************
 	 *
 	 *  Kernel & interface information,
@@ -132,6 +166,19 @@
 static struct disklabel HRD_info;
 #endif
 
+#ifdef darwin
+static int64_t  HRD_cap;
+static int      HRD_access;
+static int      HRD_type;
+static int      HRD_removable;
+static char     HRD_model[40];
+static int      HRD_saved_access;
+static int      HRD_saved_type;
+static int      HRD_saved_removable;
+static int _get_type_from_protocol( const char *prot );
+static int _get_type_value( const char *str_type );
+#endif
+
 static void     parse_disk_config(const char *, char *);
 static void     free_disk_config(void);
 
@@ -185,6 +232,8 @@
                       "/dev/rdsk/c%dt%dd0s0", 0, 7);
     Add_HR_Disk_entry("/dev/rdsk/c%dd%ds%d", 0, 7, 0, 15,
                       "/dev/rdsk/c%dd%ds0", 0, 7);
+#elif defined(darwin)
+    Add_HR_Disk_entry("/dev/disk%ds%d", -1, -1, 0, 32, "/dev/disk%d", 1, 32);
 #elif defined(freebsd4) || defined(freebsd5)
     Add_HR_Disk_entry("/dev/ad%ds%d%c", 0, 1, 1, 4, "/dev/ad%ds%d", 'a', 'h');
     Add_HR_Disk_entry("/dev/da%ds%d%c", 0, 1, 1, 4, "/dev/da%ds%d", 'a', 'h');
@@ -484,6 +533,7 @@
     Init_HR_Disk();
     for (;;) {
         disk_idx = Get_Next_HR_Disk();
+        DEBUGMSGTL(("host/hr_disk", "... index %d\n", disk_idx));
         if (disk_idx == -1)
             break;
         newname[HRDISK_ENTRY_NAME_LENGTH] = disk_idx;
@@ -749,6 +799,12 @@
                     HRD_history[iindex] = 0;
                     return ((HRDEV_DISK << HRDEV_TYPE_SHIFT) + iindex);
                 }
+                DEBUGMSGTL(("host/hr_disk",
+                            "Get_Next_HR_Disk: can't query %s\n", string));
+            }
+            else {
+                DEBUGMSGTL(("host/hr_disk",
+                            "Get_Next_HR_Disk: can't open %s\n", string));
             }
             HRD_history[iindex] = now;
             HRD_index++;
@@ -801,6 +857,55 @@
     return 0;
 }
 
+#ifdef darwin
+int
+Get_HR_Disk_Label(char *string, size_t str_len, const char *devfull)
+{
+    io_service_t    disk;
+    const char     *name;
+    CFDictionaryRef desc;
+
+    DEBUGMSGTL(("host/hr_disk", "Disk Label type %s\n", devfull));
+
+    if (strncmp(devfull, _PATH_DEV, strlen(_PATH_DEV))) {
+        snmp_log(LOG_ERR,
+                 "diskmgr: devfull matches _PATH_DEV for %s, skipping\n",
+                 devfull);
+        return -1;
+    }
+
+    disk = IOServiceGetMatchingService(kIOMasterPortDefault, IOBSDNameMatching(kIOMasterPortDefault, 0, devfull + strlen(_PATH_DEV)));
+    if (MACH_PORT_NULL == disk) {
+        strlcpy(string, devfull, str_len);
+        return -1;
+    }
+
+    /** model */
+    desc = (CFDictionaryRef)
+        IORegistryEntrySearchCFProperty(disk, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, kIORegistryIterateParents | kIORegistryIterateRecursively);
+    if (desc) {
+        CFStringRef         str_ref;
+        CFStringEncoding    sys_encoding = CFStringGetSystemEncoding();
+        str_ref = (CFStringRef)
+            CFDictionaryGetValue(desc, CFSTR(kIOPropertyProductNameKey));
+        if (str_ref) {
+            name =
+               CFStringGetCStringPtr(str_ref, sys_encoding);
+            strlcpy(HRD_model, name, sizeof(HRD_model));
+            DEBUGMSGTL(("verbose:diskmgr:darwin", " model %s\n", HRD_model));
+        }
+        CFRelease(desc);
+    } 
+    else {
+        strlcpy(string, devfull, str_len);
+    }
+
+    IOObjectRelease(disk);
+    
+    return 0;
+}
+#endif
+
 static void
 Save_HR_Disk_Specific(void)
 {
@@ -822,6 +927,13 @@
 #ifdef DIOCGDINFO
     HRD_savedCapacity = HRD_info.d_secperunit / 2;
 #endif
+#ifdef darwin
+    HRD_savedCapacity = HRD_cap / 1024;
+    HRD_saved_access = HRD_access;
+    HRD_saved_type = HRD_type;
+    HRD_saved_removable = HRD_removable;
+#endif
+
 }
 
 static void
@@ -845,6 +957,11 @@
                     sizeof(HRD_savedModel)-1);
     HRD_savedModel[ sizeof(HRD_savedModel)-1 ] = 0;
 #endif
+#ifdef darwin
+    strncpy(HRD_savedModel, HRD_model,
+                    sizeof(HRD_savedModel)-1);
+    HRD_savedModel[ sizeof(HRD_savedModel)-1 ] = 0;
+#endif
 }
 
 static const char *
@@ -900,6 +1017,121 @@
     result = ioctl(fd, DIOCGDINFO, &HRD_info);
 #endif
 
+#ifdef darwin
+    io_service_t        disk;
+    const char         *name;
+    CFDictionaryRef     desc;
+    CFStringRef         str_ref;
+    CFNumberRef         number_ref;
+    CFBooleanRef        bool_ref;
+    CFStringEncoding    sys_encoding = CFStringGetSystemEncoding();
+
+    if (strncmp(devfull, _PATH_DEV, strlen(_PATH_DEV))) {
+        snmp_log(LOG_ERR,
+                 "diskmgr: devfull matches _PATH_DEV for %s, skipping\n",
+                 devfull);
+        return -1;
+    }
+
+    disk = IOServiceGetMatchingService(kIOMasterPortDefault, IOBSDNameMatching(kIOMasterPortDefault, 0, devfull + strlen(_PATH_DEV)));
+    if (MACH_PORT_NULL == disk) {
+        snmp_log(LOG_ERR,
+                 "diskmgr: couldn't get matching service for %s, skipping\n",
+                 devfull);
+        return -1;
+    }
+
+    number_ref = (CFNumberRef)
+        IORegistryEntryCreateCFProperty(disk, CFSTR(kIOMediaSizeKey), kCFAllocatorDefault, 0);
+    if (number_ref) {
+        CFNumberGetValue(number_ref, kCFNumberSInt64Type, &HRD_cap);
+        CFRelease(number_ref);
+    }
+    else
+        HRD_cap = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " size %lld\n", HRD_cap));
+
+    /** writable?  */
+    bool_ref = (CFBooleanRef)
+        IORegistryEntryCreateCFProperty(disk, CFSTR(kIOMediaWritableKey), kCFAllocatorDefault, 0);
+    if (bool_ref) {
+        HRD_access = CFBooleanGetValue(bool_ref);
+        CFRelease(bool_ref);
+    }
+    else
+        HRD_access = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " writable %d\n",
+                HRD_access));
+
+    /** removable?  */
+    bool_ref = (CFBooleanRef)
+        IORegistryEntryCreateCFProperty(disk, CFSTR(kIOMediaRemovableKey), kCFAllocatorDefault, 0);
+    if (bool_ref) {
+        HRD_removable = CFBooleanGetValue(bool_ref);
+        CFRelease(bool_ref);
+    }
+    else
+        HRD_removable = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " removable %d\n",
+                HRD_removable));
+
+    /** get type */
+    str_ref = (CFStringRef)
+        IORegistryEntryCreateCFProperty(disk, CFSTR(kIOBDMediaTypeKey), kCFAllocatorDefault, 0);
+    if (str_ref) {
+        HRD_type = _get_type_value(CFStringGetCStringPtr(str_ref,
+                                                         sys_encoding));
+        DEBUGMSGTL(("verbose:diskmgr:darwin", " type %s / %d\n",
+                    CFStringGetCStringPtr(str_ref, sys_encoding),
+                    HRD_type));
+        CFRelease(str_ref);
+    }    else {
+        desc = (CFDictionaryRef)
+            IORegistryEntrySearchCFProperty(disk, kIOServicePlane, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kIORegistryIterateParents | kIORegistryIterateRecursively);
+        if (desc) {
+            str_ref = (CFStringRef)
+                CFDictionaryGetValue(desc, CFSTR(kIOPropertyPhysicalInterconnectTypeKey));
+            if (str_ref) {
+                HRD_type =
+                   _get_type_from_protocol(CFStringGetCStringPtr(str_ref,
+                                                                  sys_encoding));
+                DEBUGMSGTL(("verbose:diskmgr:darwin", " type %s / %d\n",
+                            CFStringGetCStringPtr(str_ref, sys_encoding),
+                            HRD_type));
+            }
+            else {
+                DEBUGMSGTL(("verbose:diskmgr:darwin", " could not get type from description '%s'\n",
+                            desc));
+                HRD_type = HRDISKSTORAGEMEDIA_UNKNOWN;
+            }
+            CFRelease(desc);
+        }
+        else {
+            DEBUGMSGTL(("verbose:diskmgr:darwin", " could not get description to look up type\n"));
+            HRD_type = HRDISKSTORAGEMEDIA_UNKNOWN;
+        }
+    }
+
+    /** model */
+    desc = (CFDictionaryRef)
+        IORegistryEntrySearchCFProperty(disk, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, kIORegistryIterateParents | kIORegistryIterateRecursively);
+    if (desc) {
+        str_ref = (CFStringRef)
+            CFDictionaryGetValue(desc, CFSTR(kIOPropertyProductNameKey));
+        if (str_ref) {
+            name =
+               CFStringGetCStringPtr(str_ref, sys_encoding);
+            strlcpy(HRD_model, name, sizeof(HRD_model));
+            DEBUGMSGTL(("verbose:diskmgr:darwin", " model %s\n", HRD_model));
+        }
+        CFRelease(desc);
+    } 
+    else
+        HRD_model[0] = 0;
+    IOObjectRelease(disk);
+    result = 0;
+#endif
+
     return (result);
 }
 
@@ -918,6 +1150,11 @@
         return (2);             /* read only */
 #endif
 
+#ifdef darwin
+    if (!HRD_access)
+        return (2);
+#endif
+
     return (1);                 /* read-write */
 }
 
@@ -990,6 +1227,9 @@
     }
 #endif
 
+#ifdef darwin
+    return HRD_type;
+#endif
 
     return (2);                 /* Unknown */
 }
@@ -1020,5 +1260,63 @@
         return (1);             /* true */
 #endif
 
+#ifdef darwin
+    if (HRD_removable)
+        return (1);
+#endif
+
     return (2);                 /* false */
 }
+
+#ifdef darwin
+typedef struct type_value_map_s {
+     const char *type;
+     uint32_t    value;
+} type_value_map;
+
+static type_value_map media_type_map[] = {
+    { "CD-ROM", HRDISKSTORAGEMEDIA_OPTICALDISKROM},
+    { "DVD-R", HRDISKSTORAGEMEDIA_OPTICALDISKWORM},
+    { "DVD+R", HRDISKSTORAGEMEDIA_OPTICALDISKWORM},
+};  
+static int media_types = sizeof(media_type_map)/sizeof(media_type_map[0]);
+
+static int
+_get_type_value( const char *str_type )
+{
+    int           i, len;
+    
+    if (NULL == str_type)
+        return HRDISKSTORAGEMEDIA_UNKNOWN;
+
+    len = strlen(str_type);
+    for(i=0; i < media_types; ++i) {
+        if (0 == strcmp(media_type_map[i].type, str_type))
+            return media_type_map[i].value;
+    }
+
+    return HRDISKSTORAGEMEDIA_UNKNOWN;
+}
+
+static type_value_map proto_map[] = {
+    { "ATA", HRDISKSTORAGEMEDIA_HARDDISK},
+    { "ATAPI", HRDISKSTORAGEMEDIA_OPTICALDISKROM}
+};
+static int proto_maps = sizeof(proto_map)/sizeof(proto_map[0]);
+
+static int _get_type_from_protocol( const char *prot )
+{   
+    int           i, len;
+
+    if (NULL == prot)
+        return TV_FALSE;
+
+    len = strlen(prot);
+    for(i=0; i < proto_maps; ++i) {
+        if (0 == strcmp(proto_map[i].type, prot))
+            return proto_map[i].value;
+    }
+
+    return HRDISKSTORAGEMEDIA_UNKNOWN;
+}
+#endif
diff -I '\$Id: ' -u -r -b -w -p -d --new-file --exclude-from=/Users/rstory/.rcfiles/diff-ignore SVN/agent/mibgroup/host/hr_filesys.c APPLE/agent/mibgroup/host/hr_filesys.c
--- SVN/agent/mibgroup/host/hr_filesys.c
+++ APPLE/agent/mibgroup/host/hr_filesys.c
@@ -4,9 +4,13 @@
  */
 
 #include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+#include <net-snmp/agent/hardware/memory.h>
 #include "host_res.h"
 #include "hr_filesys.h"
 #include "hr_storage.h"
+#include "hr_disk.h"
 #include <net-snmp/utilities.h>
 
 #if HAVE_MNTENT_H
@@ -535,7 +539,7 @@ var_hrfilesys(struct variable *vp,
             long_return = 2;    /* others probably aren't */
         return (u_char *) & long_return;
     case HRFSYS_STOREIDX:
-        long_return = fsys_idx + HRS_TYPE_FIXED_MAX;
+        long_return = fsys_idx + NETSNMP_MEM_TYPE_MAX;
         return (u_char *) & long_return;
     case HRFSYS_FULLDUMP:
         return when_dumped(HRFS_entry->HRFS_name, FULL_DUMP, var_len);
diff -I '\$Id: ' -u -r -b -w -p -d --new-file --exclude-from=/Users/rstory/.rcfiles/diff-ignore SVN/agent/mibgroup/host/hr_partition.c APPLE/agent/mibgroup/host/hr_partition.c
--- SVN/agent/mibgroup/host/hr_partition.c
+++ APPLE/agent/mibgroup/host/hr_partition.c
@@ -35,6 +35,9 @@
 static int      HRP_savedDiskIndex;
 static int      HRP_savedPartIndex;
 static char     HRP_savedName[1024];
+#ifdef NETSNMP_CAN_GET_DISK_LABEL
+static char     HRP_savedLabel[1024];
+#endif
 
 static int      HRP_DiskIndex;
 
@@ -135,6 +138,7 @@ header_hrpartition(struct variable *vp,
             (name[HRPART_DISK_NAME_LENGTH] &
              ((1 << HRDEV_TYPE_SHIFT) - 1));
 
+        DEBUGMSGTL(("host/hr_partition", "... low index %d\n", LowDiskIndex));
         while (HRP_DiskIndex < LowDiskIndex) {
             Init_HR_Partition();        /* moves to next disk */
             if (HRP_DiskIndex == -1)
@@ -144,6 +148,7 @@ header_hrpartition(struct variable *vp,
 
     for (;;) {
         part_idx = Get_Next_HR_Partition();
+        DEBUGMSGTL(("host/hr_partition", "... part index %d\n", part_idx));
         if (part_idx == 0)
             break;
         newname[HRPART_DISK_NAME_LENGTH] =
@@ -225,8 +230,13 @@ var_hrpartition(struct variable * vp,
         long_return = part_idx;
         return (u_char *) & long_return;
     case HRPART_LABEL:
+#ifdef NETSNMP_CAN_GET_DISK_LABEL
+        *var_len = strlen(HRP_savedLabel);
+        return (u_char *) HRP_savedLabel;
+#else
         *var_len = strlen(HRP_savedName);
         return (u_char *) HRP_savedName;
+#endif
     case HRPART_ID:            /* Use the device number */
         sprintf(string, "0x%x", (int) stat_buf.st_rdev);
         *var_len = strlen(string);
@@ -259,9 +269,11 @@ static int      HRP_index;
 static void
 Init_HR_Partition(void)
 {
+    DEBUGMSGTL(("host/hr_partition", "Init_HR_Partition\n"));
     HRP_DiskIndex = Get_Next_HR_Disk();
     if (HRP_DiskIndex != -1)
         HRP_DiskIndex &= ((1 << HRDEV_TYPE_SHIFT) - 1);
+    DEBUGMSGTL(("host/hr_partition", "...  %d\n",HRP_DiskIndex));
 
     HRP_index = -1;
 }
@@ -272,6 +284,7 @@ Get_Next_HR_Partition(void)
     char            string[1024];
     int             fd;
 
+    DEBUGMSGTL(("host/hr_partition", "Get_Next_HR_Partition %d\n",HRP_DiskIndex));
     if (HRP_DiskIndex == -1) {
         return 0;
     }
@@ -309,4 +322,7 @@ Save_HR_Partition(int disk_idx, int part
     HRP_savedDiskIndex = disk_idx;
     HRP_savedPartIndex = part_idx;
     (void) Get_Next_HR_Disk_Partition(HRP_savedName, sizeof(HRP_savedName), HRP_index);
+#ifdef NETSNMP_CAN_GET_DISK_LABEL
+    (void) Get_HR_Disk_Label(HRP_savedLabel, sizeof(HRP_savedLabel), HRP_savedName);
+#endif
 }
diff -I '\$Id: ' -u -r -b -w -p -d --new-file --exclude-from=/Users/rstory/.rcfiles/diff-ignore SVN/agent/mibgroup/host/hr_partition.h APPLE/agent/mibgroup/host/hr_partition.h