Disk Check with Alert



This script checks the disk usage of servers and emails. sshpass is used only for pfsense for the rest servers ssh keys are used to connect.

#!/bin/bash
 DATE=`date +%Y-%m-%d`
 logfolder=/var/log/disk_usage/
 logfile=$logfolder/$DATE-disk-usage.log
 alertlogfile=$logfolder/$DATE-disk-usage-alert.log
 key=/example/keys/id_rsa_nagios
 sendemail=/example/scripts/sendEmail.pl
 SENDEMAIL=/example/scripts/sendEmail.pl
 MAIL_FROM="My Domain <automation@example.com>"
 MAIL_TO="admin@example.com"
 MAIL_HOST='example.com:587'
 MAILUSER=emailuser@example.com
 MAILPASS='emailuserpass'
 subject=$DATE-disk-usage-alert
 if [ ! -d $logfolder ]
 then
 mkdir -p $logfolder
 fi
 #addspace=`echo "" >> $logfile`
 ALERT=80
 IP=(192.168.0.10 192.168.0.20 192.168.0.30 192.168.0.40 192.168.0.50 192.168.0.60 192.168.0.10 192.168.0.70 192.168.0.10 192.168.0.80) # my server ips

function alert
 {
 grep -vE '^Filesystem|tmpfs|cdrom|devfs' | awk '{ print $5 " " $1 " " $6 }' | while read output;
 do
 #    echo output is $output for $Host
 usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f 1 )
 partition=$(echo $output | awk '{ print $2 }' )
 usem=$(echo $output | awk '{print $3}')
 # echo usep=$usep  usem=$usem partiton=$partition
 if [ $usep -ge $ALERT ]; then
 #echo "Running out of space \"$partition ($usep%)\" on $Host as on $DATE" |
 #mail -s "Alert: Almost out of disk space $usep" $ADMIN
 echo "Running out of space  \"$partition ($usep%) $usem\""  >> $alertlogfile
 fi
 done

}

## only for pfsense
 #:'
 echo "" >> $logfile
 Host=`sshpass -p PASSWD ssh -o StrictHostKeyChecking=no  -o 'KbdInteractiveDevices no'  root@192.168.0.1 hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "" >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no'  root@192.168.0.1 df -P -h >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no' root@192.168.0.1 df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 #'

#exit

function disk-check_local
 {
 ip=$1
 echo $ip
 echo "" >> $logfile
 Host=`ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "            $Host" >> $alertlogfile
 echo "" >> $logfile
 echo "" >> $alertlogfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h >> $logfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 echo "-----------------------------------------------------------------------" >> $alertlogfile
 }

for i in ${IP[@]}; do
 echo "processing $ip"
 disk-check_local $i
 #  echo "done with $ip"
 done

#exit
 cat $logfile
 cat $alertlogfile
 report=`cat $alertlogfile`

$SENDEMAIL -f "$MAIL_FROM" -t $MAIL_TO -s $MAIL_HOST -xu $MAILUSER -xp $MAILPASS -u "$subject" -m "$report" -a $logfile

Monitor Server Status and Notify when up/down

#!/bin/bash
## Global variables
TO=admin@example.com
CC=admin@example.com
FROM="Your Server Remote Monitor <automation@example.com>"
user=gmailuser
pass=gmailuserpass
SMTP=smtp.gmail.com:587
nitstatus=/tmp/nit

###### Check line up or down 
ping -c3 example.com ; [ $? == 0 ] && checkstatus=up || status is checkstatus=down ; clear ; echo status is  $checkstatus |tail -1

if [[  ! -f $nitstatus ]];
then
echo up > $nitstatus
fi
oldstatus=`cat /tmp/nit`
echo old status is $oldstatus
echo $checkstatus
if [[ $checkstatus == "up" ]]
then
newstatus=up
elif [[ $checkstatus == "down" ]]
then
newstatus=down
fi
###### Email notification ###

echo new status is $newstatus

if
[[ $oldstatus != $newstatus ]]
then

