#!/bin/bash


#######收集系统硬件信息

count=`cat list.txt |wc -l`

rm -rf list.txt.swp

i=0

while ((i < $count))

do

i=`expr $i + 1`

sed "${i}s/^/&${i} /g" list.txt >>list.txt.swp

ip=`awk -v I="$i" '{if(I==$1)print $2}' list.txt.swp`

cpu_info=`ssh -q -l root $ip cat /proc/cpuinfo |grep "model name"|tail -1 |awk -F: '{print $NF}' |sed 's/^//g' |awk '{print $1,$2,$3,$4,$6}'`             ##CPU信息

cpu_info2=`ssh -q -l root $ip cat /proc/cpuinfo |grep "physical id" |wc -l`                                                                                                                     ##CPU个数

server_info=`ssh -q -l root $ip hostname |tail -l`

disk_info=`ssh -q -l root $ip fdisk -l |grep "Disk" |grep -v "identifier" |grep -v "label type" |awk '{print $1,$2,$3,$4}' |sed 's/,//g'`

mem_info=`ssh -q -l root $ip free -mh |grep "Mem" |awk '{print $2}'`

swp_info=`ssh -q -l root $ip free -mh |grep "Swap" |awk '{print $2}'`

load_info=`ssh -q -l root $ip uptime |awk '{print "Current Load: " $(NF-2)}' |sed 's/,//g'`

echo -e  "IP:$ip   \nCPU:$cpu_info*$cpu_info2   \n主机名:$server_info    \n硬盘大小:$disk_info    \n内存:$mem_info     \nSWAP:$swp_info    \n负载:$load_info  \n"


mysql -uroot -p123456 -h192.168.1.201  -D gqtest -e "INSERT INTO gqtest.audit_audit_system (ip_info, server_info, cpu_info, cpu_info2, disk_info, mem_info,swp_info, load_info) VALUES ('${ip}','$server_info','$cpu_info','$cpu_info2','$disk_info','$mem_info','$swp_info','$load_info');"


done 



[root@localhost shell]# cat list.txt

192.168.1.131 

192.168.1.86  

192.168.1.201 

192.168.1.205 

CREATE TABLE `audit_audit_system` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ip_info` varchar(22) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `server_info` varchar(55) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `cpu_info` varchar(55) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `cpu_info2` int(11) NOT NULL DEFAULT '0' COMMENT 'CPU颗数',
  `disk_info` varchar(55) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `mem_info` varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `swp_info` varchar(121) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `load_info` varchar(55) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `mark_info` varchar(66) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci