Collect Server Information using Script

#!/bin/sh -e

# This script will collect Server info and it will send a Email
# Author        : Ankam Ravi Kumar
# Date          : 13-March-2015

MAILIST="redhat.ark@gmail.com"
mkdir -p /tmp/asset/
touch /tmp/asset/`hostname`.txt
touch /tmp/temptext
TEMP=/tmp/temptext
LOG=`ls /tmp/asset/*.txt`
echo "## Host Information" > $LOG
echo "Host Name : `hostname` " >> $LOG
echo "`sudo /sbin/ifconfig -a |grep "inet addr" | awk 'BEGIN { FS = ":" } ; { print $2 }'`" >> $TEMP
echo "IP Address : `egrep '^10' $TEMP |awk '{ print $1}'`" >> $LOG

echo -en '\n' >> $LOG

## Colleting Hardware Details ##
echo " " >> $LOG
echo "## Hardware Information" >> $LOG
echo " " >> $LOG
echo "Serial Number     : `lshal |grep system.hardware.serial`" >> $LOG
echo "Serial Number     : `sudo /usr/sbin/dmidecode -s system-serial-number`" >> $LOG
echo "Model Number      : `lshal |grep system.hardware.product`" >> $LOG
echo "Model Number      : `sudo /usr/sbin/dmidecode |grep "SKU Number"`" >> $LOG
echo "Hardware Vendor   : `lshal |grep system.hardware.vendor`" >> $LOG
echo "Hardware Info     : `dmesg |grep DMI`" >> $LOG

## Redhat Version ##
echo " " >> $LOG
echo "## OS Version" >> $LOG
head -n1 /etc/issue >> $LOG
echo -en '\n' >> $LOG
uname -a >> $LOG

## CPU Info ##
echo " " >> $LOG
echo " " >> $LOG
echo "## CPU Information" >> $LOG
grep "model name" /proc/cpuinfo >> $LOG

## RAM/MEMORY Info ##
echo " " >> $LOG
echo " " >> $LOG
echo "## Memory Information" >> $LOG
grep MemTotal /proc/meminfo >> $LOG
y=`grep MemTotal /proc/meminfo |awk '{ print $2 }'`
mb="$(( $y / 1024 ))"
gb="$(( $mb / 1024 ))"
echo "RAM : $gb GB" >> $LOG

## Verify the machine is using NIS ##
echo -en '\n' >> $LOG
cat /etc/yp.conf |grep finnis > /tmp/yptemp.txt
count=`cat /tmp/yptemp.txt | wc -l`
if [ $count -gt 0 ];
then
echo "`hostname` is part of NIS Domain" >> $LOG
cat /etc/yp.conf |grep finnis >> $LOG
else
echo "`hostname` is not part of NIS" >> $LOG
fi

cat /etc/yp.conf |grep domain >> $LOG
/bin/mail -s "Linux Asset Inventory `hostname`" $MAILIST < $LOG

Installation and configuration of iSCSI initiator

Installation and configuration of iSCSI initiator, Mapping the LUN

iSCSI is a way of connecting storage devices over a network using TCP/IP. It can be used over a local area network (LAN), a wide area network (WAN), or the Internet. iSCSI devices are disks, tapes, CDs, and other storage devices on another networked computer that you can connect to.

First install the iSCSI utilities in Redhat Linux
# yum install iscsi* 

after completion of iscsi utilities, collect iscsi initiator name using below command.
 sample output of the command
# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:e5aa9bte281

then take this iqn number and map lun to this iqn using iscsi protocol from storage. after mapping the lun to server run below command to see the mapped LUN's.

# iscsiadm -m discovery -t st -p <Storage IP>
Starting iscsid:                                           [  OK  ]
10.100.201.236:3260,2000 iqn.1992-08.com.netapp:sn.118057100
10.101.127.18:3260,2001 iqn.1992-08.com.netapp:sn.118057100


