[root@shell ~]# cat test.sh
#!/bin/sh
# auto get system info
# 0528 by caq
ip_info=`ip a show dev ens32|grep "global"|awk '{print $2}'|awk -F '/' '{print $1}'`
cpuinfo=`cat /proc/cpuinfo |grep "model name"|awk -F : '{print $2}'`
serv_info=`hostname`
disk_info=`fdisk -l |grep "Disk"|grep -v "identifier"|awk '{print $2,$3,$4}'|sed 's/,//g'`
mem_info=`free -m|grep "Mem"|awk '{print "Total",$1 $2"M"}'`
load_info=`uptime |awk '{print "Current Load: "$(NF-2)}'|sed 's/,//g'`
echo "------------------------------------------------------------------------------"
echo IPADDR:${ip_info}
echo HOSTNAME:$serv_info
echo CPU_INFO:$cpuinfo
echo DISK_INFO:$disk_info
echo MEM_INFO:$mem_info
echo LOAD_INFO:$load_info
echo "------------------------------------------------------------------------------"
read -p 'Do you want to create sys_info database(yes or no)' ensure
if [[ $ensure == yes ]];then
mysql -uroot -pa <<EOF
create database test;
use test;
CREATE TABLE audit_audit_system ( id int(11) NOT NULL AUTO_INCREMENT,ip_info varchar(50) NOT NULL,serv_info varchar(50) NOT NULL,cpu_info varchar(50) NOT NULL,disk_info varchar(50) NOT NULL, mem_info varchar(50) NOT NULL, load_info varchar(50) NOT NULL, mark_info varchar(50) NOT NULL, PRIMARY KEY (id), UNIQUE KEY ip_info (ip_info), UNIQUE KEY ip_info_2 (ip_info) );
insert into audit_audit_system values('1','$ip_info','$serv_info','$cpuinfo','$disk_info','$mem_info','$load_info','BejingIDC');
EOF
echo "insert data successful!"
else
echo "insert data error!"
if [[ $ensure == no ]];then
echo "wait quit"
sleep 1
exit
fi
[root@docker1 ~]# sh test.sh
------------------------------------------------------------------------------
IPADDR:192.168.1.10
HOSTNAME:docker1
CPU_INFO: AMD Ryzen 5 3550H with Radeon Vega Mobile Gfx
DISK_INFO:/dev/sda: 21.5 GB label type: dos /dev/sdb: 21.5 GB /dev/mapper/centos-root: 18.2 GB /dev/mapper/centos-swap: 2147 MB /dev/mapper/vg1-lv1: 306 MB
MEM_INFO:Total Mem:972M
LOAD_INFO:Current Load: 0.07
------------------------------------------------------------------------------
Do you want to create sys_info database(yes or no)yes
insert data successful!