ping_success_status
ping_success_status
echo "$IP Ping is failure!"
done
7、监控CPU、内存和硬盘利用率
#!/bin/bash8、批量主机磁盘利用率监控前提监控端和被监控端SSH免交互登录或者密钥登录 。
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 |awk '/netmask/ {print $2}')
if ! which vmstat &> /dev/null; then
echo "vmstat command no found, Please install procps package."
exit 1
fi
## CPU
US=$(vmstat |awk 'NR==3 {print $13}')
SY=$(vmstat |awk 'NR==3 {print $14}')
IDLE=$(vmstat |awk 'NR==3 {print $15}')
WAIT=$(vmstat |awk 'NR==3 {print $15}')
USE=$(($US+$SY))
if [ $USE -ge 50 ];then
echo "
Date: $DATE
Host: $IP
Problem: CPU utilization $USE"
fi
## Mem
TOTAL=$(free -m |awk '/Mem/ {print $2}')
USE=$(free -m |awk '/Mem/ {print $3}')
FREE=$(free -m |awk '/Mem/ {print $4+$6}')
if [ $FREE -lt 1024 ]; then
echo "
Date: $DATE
Host: $IP
Problem: Total=$TOTAL,Use=$USE,Free=$FREE"
fi
#disk
PART_USE=$(df -h |awk -F'[% ]+' 'BEGIN{OFS="="} /^/dev/ {print $1,$2,$5,$6}')
for i in $PART_USE; do
PART=$(echo $i |cut -d"=" -f1)
TOTAL=$(echo $i |cut -d "=" -f2)
USE=$(echo $i |cut -d"=" -f3)
MOUNT=$(echo $i |cut -d"=" -f4)
if [ $USE -gt 80 ]; then
echo "
Date: $DATE
Host: $IP
Total: $TOTAL
Problem: $PART=$USE($MOUNT)"
fi
done
写一个配置文件保存被监控主机SSH连接信息,文件内容格式:IP User Port
#!/bin/bash9、检查网站可用性
HOST_INFO=host.info
for IP in $(awk '/^[^#]/ {print $1}' $HOST_INFO); do
USER=$(awk -v ip=$IP 'ip==$1 {print $2}' $HOST_INFO)
PORT=$(awk -v ip=$IP 'ip==$1 {print $3}' $HOST_INFO)
TMP_FILE=/tmp/disk.tmp
ssh -p $PORT $USER@$IP df -h > $TMP_FILE
USE_RATE_LIST=$(awk 'BEGIN{OFS="="} /^/dev/ {print $NF,int($5)}' $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*} ##从右到左,非贪婪匹配,匹配到的删除
USE_RATE=${USE_RATE#*=} ##从左到右,非贪婪匹配,匹配到的删除
if [ $USE_RATE -ge 10 ];then
echo "Warning: $IP $PART_NAME Partition usage $USE_RATE%!"
fi
done
done
#!/bin/bash#方法2:
#--------------------
#1)检查URL可用性
#方法1:
check_url() {
HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $1)
if [ $HTTP_CODE -ne 200 ]; then
echo "Warning: $1 Access failure!"
fi
}
check_url_2() {#2)判断三次URL可用性
if ! wget -T 10 --tries=1 --spider $1 &>/dev/null; then
#-T超时时间,--tries尝试1次,--spider爬虫模式
echo "Warning: $1 Access failure!"
fi
}
#check_url www.baidu.com
#check_url_2 www.aaaa.com
#思路与上面检查主机存活状态一样 。#方法3:错误次数保存到数组
#---------------------------------
URL_LIST="www.baidu.com www.agasgf.com"
#------
#方法1:利用循环技巧,如果成功就跳出当前循环,否则执行到最后一行
check_url_3() {
HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $1)
if [ $HTTP_CODE -eq 200 ]; then
continue
fi
}
for URL in $URL_LIST; do
check_url_3 $URL
check_url_3 $URL
check_url_3 $URL
echo "Warning: $URL Access failure!"
done
#------
#方法2:错误次数保存到变量
for URL in $URL_LIST; do
FAIL_COUNT=0
for ((i=1;i<=3;i++)); do
HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)
if [ $HTTP_CODE -ne 200 ]; then
let FAIL_COUNT++
else
break
【Linux Shell脚本经典案例】fi
done
if [ $FAIL_COUNT -eq 3 ]; then
echo "Warning: $URL Access failure!"
fi
done
#------
for URL in $URL_LIST;do
NUM=1
unset FAIL_COUNT
while [ $NUM -le 3 ]; do
HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)
if [ $HTTP_CODE -ne 200 ]; then
FAIL_COUNT[$NUM]=$URL
let NUM++
else
break
fi
done
if [ ${#FAIL_COUNT[@]} -eq 3 ];then
echo "Warning: $URL Access failure!"
推荐阅读
- Linux网络服务的种类
- Linux下配置SVN的bash脚本封装
- Linux操作系统中的零拷贝技术,是怎么一回事
- LINUX下DNS配置文件介绍
- Linux性能实时监测工具 Netdata
- 详解3种区别Linux服务器是物理机或者虚拟机的方法
- Linux :磁盘分区、挂载知多少?
- 使用shell脚本在centos8平台安装nginx服务
- 怎么找出Linux内存瓶颈
- 绝对经典,看了必会 linux中部署mysql主从同步示例详解