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,15 @@
 #include <limits.h>
 #endif
 
+#ifdef darwin
+#include <CoreFoundation/CoreFoundation.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/storage/IOBlockStorageDriver.h>
+#include <IOKit/storage/IOMedia.h>
+#include <IOKit/IOBSD.h>
+#include <DiskArbitration/DADisk.h>
+#endif
+
 #ifdef linux
 /*
  * define BLKGETSIZE from <linux/fs.h>:
@@ -79,6 +88,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 +162,19 @@ static struct hd_driveid HRD_info;
 static struct disklabel HRD_info;
 #endif
 
+#ifdef darwin
+static int64_t  HRD_cap;
+static int      HRD_access;
+static int      HRD_type;
+static int      HRD_removeble;
+static char     HRD_model[40];
+static int      HRD_saved_access;
+static int      HRD_saved_type;
+static int      HRD_saved_removeble;
+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 +228,8 @@ init_hr_disk(void)
                       "/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 +529,7 @@ header_hrdisk(struct variable *vp,
     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 +795,12 @@ Get_Next_HR_Disk(void)
                     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 +853,66 @@ Get_Next_HR_Disk_Partition(char *string,
     return 0;
 }
 
+#ifdef darwin
+int
+Get_HR_Disk_Label(char *string, size_t str_len, const char *devfull)
+{
+    DASessionRef        sess_ref;
+    DADiskRef           disk;
+    CFDictionaryRef     desc;
+    CFStringRef         str_ref;
+    CFStringEncoding    sys_encoding = CFStringGetSystemEncoding();
+
+    DEBUGMSGTL(("host/hr_disk", "Disk Label type %s\n", devfull));
+
+    sess_ref = DASessionCreate( NULL );
+    if (NULL == sess_ref) {
+        strncpy(string, devfull, str_len);
+        string[str_len-1] = 0;
+        return -1;
+    }
+
+    disk = DADiskCreateFromBSDName( NULL, sess_ref, devfull );
+    if (NULL == disk) {
+        CFRelease(sess_ref);
+        strncpy(string, devfull, str_len);
+        string[str_len-1] = 0;
+        return -1;
+    }
+
+    desc = DADiskCopyDescription( disk );
+    if (NULL == desc) {
+        snmp_log(LOG_ERR,
+                 "diskmgr: couldn't get disk description for %s, skipping\n",
+                 devfull);
+        CFRelease(disk);
+        CFRelease(sess_ref);
+        strncpy(string, devfull, str_len);
+        return -1;
+    }
+
+    /** model */
+    str_ref = (CFStringRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionMediaNameKey);
+    if (str_ref) {
+        strncpy(string, CFStringGetCStringPtr(str_ref, sys_encoding),
+                str_len);
+        string[str_len-1] = 0;
+        DEBUGMSGTL(("verbose:diskmgr:darwin", " name %s\n", string));
+    }
+    else {
+        strncpy(string, devfull, str_len);
+        string[str_len-1] = 0;
+    }
+    
+    CFRelease(disk);
+    CFRelease(desc);
+    CFRelease(sess_ref);
+    
+    return 0;
+}
+#endif
+
 static void
 Save_HR_Disk_Specific(void)
 {
@@ -822,6 +934,13 @@ Save_HR_Disk_Specific(void)
 #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_removeble = HRD_removeble;
+#endif
+
 }
 
 static void
@@ -845,6 +964,11 @@ Save_HR_Disk_General(void)
                     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 +1024,104 @@ Query_Disk(int fd, const char *devfull)
     result = ioctl(fd, DIOCGDINFO, &HRD_info);
 #endif
 
+#ifdef darwin
+    DASessionRef        sess_ref;
+    DADiskRef           disk;
+    CFDictionaryRef     desc;
+    CFStringRef         str_ref;
+    CFNumberRef         number_ref;
+    CFBooleanRef        bool_ref;
+    CFStringEncoding    sys_encoding = CFStringGetSystemEncoding();
+
+    sess_ref = DASessionCreate( NULL );
+    if (NULL == sess_ref)
+        return -1;
+
+    disk = DADiskCreateFromBSDName( NULL, sess_ref, devfull );
+    if (NULL == disk) {
+        CFRelease(sess_ref);
+        return -1;
+    }
+
+    desc = DADiskCopyDescription( disk );
+    if (NULL == desc) {
+        CFRelease(disk);
+        CFRelease(sess_ref);
+        return -1;
+    }
+
+    number_ref = (CFNumberRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionMediaSizeKey);
+    if (number_ref)
+        CFNumberGetValue(number_ref, kCFNumberSInt64Type, &HRD_cap);
+    else
+        HRD_cap = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " size %lld\n", HRD_cap));
+
+    /** writable?  */
+    bool_ref = (CFBooleanRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionMediaWritableKey);
+    if (bool_ref) {
+        HRD_access = CFBooleanGetValue(bool_ref);
+    }
+    else
+        HRD_access = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " writable %d\n",
+                HRD_access));
+
+    /** removable?  */
+    bool_ref = (CFBooleanRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionMediaRemovableKey);
+    if (bool_ref) {
+        HRD_removeble = CFBooleanGetValue(bool_ref);
+    }
+    else
+        HRD_removeble = 0;
+    DEBUGMSGTL(("verbose:diskmgr:darwin", " removable %d\n",
+                HRD_removeble));
+
+    /** get type */
+    str_ref = (CFStringRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionMediaTypeKey);
+    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));
+    }
+    else {
+        str_ref = (CFStringRef)
+            CFDictionaryGetValue(desc, kDADiskDescriptionDeviceProtocolKey);
+        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
+            HRD_type = HRDISKSTORAGEMEDIA_UNKNOWN;
+    }
+
+    /** model */
+    str_ref = (CFStringRef)
+        CFDictionaryGetValue(desc, kDADiskDescriptionDeviceModelKey);
+    if (str_ref) {
+        strncpy(HRD_model, CFStringGetCStringPtr(str_ref, sys_encoding),
+                sizeof(HRD_model));
+        HRD_savedModel[ sizeof(HRD_savedModel)-1 ] = 0;
+        DEBUGMSGTL(("verbose:diskmgr:darwin", " model %s\n", HRD_model));
+    }
+    else
+        HRD_model[0] = 0;
+    CFRelease(disk);
+    CFRelease(desc);
+    CFRelease(sess_ref);
+    result = 0;
+#endif
+
     return (result);
 }
 
@@ -918,6 +1140,11 @@ Is_It_Writeable(void)
         return (2);             /* read only */
 #endif
 
+#ifdef darwin
+    if (!HRD_access)
+        return (2);
+#endif
+
     return (1);                 /* read-write */
 }
 
@@ -990,6 +1217,9 @@ What_Type_Disk(void)
     }
 #endif
 
+#ifdef darwin
+    return HRD_type;
+#endif
 
     return (2);                 /* Unknown */
 }
@@ -1020,5 +1250,63 @@ Is_It_Removeable(void)
         return (1);             /* true */
 #endif
 
+#ifdef darwin
+    if (HRD_removeble)
+        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