CentOS – RedHat: A “systeminfo” like script for Linux

Windows has a nice command 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 root to run this script. Unless of course you have setup sudo to run the “lsof” command.

#!/bin/bash
############################################
#This script is broken down into functions #
############################################
############################################
#Below all the functions are declared.
############################################
clear
function Operating_SystemVersion_info ()
{
V_Server_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' | awk '{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()
{
CPU_Cores=`cat /proc/cpuinfo | grep 'processor' | wc -l`
CPU_15min_Load=`uptime | awk '{print $10}'`
CPU_10min_Load=`uptime | awk '{pring $11}'`
CPU_15min_Load=`uptime | awk '{print $12}'`
}

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"
Links=`find /etc/sysconfig/network-scripts/ -name ifcfg-* -type f | xargs  grep 'IPADDR' | awk -F"/" '{print $5}' | awk '1;!(NR%1){print " ";}'`
echo $Links | tr ' ' '\n' > F_net_interfaces
for loop_interfaces in `cat F_net_interfaces`;
do
printf  "\t""\t""$loop_interfaces""\n";
done
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 "HYPERVISOR STATUS""\n""\n"
dmesg | grep -i '"Hypervisor detected: VMware"' >> /dev/null  2>&1
if [[ $? -eq 0 ]];
then
printf "\t""\t""This is a VMware Virtual Machine""\n"
else
printf "\t""\t""This is Physical Machine""\n"
fi

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.