1) 监控目标主机存活状态
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 07:22:12 AM CST
#filename:host_status.sh
#Script description:监控目标主机状态
########################################
# 监控方法 ping ICMP协议
# ping 通 host up
# ping 不通 host down
# 问题
# 1.关于禁ping 防止DDOS
# 禁的是陌生人 禁止所有 允许你的IP
# 满足条件
# 网络有延迟 假报警问题
# ping 的取值 报警阈值 3次全部失败 报警机器down
# ping 的频率 秒级 5秒 or 1秒
# main
for ((i=1;i<4;i++));do
# 测试代码
if ping -c1 $1 &>/dev/null;then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
# 时间间隔
sleep 0.3
done
# 3次失败报警
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ];then
echo "$1 is down"
else
echo "$1 is up"
fi
unset ping_count1
unset ping_count2
unset ping_count3
[maxwell@MaxwellDBA datas]$ sh -x host_status.sh 192.168.0.2
+ (( i=1 ))
+ (( i<4 ))
+ ping -c1 192.168.0.2
+ export ping_count1=0
+ ping_count1=0
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ ping -c1 192.168.0.2
+ export ping_count2=0
+ ping_count2=0
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ ping -c1 192.168.0.2
+ export ping_count3=0
+ ping_count3=0
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ '[' 0 -eq 0 ']'
+ '[' 0 -eq 0 ']'
+ '[' 0 -eq 0 ']'
+ echo '192.168.0.2 is down'
192.168.0.2 is down
+ unset ping_count1
+ unset ping_count2
+ unset ping_count3
[maxwell@MaxwellDBA datas]$
[maxwell@MaxwellDBA datas]$ sh -x host_status.sh www.baidu.com
+ (( i=1 ))
+ (( i<4 ))
+ ping -c1 www.baidu.com
+ export ping_count1=1
+ ping_count1=1
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ ping -c1 www.baidu.com
+ export ping_count2=1
+ ping_count2=1
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ ping -c1 www.baidu.com
+ export ping_count3=1
+ ping_count3=1
+ sleep 0.3
+ (( i++ ))
+ (( i<4 ))
+ '[' 1 -eq 1 ']'
+ '[' 1 -eq 1 ']'
+ '[' 1 -eq 0 ']'
+ echo 'www.baidu.com is up'
www.baidu.com is up
+ unset ping_count1
+ unset ping_count2
+ unset ping_count3
[maxwell@MaxwellDBA datas]$
2)监控一个端口存活状态 等价于监控系统服务
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 07:57:04 AM CST
#filename:port_status.sh
#Script description:监控一个服务端口
########################################
# monitor method
#1) use systemctl service service start status
#2) lsof check out if the port exist
#3) check out if the process exist
#### What should we do if the service died | pressure is huge , no response | service is down all of process all exists
#4) test port if the response exist recommand
# main
port_status () {
temp_file=`mktemp port_status.XXX`
# 1. verify dependence command telnet if exist
[ ! -x /usr/bin/telnet ]&&echo "telnet: not found command"&& exit 1
# 2. 测试端口 $1 IP $2 port
( telnet $1 $2 <<EOF
quit
EOF
) &>$temp_file
#3. analysis file's content , verify the result
if egrep "\^]" $temp_file &>/dev/null;then
echo "$1 $2 is open"
else
echo "$1 $2 is close"
fi
rm -f $temp_file
}
# 4.call 函数带参的问题
port_status $1 $2
[maxwell@MaxwellDBA datas]$ vim port_status.sh]
[maxwell@MaxwellDBA datas]$ cat port_status.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 07:57:04 AM CST
#filename:port_status.sh
#Script description:监控一个服务端口
########################################
# monitor method
#1) use systemctl service service start status
#2) lsof check out if the port exist
#3) check out if the process exist
#### What should we do if the service died | pressure is huge , no response | service is down all of process all exists
#4) test port if the response exist recommand
# main
port_status () {
temp_file=`mktemp port_status.XXX`
# 1. verify dependence command telnet if exist
[ ! -x /usr/bin/telnet ]&&echo "telnet: not found command"&& exit 1
# 2. 测试端口 $1 IP $2 port
( telnet $1 $2 <<EOF
quit
EOF
) &>$temp_file
#3. analysis file's content , verify the result
if egrep "\^]" $temp_file &>/dev/null;then
echo "$1 $2 is open"
else
echo "$1 $2 is close"
fi
rm -f $temp_file
}
# 4.call 函数带参的问题
port_status $1 $2
[maxwell@MaxwellDBA datas]$
[maxwell@MaxwellDBA datas]$ sh -x port_status.sh 172.26.5.177 22
+ port_status 172.26.5.177 22
++ mktemp port_status.XXX
+ temp_file=port_status.FMY
+ '[' '!' -x /usr/bin/telnet ']'
+ egrep '\^]' port_status.FMY
+ echo '172.26.5.177 22 is open'
172.26.5.177 22 is open
+ rm -f port_status.FMY
[maxwell@MaxwellDBA datas]$
3)找出使用CPU|memory前十的进程
[maxwell@MaxwellDBA datas]$ vim memory_cpu_use.sh
[maxwell@MaxwellDBA datas]$ cat memory_cpu_use.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 11:07:23 AM CST
#filename:memory_use.sh
#Script description:统计使用内存和CPU前十名进程
########################################
# 统计系统中前十名使用内存最多的进程
memory(){
#1. 收集任务管理器进程信息
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#2. 按进程统计内存使用大小
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
rm -f $temp_file
}
# 统计系统中前十名使用CPU最多的进程
cpu(){
#1. 收集任务管理器进程信息
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#2. 按进程统计内存使用大小
tail -n +8 $temp_file | awk '{array[$NF]+=$9}END{for (i in array) print array[i], i}' | sort -k 1 -n -r|head -10
rm -f $temp_file
}
echo memory
memory
echo cpu
cpu
[maxwell@MaxwellDBA datas]$ sh memory_use.sh
sh: memory_use.sh: No such file or directory
[maxwell@MaxwellDBA datas]$ sh memory_cpu_use.sh
memory
419588 mysqld
132484 db2syscr
130436 db2sysc
67244 db2vend
60624 postmaster
49616 db2fmp
41116 sssd_nss
30796 AliYunDun
30492 tuned
28144 systemd-journal
cpu
13.3 AliYunDun
6.7 db2sysc
6.7 AliYunDunUpdate
0 xprtiod
0 xfs-reclaim/vda
0 xfs_mru_cache
0 xfs-log/vda1
0 xfs-eofblocks/v
0 xfs-conv/vda1
0 xfs-cil/vda1
[maxwell@MaxwellDBA datas]$
4)监控内存使用率脚本
5)监控硬盘IO脚本
磁盘说明
磁盘在系统中负责存储和读取任务,磁盘的处理速度直接影响了计算机的速度,目前常用的磁盘有两种,固态和机械磁盘。
固态磁盘: 没有IO瓶颈,读写快,存储颗粒有擦写限制,价格高,容量小。
机械磁盘:靠电机带动磁盘转动,通过磁头读取或存储数据,读写速度和磁盘转速有关,容量大、价格低,大量读写容易造成IO瓶颈。
监控目的:随时掌握IO的使用情况,防止IO性能瓶颈。
监控指标:
- IO队列长度
- IOPS
- 磁盘吞吐量
监控命令:
- iostat(sysstat包提供)
- iozone (第三方提供)
- iostat命令详解
用法: iostat [选项][<时间间隔>[<次数>]]
常用选项说明:
-c: 只显示系统CPU统计信息,即单独输出avg-cpu结果,不包括device结果
-d: 单独输出Device结果,不包括cpu结果
-k/-m: 输出结果以kB/mB为单位,而不是以扇区数为单位
-x: 输出更详细的io设备统计信息
interval/count: 每次输出间隔时间,count表示输出次数,不带count表示循环输出。
[maxwell@MaxwellDBA datas]$ iostat
Linux 4.18.0-348.7.1.el8_5.x86_64 (MaxwellDBA) 10/12/2022 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.52 0.01 0.96 0.00 0.00 98.50
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 1.23 13.39 11.16 1096934 914304
[maxwell@MaxwellDBA datas]$
iostat,结果为从系统开机到当前执行时刻的统计信息。
输出含义:
avg-cpu:总体cpu使用情况统计信息,对于多核cpu,这里为所有cpu的平均值。重点关注iowait值,表示CPU用于等待io请求的完成时间。
%user : CPU处在用户模式下的时间百分比
%nice: CPU处在带NICE值的用户模式下的时间百分比
%system: CPU处在系统模式下的时间百分比
%iowait: CPU 等待输入输出完成时间的百分比
%steal: 管理程序维护另一个虚拟处理器时,虚拟CPU的无意识等待时间百分比。
%idle: CPU空闲时间百分比
Device:各磁盘设备的IO统计信息。各列含义如下:
Device:以sdX形式显示的设备名称
tps:每秒进程下发的IO读、写请求数量
KB_read/s:每秒从驱动器读入的数据量,单位为K。
KB_wrtn/s : 每秒从驱动器写入的数据量,单位为K
KB_read: 读入数据总量,单位为K
KB_wrtn: 写入数据总量,单位为 K.
以上各列的含义如下
[maxwell@MaxwellDBA datas]$ iostat -x
Linux 4.18.0-348.7.1.el8_5.x86_64 (MaxwellDBA) 10/12/2022 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.52 0.01 0.96 0.00 0.00 98.50
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
vda 0.33 0.90 13.24 11.07 0.00 0.16 0.75 15.01 0.56 0.53 0.00 40.51 12.34 0.48 0.06
[maxwell@MaxwellDBA datas]$
rrqm/s: 每秒对该设备的读请求被合并次数,文件系统会对读取同块(block)的请求进行合并
wrqm/s: 每秒对该设备的写请求被合并次数。
r/s:每秒完成的读次数
w/s: 每秒完成的写次数
wkB/s: 每秒写数据量(kB为单位)
avgrq-sz : 平均每次IO操作的数据量(扇区数为单位)
avgqu-sz: 平均等待处理的IO请求队列长度。
await: 平均每次IO请求等待时间(包括等待时间和处理时间,毫秒为单位)
swctm: 平均每次IO请求的处理时间(毫秒为单位)
%util : 采用周期内用于IO操作的时间比率,即IO队列非空的时间比率。

[maxwell@MaxwellDBA datas]$ vim io_status.sh
[maxwell@MaxwellDBA datas]$ iostat -x 1 3|egrep "^sd[a-z]" | tail -n +$((device_num+1)) |awk '{io_long[$1]+=$9}END{for (i in io_long)print io_long[i],i}'
[maxwell@MaxwellDBA datas]$ iostat -x 1 3|egrep "^vd[a-z]" | tail -n +3 |awk '{io_long[$1]+=$9}END{for (i in io_long)print io_long[i],i}'
0 vda
[maxwell@MaxwellDBA datas]$ vim io_status.sh
[maxwell@MaxwellDBA datas]$ cat io_status.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Wed 12 Oct 2022 03:09:51 PM CST
#filename:io_status.sh
#Script description:
########################################
# 磁盘io监控 明确知晓HD的使用情况
# 监控指标 IO队列长度 IOPS 吞吐量
# iostat iozone
# iostat 队列长度 明确反馈IO是否忙
# iostat 由sysstat
# iostat 命令
io(){
device_num=`iostat -x 1 3|egrep "^sd[a-z]"|wc -l`
iostat -x 1 3|egrep "^sd[a-z]" | tail -n +$((device_num+1)) |awk '{io_long[$1]+=$9}END{for (i in io_long)print io_long[i],i}'
}
# 阈值如何判断 2-3
#while true
#do
io
# sleep 5
#done
[maxwell@MaxwellDBA datas]$
明确监控项 阈值是多少 监控方法(命令等)
[root@MaxwellDBA datas]# sh -x nginx_install.sh
+ nginx_pkg=nginx-1.22.0.tar.gz
++ echo nginx-1.22.0.tar.gz
++ cut -d . -f1-3
+ nginx_source_doc=nginx-1.22.0
+ nginx_install_doc=/usr/local/nginx
+ nginx_user=WWW
+ nginx_group=WWW
+ install
+ useradd -r -s /sbin/nologin wwww
+ cd nginx-1.22.0
+ echo 'nginx configure....'
nginx configure....
+ ./configure --prefix=/usr/local/nginx --user=WWW --group=WWW
+ echo 'nginx make....'
nginx make....
+ make
+ echo 'nginx install....'
nginx install....
+ make install
+ echo 'nginx install success'
nginx install success
[root@MaxwellDBA datas]# cat nginx_install.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Wed 12 Oct 2022 03:35:00 PM CST
#filename:nginx_install.sh
#Script description:
########################################
#nginx install script from maxwell
# install user root
# preinstall: dependent packaege | resource pakage
# installation
# startup test
# variables
nginx_pkg="nginx-1.22.0.tar.gz"
nginx_source_doc=`echo $nginx_pkg| cut -d "." -f1-3`
nginx_install_doc="/usr/local/nginx"
nginx_user="WWW"
nginx_group="WWW"
#function
check () {
# monitor current user, root user be required
if [ "$USER" != 'root' ];then
echo "need to be root so that"
exit 1
fi
# check out wget command
# if [ ! -x /usr/bin/wget ];then
# echo "not found command /usr/bin/wget"
# exit 1
# fi
[ ! -x /usr/bin/wget ]&& echo "not found command /usr/bin/wget" && exit 1
}
install_pre (){
# 1 installation dependency
if ! (yum -y install gcc-* pcre-devel zlib-devel elinks 1>/dev/null);then
echo "ERROR: yum install error"
exit 1
fi
# 2. download source package
if wget http://nginx.org/download/$nginx_pkg &>/dev/null;then
tar xf $nginx_pkg
if [ ! -d $nginx_source_doc ];then
echo "ERROR:not found $nginx_source_doc "
exit 1
fi
else
echo "ERROR : download file $nginx_pkg fail"
exit 1
fi
}
install (){
# 1.create management user
useradd -r -s /sbin/nologin wwww
#2. install nginx source code
cd $nginx_source_doc
echo "nginx configure...."
if ./configure --prefix=$nginx_install_doc --user=$nginx_user --group=$nginx_group 1>/dev/null;then
echo "nginx make...."
if make 1>/dev/null;then
echo "nginx install...."
if make install 1>/dev/null;then
echo "nginx install success"
else
echo "ERROR: make install fail";exit 1
fi
else
echo "ERROR: nginx make fail";exit 1
fi
else
echo "ERROR: nginx configure fail";exit 1
fi
}
nginx_test(){
if $nginx_install_doc/sbin/nginx;then
echo "nginx start SUCCESS!"
elinks http://localhost -dump
else
echo "nginx stop FAIL"
fi
}
install
[root@MaxwellDBA datas]#
3445

被折叠的 条评论
为什么被折叠?



