GIT – has a nice called “systeminfo” which provides a collection of general information for the Operating System. I created a Bash script that does something similar.

Note 1) this is part of a larger script I am creating which will import Operating System data into a Database.

Note 2) In order to view the network connection called in one of the functions in this script, you will have to be logged in as to run this script. Unless of course you have setup sudo to run the “lsof” command.

Download : http://www.gocit.vn/files/check.sh

Running :

yum install lsof bc

wget http://www.gocit.vn/files/check.sh

chmod +x check.sh

./check.sh

info1

info2

#!/bin/bash
############################################
#This script is broken down into functions #
############################################
############################################
#Below all the functions are declared.
############################################
clear
function Operating_SystemVersion_info ()
{
V__Name=`hostname`
V_OS_Version=`cat /etc/redhat-release`
echo “Host: $V_Server_Name Version: $V_OS_Version”;
echo “-”
}

function Memory_Usage()
{
Total_Memory=`free -m | grep ‘Mem’ | ‘{print $2}’`
Used_Memory=`free -m | grep ‘Mem’ | awk ‘{print $3}’`
Free_Memory=`free -m | awk ‘FNR == 2 {print $4}’`
Swap_Size=`cat /proc/swaps | grep ‘partition’ | awk ‘{print $3}’`
Swap_Used_Space=`cat /proc/swaps | grep ‘partition’ | awk ‘{print $4}’`
Swap_info=`cat /proc/swaps | grep ‘partition’ | awk ‘{print “partition: ” $1 ” Priority ” $5}’`
printf “MEMORY INFO \n”
printf “\t””\t””Total Memory $Total_Memory-MBs””\n”
printf “\t””\t””Used Memory $Used_Memory-MBs””\n”
printf “\t””\t””Free Memory $Free_Memory-MBs””\n”
printf “\t””\t””Used Swap $Swap_Used_Space-MBs””\n”
printf “\t””\t””Swap priority info $Swap_info””\n”
echo “”
}

function CPU_Load_Usage()
{
printf “CPU INFO \n”
#printf “\t””\t””$CPU_Cores $CPU_15min_Load $CPU_10min_Load $CPU_15min_Load “”\n”
load_5min=`uptime | awk ‘{print $9}’ | ‘s/,//’`;
load_10min=`uptime | awk ‘{print $10}’ | sed ‘s/,//’`;
load_average=`echo “(( $load_5min + $load_10min ) / 2)” | bc -l | cut -c 1-4`;
cpu_cores=`cat /proc/cpuinfo | grep ‘processor’ | wc -l`
cpu_model=`cat /proc/cpuinfo | grep ‘model name’| uniq`
printf “\t””\t””Load 5 min $load_5min””\n”
printf “\t””\t””Load 10 min $load_10min””\n”
printf “\t””\t””CPU Load average $load_average””\n”
printf “\t””\t””CPU Model: $cpu_model””\n”
printf “\t””\t””Num of cored $cpu_cores””\n”
echo “”
#—
if [[ “$cpu_cores” > “$load_average” ]]
then
printf “\t””\t””CPU Load is at Normal levels – Below the number of cores.””\n”
printf “\t””\t””Load is at $load_average – Number of cores is $cpu_cores.””\n”
fi
#—
if [[ “$cpu_cores” < “$load_average” ]]
then
printf “\t””\t””CPU Load is above number of cores.””\n”
printf “\t””\t””Load is at $load_aver – Number of cores is $cpu_cores.””\n”
mailx -s “CRITICAL: CPU Load is above number of cores.””\n”
fi
#—
if [[ “$cpu_cores” = “$load_average” ]]
then
printf “\t””\t””CPU Load is at maximum capacity. Please check””\n”
printf “\t””\t””Load is at $load_aver – Number of cores is $cpu_cores.””\n”
mailx -s printf “\t””\t””WARNING CPU Load is at maximum capacity. Please check””\n”
fi
echo “”
}

function Storage_usage ()
{
printf “STORAGE INFO \n”
df -ah | awk ‘/%/ {print “\t””\t”$3 ” ” $4 ” ” $5 ” ” $6}’
echo “”
swap_size=`cat /proc/sys/vm/swappiness`
printf “\t””\t””Swap File usage: $swap_size””\t””\n”
printf “\t””\t””0 = OS is not using swap filesystem””\n”
printf “\t””\t””100 = OS is fully using swap filesystem””\n””\n”
}

function Top_output {
echo “RUNNING PROCESSES”
ps -auxe 2>/dev/null | awk ‘{print $1}’ | sort | uniq -c
echo “”
}

function Network_Setup_Information ()
{
printf “NET INTERFACES \n”
printf “\t””\t”
/sbin/ifconfig | grep -v ‘127.0.0.1’ |grep -B1 “inet addr” |awk ‘{ if ( $1 == “inet” ) { print $2 } else if ( $2 == “Link” ) { printf “%s:” ,$1 } }’ |awk -F: ‘{ print $1 “: ” $3 }’

echo “”
}

function Network_Activity_Information ()
{
touch F_port_scan
>F_port_scan
printf “NETWORK CONNECTIONS \n\n”
lsof -i -n -P | awk ‘/LISTEN/ {print “Service: ” $1, “is listening on port: ” $9}’ | grep -v 127.0.0.1 | sort -u >> F_port_scan
while read F_port_scan;
do
printf “\t””\t””$F_port_scan””\t””\n”;
done < F_port_scan;
echo “”
}
function swappiness_Information ()
{
swap_size=`cat /proc/sys/vm/swappiness`
printf “\t””\t”0 = OS is not using swap filesystem”
printf “\t””\t”100 = OS is fully using swap filesystem”
printf “\t””\t””$swap_size””\t””\t”
}
Operating_SystemVersion_info;
Memory_Usage;
CPU_Load_Usage;
Storage_usage;
Top_output;
Network_Setup_Information;
Network_Activity_Information;
#—
#Checking to see if it s a virtual server or not.
printf “VIRTUAL SERVER STATUS””\n””\n”
dmesg | grep -i ‘VMware’ >> /dev/null 2>&1
if [[ $? -eq 0 ]];
then
printf “\t””\t””This is a Virtual Machine””\n”
else
printf “\t””\t””This is Physical Machine””\n”
fi

Print Friendly

Comments

comments

Bài viết liên quan