使用dstat模拟vmstat
vmstat 1 5
dstat -psmgdyc 1 5
修改脚本,输出主机名
修改/usr/share/dstat/dstat_helloworld.py
self.name = 'plugin title'
替换为:
self.name = 'hostname'
self.nick = ('counter',)
替换为:
self.nick = ('------------',)
self.val['text'] = 'Hello world!'
替换为:
self.val['text'] = os.popen('hostname').readlines()[0].split()
修改-t参数时间戳输出格式
export DSTAT_TIMEFMT='%Y-%m-%d %H:%M:%S'
测试监控命令
dstat --helloworld -tlcp \
--proc-count -y \
--top-cpu-adv \
--top-cputime-avg \
--top-latency-avg \
--top-bio-adv \
--top-io-adv \
--top-mem \
--top-childwait \
-mgsn --net-packets --tcp --udp \
--fs --lock \
-dr --aio --disk-tps --disk-util \
--freespace --noheaders \
1 10 >>checkOS.info_$(date +%F_%H_%M_%S)
命令解释
dstat --helloworld -t \
# 1列主机名,2列时间戳
-lc \
# 3列负载,4列cpu状态
-p --proc-count -y \
# 5列进程队列,6列进程总计,7列中断和上下文切换
--top-cpu-adv \
# 8列最耗CPU进程
--top-cputime-avg \
# 9列平均最耗CPU时间进程
--top-latency-avg \
# 10列平均最耗延迟进程
--top-bio-adv \
# 11列最耗磁盘IO进程
--top-io-adv \
# 12列最耗总IO进程
--top-mem \
# 13列最耗内存进程
--top-childwait \
# 14列子进程等待最多进程
-m \
# 15列内存使用状态
-gs \
# 16列换页换进换出,17列换页空间使用状态
-n --net-packets --tcp --udp \
# 18列网络收发速度,19列网络收发包数量,
# 20列tcp连接状态,21列udp连接状态
--fs --lock \
# 22列打开文件数和打开inode数
# 23列文件锁状态 \
-dr --aio --disk-tps --disk-util \
# 24列磁盘读写速度,25列IO读写请求次数
# 26列异步IO速度,27列磁盘秒事物数
# 28列每个磁盘设备的使用百分比
--freespace --noheaders \
# 29列文件系统使用空闲状况
1 10 >>checkOS.info_$(date +%F_%H_%M_%S)
# 数据搂取间隔为1秒,搂取10次,输出到日志文件中
搂取日志文件,展示系统状态,样例
# 系统基本状态搂取
awk 'BEGIN{OFS="|"}/system/{print "|"$1,$2,$3,$4,$5,$6,$15,$16,$17,$18,$24"|"}' checkOS.info_* >result;\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"$1,$2,$3,$4,$5,$6,$15,$16,$17,$18,$24"|"}' checkOS.info_* >>result
# 以第4列,一分钟内的负载进行倒序排列
head -2 result ;grep -v '|-' result |sort -t'|' -nrk4
# 内存瓶颈确认
awk 'BEGIN{OFS="|"}/system/{print "|"$1,$2,$3,$4,$6,$15,$17,$16,$24,$13"|"}' checkOS.info_* >result;\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"$1,$2,$3,$4,$6,$15,$17,$16,$24,$13"|"}' checkOS.info_* >>result
# 以第4列,一分钟内的负载进行倒序排列
# 根据负载高峰时期的换页使用情况来判断内存是否有瓶颈
head -2 result ;grep -v '|-' result |sort -t'|' -nrk4
最终脚本部署样例
该部分即是模板机部署时候的系统自检部分的部署脚本
# 安装dstat
cd /tmp
rpm -ivh dstat-0.7.2-1.el6.rfx.noarch.rpm
rm -rf dstat-0.7.2-1.el6.rfx.noarch.rpm
# 修改脚本,输出主机名
sed -i 's/plugin title/Hostname/g' /usr/share/dstat/dstat_helloworld.py
sed -i 's/counter/------------/g' /usr/share/dstat/dstat_helloworld.py
sed -i "s/'Hello world\!'/os.popen('hostname').readlines()[0].split()/g" \
/usr/share/dstat/dstat_helloworld.py
dstat --helloworld 1 3
# 创建检测脚本并启动
mkdir -p /root/checkOS
cd /root/checkOS
cat >/root/checkOS/checkOS.sh<<EOF
#!/bin/bash
export DSTAT_TIMEFMT='%Y-%m-%d %H:%M:%S'
# kill dstat
for i in \$(ps -ef|grep '/usr/bin/dstat --helloworld'|grep -v grep|awk '{print \$2}');do kill -9 \$i;done
# start dstat
/usr/bin/dstat --helloworld -tlcp \\
--proc-count -y \\
--top-cpu-adv \\
--top-cputime-avg \\
--top-latency-avg \\
--top-bio-adv \\
--top-io-adv \\
--top-mem \\
--top-childwait \\
-mgsn --net-packets --tcp --udp \\
--fs --lock \\
-dr --aio --disk-tps --disk-util \\
--freespace --noheaders 10 8640 >>/root/checkOS/checkOS.info_\$(date +%F_%H_%M_%S) &
/usr/bin/find /root/checkOS/checkOS.info_* -mtime +30 -exec rm -rf {} \;
EOF
chmod 700 /root/checkOS/checkOS.sh
/root/checkOS/checkOS.sh &
echo '/root/checkOS/checkOS.sh &'>>/etc/rc.local
# 设置自动任务每日启动监控脚本
crontab -l>/tmp/crontab.tmp
echo -e '\n#OS Check Dstat'>>/tmp/crontab.tmp
echo '0 0 * * * /bin/bash /root/checkOS/checkOS.sh'>>/tmp/crontab.tmp
cat /tmp/crontab.tmp |crontab
rm -rf /tmp/crontab.tmp
# 生成脚本,输出昨日系统基本信息
cat >/root/checkOS/checkYesterday.sh<<EOF
head -1 /root/checkOS/checkOS.info_\$(date '+%F' -d '1 day ago')*|\\
awk 'BEGIN{OFS="|"}/system/{print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>/root/checkOS/result.txt
cat /root/checkOS/checkOS.info_\$(date '+%F' -d '1 day ago')*|grep -v '\----system----'|\\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>>/root/checkOS/result.txt
head -2 result.txt ;grep -v '|-' result.txt |sort -t'|' -nrk4|head -30
EOF
chmod +x checkYesterday.sh
# 生成脚本,输出今日系统基本信息
cat >/root/checkOS/checkToday.sh<<EOF
head -1 /root/checkOS/checkOS.info_\$(date '+%F')*|\\
awk 'BEGIN{OFS="|"}/system/{print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>/root/checkOS/result.txt
cat /root/checkOS/checkOS.info_\$(date '+%F')*|grep -v '\----system----'|\\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>>/root/checkOS/result.txt
head -2 result.txt ;grep -v '|-' result.txt |sort -t'|' -nrk4|head -30
EOF
chmod +x checkToday.sh
chattr +i *.sh
[TOC]