#!/bin/sh #collect the information from system
/usr/local/bin/top -b -n 1 -d 5 >/mydir/top.txt
pwd=/mydir
#seperate the cpu info\mem\swap from top.txt
/usr/bin/awk '{ if (NR==3) print $0 }' ${pwd}/top.txt >${pwd}/cpu.txt
/usr/bin/awk '{ if (NR==4) print $0 }' ${pwd}/top.txt >${pwd}/mem.txt
#memory max value
/usr/bin/awk -F, '{ print $1}' ${pwd}/mem.txt >${pwd}/mem_max.txt
/usr/bin/awk '{ print $2}' ${pwd}/mem_max.txt >${pwd}/mem_max_value.txt
/usr/bin/cat ${pwd}/mem_max_value.txt | /usr/bin/awk '{print substr($0,1,((length($0)-1)))}' >${pwd}/mem_max_real_value.txt
/usr/bin/cat ${pwd}/mem_max_value.txt | /usr/bin/awk '{print substr($0,((length($0))))}' >${pwd}/mem_max_unit_value.txt
#memory free value
/usr/bin/awk -F, '{ print $2}' ${pwd}/mem.txt >${pwd}/mem_free.txt
/usr/bin/awk '{ print $1}' ${pwd}/mem_free.txt >${pwd}/mem_free_value.txt
/usr/bin/cat ${pwd}/mem_free_value.txt | /usr/bin/awk '{print substr($0,1,((length($0)-1)))}' >${pwd}/mem_free_real_value.txt
/usr/bin/cat ${pwd}/mem_free_value.txt | /usr/bin/awk '{print substr($0,((length($0))))}' >${pwd}/mem_free_unit_value.txt
#max swap
/usr/bin/awk -F, '{ print $3}' ${pwd}/mem.txt >${pwd}/swap_max.txt
/usr/bin/awk '{ print $1}' ${pwd}/swap_max.txt >${pwd}/swap_max_value.txt
/usr/bin/cat ${pwd}/swap_max_value.txt | /usr/bin/awk '{print substr($0,1,((length($0)-1)))}' >${pwd}/swap_max_real_value.txt
/usr/bin/cat ${pwd}/swap_max_value.txt | /usr/bin/awk '{print substr($0,((length($0))))}' >${pwd}/swap_max_unit_value.txt
#free swap
/usr/bin/awk -F, '{ print $4}' ${pwd}/mem.txt >${pwd}/swap_free.txt
/usr/bin/awk '{ print $1}' ${pwd}/swap_free.txt >${pwd}/swap_free_value.txt
/usr/bin/cat ${pwd}/swap_free_value.txt | /usr/bin/awk '{print substr($0,1,((length($0)-1)))}' >${pwd}/swap_free_real_value.txt
/usr/bin/cat ${pwd}/swap_free_value.txt | /usr/bin/awk '{print substr($0,((length($0))))}' >${pwd}/swap_free_unit_value.txt
#calculate the used mem with M unit
mem_max_real_value="`/usr/bin/cat ${pwd}/mem_max_real_value.txt`"
mem_free_real_value="`/usr/bin/cat ${pwd}/mem_free_real_value.txt`"
mem_max_unit_value="`/usr/bin/cat ${pwd}/mem_max_unit_value.txt`"
mem_free_unit_value="`/usr/bin/cat ${pwd}/mem_free_unit_value.txt`"
if [ $mem_max_unit_value = "M" ] && [ $mem_free_unit_value = "M" ]; then
/usr/bin/echo "`/usr/bin/expr $mem_max_real_value - $mem_free_real_value`\c" > ${pwd}/mem_used.txt
/usr/bin/echo "M" >>${pwd}/mem_used.txt
fi
#calculate the used mem with G unit
if [ $mem_max_unit_value = "G" ] && [ $mem_free_unit_value = "G" ]; then
/usr/bin/echo "`/usr/bin/expr $mem_max_real_value - $mem_free_real_value`\c" > ${pwd}/mem_used.txt
/usr/bin/echo "G" >>${pwd}/mem_used.txt
fi
#calculate the swap with M unit
swap_max_real_value="`/usr/bin/cat ${pwd}/swap_max_real_value.txt`"
swap_free_real_value="`/usr/bin/cat ${pwd}/swap_free_real_value.txt`"
swap_max_unit_value="`/usr/bin/cat ${pwd}/swap_max_unit_value.txt`"
swap_free_unit_value="`/usr/bin/cat ${pwd}/swap_free_unit_value.txt`"
if [ $swap_max_unit_value = "M" ] && [ $swap_free_unit_value = "M" ]; then
/usr/bin/echo "`/usr/bin/expr $swap_max_real_value - $swap_free_real_value`\c" > ${pwd}/swap_used.txt
/usr/bin/echo "M" >>${pwd}/swap_used.txt
fi
#calculate the used swap with G unit
if [ $mem_max_unit_value = "G" ] && [ $mem_free_unit_value = "G" ]; then
/usr/bin/echo "`/usr/bin/expr $mem_max_real_value - $mem_free_real_value`\c" > ${pwd}/mem_used.txt
/usr/bin/echo "G" >>${pwd}/mem_used.txt
fi
#calculate used cpu%
/usr/bin/awk -F, '{ print $1}' ${pwd}/cpu.txt >${pwd}/cpu_idle.txt
/usr/bin/awk '{ print $3}' ${pwd}/cpu_idle.txt >${pwd}/cpu_idle_real_value_percent.txt
/usr/bin/cat ${pwd}/cpu_idle_real_value_percent.txt | /usr/bin/awk '{print substr($0,1,((length($0)-1)))}' >${pwd}/cpu_idle_real_value.txt
/usr/bin/awk -F. '{print $1}' ${pwd}/cpu_idle_real_value.txt >${pwd}/cpu_idle_real_value_int.txt
cpu_idle_real_value="`/usr/bin/cat ${pwd}/cpu_idle_real_value_int.txt`"
/usr/bin/echo "`/usr/bin/expr 100 - $cpu_idle_real_value`\c" >${pwd}/cpu_used.txt
/usr/bin/echo "%" >>${pwd}/cpu_used.txt
#all info load into all.txt
/usr/bin/echo "`/bin/hostname`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/bin/date`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/usr/bin/cat ${pwd}/cpu_used.txt`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/usr/bin/cat ${pwd}/mem_max_value.txt`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/usr/bin/cat ${pwd}/mem_used.txt`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/usr/bin/cat ${pwd}/swap_max_value.txt`;\c" >>${pwd}/all.txt
/usr/bin/echo "`/usr/bin/cat ${pwd}/swap_used.txt`" >>${pwd}/all.txt
id for instruction and system information
if [ -d ${pwd}/ServStatus ]
then
echo "The file for collecting system information will be put into ServStatus directoy!"
else
/usr/bin/mkdir ${pwd}/ServStatus
fi
hostname=`/usr/bin/hostname`
date=`/usr/bin/date +%Y%m%d%H%M%S`
filename=ServStatus_${hostname}_${date}
/bin/awk 'BEGIN{ print "hostname"";""date"";""cpu%"";""mem_max"";""mem_used"";""swap_max"";""swap_used"} { print $0}' ${pwd}/all.txt >${pwd}/ServStatus/${filename}.txt
#remove all txt files
rm -f ${pwd}/cpu*.txt
rm -f ${pwd}/mem*.txt
rm -f ${pwd}/swap*.txt
rm -f ${pwd}/all.txt
rm -f ${pwd}/top.txt
if [ -d ${pwd}/BackUpServStatus ]
then
/bin/mv ${pwd}/ServStatus/*.txt ${pwd}/BackUpServStatus/
echo "The file for collecting system information will be moved into BackUpServStatus directoy!"
else
mkdir ${pwd}/BackUpServStatus
/usr/bin/mv ${pwd}/ServStatus/*.txt ${pwd}/BackUpServStatus/
fi
echo "It has been successfully stored!"
|