The system acquires and stores information into a database table

1、创建数据库表
CREATE TABLE audit_system (
ID int(11) NOT NULL AUTO_INCREMENT,
IP_INFO varchar(255) NOT NULL,
SERV_INFO varchar(255) NOT NULL,
CPU_INFO varchar(255) NOT NULL,
MEM_INFO varchar(255) NOT NULL,
DISK_IFNO varchar(255) NOT NULL,
LOAD_INFO varchar(255) NOT NULL,
MARK_INFO varchar(255) NOT NULL,
PRIMARY KEY (ID)
);

2, execution of the script, the script is as follows:
! # / Bin / SH
#auto GET System info
#by ZKG 2019-07-11

echo -e "\033[34m \033[1m"
cat << EOF

-------------- ---------------- Check the system-related information

EOF
echo -e "\033[32m \033[0m"

# Define information about the system variables
IP_INFO = ifconfig|grep "Bcast"|tail -1|awk '{print $2}'|awk -F: '{print $2}'
SERV_INFO = hostname|tail -1
CPU_INFO1 = cat /proc/cpuinfo |grep "model name"|awk -F: '{print $2}'|awk '{print $1,$2,$3,$4,$7}'|tail -1
CPU_INFO2 = cat /proc/cpuinfo|grep "physical id"|wc -l
The cpu_info = "$ CPU_INFO1 the X-$ CPU_INFO2"
MEM_INFO = free -m|grep "Mem"|awk '{print "total",$2"M"}'
disk_info = fdisk -l|grep "Disk"|grep -v "identifier"|grep -v "VolGroup"|awk -F, '{print $1}'|awk '{print $2,$3,$4}'
LOAD_INFO = uptime|awk -F, '{print $4,$5,$6}'|awk -F: '{print $2}'|awk '{print "1分钟负载:",$1,"5分钟负载:",$2,"15分钟负载:",$3}'
MARK_INFO = "HangZhou_AnHeng"
# Output system information
echo -e "\ 033 [32m --- ------------------------------------------------- \ 033 [1M "
echo" IP_INFO: $ IP_INFO "
echo" SERV_INFO: $ SERV_INFO "
echo" The cpu_info: $ The cpu_info "
echo" MEM_INFO: $ MEM_INFO "
echo" disk_info: $ disk_info "
echo" LOAD_INFO: $ LOAD_INFO "
echo" MARK_INFO: MARK_INFO $ "
echo -e" \ 033 [32M --------------------------------------- ------------- \ 033 [0m "

#输出到数据库表中
echo -e "\033[32mYou want to write the data to the Database?[YES/Yes/yes/y/Y]or[NO/no/n/N]:\033[0m"
read ENSURE
if [ "$ENSURE" == "YES" -o "$ENSURE" == "Yes" -o "$ENSURE" == "yes" -o "$ENSURE" == "y" -o "$ENSURE" == "Y" ];then
echo "insert into audit_system values('','$IP_INFO','$SERV_INFO','$CPU_INFO','$MEM_INFO','$DISK_INFO','$LOAD_INFO','$MARK_INFO')"|mysql -usoc -psoc -Dsoc
echo -e "\033[32mwrite the data to the database successfully!\033[0m"
elif [ "$ENSURE" == "NO" -o "$ENSURE" == "no" -o "$ENSURE" == "n" -o "$ENSURE" == "N" ];then
exit 0
else
exit
fi

Guess you like

Origin blog.51cto.com/1009516/2425817