Quantcast
Channel: Syed Jahanzaib – Personal Blog to Share Knowledge !
Viewing all articles
Browse latest Browse all 408

Bash Script for General Customizable Report via Email

$
0
0

Note for MySelf:

This post contains bash script sample , which upon executed, can query various system components & send the report via email. Useful to monitor remote server. Further functions can be added or existing can be customized according to the requirements. I opted for LOOP Formula to show mysql DB sizes in MB/GB using IF ELSE statements & some other fun stuff for myself as well.

The script is bit messy & scrambled in terms of proper organized display, but it works fine. You may customized or trim as per your taste

Feel free to use as you like …

Regard’s
Syed Jahanzaib


#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc
#set -x
# Version 1.1 / 10th January, 2014
# Last Modified / 3rd-MARCH-2021
# Syed Jahanzaib / Web: https://aacable.wordpress.com / Email: aacabl AT hotmail DOT com
# This script generalized & customized DISK reports and email to admin
# Adjust below DATA fields accordingly. remove / add desired tasks.
# Settings various VARIABLES for the script
clear
# Colors Config ... [[ JZ ... ]]
COMPANY="MY COMPANY - PK"
#MYSQL DETAILS
SRV="mysql"
SQLUSER="root"
SQLPASS="MYSQL_PASSWD"
export MYSQL_PWD=$SQLPASS
CMD="mysql -uroot --skip-column-names -e"
ALL_DB_TEMP_LIST="/tmp/mysq_all_dbs.txt"
DB1="radius"
DB2="conntrack"
DB3="syslog"
SQL_ACCOUNTING_TABLE="radacct"
CMD="mysql -u$SQLUSER --skip-column-names -s -e"
EMAILMSG="/tmp/report1.log"
> $EMAILMSG
HOSTNAME=`hostname`
INT_IP1=`hostname -I`
INT_IP2=`ip route get 1 | awk '{print $NF;exit}'`
EXT_IP=`dig +short myip.opendns.com @resolver1.opendns.com`
URL="google.com"
DNS=$(cat /etc/resolv.conf | sed '1 d' | awk '{print $2}')