Run below command to enable the iscsi service in startup

 # chkconfig iscsi on
 # iscsiadm -m node -T iqn.1992-08.com.netapp:sn.118057100 -p 10.100.201.236 -l
Logging in to [iface: default, target: iqn.1992-08.com.netapp:sn.118057100, portal: 10.100.201.236,3260] (multiple)
Login to [iface: default, target: iqn.1992-08.com.netapp:sn.118057100, portal: 10.100.201.236,3260] successful.



to see the mapped disk in your linux server 
#lsblk
Now you can able to see the new disk in your Linux server


Use the newdisk  and make the partitions Done.!!

Find WWN Number of HBA Card in Redhat Linux & CentOS

::Find WWN Number of HBA Card in Redhat & CentOS::

 # systool -c fc_host -v |grep "port_name"
    port_name           = "0x2100001b321fb7f6"

Alternate method to get
#cat /sys/class/scsi_host/hostN/device/fc_host/hostN/port_name
 
Check Port State using below command
 
#cat /sys/class/scsi_host/hostN/device/fc_host/hostN/port_state 

Where “N” is the number of device for your fiber HBAs.

Linux User Password expiry Monitoring

:: Linux User Password Expiry Monitoring ::

STEP 1: Create one file using bellow command

#touch expiry.sh

copy the below code and paste in expiry.sh file

#!/bin/bash
# Check Password expiry of specified user
# Author: Ankam Ravi Kumar
# Date: 6th July 2015


function check_usage {
        if (( $# != 1 ))
        then
                echo "Usage: ./expiry.sh <username>"
        exit 3
        fi
}

function calculate_days_till_expiry {
        get_expiry_date=$(/usr/sbin/lchage -l $1 | grep 'Password Expires' | cut -d: -f2)
        #if [[ $get_expiry_date = 'Never' ]]
        if [ $get_expiry_date = 'Never' ];
        then
                echo "Host Name: `hostname` User: $1 Password never expires"
        exit 0
        elif
        password_expiry_date=`date -d "$get_expiry_date" "+%s"`
        current_date=$(date "+%s")
        diff=$(($password_expiry_date-$current_date))
        let DAYS=$(($diff/(60*60*24)))
        then
        if (($DAYS>=15 && $DAYS<=90))
        then
                echo "OK - Host Name: `hostname` User: $1 Password is $DAYS days from expiry"
        exit 0
        elif (($DAYS>=5 && $DAYS<=14))
        then
                echo "WARNING - Host Name: `hostname` User: $1 Password is $DAYS days from expiry" > /tmp/tempuser
                mail -s "WARNING - Host Name: `hostname` User: $1 Password is $DAYS days from expiry" aravikumar48@gmail.com,$1 < /tmp/tempuser
        exit 1
        elif (($DAYS>=0 && $DAYS<=4))
        then
                echo "CRITICAL - Host Name: `hostname` User: $1 Password is $DAYS days from expiry"
                mail -s "CRITICAL - Host Name: `hostname` User: $1 Password is $DAYS days from expiry" aravikumar48@gmail.com,$1 < /tmp/tempuser
        exit 2
        fi
fi
}

check_usage $1
calculate_days_till_expiry $1


WARNING: Replace email address with your email address before execute

STEP 2: Then create one more called passwdcheck.sh using below command

#touch passwdcheck.sh

then copy the below code and paste in passwdcheck.sh file

 #!/bin/bash
# Check All the existing users Password expiry status
# Author: Ankam Ravi Kumar
# Date: 6th July 2016

for line in `cat /etc/passwd | cut -d: -f1` ; do
sh expiry.sh $line
done


STEP3: Execute the above file  using sh 

# sh passwdcheck.sh

STEP4: Add user email address in the below file to sent alerts directly to user

#vi /etc/aliases

in Last line of above file add

root:      ADMINEMAIL@DOMAIN.COM

Save & Exit