收集服务器信息脚本并写入数据库

#!/bin/bash
# **********************************************************
# * Author        : quanzhiqiang
# * Email         : 
# * Create time   : 2019-10-27 21:34
# * Filename      : server_info.sh
# * Description   : 
# *********************************************************
echo -e "\033[34m \033[1m"
cat <<EOF
----------------------------------------------------------
-----------------server system information collect-----------
-------------------------------------------------------------
EOF

######################################统计服务器的基本信息###########################################
#function sum_info(){
    all_ip_info=`ifconfig |grep "Bcast"|awk  ‘{print $2}‘ |cut -d: -f 2 |tr ‘\n‘ ‘\t‘`
    cpu_info_brand=`cat /proc/cpuinfo |grep "model name" |awk -F: ‘{print $2}‘|sed ‘s/^ //g‘|cut -d " " -f 1,3,4,6`
    cpu_heshu=`cat /proc/cpuinfo |grep "^physical" |sort|uniq -c |tr -s  ‘ ‘|tail -1 |cut -d ‘ ‘ -f 2`
    cpu_geshu=`cat /proc/cpuinfo |grep "^physical" |sort|uniq -c |wc -l`
    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/,//‘`
#    }
######################################输出服务器的基本信息#############################################
function print_info(){
    echo -e "\033[32m >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[1m"
    echo "ALL_IPADDR:    ${all_ip_info}"
    echo "CUP_BRAND:    ${cpu_info_brand}"
    echo "CPU_AUDITING:    ${cpu_heshu}"
    echo "CPU_NUMBER:    ${cpu_geshu}"
    echo "DISK_INFO:    ${disk_info}"
    echo "MEM_INFO:     ${mem_info}"
    echo "LOAD_INFO:    ${load_info}"
    echo -e "\033[0m"
    }
#####################################三个点的函数#############################################
function wait(){
    for ((i=0;i<3;i++))
    do
        echo -n "."
        sleep 1
    done
    }

#######################################数据库的写入#####################################
function write_db(){
    echo -e "\033[35m <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \033[0m"
    echo -e "\033[35m Write this data to the databases? \033[0m"
    echo -e  "\033[35m Please input Y/N \033[0m" 
    read input
    if [ "$input" == "Y" ] ;then
        echo -e "\033[31m"
        echo -n "Begin to write"
        wait
        echo -e "\033[0m"
    ######write databases
    #mysql -u root -p 2004 -D test -e "insert into server_data values(‘‘,‘${all_ip_info}‘,‘*******************‘)"
        echo ""
    else
        echo -e "\033[31m"
        echo -n "Begin to exit"
        wait
        echo -e "\033[0m"
        exit
    fi
    }
#####################################调用###################################
print_info
write_db

相关推荐