Description: There are 100 centos servers that communicate with each other via intranet ssh, the user name and password are known, and the server ip list is known. Please design a technical solution for the server with an output disk usage rate exceeding 90%. It is required to write a detailed technical solution, please write the code for the commands and scripts involved, and please introduce the installation instructions for the third-party libraries.
Program ideas:
-
Test if the mailbox is available
-
Set a scheduled task to execute the following script every five minutes to view the occupancy
-
Send alarm email when it is found that the occupancy ratio exceeds 90%
-
Install ansible service, compile yml file to execute tasks in batch
Test mail
#Here we use qq mailbox as the recipient vim /etc/mail.rc set bsdcompat set from = [email protected] #receiver name set smtp=smtp.qq.com #smtp地址 set smtp -auth-user = [email protected] #recipient address set smtp -auth-password = xxxxxxx #smtp verification code, operate in the qq mailbox, send the information through the mobile phone to obtain the verification code set smtp-auth=login #test echo " Mail Test (Test Content) " | mail -s " Test Results (Subject) " [email protected]
Script content
vim /server/scripts/df.sh #!/bin/bash >/tmp/disk.log #Here is the test threshold D_Value=90 #Remove disk percentage D_Use=`df -h|grep '^/dev'|awk '{print $5}'|sed 's#%##'` #Remove the host private IP address IP_Addr=`/sbin/ifconfig eth1|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"` for i in $D_Use do if [ $i -ge $D_Value ];then D_Name=`df -h|sed -n '/'"$i"'/p'|awk '{print $1}'` D_Part = ` df -h | sed -n ' / '" $ i "' / p ' | awk ' {print $ 6} ' ` date >> / tmp / disk.log echo " $ IP_Addr server's $ D_Name (partition $ D_Part) The usage rate exceeds $ D_Value%, and the current usage rate is $ i%. " >> / tmp / disk.log fi done if [-s /tmp/disk.log]; then cat /tmp/disk.log|mail -s " Warning-Used DISK " 1354586675 @ qq.com fi
Scheduled tasks
crontab -e #Execute the script every five minutes */5 * * * * /server/scripts/df.sh
Deploy ansible
yum install warm-release - and yum install ansible - y ansible --version #Set the host list, use sequence matching method vim /etc/ansible/hosts [ubuntu_server] 172.16.1.[41:100] #Distribute keys to 100 hosts ssh-keygen ansible ubantu_server -m copy -a "src=/root .ssh/id_rsa.pub dest=/root.ssh" mkdir /etc/ansible/palybook #Write yml file vim df .yml - hosts: ubantu_server remote_user: root tasks: - name: Create a script directory File : path: /server/scripts state: directory mode: 0755 - name: push script copy: src: / server / scripts / df . sh dest: / server / scripts / - name: Push mail profile copy: src: /etc/mail.rc dest: / etc / force: yes - name: create a scheduled task cron: name: "Disk percentage check" minute: "*/5" job: "sh /server/scripts/df.sh" #Execution script ansible -playbook ubantu.yml