shell系统检测->

系统状态检测脚本练习

1-> 查看磁盘状态

思路:
查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件
1.获取磁盘当前使用的值 df -h|grep /$
2.从获取到的值中提取出,对应的使用率 df -h|grep /$|awk -F "[ %]+" '{print $(NF-1)}'
3.进行数字比较(如果提取出来的值大于80则报警,如果提取出来的值小于80则不处理)

脚本:

#!/usr/bin/bash
Disk=$(df -h|grep /$|awk -F "[ %]+" '{print $(NF-1)}')

if [ $Disk -gt 80 ];then
	echo "磁盘使用率超标, 当前使用率是: ${Disk}%"
else
	echo "磁盘啥事没有!!当前使用率是: ${Disk}%"
fi

 

2-> 查看内存使用情况

思路:
查看内存/当前使用状态,如果使用率超过80%则报警发邮件
1.如何查看内存的总大小 free -m|awk '/^Mem/{print $2}'
2.如何查看内存的使用率 free -m|awk '/^Mem/{print $3}'
2.使用使用率*100 除以 内存的总大小 = 使用的百分比
3.拿到对应的百分比进行比对。

脚本:    
[root@web03 day01]# cat free_use.sh 
#!/usr/bin/bash
Free_use=$(free -m|awk '/^Mem/{print int($3/$2*100)}')
if [ $Free_use -gt 10 ];then
    echo "内存使用率超标了, 当前内存的使用率是: ${Free_use}%"
else
    echo "内存使用率正常, 当前内存的使用率是: ${Free_use}%"
fi


->内存报警(低于30%) [root@bgx]# cat mem.sh Mem_Total=$(free -m|grep "^M"|awk '{print $2}') Mem_Use=$(free -m|grep "^M"|awk '{print $3}') Mem_B=$((($Mem_Use*100)/$Mem_Total)) if [ $Mem_B -ge 30 ];then echo "Memory Is Err ${Mem_B}%" else echo "Memory Is OK ${Mem_B}%" fi

 

3 -> 系统信息,cpu负载,内存

[root@jumpserver-70 scripts]# cat system.sh 
#!/bin/bash
#系统信息
system_version=$(hostnamectl | awk -F ':' '/Operating System/{print $2}')
system_kernel=$(hostnamectl | awk -F ':' '/Kernel/{print $2}')
system_network=$(hostname -I)
system_host=$(hostnamectl |awk -F ':' '/Static hostname/{print $2}')
network_out=$(curl -s icanhazip.com)

#cpu负载
cpu_load=$(w|awk -F ':' 'NR==1{print $NF}')

#内存
mem_total=$(free -h | awk '/^Mem/{print $2}')
mem_use=$(free -h | awk '/^Mem/{print $3}')
mem_B=$(free -m | awk '/^Mem/{print int($3*100/$2)}')
echo "-------------------------------system info-----------------------------------------"
echo "当前系统的版本:$system_version
当前系统的内核:$system_kernel
当前系统的虚拟化平台:
当前系统的主机名:$system_network
当前系统的内网ip:$system_host
当前系统的外网ip:$network_out
"
echo "-------------------------------system cpu-----------------------------------------"

echo "当前cpu负载情况 : 1分钟,5分钟,15分钟使用:$cpu_load"

echo "-------------------------------system mem---------------------------------------"

echo "当前总内存大小:$mem_total
当前内存使用率:$mem_use
当前内存使用百分比:$mem_B%
"
echo "-------------------------------system disk---------------------------------------"
for ((i=2;i<=8;i++))
do
    df -h |awk 'NR=='$i'{print "磁盘分区:"$NF,"使用了"$(NF-1)}'
done
echo "--------------------------------------------------------------------------------------"

 

4->查看ip是否畅通

[root@jumpserver-70 scripts]# cat ping.sh 
#!/bin/bash

read -p "input ip: " ip
ping -c2 $ip &>/dev/null

if [ $? -eq 0 ];then
    echo "host $ip is ok"
else
    echo "host $ip is error"
fi

->测试ip畅通(并行)

[root@web03 day01]# cat for.sh 
#!

IP=10.0.0

for i in {1..254}
do
    Addr="$IP"."$i"
    {
    ping -c1 "$Addr" &>/dev/null
    if [ $? -eq 0 ];then
        echo "$Addr" OK!
    fi
    }&
done
    wait
    echo "所有的ping都结束了....."

 

5-> 系统管理工具脚本

[root@jumpserver-70 scripts]# cat action.sh 
#!/usr/bin/bash

cat <<EOF
===================
h 显示命令帮助    
f 显示磁盘分区 
d 显示磁盘挂载 
m 查看内存使用 
u 查看系统负载 
q 退出程序
===================
EOF


while true;
do
        read -p "请输入你想查看系统状态对应码[h/f/d/m/u/q]: " sys
        case "$sys" in 
                h)
                        clear
            help
                        ;;
                f)
                        clear
                        lsblk
                        ;;
                d)
                        clear
                        df -h
                        ;;
                m)
                        clear
                        free -m
                        ;;
                u)
                        clear
                        uptime
                        ;;
                q)
                        break
                        ;;
                *)
            read -p "请输入你想查看系统状态对应码[h/f/d/m/u/q]: " sys
        esac
done

 

6-> 跳板机实现脚本

[root@jumpserver-70 scripts]# cat jumpserver.sh 
#!/bin/bash

echo "正在启动。。。"
yum install -y sshpass &>/dev/null

cat <<EOF
==================
web01=10.0.0.7--> 1
web02=10.0.0.8--> 2
web03=10.0.0.9--> 3
==================
EOF

while true;
do
read -p "请选择登录的主机编号:" host

case $host in
    1)
        sshpass -p123456 ssh root@10.0.0.7
        ;;
    2)
        sshpass -p123456 ssh root@10.0.0.8
        ;;
    3)
        sshpass -p123456 ssh root@10.0.0.9
        ;;
    exec)
        exit 1
        ;;
    *)
        echo "请检查主机编号是否正确"
        ;;
    esac
done    

 

7-> 创建文件加随机字母并修改

[root@jumpserver-70 scripts]# cat make_file.sh 
#!/bin/bash

for i in {1..10}
do
    file=$(uuidgen | sed -r 's#[0-9-]+##g'|cut -c 1-10)
    [ -f test_$file.html ]    
    if [ $? -eq 0 ];then
        echo "这个文件已经存在。。"
    else
        touch /opt/test_${file}.html &>/dev/null
        echo  test_${file}.html >>/opt/test1.txt
        echo "你已经创建了 test_$file.html 文件"
    fi
done

echo "------------正在执行操作------------"

for n in {1..10}
do    
name=$(sed -rn ${n}p /opt/test1.txt | sed -r 's#.*_(.*).html#\1#g')
name_new=test_done_${name}.HTML
        mv /opt/test_${name}.html /opt/$name_new 
        echo "文件 $name_new 修改成功"
done     

> /opt/test1.txt


->第二种方法
[root@web03 ~]# cat for_file2.sh 
#!/bin/bash

#1.循环创建10个随机文件名的html文件
for i in {1..10}
do
File_Name=$(openssl rand -base64 40 |sed 's#[^a-z]##g'|cut -c1-10)
touch test_${File_Name}.html
done

#2.查找当前目录下.html结尾的文件,写入一个文件中
find ./ -type f -name "*.html" > 3.txt
for i in $(cat 3.txt)
do
#4.将带html修改为HTML ${i/html/HTML} 将文件名后缀是html替换为HTML
mv  $i ${i/html/HTML}
done

 

8-> 判断脚本语法错误

[root@jumpserver-70 day01]# cat syntax.sh 
#!/bin/bash

read -p "请输入脚本的名字:" name

if [ -f $name ];then

    bash -n $name     #如果不想看到语法错误  &>/dev/null

    if [ $? -ne 0 ];then
        read -p "你的脚本存在语法错误 
        ->退出(q|Q) 
        ->继续编辑按任意键:" command
        case $command in
            q|Q)
                exit 1
                ;;
            *)
                vim $name
            esac
    else
        echo "你的脚本没有语法错误。。。"
    fi

else
    echo "检查脚本是否存在。。。"
fi 

 

9-> 查看ip地址-端口

写一个shell脚本批量查看10.0.0.0/24网段的所有IP地址,判断哪些IP开放了80端口。

[root@Shell timujia]# cat ping.sh 
#!/bin/bash
ip=10.0.0.
for i in {1..10}
do
  addr=${ip}${i}
  #1.循环的ping整个网段的主机
  ping -c1 $addr &>/dev/null

  #2.判断是否能ping通
  if [ $? -eq 0 ];then
    Nmap=$(nmap -p 80 $addr|grep open|wc -l)

  #3.如果能ping通则检测端口是否存在
  if [ $Nmap -eq 1 ];then
    echo "$addr ON 80 Port"
  fi
fi
done

->监控80端口是否开启

[root@web03 ~]# cat status.sh 
#!/usr/bin/bash
while true
do
#1.检查Nginx是否存或,让wc统计行数,方便整数比对
Nginx_status=$(netstat -lntp|awk '{print $4}'|grep :80$|wc -l)

#2.检查Nginx的值是1还是0,如果是0则代表没启动Nginx,如果是1则不做处理
if [ $Nginx_status -eq 0 ];then
  echo  "正在尝试重启..."
  systemctl restart nginx &>/dev/null
if [ $? -eq 0 ];then
  echo "重启成功..."
else
  echo "重启失败"
fi
fi

#
3.判断结束后,休息5s sleep 5 done

 

10-> 查找文件->打包->还原

[root@web03 ~]# cat file.sh 

#1.修改名称
find /backup/ -type f -name "*.txt" > /tmp/1.txt

for i in $(cat /tmp/1.txt)
do
mv $i ${i}.bak
done

#2.重新查找,bak文件,然后进行打包
cd /backup && tar czf 123.tar.gz $(find /backup/ -type f -name "*.bak")

#3.将.bak还原.txt
find /backup/ -type f -name "*.bak" >/tmp/2.txt

for i in $(cat /tmp/2.txt)
do
mv $i ${i%.*}
done

 

posted @ 2018-09-25 11:11  少校的小木屋  阅读(597)  评论(0编辑  收藏  举报