Server Monitoring Script

Server Monitoring Script
Server Monitoring Script

Using Shell Scripts to monitor Linux and Unix Computers

ActiveXperts Network Monitor comes with a set of shell scripts that can be used to monitor remote Linux and Unix machines. An RSH check and an SSH check are the two sorts of checks available.

SSH enables secure network services to be delivered over an insecure network like the Internet. ActiveXperts Network Monitor can use SSH to connect to a remote machine that runs the SSH daemon and run a command or shell script. RSH is another IP-based protocol for running programmes on a distant Linux/Unix machine, however it is less common due to the fact that it is insecure. As a result, SSH is suggested for remote Linux and Unix computer monitoring.

The output (i.e. StdOut) of ActiveXperts Network Monitor must be prepared according to the following syntax:

[ SUCCESS | ERROR | UNCERTAIN]: { <explanation> } { DATA:<value>}

The ‘Last Response’ column in the Manager application will display the ‘explanation>. In the Manager application, the value> field will be displayed in the ‘Data’ column.

The shell script must be installed on the Linux system; a script installed on the Windows monitoring server will not work. The ‘Execute’ permission flag must be set on the shell script.

Here’s a shell script that works with Network Monitor as an example. This script determines a directory’s size:

#!/bin/bash
# Validate number of arguments
if [ $# -ne 2 ] ; then
  echo "UNCERTAIN: Invalid number of arguments - Usage: directory-size sDirectory nMaxSizeMB"
  exit 1
fi

# Validate numeric parameter nMaxSizeMB
regExpNumber='^[0-9]+$'
if ! [[ $2 =~ $regExpNumber ]] ; then
  echo "UNCERTAIN: Invalid argument: nMaxSizeMB (number expected)"
  exit 1
fi

# Check the size of the directory specified
if [ ! -d "$1" ]; then
  echo "UNCERTAIN: Directory [$1] does not exist"
  exit 0
fi

nSizeKB=`du -s "$1" | awk '{ print $1; }'`
nSizeMB=$( echo "$nSizeKB / 1024" | bc )

# Round a float to an integer value
nRoundSizeMB=$( echo "$nSizeMB / 1" | bc )

# Round a float to an integer value
nRoundMaxMB=$( echo "$2 / 1" | bc )

if [ $nRoundSizeMB -le $nRoundMaxMB ] ; then
  echo "SUCCESS: Size=[$nRoundSizeMB MB], max. allowed=[$nRoundMaxMB MB] DATA:$nRoundSizeMB"
else
  echo "ERROR: Size=[$nRoundSizeMB MB], max. allowed=[$nRoundMaxMB MB] DATA:$nRoundSizeMB"
fi

exit 0

How do I install and run a script?

To make the monitor script "tecmint_monitor.sh” executable, first use the wget command .

# wget https://techmint.com/wp-content/scripts/techmint_monitor.sh # chmod 755 tecmint_monitor.sh

It is recommended to install the script as a user, and not as root. It will ask you for your root password, and it will install all necessary components in the required locations.

To install "tecmint_monitor.sh” script, simply use -i (install), as shown below.

./techmint_monitor.sh

When prompted, enter root passcode. If all goes well, you will receive the following success message.

Password: Congratulations! Now run the monitor command, script installed

You can call command "monitor" from any user or location after installation. You can choose not to install it.

# ./Path/to/script/techmint_monitor.sh

Simply follow these steps to run the monitor command anywhere you want using any account:

$ Monitor

Once you have run the command, you will receive various System-related information.

  1. Internet Connectivity
  2. OS Type
  3. OS Name
  4. OS Version
  5. Architecture
  6. Kernel Release
  7. Hostname
  8. Internal IP
  9. External IP
  10. Name Servers
  11. Logged In users
  12. Ram Uses
  13. Swap Uses
  14. Disk Usages
  15. Load Average
  16. System uptime

You can check the version of script that is installed using –v (version).

$ monitor -v tecmint_monitor Version 0.1 Designed By Tecmint.com Published Under Apache 2.0 License

The Network Monitor Shell Script performs checks on the network.

A set of shell scripts is included with ActiveXperts Network Monitor. These scripts are ready to use right now. The scripts are in the installation-dir> directory. (e.g., C:Program FilesActiveXpertsNetwork MonitorScriptsMonitor) ScriptsMonitor (Linux) (Linux). In order to run the script, you must first copy it to the linux machine.

Server Monitoring Script Samples

CPU usage check

Click here for a CPU monitoring script.

Directory size check

Click here for a directory size check.

Disk Existence check

Click here for a disk existence check.

Disk Free Space check

Click here for a disk free space check.

Disk Used Space check

Click here for a disk used space check.

Disk Used Space Percentage check

Click here for a disk used space percentage check.

E-mail POP3 check

Click here for an e-mail POP3 check.

E-mail SMTP check

Click here for an e-mail SMTP check.

File content check

Click here for a file content check.

File existence check

Click here for a file existence check.

Memory usage check

Click here for a memory monitoring script.

Ping check

Click here for a ping check.

Process check

Click here for a process check.

User Exists

Click here for a Linux User Existence check.

Conclusion

The script works on several machines that I tested. It should also work for you. Please let us know if you discover any bugs in the comments. This isn’t the end. This is only the beginning. From here, you can go to any level.

There have been few complaints about the script not working with the limited Linux distributions. One of our regular readers, Mr. Andres Tarallo, has taken the initiative and made the script compatible with all Linux distributions, you can find the updated script on GitHub at https://github.com/atarallo/TECMINT_MONITOR/.

You can edit the script and make it more interesting. Please give us proper credit. Also, please share the updated script with our editors so we can improve this article and give you proper credit.

Do not forget to send us your thoughts and your script. We are here to assist you. We are grateful for your love and support. Keep connected! Keep watching.

Jennifer Thomas
Jennifer Thomas is the Co-founder and Chief Business Development Officer at Cybers Guards. Prior to that, She was responsible for leading its Cyber Security Practice and Cyber Security Operations Center, which provided managed security services.