{"id":117,"date":"2009-01-21T13:30:27","date_gmt":"2009-01-21T04:00:27","guid":{"rendered":"http:\/\/www.clearchain.com\/blog\/?p=117"},"modified":"2010-06-23T15:29:23","modified_gmt":"2010-06-23T05:59:23","slug":"raid-status-script","status":"publish","type":"post","link":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script","title":{"rendered":"Raid Status Script"},"content":{"rendered":"<p>One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a <a class=\"external text\" title=\"http:\/\/freebsd.org\" rel=\"nofollow\" href=\"http:\/\/freebsd.org\/\">FreeBSD<\/a> server at home that has a SATA RAID 1 using a <a class=\"external text\" title=\"http:\/\/www.promise.com\" rel=\"nofollow\" href=\"http:\/\/www.promise.com\/ \">Promise<\/a> controller in particular:<\/p>\n<pre>atapci0: &lt;Promise PDC20371 SATA150 controller&gt; port 0xc000-0xc03f,0xc400-0xc40f,0xc800-0xc87f mem 0xde031000-0xde031fff,0xde000000-0xde01ffff irq 11 at device 1.0 on pci1<\/pre>\n<p>the problem is I will only get an email alert at the end of the day if something goes wrong. What I really want is for the server to start driving me nuts, calling out my name, flashing lights and kicking up all merry hell.<\/p>\n<p>Alas, I couldn&#8217;t find an easy way for it to do that. Hence the program below will check the raid status of 1 or more <strong>ar<\/strong> devices, and if one of them is not normal it will:<\/p>\n<ul>\n<li> Email me to let me know which device has failed<\/li>\n<li> Play a tune through the PC Speaker<\/li>\n<\/ul>\n<p>The other benefit to this is it will keep playing the tune whilst the raid is rebuilding and keep email me to provide me with a status report. A bit like the old Alpha&#8217;s did &#8211; &lt;sigh&gt; I miss the Alpha.<\/p>\n<h1><span class=\"mw-headline\">Installation <\/span><\/h1>\n<p>To install and use this program simply:<\/p>\n<ol>\n<li> Grab the source code (drop me an email as a thank if you can\u00a0\ud83d\ude42<\/li>\n<li> Modify the <strong>RAID_ARRAYS<\/strong> line to indicate which arrays you want monitored. Ie:  <code>{\"ar0\", \"ar1\", \"ar2\", NULL} <\/code> would monitor 3 arrays<\/li>\n<li> Compile the app using: <code>gcc -o raidstatus yourfilename.c<\/code> to build the executable<\/li>\n<li> Run application to test: <code>.\/raidstatus<\/code><\/li>\n<li> If you want sound you&#8217;ll need the to install the <em>speaker<\/em> kernel module. Putting <code>speaker_load=\"YES\"<\/code> in <strong>\/boot\/loader.conf<\/strong> will make sure it loads at boot time.<\/li>\n<li> Finally, set a cron job to have it monitor your system as often as you want. By default it won&#8217;t output anything so you won&#8217;t get an email. However, if something fails you&#8217;ll start hearing \/ receiving email. The following crontab checks the raid status every 10 minutes.<\/li>\n<\/ol>\n<pre>*\/10 * * * *    \/root\/bin\/raidstatus &gt; \/dev\/null<\/pre>\n<h1>Code <\/h1>\n<p>This script is designed to run on FreeBSD 6.x.<\/p>\n<p>It should work on 5.2+ but won&#8217;t work on FreeBSD 4.x or below due to changes in the ata subsystem.<\/p>\n<p>It can be used to monitor any ata based raid. Including RAID0, RAID1, RAID3, RAID4 &amp; RAID5 (note at the time of writing RAID5 operates like a RAID0 under FreeBSD &#8211; see: <a class=\"external free\" title=\"http:\/\/www.freebsd.org\/cgi\/query-pr.cgi?pr=bin\/106431\" rel=\"nofollow\" href=\"http:\/\/www.freebsd.org\/cgi\/query-pr.cgi?pr=bin\/106431\">http:\/\/www.freebsd.org\/cgi\/query-pr.cgi?pr=bin\/106431<\/a> )<\/p>\n<p>Hope the script helps!<\/p>\n<p>&#8211;Benjsc 08:18, 8 May 2007 (EIT)<\/p>\n<pre>\/**\r\n * A simple little application that queries an ata raid\r\n * device looking for any errors. It outputs the date\/time\r\n * the query was made and the status for each device.\r\n * If a device has failed and is in the degraded mode\r\n * it exits printing the failed device to stderr with\r\n * error code 127. Any error creates a return status greater than\r\n * zero; On failure root is also emailed indicating the failed raid\r\n * device and a tune is played to \/dev\/speaker\r\n *\r\n * Copyright(C) 2007 Benjamin Close &lt;Benjamin.Close@clearchain.com&gt;\r\n *\r\n * Usage: raidstatus\r\n *\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;errno.h&gt;\r\n#include &lt;err.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/ata.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n\r\n#define RAID_ARRAYS {\"ar0\",NULL}\r\n\r\nmain(int argc, char **argv)\r\n{\r\n    int fd, fd2, i;\r\n    char *devices[] = RAID_ARRAYS;\r\n    char **device = devices;\r\n    int status = 0;\r\n    int notified= 0;\r\n    struct ata_ioc_raid_config config;\r\n\r\n    if((fd=open(\"\/dev\/ata\", O_RDWR)) &lt; 0 ){\r\n        err(1,\"control device not found\");\r\n        exit(1);\r\n    }\r\n\r\n    while (*device\u00a0!= NULL ){\r\n\r\n        if (!(sscanf(*device, \"ar%d\", &amp;config.lun) == 1)) {\r\n            fprintf(stderr, \"atacontrol: Invalid array %s\\n\", *device);\r\n            printf(\"Invalid ar device\");\r\n            status=1;\r\n        }\r\n        if (ioctl(fd, IOCATARAIDSTATUS, &amp;config) &lt; 0)\r\n          err(1, \"ioctl(IOCATARAIDSTATUS)\");\r\n\r\n        printf(\"ar%d: ATA \", config.lun);\r\n        switch (config.type) {\r\n            case AR_RAID0:\r\n                printf(\"RAID0 stripesize=%d\", config.interleave);\r\n                break;\r\n            case AR_RAID1:\r\n                printf(\"RAID1\");\r\n                break;\r\n            case AR_RAID01:\r\n                printf(\"RAID0+1 stripesize=%d\", config.interleave);\r\n                break;\r\n            case AR_RAID5:\r\n                printf(\"RAID5 stripesize=%d\", config.interleave);\r\n                break;\r\n            case AR_JBOD:\r\n                printf(\"JBOD\");\r\n            case AR_SPAN:\r\n                printf(\"SPAN\");\r\n                break;\r\n        }\r\n        printf(\" subdisks: \");\r\n        for (i = 0; i &lt; config.total_disks; i++) {\r\n            if (config.disks[i] &gt;= 0)\r\n              printf(\"ad%d \", config.disks[i]);\r\n            else\r\n              printf(\"DOWN \");\r\n        }\r\n        printf(\"status: \");\r\n        switch (config.status) {\r\n            case AR_READY:\r\n                printf(\"READY\\n\");\r\n                break;\r\n            case AR_READY | AR_DEGRADED:\r\n                printf(\"DEGRADED\\n\");\r\n                status=127;\r\n                break;\r\n            case AR_READY | AR_DEGRADED | AR_REBUILDING:\r\n                printf(\"REBUILDING %d%% completed\\n\",\r\n                            config.progress);\r\n                status=127;\r\n                break;\r\n            default:\r\n                printf(\"BROKEN\\n\");\r\n                status=127;\r\n        }\r\n\r\n        if ( status &gt; 0 &amp;&amp;\u00a0! notified ){\r\n\r\n            char buffer[1024];\r\n\r\n            const char *song = \"mst200o2ola.l8bc.~a.~&gt;l2d#\";\r\n\r\n            \/\/ Play a tune to the speaker\r\n            if ( (fd2 = open(\"\/dev\/speaker\",O_RDWR)) &lt; 0 ){\r\n                fprintf(stderr, \"Unable to open speaker\");\r\n            } else {\r\n                \/\/ Send the tune to the speaker\r\n                write(fd2, song, strlen(song));\r\n                close(fd2);\r\n            }\r\n\r\n            sprintf(buffer, \"\/sbin\/atacontrol status %s | \/usr\/bin\/mail -s Raid_Fault root\", *device);\r\n\r\n            \/\/ Send an email indicating things are broken\r\n            system(buffer);\r\n\r\n            notified = 1;\r\n        }\r\n\r\n        device++;\r\n    }\r\n\r\n    close(fd);\r\n    exit(status);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a FreeBSD server at home that has a SATA RAID 1 using a Promise controller in particular: atapci0: &lt;Promise PDC20371 SATA150 controller&gt; port 0xc000-0xc03f,0xc400-0xc40f,0xc800-0xc87f mem 0xde031000-0xde031fff,0xde000000-0xde01ffff irq 11 at device<a href=\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\"> <font size=-2>[..more..]<\/font><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-other"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Raid Status Script - ClearChain<\/title>\n<meta name=\"description\" content=\"One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a FreeBSD server\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Close\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\"},\"author\":{\"name\":\"Benjamin Close\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"headline\":\"Raid Status Script\",\"datePublished\":\"2009-01-21T04:00:27+00:00\",\"dateModified\":\"2010-06-23T05:59:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\"},\"wordCount\":391,\"commentCount\":0,\"articleSection\":[\"Other\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\",\"url\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\",\"name\":\"Raid Status Script - ClearChain\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\"},\"datePublished\":\"2009-01-21T04:00:27+00:00\",\"dateModified\":\"2010-06-23T05:59:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"description\":\"One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a FreeBSD server\",\"breadcrumb\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.clearchain.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Raid Status Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\",\"url\":\"https:\/\/www.clearchain.com\/blog\/\",\"name\":\"ClearChain\",\"description\":\"-= Daily Happenings =-\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\",\"name\":\"Benjamin Close\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"caption\":\"Benjamin Close\"},\"sameAs\":[\"http:\/\/www.clearchain.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Raid Status Script - ClearChain","description":"One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a FreeBSD server","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script","twitter_misc":{"Written by":"Benjamin Close","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#article","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script"},"author":{"name":"Benjamin Close","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"headline":"Raid Status Script","datePublished":"2009-01-21T04:00:27+00:00","dateModified":"2010-06-23T05:59:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script"},"wordCount":391,"commentCount":0,"articleSection":["Other"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script","url":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script","name":"Raid Status Script - ClearChain","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/#website"},"datePublished":"2009-01-21T04:00:27+00:00","dateModified":"2010-06-23T05:59:23+00:00","author":{"@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"description":"One thing that has always bugged me about the free operating systems is a lack of notification when things go wrong. In particular I run a FreeBSD server","breadcrumb":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.clearchain.com\/blog\/posts\/raid-status-script#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.clearchain.com\/blog"},{"@type":"ListItem","position":2,"name":"Raid Status Script"}]},{"@type":"WebSite","@id":"https:\/\/www.clearchain.com\/blog\/#website","url":"https:\/\/www.clearchain.com\/blog\/","name":"ClearChain","description":"-= Daily Happenings =-","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98","name":"Benjamin Close","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","caption":"Benjamin Close"},"sameAs":["http:\/\/www.clearchain.com"]}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":5,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}