Zabbix monitors the total space size of all disks in the Linux system script

First of all, I am a little white who has just turned into the company. Everyone, please don't hesitate to talk about me, because writing this script seems easy, but it actually took me a lot of time. As soon as I was on a business trip to a city, I was asked by an old colleague to write a script that the client needed to get the total disk space of the system disk.
When I just received this task, I must have gotten my enthusiasm. I thought it was not easy. Baidu will get the result at a glance, and then modify the script and negotiate! It’s a good idea, but it’s difficult to implement it.
At the beginning, I was thinking how to get the total disk space. Is the df -h command to add all the spaces in the Size? But some directories that are mounted inside are also included, which is definitely not correct. Or directly calculate the total size in the / directory? This seems to be the case, but in fact it is not included in the external hard drive, which is wrong. I will go to Baidu.
As a result, I have been Baidu for a long time, and I don’t know how to calculate the space size of all disks. I also think about it while working, and I also asked some other colleagues. I don’t say what the process is. Anyway, I finally decided to use fdisk -l. Calculate the total value to calculate the total disk space, because the fdisk command can see other mounted hard disk information and partition information. Use df to calculate the total used space size, because here it really shows all disk usage information.
The command result of fdisk -l is as follows:
Zabbix monitors the total space size of all disks in the Linux system script
I just want to calculate the total size of the disk in the box to represent the total disk space. I originally thought this idea was wrong, but I don't know how to replace the total disk space. But no matter what, I just want to use the three swordsmen of the shell script-awk, grep, sed to get the character content in the command.
Here I use the parameter of grep -n'content' to filter the lines with'content' characters in the information characters, use awk $n to print the number of parameters in each line, and also use awk to count words The sum of the sections. I originally wanted to use the calculation of unit conversion to calculate the sum, but I still think it’s better to keep it simple.
So the initial shell script has been tested many times, and after checking the data with manual calculation, it has a prototype:
#!/bin/bash
d_t=$(fdisk -l | grep -n'disk' |grep -n'byte'|awk'{print $4}'|awk'BEGIN{sum=0}{sum+=$1}END{print sum}' ) #Total
disk space
echo -e "${d_t}" #Total
used disk space
d_u=$(df | awk'{print $3}'| grep -v used | awk'BEGIN{sum=0}{sum+ =$1}END{print sum}')
echo -e "${d_u}" #Total
remaining disk space
d_f=$[${d_t}-${d_u}]
This is a main core algorithm. This is slowly calculated by looking at the basics of shell scripts learned before. Among them, awk'BEGIN{sum=0}{sum+=$1}END{print sum}' is to calculate the sum of a set of numbers, I believe netizens will use it in the future.
Then, he ran to the old colleague and said that the general idea came out, to see if there was any next requirement, and then he checked it. It is proposed to add a judgment, the input value will have the result, and there is a Linux system that meets the requirements of both Chinese and English.
I thought about it. Isn't this the case and if, then? Learned before, and then improved the script, as follows:
#!/bin/bash #Total
disk space size, in bytes
disk_total() {
d_t=$(fdisk -l | grep -n'disk' |grep -n 'Byte'|awk'{print $4}'

d_t2=$(fdisk -l | grep -n'Disk' |grep -n'bytes'|awk'{print $5}'|awk'BEGIN{sum=0}{sum+=$1}END{print sum}')
echo -e "${d_t2}"
#echo -e "Total disk space is: ${d_t2}B"
else
echo -e "${d_t}"
#echo -e "Total disk space is: ${d_t }B"
fi
} #Total
used disk size, note that the unit is 1k
disk_used() {
d_uk=$(df | awk'{print $3}'| grep -v used | awk'BEGIN{sum=0}{sum+= $1}END{print sum}')
d_u=$((1000 d_uk))
if [${d_u} -eq 0 ]; then
d_u2k=$(df | awk'{print $3}'| grep -v Used| awk 'BEGIN{sum=0}{sum+=$1}END{print sum}')
d_u2=$((1000
d_u2k))
echo -e "${d_u2}"
else
echo -e "${d_u}"
fi
} #Calculating the
total remaining space
disk_free() {
d_t=$(fdisk -l | grep -n '磁盘' |grep -n '字节'|awk '{print $4}'|awk 'BEGIN{sum=0}{sum+=$1}END{print sum}')
d_u=$(df | awk '{print $3}'| grep -v 已用| awk 'BEGIN{sum=0}{sum+=$1}END{print sum}')
d_f=$[${d_t}-d_u1000]
#d_f="expr $d_t-$d_u"
#echo "$d_u"
#echo "${d_u}"
if [ ${d_t} -eq 0 ];then
d_t2=$(fdisk -l | grep -n 'Disk' |grep -n 'bytes'|awk '{print $5}'|awk 'BEGIN{sum=0}{sum+=$1}END{print sum}')
d_u2=$(df | awk '{print $3}'| grep -v Used| awk 'BEGIN{sum=0}{sum+=$1}END{print sum}')
d_f2=$[${d_t2}-d_u
1000]
#d_f2=expr $d_t2-$d_u2
echo -e "${d_f2}"
else
echo -e "$ {d_f} " ;;disk_totaldt)case" $ 1 "in}
fi





df)
disk_free
;;
du)
disk_used
;;
*)
echo "Usage: $0 {dt=disk_total}{df=disk_free}{du=disk_used}"
;; The
esac
script is also tested and perfected many times before gradually discovering some problems For example: when you enter the df command, the unit of used is not bytes, but kB. I haven't read it carefully before, and I found the problem after testing multiple hosts.
The results of the script test are as follows: It
Zabbix monitors the total space size of all disks in the Linux system script
looks like it's going well, so I can take it to an old colleague.

Guess you like

Origin blog.51cto.com/14483703/2546139