1.脚本部分 #!/bin/bash #auto get system info echo -e "\033[34m\033[1m" cat <<EOF +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++Welcome to use system Coolect++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ EOF ip_info=`ifconfig eth1|grep "Bcast"|awk ‘{print $2}‘ |cut -d: -f 2` cpu_info1=`cat /proc/cpuinfo |grep "model name" |awk -F: ‘{print $2}‘|sed ‘s/^//g‘|awk ‘{print $1,$3,$4,$NF}‘` cpu_info2=`cat /proc/cpuinfo |grep "physical id"|sort|uniq -c|wc -l` serv_info=`hostname |tail -1` 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 "CurrentLoad: "$(NF-2)}‘|sed ‘s/\,//g‘` make_info=‘BeiJing_IDC‘ echo -e "\033[32m-------------------------------------\033[1m" echo IPADDR:${ip_info} echo HOSTNAME:$serv_info echo CPU_INFO:${cpu_info1}X${cpu_info2} echo Disk_INFO:$disk_info echo MEM_INFO:$mem_info echo LOAD_INFO:$load_info echo -e -n "\033[36m You want to write the data to the databases?\033[1m";read ensure if [ "$ensure" == "yes" -o "$ensure" == "y" -o "$ensure" == "Y" ];then echo "------------------------" #echo -e ‘\033[31mmysql -uroot -p 123456 -D audit -e‘‘‘ "insert into audit_audit_system values(‘‘,${ip_info}‘,‘$serv_info‘,‘${cpu_info1}X${cpu_info2}‘,‘$disk_info‘,‘$mem_info‘,‘$load_info‘,‘$make_info‘)" ‘‘‘ \033[0m‘ mysql -uroot -proot -D zzx -e "insert into systeminfo values(‘‘,‘${ip_info}‘,‘$serv_info‘,‘${cpu_info1}X${cpu_info2}‘,‘$disk_info‘,‘$mem_info‘,‘$load_info‘,‘$make_info‘);" else echo "what exit" exit fi #向数据库插入数据 #mysql -uroot -proot -e ‘use zzx;select * from syslog;‘|sed ‘s/-//g‘|grep -v "id" 2.数据库创建 CREATE TABLE `systeminfo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip_info` varchar(50) NOT NULL, //主机IP `serv_info` varchar(50) NOT NULL,//主机名 `cpu_info` varchar(50) NOT NULL,//cup型号 `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`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 3.web页面显示 <html> <head> <title>服务器管理统计</title> </head> <body> <?php $con = mysql_connect("192.168.250.190","root","root"); if (!$con) { die(‘数据库连接失败: ‘ . mysql_error()); } else { mysql_query("SET NAMES UTF8"); mysql_query("set character_set_client=utf8"); mysql_query("set character_set_results=utf8"); mysql_select_db("zzx", $con); $result = mysql_query("SELECT * FROM systeminfo"); //在表格中输出显示结果 echo "<table border=‘1‘ > <tr> <th>主机IP</th> <th>主机名</th> <th>cup型号</th> <th>磁盘</th> <th>内存</th> <th>负载</th> <th>机房</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[‘ip_info‘] . "</td>"; echo "<td>" . $row[‘serv_info‘] . "</td>"; echo "<td>" . $row[‘cpu_info‘] . "</td>"; echo "<td>" . $row[‘disk_info‘] . "</td>"; echo "<td>" . $row[‘mem_info‘] . "</td>"; echo "<td>" . $row[‘load_info‘] . "</td>"; echo "<td>" . $row[‘mark_info‘] . "</td>"; echo "</tr>"; } echo "</table>"; } mysql_close($con); ?> </body>
显示结果:
时间: 2024-09-28 01:37:52