Liunx Shell programming entry ---- write scripts and FTP monitoring system to detect script

1. Write a system monitor script sysmon.sh

The script implementation: usage monitoring system memory, cpu, disk

1.1 Command Interpreter

cat /proc/meminfo

Here Insert Picture Description

/proc/meminfoLinux system memory usage to understand the status of the primary interface

Our most commonly used free, vmstatsuch as the command is to obtain data through it, the /proc/meminfoinformation contained ratio freecommands much richer

Linux bccommand

bc arbitrary precision calculator language command is usually used as a calculator at linux.

It is similar to the basic calculator, use this calculator can do basic math.

Bc command prompt system does not need to yum install bcinstall

Monitor memory usage

#memory_used_rate
LoadMemory=$(cat /proc/meminfo | awk '{print $2}')
Total=$(echo $LoadMemory | awk '{print $1}')
Free1=$(echo $LoadMemory | awk '{print $2}')
Free2=$(echo $LoadMemory | awk '{print $3}')
Free3=$(echo $LoadMemory | awk '{print $4}')
Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr  $Used/$Total*100 | bc -l`
Memory_Used_Rate=`expr  $Used_Rate/1 | bc`
echo $Memory_Used_Rate%

Monitoring disk usage

Use dfthe command to view the disk -hformatted for easy viewing format

[root@localhost bin]# df -h /dev/sda*
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        475M     0  475M    0% /dev
/dev/sda1      1014M  160M  855M   16% /boot
devtmpfs        475M     0  475M    0% /dev
#disk_used_rate
Location=/dev/sda1 # 这里根据自己的磁盘位置修改
Disk_Used_Rate=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate

Cpu usage monitoring

#cpu_used_rate
Log=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Free=$(echo $Log | awk '{print $4}')
Total=$(echo $Log | awk '{print $1+$2+$3+$4+$5+$6+$7}')
Cpu_Used=`expr $Total - $Free`
Cpu_Used_Rate=`expr  $Cpu_Used/$Total*100 |bc -l`
echo  $Cpu_Used_Rate

1.2 script code

# Colors
red='\033[31m' # 定义红色
green='\033[32m' # 定义绿色

#memory_used_rate
LoadMemory=$(cat /proc/meminfo | awk '{print $2}')
Total=$(echo $LoadMemory | awk '{print $1}')
Free1=$(echo $LoadMemory | awk '{print $2}')
Free2=$(echo $LoadMemory | awk '{print $3}')
Free3=$(echo $LoadMemory | awk '{print $4}')
Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr  $Used/$Total*100 | bc -l`
Memory_Used_Rate=`expr  $Used_Rate/1 | bc`

# disk_used_rate
Location=/dev/sda1
Disk_Used_Rate=$(df -h | grep $Location | awk '{print $5}')

#cpu_used_rate
Log=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Free=$(echo $Log | awk '{print $4}')
Total=$(echo $Log | awk '{print $1+$2+$3+$4+$5+$6+$7}')
Cpu_Used=`expr $Total - $Free`
Cpu_Used_Rate=`expr  $Cpu_Used/$Total*100 |bc -l`
cpu_Used_Rate=`expr  $Cpu_Used_Rate/1 | bc`

echo -e "+----------------------------------------------------------------"

echo -e "${green} 内存使用率:${red}"$Memory_Used_Rate%
echo -e "${green} 磁盘使用率:${red}"$Disk_Used_Rate
echo -e "${green} Cpu使用率:${red}"$cpu_Used_Rate%

echo -e "+----------------------------------------------------------------"
echo -e " "

Here Insert Picture Description

crontab -eInto the timing system, set up a scheduled task
input: */1 * * * * /root/bin/sysmon.sh
Here Insert Picture Description
receive e-mail:
Here Insert Picture Description
Here Insert Picture Description

2. Write a script FTP probe

# 输入想要查询的ip段
IP1=`echo $1 |awk -F"." '{print $1}'`
IP2=`echo $1 |awk -F"." '{print $2}'`
IP3=`echo $1 |awk -F"." '{print $3}'`
IP4=`echo $1 |awk -F"." '{print $4}'`

for ((i=3; i>0; i--))
do
    echo -e "倒数"$i"秒,开始扫描"
done
# 开始连接
wget  ftp://$IP1.$IP2.$IP3.$IP4 &>null

# 如果连接成功
if [ $? -eq 0 ]
then echo "$IP1.$IP2.$IP3.$i is open"
fi

Here Insert Picture Description

Published 140 original articles · won praise 50 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43442524/article/details/104814018