Configuration mail bash script for server utilization

Posted By : Yash Vishwakarma | 21-Dec-2020

An Introduction about Configuration mail bash script for server utilization  

 

Initialization:

 

Many Linux command is available to send email from the command or by using a bash script. But any email sending command will not work if no SMTP server is set up properly in the system. You can set up your own SMTP server to send emails from the command or you can use any free SMTP server of any well-known email service provider  Gmail or Yahoo. The SMTP server of the Gmail account is used in this tutorial. To do this task, you have to select the Gmail account and enable the option of Allow less secure apps for that account to send the email. You can visit the following tutorial to enable this option.


 

Advantage:-

 

1. Receive mail alerts from time to time.

2. It is helpful for server utilization status via mail alerts.

3. Email is delivered extremely fast when compared to traditional posts


 

Installation scripts-

# !/bin/bash

# Shell script to monitor or watch low-disk space utilization.

#: It will send an email to $ADMIN if the (free available) percentages

#: of space is >= 80%

#: Disk Space Monitoring Script Get Email Alerts.

#### Script START here ###

## You can change your thresholds value whatever you want ##

THRESHOLD=80

PATHS=/

AWK=/bin/awk

DU=`/usr/bin/du -ks`

GREP=/bin/grep

SED=/bin/sed

CAT=/bin/cat

MAILFILE=/tmp/mailviews$$

MAILER=mail

## Change ADMIN Mail address as per the requirements ##

[email protected]

 

for path in $PATHS

do

## Validate the Disk Space Percentage ##

DISK_AVAIL=`/bin/df -k / | grep -v Filesystem |awk '{print $5}' |sed 's/%//g'`

if [ $DISK_AVAIL -ge $THRESHOLD ]

then

echo "Please clean up your stuff nn"

echo "Please clean up your stuff nn" > $MAILFILE

$CAT $MAILFILE | $MAILER -s "Clean up stuff" $mailto

fi

done

## END of the Script ##

 

Before we run this script we need to setup SMTP

Prerequisites

1.Linux operating system

2.SMTP Configuration (SMTP server details and authentication credentials)Your favorite editor (Optional)

3.Configure Gmail SMTP/ready with any other custom SMTP server details. In this tutorial, you will learn sending emails using the Gmail SMTP, so ensure that; 

 

The following are a few Google SMTP server configurations:

 

1.SMTP Server/Hostname: smtp.gmail.com

2.SMTP Username: [Your Gmail Address]

3.SMTP password: [Your Gmail Password]

4.SMTP Port: 587

5. TLS/SSL: Required



 

Step to configure it.

# sudo apt-get install ssmtp

# sudo yum install ssmtp

# sudo yum --enablerepo=extras install epel-release

# sudo vim /etc/ssmtp/ssmtp.conf 


 

mailhub=smtp.gmail.com:587

useSTARTTLS=YES

AuthUser=username-here

AuthPass=password-here

TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

 

# echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv [email protected]

 

#!/bin/sh  

SUBJECT="Test Subject"

TO="[email protected]"

MESSAGE="Hey There! This is a test mail"

 

echo $MESSAGE | sudo ssmtp -vvv $TO

 

# sudo chmod 755 mail.sh

# sudo ./mail.sh

Hope now you are able to send mails using the shell script too.

 

We are a software development company that provides end-to-end web and mobile app development solutions for cross-industry use cases. Explore our SaaS app development services here. For more detail, contact us at [email protected].

About Author

Author Image
Yash Vishwakarma

He is DevOps engineer and enthusiastic to work in Linux, Cloud technology like AWS (Amazon web services).AWS, docker, gitlab in DevOps envirnment and willing to learn new technologies and like to play table tennis and snooker.

Request for Proposal

Name is required

Comment is required

Sending message..