echo "Your Server server is $newstatus"
echo $newstatus > $nitstatus
                  if [[ $newstatus == "down" ]]
                  then
                  echo sending email for down
                  /scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Down" -m "
Your Server server is down on `date +%T`"
                  elif [[ $newstatus == "up" ]]
                  then 
                  echo sending email for up
                  /scripts/sendEmail.pl -f "$FROM" -t "$TO"  -s  "$SMTP" -xu "$user" -xp "$pass"  -u "Your Server Server is Up" -m "
Your Server server is up on `date +%T`" 
                  fi

else
echo no change
fi

Get inventory in the lan

This script needs whoe network pcs having linux os, I have used sshpass for understanding you can use ssh keys also. This is written for 3  seperate networks in lan ( 192.168.10.0/24 192.168.100.0/24 and 192.168.20.0/24)

The script will provide the username ip address mac address memory cpu info (* note my user having . in username so i wrote accordingly. you may have different and so you have to edit the script according to you.




#!/bin/bash
 downhosts=/var/log/hostsdetails/down
 uphosts=/var/log/hostsdetails/up
 inventory=/var/log/hostsdetails/inventory
 old=/var/log/hostsdetails/old/
 pass=PASSWORD
 net=(10 100 20)
 hour=0
 start_time=$(date +%s)

mv "$downhosts" "$old"
 mv "$uphosts" "$old"
 mv "$inventory" "$old"
 echo "USERNAME <-> IP-ADDRESS <-> MAC-ADDRESS <-> MEMORY <->  CPU-INFORMATION" >> $inventory

for i in ${net[@]}; do
 echo  "for '$i'th network"
 for j in $(seq 1 254); do ip=192.168.$i.$j && ping -c 1 $ip>/dev/null; [ $? -eq 0 ] && echo "$ip is UP" && mac=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ifconfig | grep eth0 | awk '{ print $5}'` && mem=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip"  free -m | grep Mem | awk '{print $2}'`&& user=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ls -l /proc /| awk '{print $3}'| grep "\." | uniq` && if [ "$user" == "" ]; then  echo Unable to get the logged in user information for $ip; fi && cpu=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" cat /proc/cpuinfo |grep 'model name' | head -n 1 | cut -d ":" -f 2` && echo "$user <-> $ip <-> $mac <-> $mem <-> $cpu" >> $inventory && echo "$ip" >> $uphosts || echo "$ip" >> $downhosts ; done
 done

finish_time=`date +%s`
 time_taken=`expr $finish_time - $start_time`
 if [ $time_taken -ge 60 ];
 then
 min=`expr $time_taken / 60`
 sec=`expr $time_taken % 60`
 if [ $min -ge 60 ];
 then
 hour=`expr $min / 60`
 min=`expr $min % 60`
 fi
 fi
 hour=`printf "%0*d\n" 2 $hour`
 min=`printf "%0*d\n" 2 $min`
 sec=`printf "%0*d\n" 2 $sec`
 echo "Script execution time: $hour:$min:$sec"

 

Mysql Backup script using mysqldump

This script is written to backup and email the status of backup

Please make sure to replace your domain with example.com, and user and passwords accordingly




#!/bin/bash

### GLOBAL VARIABLES ###
 HOST=`hostname`
 MYSQL="/usr/bin/mysql"
 MYSQLDUMP="/usr/bin/mysqldump"
 MYSQL_USER=backupuser
 MYSQL_PASS=DBPASSWORD
 TSTAMP=`date +%F`
 BACKUP_DIR=/example/backup/db/$HOST
 mkdir $BACKUP_DIR/$TSTAMP
 databases=`$MYSQL -u $MYSQL_USER -p$MYSQL_PASS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|phpmyadmin)"`
 DB_VERSIONS=7
 DELVERSION=`expr "$DB_VERSIONS" + 1`
 DELDATE=`date +%F --date="$DELVERSION days ago"`

### EMAIL VARIABLES ###

TO=admin@example.com
 FROM="Backup Automation <automation@example.com>"
 EMAILUSER=user@mydomain
 EMAILPASS=PASSWORD
 SMTP=mail.example.com
 SUBJECT="`hostname` - Backup status (DB) : $TSTAMP"
 SENDEMAIL="/example/scripts/sendEmail.pl"

###################### Creating Fresh Backup ###################

STARTTIME=`date +%s`
 START=`date +%r`

for db in $databases; do
 $MYSQLDUMP --routines --triggers --force --opt --max_allowed_packet=2G -u $MYSQL_USER  -p$MYSQL_PASS $db  > "$BACKUP_DIR/$TSTAMP/$db.sql" && mv "$BACKUP_DIR/$TSTAMP/$db.sql" "$BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql"
 done

BTIME=`date +%s`
 BT=`date +%r`
 BSTATUS=`echo $?`
 if [[ "$BSTATUS" == 0 ]]
 then
 BACKUPSTATUS=SUCCESSFULL
 else
 BACKUPSTATUS=FAILED
 fi

#for db in $databases;do
 #     $MYSQLDUMP --force --opt --routines --user=$MYSQL_USER -p$MYSQL_PASS --databases $db | gzip > "$BACKUP_DIR/$TSTAMP/$db.sql.gz" && mv "$BACKUP_DIR/$TSTAMP/$db.sql.gz" "$BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql.gz"
 #done

####################### Zipping Backups #########################

for db in $databases; do gzip -f $BACKUP_DIR/$TSTAMP/$TSTAMP-$db.sql; done
 ZSTATUS=`echo $?`
 CTIME=`date +%s`
 CT=`date +%r`

if [[ "$ZSTATUS" == 0 ]]
 then
 ZIPSTATUS=SUCCESSFULL
 else
 ZIPSTATUS=FAILED
 fi

####################### Deleting old backups ########################

echo rm -frv "$BACKUP_DIR/$DELDATE" >> /tmp/deleteinfo
 rm -frv "$BACKUP_DIR/$DELDATE"
 DSTATUS=`echo $?`

if [[ "$DSTATUS" == 0 ]]
 then
 DELSTATUS=SUCCESSFULL
 else
 DELSTATUS=FAILED
 fi
 #find "$BACKUP_DIR" -mtime +"$DELDATE" |while read DEL ; do rm -frv "$DEL";done

ENDTIME=`date +%s`
 END=`date +%r`

$SENDEMAIL -f "$FROM" -t "$TO" -s "$SMTP" -xu "$EMAILUSER" -xp "$EMAILPASS" -u "$SUBJECT" -m "
 Databases : `echo $databases| tr ' ' ,`

Summary :

DB Backup started at $START
 DB Backup finished at $BT
 DB Backup time is `expr $BTIME - $STARTTIME` seconds

DB Zipping starged at $BT
 DB Zipping finished at $CT
 DB Zipping time is `expr $CTIME - $BTIME` seconds

DB Deletion started at  $CT
 DB Deletion finished at $END
 Old DB Deleting time is `expr $ENDTIME - $CTIME` seconds

Total Time is `expr $ENDTIME - $STARTTIME` seconds

BACKUP is $BACKUPSTATUS
 GZIPPING SQL DB FILES is $ZIPSTATUS
 DELETING OLDVERSION is $DELSTATUS
 "
 echo Done

YUM repository configuration in RHEL 7

Adding local repository ( Using DVD )

1) mkdir /media/cdrom

