老板非要让书写一个读取系统主要系统的脚本,我只负责把系统的信息收集了放到一个文件中。然后dba负责用sqlloader将它作为一条记录插入到数据库中!
脚本写的不好,但可以用!其实这个脚本的思想很简单:通过awk剥离top中的信息!
#!/bin/sh
TERM=linux
export TERM
#collect the information from system
/usr/bin/top -b -d 5 -n 1 >${HOME}/mydir/top.txt
pwd=~/mydir
#seperate the cpu info\mem\swap from top.txt
/bin/awk '{ if (NR==3) print $0 }' ${pwd}/top.txt >${pwd}/cpu.txt
/bin/awk '{ if (NR==4) print $0 }' ${pwd}/top.txt >${pwd}/mem.txt
/bin/awk '{ if (NR==5) print $0 }' ${pwd}/top.txt >${pwd}/swap.txt
#max mem
/bin/awk -F, '{ print $1}' ${pwd}/mem.txt >${pwd}/mem_max.txt
/bin/awk '{ print $2}' ${pwd}/mem_max.txt >${pwd}/mem_max_value.txt
#usede mem
/bin/awk -F, '{ print $2}' ${pwd}/mem.txt >${pwd}/mem_used.txt
/bin/awk '{ print $1}' ${pwd}/mem_used.txt>${pwd}/mem_used_value.txt
#max swap
/bin/awk -F, '{ print $1}' ${pwd}/swap.txt >${pwd}/swap_max.txt
/bin/awk '{ print $2}' ${pwd}/swap_max.txt >${pwd}/swap_max_value.txt
#usede swap
/bin/awk -F, '{ print $2}' ${pwd}/swap.txt >${pwd}/swap_used.txt
/bin/awk '{ print $1}' ${pwd}/swap_used.txt>${pwd}/swap_used_value.txt
#cpu availability
/bin/awk -F, '{ print $1}' ${pwd}/cpu.txt >${pwd}/cpu_availability.txt
/bin/awk '{ print $2}' ${pwd}/cpu_availability.txt >${pwd}/cpu_availability_value.txt
#all info load into all.txt
/bin/echo -e "`/bin/hostname`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/date`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/cat ${pwd}/cpu_availability_value.txt`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/cat ${pwd}/mem_max_value.txt`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/cat ${pwd}/mem_used_value.txt`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/cat ${pwd}/swap_max_value.txt`;\c" >>${pwd}/all.txt
/bin/echo -e "`/bin/cat ${pwd}/swap_used_value.txt`" >>${pwd}/all.txt
# print the head for instruction and system information
if [ -d ~/mydir/ServStatus ]
then
echo "The file for collecting system information will be put into ServStatus directoy!"
else
mkdir ~/mydir/ServStatus
fi
hostname=`hostname`
date=`/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 >~/mydir/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
提示:
TERM=linux
export TERM是需要的, 否则会在/var/spool/mail/xliu产生如下的提示: TERM environment variable not set.
top运行时必须加上-b参数, 否则放到cron中,尽管脚本运行,但是不会将top后的输出放到top.txt中去, 在这里花费了两个小时左右的时间!
这个脚本已经运用到生产环境中了, 用oracle中的sqlloader导入这一条数据,很容易搞定的!
阅读(1564) | 评论(0) | 转发(0) |