shell脚本_获取服务器数据并插入到数据库

今天写一个比较实用的脚本,实现的功能是获取服务器的各项数据并写入到数据库中,我们获取的数据有:服务器ip,
服务器的处理器型号,服务器存储容量,服务器运存大小,服务器当前负载。

IP=`ifconfig|grep 'Bcast'|awk  '{print $2}'|sed 's/addr://g'` #ip地址

CPU_INFO=`cat /proc/cpuinfo|grep "model name"|awk '{print $4,$5,$8,$10}'` #cpu型号

DISK=`fdisk -l|grep "GiB"|awk '{print $1,$2,$3,$4 }'|sed 's/,//g'`; #存储大小

MEM=`free -m|grep "Mem"|awk '{print "total " $1,$2"m"}'`;       #运存大小

CURRENT_LOAD=`uptime|awk '{print "current load:" $(NF-2)}'`;    #当前负载

然后创建一个pc数据库,创建一个表system存储该数据

#创建数据库pc
function create_db(){
        echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"
        mysql -uroot -p123456 -e "use pc";
        if [ $? -eq 0 ];then
        echo "pc is exists"
        else
        echo "创建数据库pc";
        mysql -uroot -p123456 -e "create database pc;"
        fi      
}
create_db    

创建一个system表:

#创建table
function create_system(){
         echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"
    
        mysql -uroot -p123456 -e" 
                                use pc; 
                                create table system(
                                        IP varchar(20) unique,
                                        CPU_NIFO varchar(100),
                                        DISK varchar(100),
                                        MEM varchar(100),
                                        CURRENT_LOAD varchar(100)
                                );"
        if [ $? -eq 0 ];then
                echo " create table system is ok"
        else
                echo "create table system is exists"
        fi
}
create_system

创建insert函数:

#写入数据
function insert(){
         echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"

        mysql -uroot -p123456 -e"
                use pc;
                insert into system values('$IP','$CPU_INFO','$DISK','$MEM','$CURRENT_LOAD');"
        if [ $? -eq 0 ];then
                echo " insert data is ok"
        else
                echo "this data is exists"
        fi
}
insert

最后总的实现脚本代码:

#!/bin/bash
#authors ym 2019

IP=`ifconfig|grep 'Bcast'|awk  '{print $2}'|sed 's/addr://g'` #ip地址
CPU_INFO=`cat /proc/cpuinfo|grep "model name"|awk '{print $4,$5,$8,$10}'` #cpu型号
DISK=`fdisk -l|grep "GiB"|awk '{print $1,$2,$3,$4 }'|sed 's/,//g'`; #存储大小
MEM=`free -m|grep "Mem"|awk '{print "total " $1,$2"m"}'`;	#运存大小
CURRENT_LOAD=`uptime|awk '{print "current load:" $(NF-2)}'`;	#当前负载
USER=root;
PASSWD=123456;

echo -e "\033[033m------------------------------------------Wecome---------------------------------------------\033[0m"
echo " ip: $IP";
echo " cpu:$CPU_INFO"
echo " disk: $DISK";
echo " $MEM";
echo " $CURRENT_LOAD"

#创建数据库pc
function create_db(){
	echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"
	mysql -uroot -p123456 -e "use pc";
	if [ $? -eq 0 ];then
	echo "pc is exists"
	else
	echo "创建数据库pc";
	mysql -uroot -p123456 -e "create database pc;"
	fi	
}
create_db

#创建table
function create_system(){
	 echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"

	mysql -uroot -p123456 -e"
				use pc;
				create table system(
					IP varchar(20) unique,
					CPU_NIFO varchar(100),
					DISK varchar(100),
					MEM varchar(100),
					CURRENT_LOAD varchar(100)
				);"
	if [ $? -eq 0 ];then
		echo " create table system is ok"
	else
		echo "create table system is exists"
	fi
}
create_system

#写入数据
function insert(){
	 echo -e "\033[032m----------------------------------------create_db---------------------------------------------\033[0m"

	mysql -uroot -p123456 -e"
		use pc;
		insert into system values('$IP','$CPU_INFO','$DISK','$MEM','$CURRENT_LOAD');"
	if [ $? -eq 0 ];then
		echo " insert data is ok"
	else
		echo "this data is exists"
	fi
}
insert
 echo -e "\033[033m----------------------------------------create_db---------------------------------------------\033[0m"


shell脚本_获取服务器数据并插入到数据库_第1张图片

你可能感兴趣的:(linux)