随笔 - 72,  文章 - 0,  评论 - 1,  阅读 - 22557

监控主机存活

复制代码
 1 for ((i=1;i<4;i++));do                 #循环3次,如果3次都ping不通 则认为主机不存活
 2    if ping -c1 $1 &>/dev/null;then     #-c1 表示仅发送一个包
 3      export  ping_count"$i"=1          #export 设置为全局变量
 4    else
 5      export  ping_count"$i"=0
 6    fi
 7    sleep 2                             #间隔的时间
 8 done
 9 
10 if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ];then  #满足ping不通的条件
11     echo "$1 is down"
12 else
13     echo "$1 is up"
14 fi
15 
16 unset ping_count1
17 unset ping_count2
18 unset ping_count3
复制代码

监控服务存活状态

复制代码
 1 port_status () {
 2 temp_file=`mktemp port_status.XXX`             #生成随机文件用于接收telnet回显
 3 
 4 [ ! -x /usr/bin/telnet ]&& echo "please install telnet"&& exit 1    #没有安装telnet就报错
 5 
 6 ( telnet $1 $2 <<EOF                           #将telnet输出到之前创建的临时文件中
 7 quit
 8 EOF
 9 ) &>$temp_file
10 
11 if egrep "\^]" $temp_file &>/dev/null;then     #如果在临时文件中到^[则认为服务正常
12     echo "$1 $2 is open"
13 else
14     echo "$1 $2 is close"
15 fi
16 
17 rm -f $temp_file                               #删除临时文件
18 }
19 port_status $1 $2                              #函数传参
复制代码

 统计内存使用率前10的进程

1 memory() {
2   temp_file=`mktemp momory.XXX`
3   top -b -n 1 > $temp_file      #其中 -b 为让top 一次显示所有的信息 -n 为显示1次
4 
5   tail -n +8 $temp_file | awk '{array[$NF]+=$6}END{for (i in array) print array[i],i}' |sort -k 1 -n -r |head -10
6   rm -f $temp_file  #删除临时文件
7 }     #上面的 tail -n +8 是从第8行开始显示  array[i]为数组的下标 i为数组的值  sort -k 1 是根据第一列排序 -n 按照数字排序 -r 反序  head -10 显示前10行
8 memory

 如果还想再统计pid信息,最后显示的是 进程 内存使用数 进程号 可以这样写(创建2个array)

1 memory() {
2   temp_file=`mktemp momory.XXX`
3   top -b -n 1 > $temp_file
4 
5   tail -n +8 $temp_file | awk '{array[$NF]+=$6;array2[$NF]+=$1}END{for (i in array) print i,array[i],array2[i]}' |sort -k 2 -n -r |head -10
6   rm -f $temp_file
7 }
8 memory

套用刚刚的做法,还可以统计cpu使用率前10的进程等

关于awk的数组 可以参考下面3篇文章,写的很好

http://www.zsythink.net/archives/2093/

https://www.cnblogs.com/liujunjun/p/11963931.html

https://blog.csdn.net/admin_root1/article/details/78911577

 

京峰教育公开课中关于三剑客的综合使用案例

1) 基于SHELL编程三剑客Awk、Sed、Grep分析线上Nginx日志,分析和统计Nginx全天09:00-11:00之间总的请求数(访问量),将IP地址打印出来,同时将排前20名的IP打印,将访问次数超过500的IP加入Linux黑名单文件中,操作的指令和方法如下:
#将09:00-11:00段用户的IP地址打印;
sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|awk '{print $1}'
sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"
sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|cut -d" " -f1
#同时将用户访问排前20名的IP打印;
sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort -n|uniq -c|sort -nr|head -20
#将访问次数超过500的IP加入Linux黑名单;
sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort -nr|uniq -c|sort -nr|awk '{if(($1>=500)) print $2}'
for ip in $(sed -n '/2020:09:00/,/2020:11:00/'p access_20200228.log|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort -nr|uniq -c|sort -nr|awk '{if(($1>=500)) print $2}');do iptables -t filter -A INPUT -s $ip/32 -m tcp -p tcp --dport 80 -j DROP ;done

 

posted on   wilson'blog  阅读(180)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示