欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

获取磁盘IO与系统负载Load的shell脚本

程序员文章站 2023-01-08 20:18:41
1,提取系统负载信息 复制代码 代码如下:#!/bin/shhost=$(hostname)channel=$(hostname | sed 's/[0-9]//g')r...

1,提取系统负载信息

复制代码 代码如下:

#!/bin/sh
host=$(hostname)
channel=$(hostname | sed 's/[0-9]//g')
runday=$(date +%y-%m-%d)
iphost=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
i=1
 while [ $i -le 120 ];do
 rundaytime=$(date "+%y-%m-%d %h:%m:%s")
 /bin/cat /proc/loadavg | awk -f " " '{print "'"$rundaytime"'"","$1","$2","$3","$4","$5}' >> /tmp/$(hostname)-${runday}load.txt
 /usr/bin/sar -u 1 1 | grep -v average | /bin/sed -n '4,$p' | awk -f " " '{print "'"$rundaytime"'"","$4","$6","$7","$9}'  >>  /tmp/$(hostname)-${runday}sar.txt
 i=$((i+1))
 sleep 59
done
awk -f "," '{print "'"$host"'"",""'"$channel"'"",""'"$iphost"'"","$1","$2","$3","$4","$5}' /tmp/$(hostname)-${runday}sar.txt > /tmp/$(hostname)${runday}sar.txt
awk -f "," '{print "'"$host"'"",""'"$channel"'"",""'"$iphost"'"","$1","$2","$3","$4","$5","$6}' /tmp/$(hostname)-${runday}load.txt  > /tmp/$(hostname)${runday}load.txt
rm -rf /tmp/$(hostname)-${runday}sar.txt
rm -rf /tmp/$(hostname)-${runday}load.txt

2,代码2

复制代码 代码如下:

#!/bin/sh
pdir=/monitor/nagios/shell/sarlog
runday=$(date +%y-%m-%d)
tmpdir=/monitor/nagios/tmp
for i in `/bin/cat /monitor/nagios/shell/sarlog/machine.lst`
do
   /usr/bin/scp ${pdir}/sarlog.sh ${i}:/root && /usr/bin/rsh ${i} /root/sarlog.sh &
done

3,代码3

复制代码 代码如下:

#!/bin/sh
pdir=/monitor/nagios/shell/sarlog
runday=$(date +%y-%m-%d)
tmpdir=/monitor/nagios/shell/sarlog/log
machinelst=/monitor/nagios/shell/sarlog/machine.lst
for i in `/bin/cat /monitor/nagios/shell/sarlog/machine.lst`
do
   /usr/bin/scp ${i}:/tmp/*${runday}sar.txt ${tmpdir}
   /usr/bin/scp ${i}:/tmp/*${runday}load.txt ${tmpdir}
done
/bin/cat ${tmpdir}/*${runday}sar.txt >> ${tmpdir}/tmp${runday}.txt
/bin/cat ${tmpdir}/*${runday}load.txt >> ${tmpdir}/${runday}loadresult.txt
count=1
line=$(cat ${tmpdir}/tmp${runday}.txt |wc -l)
>${tmpdir}/result${runday}.txt
while [ "$count" -le "$line" ]
do
        idle=$(sed -n "${count}p" ${tmpdir}/tmp${runday}.txt | awk -f "," '{print $8}')
        busy=`echo "scale=2;100-$idle"|bc`
        sed -n "${count}p" ${tmpdir}/tmp${runday}.txt | sed  "s/$/,$busy/g" >> ${tmpdir}/${runday}sarresult.txt
        let count=count+1
done
/usr/local/mysql/bin/mysql -u root -d nagios -pbj7n48dm  <<eof
   load data infile '${tmpdir}/${runday}sarresult.txt' into table t_daily_stats fields terminated by  ',';
   load data infile '${tmpdir}/${runday}loadresult.txt' into table t_daily_load fields terminated by  ',';
eof
rm -rf ${tmpdir}/*${runday}sar.txt
rm -rf ${tmpdir}/*${runday}load.txt
rm -rf rm -rf ${tmpdir}/*${runday}.txt