1. Write a system monitor script sysmon.sh
The script implementation: usage monitoring system memory, cpu, disk
1.1 Command Interpreter
cat /proc/meminfo
/proc/meminfo
Linux system memory usage to understand the status of the primary interface
Our most commonly used free
, vmstat
such as the command is to obtain data through it, the /proc/meminfo
information contained ratio free
commands much richer
Linux bc
command
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 bc
install
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 df
the command to view the disk -h
formatted 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 " "
crontab -e
Into the timing system, set up a scheduled task
input: */1 * * * * /root/bin/sysmon.sh
receive e-mail:
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