2 ) mount /dev/cdrom /media/cdrom

3 ) create a repofile by following command

vi /etc/yum.repos.d/rhel.repo

name=Local Repository
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Adding external repositories

1 ) cd /tmp

2) wget https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
3) install the downloaded rpms

4)  sudo sed -i ‘s/enabled=0/enabled=1/g’ /etc/yum.repos.d/remi.repo

5) (optional )vi /etc/yum.repos.d/remi.repo

disable all test repositories  by setting value enabled=0

type command yum repolist to view list of repositories

Configuring apache mariadb and phpmyadmin on RHEL 7

1 ) Install apache mariadb php and phpmyadmin on server by using following command

#yum install php phpmyadmin httpd mariadb

2)  type below command and set the password for root

#mysql_secure_installation

3) #vim  /etc/httpd/conf.d/phpMyAdmin.conf

in the section/container <Directory /usr/share/phpMyAdmin/> </Directory>

add the following two lines.

Options Indexes FollowSymLinks
Require all granted

4) add firewall rules  and reload firewall

#firewall-cmd –permanent –add-port=80/tcp
#firewall-cmd –permanent –add-port=80/udp
#firewall-cmd –permanent –add-port=443/tcp
#firewall-cmd –permanent –add-port=443/udp

#firewall-cmd –reload