# Check OS Type
os=$(uname -o)
# Check OS Release Version and Name
###################################
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`

GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}

if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
ARCH=`uname -p`
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
KERNEL=`uname -r`
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SuSE-release ] ; then
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/os-release ]; then
DIST=`awk -F "PRETTY_NAME=" '{print $2}' /etc/os-release | tr -d '\n"'`
elif [ -f /etc/debian_version ] ; then
DIST="Debian `cat /etc/debian_version`"
REV=""
fi
if ${OSSTR} [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi
# Check Architecture
architecture=$(uname -m)
# Check Kernel Release
kernelrelease=$(uname -r)
#SET DATE TIME
set $(date)
time=`date |awk '{print $4}'`
DT=`date +%d.%b.%Y_time_%H.%M`
DATE=$(date +%Y-%m-%d)
DT_HMS=$(date +'%H:%M:%S')
FULL_DATE=`date`
TODAY=$(date +"%Y-%m-%d")
TODAYYMD=`date +"%d-%b-%Y"`
#Get ip which have default route
logger General report has been started @ $DATE / $DT_HMS

# Adding OS level Details in email message
# disk we want to monitor, make sure to change this
DISK=`df -h | grep 'Filesystem\|/dev/sda*' > /tmp/general_report.log`
DISK_DETAILS=`cat /tmp/general_report.log`
SESSIONS=`$CMD "use radius; SELECT username FROM $SQL_ACCOUNTING_TABLE WHERE acctstoptime IS NULL;" |wc -l`
DISKTOT=`df -h $DISK |awk '{print $2}'| sed -n 2p`
DISKUSED=`df -h $DISK |awk '{print $3}'| sed -n 2p`
DISKAVA=`df -h $DISK |awk '{print $4}'| sed -n 2p`
DISKUSEPER=`df -h $DISK |awk '{print $5}'| sed -n 2p`
MEMTOT=`free -m |awk '{print $2}'| sed -n 2p`
MEMUSED=`free -m |awk '{print $3}'| sed -n 2p`
MEMAVA=`free -m |awk '{print $4}'| sed -n 2p`
MEMUSEDPER=`free -m | grep Mem | awk '{print $3/$2 * 100.0}'`
MEMAVAPER=`free -m | grep Mem | awk '{print $4/$2 * 100.0}'`

#GMAIL Details
GMAILID="yourgmail@gmail.com "
GMAILPASS="GMAILPASS"
TO1="adminmail1@hotmail.com"
SMTP="64.233.184.108:587"

#Collect all data in file
echo "Mysql HTML Backup Info for

===============
NETWORK DETAILS:
===============
HOST: $HOSTNAME
Operating System Type $os
$OSSTR
Architecture : $architecture
Kernel Release : $kernelrelease
INT_IP1: $INT_IP1
INT_IP2: $INT_IP2
EXT_IP: $EXT_IP
DNS: $DNS

================
MYSQL DB REPORT:
================" >> $EMAILMSG

# Fetch ALL DB's & calculate there sizes and convert sizes in MB/GB
MYSQLALLDB=`$CMD "show databases;" > $ALL_DB_TEMP_LIST`
num=0
cat $ALL_DB_TEMP_LIST | while read database
do
num=$[$num+1]
DB=`echo $database | awk '{print $1}'`
MYSQLDBSIZE=`$CMD "SELECT table_schema '$DB', sum(data_length + index_length)/1024/1024 FROM information_schema.TABLES WHERE table_schema='$DB' GROUP BY table_schema;" | cut -f1 -d"." | sed 's/[^0-9]*//g'`
if [ "$MYSQLDBSIZE" -ge 1024 ]; then
MYSQLDBSIZE_FINAL=`echo "scale=2; $MYSQLDBSIZE/1024" |bc -l`
echo "$DB / $MYSQLDBSIZE_FINAL GB" >> $EMAILMSG
else
echo "$DB / $MYSQLDBSIZE_FINAL MB" >> $EMAILMSG
fi
done
echo "RADIUS Online Users = $SESSIONS Users

=============
Disk Details:
=============

$DISK_DETAILS

==============
MEMORY REPORT:
==============
Total RAM = $MEMTOT MB
Total RAM Used = $MEMUSED MB
Total RAM Available = $MEMAVA MB
Total RAM Used Percent = $MEMUSEDPER %
Total RAM Available Percent = $MEMAVAPER %

$COMPANY

Syed Jahanzaib / aacable@hotmail.com / https://aacable.wordpress.com" >> $EMAILMSG

# PRINT INFO SECTION #########
# Print Fetched Information on Screen , for info to see
cat $EMAILMSG

# EMAIL SECTION ##############
# Make sure you install sendEMAIL tool and test it properly before using email section.
#SEND EMAIL Alert As well using sendEMAIL tool using GMAIL ADDRESS.
# If you want to send email , use below ...
echo " - Sending SMS/EMAIL info ..."
#curl "http://$KHOST/cgi-bin/sendsms?username=$KID&password=$KPASS&to=$CELL2+$CELL3+$CELL4" -G --data-urlencode text@$SMSMSG
sendemail -u "$HOSTNAME - $EXT_IP - General Report- $DATE " -o tls=yes -s $SMTP -t $TO1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$EMAILMSG -o message-content-type=text

# log entry in /var/log/syslog
logger General Report have been end @ $DATE / $DT_HMS

Sample snapshot for Email Reporting !


Howto install ‘sendemail’ tool to send email via Gmail ID.

Very Well with Tested For UBUNTU 12.x , may work on other ubuntu versions too

Quick copy paste …

1
2
apt-get -y install libio-socket-ssl-perl libnet-ssleay-perl perl
apt-get -y sendemail

 


Viewing all articles
Browse latest Browse all 408

Trending Articles