Configure monitoring in Linux for a MegaRaid controller

In Linux, monitoring for a MegaRaid controller can be done using MegaCLI tools.

Some prerequisites for it are required, let’s install them.

For Debian like systems:

apt install mailutils postfix libncurses5 alien vim unzip wget

For CentOS like systems:

yum install mailx postfix ncurses-devel vim unzip wget

Then let’s download MegaCLI sources, extract, install it and clean-up.

For Debian like systems:

cd /opt
wget https://docs.broadcom.com/docs-and-downloads/raid-controllers/raid-controllers-common-files/8-07-14_MegaCLI.zip
unzip 8-07-14_MegaCLI.zip
cd Linux
alien MegaCli-8.07.14-1.noarch.rpm
dpkg -i megacli_8.07.14-2_all.deb
cd ..
rm -rf 8.07.14_MegaCLI.txt 8-07-14_MegaCLI.zip DOS/ FreeBSD/ Linux/ Solaris/ Windows/

For CentOS like systems:

cd /opt
wget https://docs.broadcom.com/docs-and-downloads/raid-controllers/raid-controllers-common-files/8-07-14_MegaCLI.zip
unzip 8-07-14_MegaCLI.zip
cd Linux
rpm -i MegaCli-8.07.14-1.noarch.rpm
cd ..
rm -rf 8.07.14_MegaCLI.txt 8-07-14_MegaCLI.zip DOS/ FreeBSD/ Linux/ Solaris/ Windows/

Create a link for easier access:

ln -s /opt/MegaRAID/MegaCli/MegaCli64 /bin/megacli

Check the actual status for disks:

/bin/megacli -PDList -aALL |grep "Drive has flagged a S.M.A.R.T alert"

Normally it will indicate “Drive has flagged a S.M.A.R.T alert : No” for each disk.

Create the script for monitoring:

vim /opt/raidmon.sh

Put the content:

#!/bin/bash
set -ex
RESP=$(/bin/megacli -PDList -aALL |grep "Drive has flagged a S.M.A.R.T alert")
SUBJ="RAID status on server.example.com"
echo "$RESP" | mail -a "From: monitoring@server.example.com" -s "$SUBJ" your@mailaddress.com

Make the script executable:

chmod 700 /opt/raidmon.sh

Add a cronjob to execute the script periodically:

crontab -e

First time when you edit the crontab it will ask to choose an editor, choose 2 and hit Enter:

Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/mcedit
4. /usr/bin/vim.tiny

Choose 1-4 [1]: 2

For me it was enough to get an update daily (at 12:04 AM):

4 0 * * * /bin/sh /opt/raidmon.sh

But it can be easily updated to notify only in case of issues:

#!/bin/bash
set -ex
RESP=$(/bin/megacli -PDList -aALL |grep "Drive has flagged a S.M.A.R.T alert")
SUBJ="RAID status alert"
if [[ "$RESP" == *"Yes"* ]]; then
echo "$RESP" | mail -a "From: monitoring@server.example.com" -s "$SUBJ" your@mailaddress.com
done

Set the crontab to run it every minute:

* * * * * /bin/sh /opt/raidmon.sh

Granting user permissions for a PostgreSQL database

If you need to manage databases and permissions for a PostgreSQL server, you can use a WEB, a Desktop or a command line client. I used pgAdmin for WEB and Desktop access.

A PostgreSQL database have an owner. I got an error setting permissions for other users being connected as different user (with root privileges), if you get too – try to grand permissions as database owner!

A databases can contains multiple schemes, each of them contains tables.

 

Setting connection permissions

First of all you have to grant CONNECTION permissions to the database:

GRANT CONNECT ON DATABASE mydb TO xxx;

Then you have to grant permissions per schemas and tables.

Grant permissions only for existing tables in a schema

  1. Grant USAGE permissions per schema:
    GRANT USAGE ON SCHEMA public TO xxx;
  2. Grant SELECT permissions per table:
    GRANT SELECT ON mytable TO xxx;

Grant default permissions for all tables

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO xxx;

Watchdog and start scripts for Rocket.Chat application

I installed recently Rocket.Chat on a server. The next day I checked it – the service was not running. So I wrote a small script that checks if the application port 3000 is in use and run another script if not.

The scrip is quite simple. The core utility nc is used to check the port availability and set the value for the parameter PORTCHECK depending on the state. Next it uses an if statement to just echo a message if the port is in use, otherwise it run the Rocket.Chat starting script.

Here is the code on that script /opt/rocketwatchdog.sh:
#!/bin/sh
PORTCHECK=$(nc -z -w 3 localhost 3000; echo $?)
DATESTAMP=$(date +”%Y-%m-%d-%T”)
if [ “$PORTCHECK” = 0 ]; then
echo “RocketChat server is already running”
echo “$DATESTAMP App is running.” >> /var/log/watchdog.log
else
echo “RocketChat server is not running. Will try to start it.”
echo “$DATESTAMP RocketChat is not running, restart it.” >> /var/log/watchdog.log
/bin/sh /opt/rocketchat.sh
fi

You should add a cronjob that will check each minute is the server is running. Enter crontab -e to open cron jobs list, add at the end:
* * * * * /bin/sh /opt/rocketwatchdog.sh

Here is the script to start Rocket.Chat /opt/rocketchat.sh:
#!/bin/sh
export PORT=3000
export ROOT_URL=https://chat.mydomain.com/
export MONGO_URL=mongodb://localhost:27017/rocketchat
cd /opt/rocket/
nohup /usr/local/bin/node main.js > /var/log/rocketchat.log 2>&1 &