OR

#firewall-cmd –permanent –add-service=http

#systemctl restart firewalld

5) Now restart apache service by typing

#service httpd restart or

#systemctl restart httpd.service

6) checking and adding apache and mariadb services to startup

#systemctl is-enabled httpd

#systemctl is-enabled mariadb

if it says its disabled then enable it by following command.

#systemctl enable httpd

#systemctl enable mariadb

Ethernet Bonding ( Centos 6.8 )

Step 1 :

Create the file /etc/sysconfig/network-scripts/ifcfg-bond0 and specify the ip address,netmask & gateway

# vi /etc/sysconfig/network-scripts/ifcfg-bond0
 DEVICE=bond0
 IPADDR=192.168.100.10
 NETMASK=255.255.255.0
 GATEWAY=192.168.100.1
 TYPE=Bond
 ONBOOT=yes
 NM_CONTROLLED=no
 BOOTPROTO=static

Step:2 

Edit the files of eth0 & eth1 and make sure you enter the master and slave entry, as shown below
 # vi /etc/sysconfig/network-scripts/ifcfg-eth0
 DEVICE=eth0
 HWADDR=90:2b:34:7b:df:a4
 TYPE=Ethernet
 ONBOOT=yes
 NM_CONTROLLED=no
 MASTER=bond0
 SLAVE=yes

# vi /etc/sysconfig/network-scripts/ifcfg-eth1
 DEVICE=eth1
 TYPE=Ethernet
 HWADDR=e2:5b:f0:60:ba:d8
 ONBOOT=yes
 NM_CONTROLLED=no
 MASTER=bond0
 SLAVE=yes

 Step:3 

Create the Bond file(bonding.conf)
 # vi /etc/modprobe.d/bonding.conf ( for old versions use modprobe.conf )
 alias bond0 bonding
 options bond0 mode=1 miimon=100  <--- This can be defined in  ifcfg-bond0  the line will be BONDING_OPTS="mode=1 miimon=100"
change above line to
options bond0 mode=1 miimon=100 fail_over_mac=1  if you are configuring bonding under vmware workstation.



Step:4 

service network restart

Important Things to know

A ) Different Modes used in bonding
 balance-rr or 0 -- round-robin mode for fault tolerance and load balancing.
 active-backup or 1 -- Sets active-backup mode for fault tolerance.
 balance-xor or 2 -- Sets an XOR (exclusive-or) mode for fault tolerance and load balancing.
 broadcast or 3 -- Sets a broadcast mode for fault tolerance. All transmissions are sent on all slave interfaces.
 802.3ad or 4 -- Sets an IEEE 802.3ad dynamic link aggregation mode. Creates aggregation groups that share the same speed & duplex settings.
 balance-tlb or 5 -- Sets a Transmit Load Balancing (TLB) mode for fault tolerance & load balancing.
 balance-alb or 6 -- Sets an Active Load Balancing (ALB) mode for fault tolerance & load balancing.




B ) Commands

To check the bonding status

watch -n .1 cat /proc/net/bonding/bond0   

or

cat /proc/net/bonding/bond0


To Change active interface in bondig 1 ) remove active interface from bonding by command  echo -eth0 > /sys/class/net/bond0/bonding/slaves this will make the other slave active then add it again into the bonding. by running command echo +eth0 > /sys/class/net/bond0/bonding/slaves or use inenslave to change the active slave in bond To make eth0 active use below command ifenslave -c bond0 eth0 eth1