[PATCH] NetBSD support for check_ide_smart

[email protected] (Emmanuel Dreyfus)
Newsgroups gmane.network.nagios.plugins.devel
Message-ID <1kvp714.1puwc4611qdspwM%[email protected]>
Hello

I would like to submit the patch below that adds NetBSD support to 
plugins/check_ide_smart.c

I used the least-intrusive approach. One could argue it would be 
better to have  smart_read_values defined as  smart_read_values_linux()
or  smart_read_values_netbsd(), or to have an array of OS-dependant 
function pointers (ie: calling osdep->smart_read_values(fd, &values). 
I can rework the patch in one theses ways if is it is preferred.

--- plugins/check_ide_smart.c.orig      2012-12-25 19:47:45.000000000 +0100
+++ plugins/check_ide_smart.c   2012-12-25 20:46:46.000000000 +0100
@@ -45,10 +45,31 @@
 
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
+#ifdef linux
 #include <linux/hdreg.h>
 #include <linux/types.h>
+
+#define OPEN_MODE O_RDONLY
+#endif /* linux */
+#ifdef __NetBSD__
+#include <sys/device.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/videoio.h> /* for __u8 and friends */
+#include <sys/scsiio.h>
+#include <sys/ataio.h>
+#include <dev/ata/atareg.h>
+#include <dev/ic/wdcreg.h>
+
+#define SMART_ENABLE WDSM_ENABLE_OPS
+#define SMART_DISABLE WDSM_DISABLE_OPS
+#define SMART_IMMEDIATE_OFFLINE WDSM_EXEC_OFFL_IMM
+#define SMART_AUTO_OFFLINE 0xdb /* undefined in NetBSD headers */
+
+#define OPEN_MODE O_RDWR
+#endif /* __NetBSD__ */
 #include <errno.h>
        
 #define NR_ATTRIBUTES  30
        
@@ -222,9 +243,9 @@
                print_help ();
                return STATE_OK;
        }
 
-       fd = open (device, O_RDONLY);
+       fd = open (device, OPEN_MODE);
 
        if (fd < 0) {
                printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
                return STATE_CRITICAL;
@@ -283,8 +304,9 @@
 
 int
 smart_read_values (int fd, values_t * values) 
 {
+#ifdef linux
        int e;
        __u8 args[4 + 512];
        args[0] = WIN_SMART;
        args[1] = 0;
@@ -295,8 +317,37 @@
                printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
                return e;
        }
        memcpy (values, args + 4, 512);
+#endif /* linux */
+#ifdef __NetBSD__
+       struct atareq req;
+       unsigned char inbuf[DEV_BSIZE];
+
+       memset(&req, 0, sizeof(req));
+       req.timeout = 1000;
+       memset(&inbuf, 0, sizeof(inbuf));
+
+       req.flags = ATACMD_READ;
+       req.features = WDSM_RD_DATA;
+       req.command = WDCC_SMART;
+       req.databuf = (char *)inbuf;
+       req.datalen = sizeof(inbuf);
+       req.cylinder = WDSMART_CYL;
+
+       if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
+               if (req.retsts != ATACMD_OK)
+                       errno = ENODEV;
+       }
+
+       if (errno != 0) {
+               int e = errno;
+               printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
+               return e;
+       }
+       
+       (void)memcpy(values, inbuf, 512);
+#endif /* __NetBSD__ */
        return 0;
 }
 
 
@@ -438,8 +489,9 @@
 int
 smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error) 
 {
        int e = 0;
+#ifdef linux
        __u8 args[4];
        args[0] = WIN_SMART;
        args[1] = val0;
        args[2] = smart_command[command].value;
@@ -449,16 +501,42 @@
                if (show_error) {
                        printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
                }
        }
+#endif /* linux */
+#ifdef __NetBSD__
+       struct atareq req;
+
+       memset(&req, 0, sizeof(req));
+       req.timeout = 1000;
+       req.flags = ATACMD_READREG;
+       req.features = smart_command[command].value;
+       req.command = WDCC_SMART;
+       req.cylinder = WDSMART_CYL;
+       req.sec_count = val0;
+
+       if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
+               if (req.retsts != ATACMD_OK)
+                       errno = ENODEV;
+               if (req.cylinder != WDSMART_CYL)
+                       errno = ENODEV;
+       }
+
+       if (errno != 0) {
+               e = errno;
+               printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
+               return e;
+       }
+#endif /* __NetBSD__ */
        return e;
 }
 
 
 
 int
 smart_read_thresholds (int fd, thresholds_t * thresholds) 
 {
+#ifdef linux
        int e;
        __u8 args[4 + 512];
        args[0] = WIN_SMART;
   args[1] = 0;
@@ -469,8 +547,37 @@
                printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
                return e;
        }
        memcpy (thresholds, args + 4, 512);
+#endif /* linux */
+#ifdef __NetBSD__
+       struct atareq req;
+       unsigned char inbuf[DEV_BSIZE];
+
+       memset(&req, 0, sizeof(req));
+       req.timeout = 1000;
+       memset(&inbuf, 0, sizeof(inbuf));
+
+       req.flags = ATACMD_READ;
+       req.features = WDSM_RD_THRESHOLDS;
+       req.command = WDCC_SMART;
+       req.databuf = (char *)inbuf;
+       req.datalen = sizeof(inbuf);
+       req.cylinder = WDSMART_CYL;
+
+       if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
+               if (req.retsts != ATACMD_OK)
+                       errno = ENODEV;
+       }
+
+       if (errno != 0) {
+               int e = errno;
+               printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
+               return e;
+       }
+       
+       (void)memcpy(thresholds, inbuf, 512);
+#endif /* __NetBSD__ */
        return 0;
 }
 
 

-- 
Emmanuel Dreyfus
http://hcpnet.free.fr/pubz
[email protected]

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________________
Nagios Plugin Development Mailing List Nagiosplug-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Unsubscribe at https://lists.sourceforge.net/lists/listinfo/nagiosplug-devel
::: Please include plugins version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.