> For the complete documentation index, see [llms.txt](https://docs.glesys.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.glesys.com/products/bare-metal/dedicated-servers/how-tos/raid-monitoring-in-freebsd-with-mfiutil.md).

# RAID monitoring in FreeBSD with mfiutil

***

`mfiutil` is a built-in tool in FreeBSD for monitoring and querying MegaRAID controller cards. Using this tool, you can view the RAID controller status on your dedicated server and be alerted if a disk is failing and needs replacement.

## Querying the RAID card

The following commands must be executed as root.

To view information about the RAID card, use `mfiutil show adapter`, such as:

<pre data-title="Commands and output (command is highlighted)"><code><strong>mfiutil show adapter
</strong>/dev/mfi0 Adapter:
    Product Name: AVAGO 3108 MegaRAID
   Serial Number: FW-AFV36IVAARBWA
        Firmware: 24.21.0-0151
     RAID Levels: JBOD, RAID0, RAID1, RAID5, RAID6, RAID10, RAID50
  Battery Backup: not present
           NVRAM: 32K
  Onboard Memory: 2048M
  Minimum Stripe: 64K
  Maximum Stripe: 1M
</code></pre>

To view information about the volumes and their status, use `mfiutil show volumes`, such as:

<pre data-title="Commands and output (command is highlighted)"><code><strong>mfiutil show volumes
</strong>/dev/mfi0 Volumes:
  Id     Size    Level   Stripe  State    Cache   Name
 mfid0 (  447G) RAID-1     256K OPTIMAL  Disabled &#x3C;Drive1>
</code></pre>

To view information about the physical drives, use `mfiutil show drives`, such as:

<pre data-title="Commands and output (command is highlighted)"><code><strong>mfiutil show drives
</strong>/dev/mfi0 Physical Drives:
 3 (  447G) ONLINE &#x3C;SAMSUNG MZ7KH480 904Q serial=S47MNA0R109694> SATA E1:S4
 6 (  447G) ONLINE &#x3C;SAMSUNG MZ7KM480 003Q serial=S2HSNX0H501894> SATA E1:S5
</code></pre>

## Automating and monitoring

The script below will send you an email if the status is not "OPTIMAL".

For the script to work, you'll need a fully working MTA (*Mail Transport Agent*) installed, such as FreeBSD's new default mail transport agent, DMA (*DragonFly Mail Agent*), or Postfix.

See the guide [Sending email from your FreeBSD server using DMA](/products/compute/guides-for-server-management/sending-email-from-your-freebsd-server-using-dma.md) for sending email using FreeBSD's new default mail agent (recommended and easiest). Or, if you would rather use Postfix, see the guide [Sending email from your server using Postfix](/products/compute/guides-for-server-management/sending-email-from-your-server-using-postfix.md) on how to set up your own Postfix server (written for Debian Linux).

Next, create a file called `raidcheck.sh` in a directory of your choice and edit the file with `vim` or `nano`. Paste the following code:

{% code title="raidcheck.sh" %}

```bash
#!/usr/local/bin/bash

FROM_EMAIL="from@example.com"
TO_EMAIL="to@example.com"
SUBJECT="Controller Status Alert"

send_email() {
    local subject="$1"
    local body="$2"

    {
        echo "Subject: $subject"
        echo "To: $TO_EMAIL"
        echo "From: $FROM_EMAIL"
        echo "MIME-Version: 1.0"
        echo "Content-Type: text/plain; charset=UTF-8"
        echo ""
        printf "$body"
    } | sendmail -t -f "$FROM_EMAIL"
}

failstate=0
controller_status=$(mfiutil show volumes)

while IFS= read -r line
    do
        if [ "$line" != "OPTIMAL" ]; then
            failstate=1
        fi
    done < <(echo "$controller_status" | grep mfid | awk '{ print $6 }')

if [ "$failstate" -ne 0 ]; then
    message="Warning: One or more volumes have a non-optimal status.\n\n$controller_status"
    send_email "$SUBJECT" "$message"
    echo "Email notification sent about the non-optimal status."
else
    echo "-----------------------------------"
    echo "All volumes reported optimal status"
fi
```

{% endcode %}

Note that this script requires `bash`; it does not work with `sh`. Install Bash using `pkg install bash` as root.

You need to change the `FROM_EMAIL` and `TO_EMAIL` variables at the beginning of the script to match your email address and the sender's email address.

You also need to make `raidcheck.sh` executable:

{% code title="Command" %}

```
chmod +x raidcheck.sh
```

{% endcode %}

Next, you'll need to add the script to root's crontab (since `mfiutil` requires root permissions to access the controller). You can open root's crontab either by using `sudo crontab -e` (if `sudo` is installed and configured), or by first switching to root with `su`, and then running `crontab -e`.

Once root's crontab is open, paste the following line. Change the path to where your script is located. This crontab will run the script 10 minutes past every hour. For more information about crontab, see [Automate tasks in Linux using cron](/products/compute/guides-for-server-management/automate-tasks-in-linux-using-cron.md).

{% code title="Root's crontab" %}

```
10 * * * * /path/to/your/script/raidcheck.sh
```

{% endcode %}

## Notes

We recommend that you test the script to ensure that it truly works before putting it into live operation. There's no guarantee it will suit your specific server and setup.
