# How to monitor hardware RAID with MegaCli (LSI RAID)

**If** you want to monitor a {{target}} computer RAID array, **and if** your
{{target}} computer is equipped with [**LSI** RAID
controller(s)](https://duckduckgo.com/?t=ffab&q=lsi+raid+controller&iax=images&ia=images), then
this section is for you!

```{note}
MegaCli ss also known as CmdTool2.
```

---

```{important}
🙏 Help is welcome / appreciated 🙏
```

🚧 WiP / TODO 🚧

Prerequisites:

* [Installation how-to](../basics/install_update_remove_how_to.md#how-to-install).
* [Firewall how-to for SSH](../basics/firewall_how_to.md#firewall-configuration-for-ssh).
* [Firewall how-to for EPICS](../basics/firewall_how_to.md#standard-firewall-configuration-for-epics).
* [SSH how-to](../basics/ssh_how_to.md).
* [`.cmd` and `.substitutions` how-to](../basics/cmd_and_sub_how_to.md)
* [Security how-to](../basics/security_how_to.md)
* [Run SSH Monitor how-to](../basics/run_ssh_monitor_how_to.md)
* [Custom shell instructions how-to](../basics/custom_shell_instructions_how_to.md)
* [EPICS records' fields how-to](../basics/epics_records_fields_how_to.md)

---

* On the {{target}} computer, install MegaCli in
  `/opt/MegaRAID/MegaCli/MegaCli64` like described
  [here](web-archive:20220312074835/https://raid.wiki.kernel.org/index.php/Hardware_Raid_Setup_using_MegaCli)
  or
  [here](web-archive:20200814101139/https://www.admin-linux.fr/memo-megacli-utilisation-de-controleur-raid-delllsi/).

<!--
> /!\ Deprecated/!\
> Not working: MegaCli seems to only like root and no other user):
>
> Allow `sshmonitor-user` to use MegaCli by creating a megacli group, adding `sshmonitor-user` and
> `/opt/MegaRAID` to it:
>
> ```{code} bash
> target-pc $ sudo groupadd --system megacli # create a system group
> target-pc $ sudo usermod -a -G megacli sshmonitor-user
> ```
> **If sshmonitor-user was connected during this step: log him out and log him back in** (in order for
> the new group to properly apply.
>
> ```{code} bash
> target-pc $ sudo chown -R root:megacli /opt/MegaRAID
> ```
> /!\ Deprecated /!\
-->

* Allow `sshmonitor-user` to use MegaCli as `root` without password (thanks to `sudo` and `visudo`):

    ```{code} bash
    target-pc # visudo
        > ...
        > ## Allows asnet user to run /opt/MegaRAID/MegaCli/Megacli64 as root without password:
        > sshmonitor-user ALL = NOPASSWD: /opt/MegaRAID/MegaCli/MegaCli64
    ```

* Create a symlink for the restricted bash environment:

    ```{code} bash
    target-pc # ln -s /opt/MegaRAID/MegaCli/MegaCli64 /home/sshmonitor-user/bin/megacli
    ```

    See the [security how-to](../basics/security_how_to.md) and the [security
    explanations](../../explanations/security_explanations.md) for more details.

* Add the following alias in the `.bashrc`:

    ```{code} bash
    target-pc # vi /home/sshmonitor-user/.bashrc

        > readonly PATH=$HOME/bin
      + >
      + > alias megacli="sudo megacli"
    ```

* Make sure MegaCli is working well with the `sshmonitor-user`:

    ```{code} bash
    target-pc $ echo $USER

        sshmonitor-user

    target-pc $ megacli -adpallinfo -aALL

        ...
    ```


---

* On the {{host}} computer, add the following to your {{substitutions}} file:

    ```{code} bash
    >     ...
  + >
  + >     # LSI RAID monitoring
  + >     {
  + >         CATEGORY="lsi_raid:"
  + > 
  + >         # Shell instruction used to get the LSI RAID state (0: OK, 1: KO), and associated EPICS alarm
  + >         PV_NAME_SCALAR_1="lsi_raid_state"
  + >         SCALAR_1_INSTRUCTION="megacli -LDInfo -Lall -aAll | grep State | awk '{print \\\\$3}' | sed 's/Optimal/0/' | sed 's/Degraded/1/' | sed 's/Failed/2/' | sort -n -r | head -n 1"
  + >         LOAD_SCALAR_1 = ""
  + >         HIHI_SCALAR_1 = "1"
  + >         HHSV_SCALAR_1 = "MAJOR"
  + >         PINI_SCALAR_1 = "1"
  + >     }
  + > 
    > }
    ```

---

🚧 TODO 🚧

Make sure that the configuration described in this how-to will work in a restricted bash
environment.
