Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2809833
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类:

2009-07-03 11:30:01

书写一个脚本, 定期收集系统的信息(利用cron每两个小时执行一次该脚本), 并通过上sqlloader把它作为一条记录存入数据库中
前面在linux下写过一个, 但在solaris下的top和linux的top显示的内容不同。 这个稍微麻烦一点。
经测试在solaris是cron是正常的!
写的不好,凑活着看吧!

#!/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!"

阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~