脚本小示例

扫描网络内存活的主机

#!/bin/bash
#Detection of IP
#Li
ip=192.168.74.
for i in $(seq 50)
do
ping -c 2 -W 2 $ip$i >/dev/null
if [ $? -eq 0 ];then
echo "$ip$i is ok" 
else
echo "$ip$i no ping"
fi
done

查看主机网卡流量

#!/bin/bash
#network
#Li
while :;do
time="`date +%m"-"%d" "%k":"%M`"
day="`date +%m"-"%d`"
rx_before=`ifconfig ens33|sed -n "5p"|awk '{print $5}'|cut -c2-`
tx_before=`ifconfig ens33|sed -n "7p"|awk '{print $5}'|cut -c2-` 
sleep 2
rx_after=`ifconfig ens33|sed -n "5p"|awk '{print $5}'|cut -c2-`
tx_after=`ifconfig ens33|sed -n "7p"|awk '{print $5}'|cut -c2-`
rx_result=$[(rx_after-rx_before)/256]
tx_result=$[(tx_after-tx_before)/256]
echo "$time Now_In_Speed:"$rx_result"kbps Now_Out_Speed:"$tx_result"kbps"
sleep 2
done

系统状况监控

#!/bin/bash
#systemstat
#Li
IP=192.168.74.12
top -n 2|grep "Cpu" >>/root/temp/cpu.txt
free -m |grep "Mem" >>/root/temp/mem.txt
df -h|grep "sda1" >>/root/temp/drive_sda1.txt
#df -h|grep "sda2" >>./tmp/drive_sda2.txt
time=`date +%m"-"%d" "%k":"%M`
connect=`netstat -na | grep “223.255.127.164:80” | wc -l`
echo "$time  $connect" >> /root/temp/connect_count.txt

监视可用磁盘空间

#!/bin/bash
#monitor available disk space
#Li
SPACE=`df |sed -n '/\/$/p'|awk '{print $5}'| sed 's/%//g'`
if [ $SPACE -ge 90 ]
then
[email protected]
fi

监控CPU和内存的使用情况

#!/bin/bash
#script to captrue system statistics
#Li
OUTFILE=/root/capstats.csv
TIME="`date +%m"-"%d" "%k":"%M`"
DAY="`date +%m"-"%d`"
TIMEOUT=`uptime`
VMOUT=`vmstat 1 2`
USERS=`echo $TIMEOUT|awk '{print $4}'`
LOAD=`echo $TIMEOUT|awk '{print $10}'`
FREE=`vmstat|sed -n '3p'|awk '{print $4}'`
IDLE=`vmstat|sed -n '3p'|awk '{print $15}'`
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE

全方位监控主机

#!/bin/bash
#check_hosts
#Li
DAY="`date +%m"-"%d`"
HOUR="`date +%H`"
DIR="/root/host_${DAY}/${HOUR}"
DELAY=60
COUNT=60
if ! test -d ${DIR}
then
/bin/mkdir -p ${DIR}
fi
# general check
export TERM=linux
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAY}.log 2>&1 &
# cpu check
/usr/bin/sar -u > ${DIR}/cpu_${DAY}.log 2>&1 &
#/usr/bin/mpstat -p 0${DELAY}${COUNT} >> ${DIR}/cpu_0_${DAT}.log 2>&1 &
# memory check
/usr/bin/vmstat ${DELAY}${COUNT} > ${DIR}/vmstat_${DAY}.log 2>&1 &
# I/0 check
/usr/bin/iostat ${DELAY}${COUNT} > ${DIR}/iostat_${DAY}.log 2>&1 &
# networkcheck
#/usr/bin/sar -n DEV ${DELAY}${COUNT} > ${DIR}/net_${DAY}.log 2>&1 &
/usr/bin/sar -n DEV > ${DIR}/net_${DAY}.log 2>&1 &


每小时自动执行
[root@localhost ~]# crontab -l   
00 * * * * /root/check_hosts.sh

猜你喜欢

转载自blog.csdn.net/weixin_42125267/article/details/82755981