1、收集导入excel表中(csv格式)
cat auto_device_csv.sh
#!/bin/bash
#Auto get system info to excel's tables.
#by authors lrj 2016-5-12
#定义各变量获取方式
ip_info=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f 2`
cpu_info1=`cat /proc/cpuinfo|grep "model name"|tail -1|awk -F: '{print $NF}'|awk '{print $1,$2,$3,$4,$NF}'`
cpu_info2=`cat /proc/cpuinfo|grep "physical id"|sort|uniq -c|wc -l`
cpu_info3=`cat /proc/cpuinfo|grep "physical id"|wc -l`
server_info=`hostname|tail -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/\,//g'`
mark_info='local_yum'
#打印脚本提示信息
echo -e "\033[34m \033[1m"
cat << EOF
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++Welcome to use system Collect+++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
#打印各变量获得结果
echo -e '\033[32m--------------------------------------------\033[1m'
echo Table_name:$mark_info
echo HOSTNAME:$server_info
echo IPADDR:$ip_info
echo CPU:$cpu_info1 X$cpu_info2 P$cpu_info3
echo Memory:$mem_info
echo Disk:$disk_info
echo Load:$load_info
echo -e '\033[32m--------------------------------------------\033[0m'
#获取结果是否存储到EXCEL表中
echo -e -n "\033[36mYou want to write this data to excel's table?\033[1m";read enter
if [ "$enter" == "yes" -o "$enter" == "y" -o "$enter" == "Y" ];then
echo "HOSTNAME,IPADDR,CPU,Memory,Disk,Load" >>"$mark_info".csv
echo "$server_info,$ip_info,$cpu_info1 X$cpu_info2 P$cpu_info3,$mem_info,$disk_info,$load_info" >>"$mark_info".csv
else
echo -e '\033[32m--------------------------------------------\033[0m'
exit
fi
echo -e '\033[32m--------------------------------------------\033[0m'
2、收集导入MySQL中
#创建数据库与表
create database server;
use server;
create table device_info(id int(11) NOT NULL AUTO_INCREMENT,
HOSTNAME varchar(50) NOT NULL,
IPADDR varchar(50) NOT NULL,
CPU varchar(80) NOT NULL,
Memory varchar(50) NOT NULL,
Disk varchar(50) NOT NULL,
Load_info varchar(50) NOT NULL,
PRIMARY KEY (id)
);
grant all on server.* to 'audit1'@'localhost' identified by '123456';
#脚本如下:
cat auto_device_mysql.sh
#!/bin/bash
#Auto get system info to mysql.
#by authors lrj 2016-5-17
#定义各变量获取方式
ip_info=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f 2`
cpu_info1=`cat /proc/cpuinfo|grep "model name"|tail -1|awk -F: '{print $NF}'|awk '{print $1,$2,$3,$4,$NF}'`
cpu_info2=`cat /proc/cpuinfo|grep "physical id"|sort|uniq -c|wc -l`
cpu_info3=`cat /proc/cpuinfo|grep "physical id"|wc -l`
server_info=`hostname|tail -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/\,//g'`
mark_info='local_yum'
#打印脚本提示信息
echo -e "\033[34m \033[1m"
cat << EOF
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++Welcome to use system Collect+++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
#打印各变量获得结果
echo -e '\033[32m------------------------------\033[1m'
echo Table_name:$mark_info
echo HOSTNAME:$server_info
echo IPADDR:$ip_info
echo CPU:$cpu_info1 X$cpu_info2 P$cpu_info3
echo Memory:$mem_info
echo Disk:$disk_info
echo Load:$load_info
echo -e '\033[32m----------------------------\033[0m'
#获取结果是否存储到Mysql
echo -e -n "\033[36mYou want to write this data to mysql?\033[1m";read enter
if [ "$enter" == "yes" -o "$enter" == "y" -o "$enter" == "Y" ];then
mysql -D server -e " insert into server.device_info(HOSTNAME,IPADDR,CPU,Memory,Disk,Load_info)values( '$server_info','$ip_info','$cpu_info1 X$cpu_info2 P$cpu_info3','$mem_info','$disk_info','$load_info' ); "
else
echo -e '\033[32m------------------------------\033[0m'
exit
fi
echo -e '\033[32m------------------------------\033[0m'