码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程基础-实例

时间:2017-02-12 15:33:13      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ifconfig   表格   操作系统   ipad   ota   管理   cal   插入数据   free   

shell自动收集服务器硬件系统信息,插入数据库并通过web页面显示。

 

一,shell自动收集服务器硬件系统信息,插入数据库。#centos 7操作系统下

#!/bin/bash
#auto get system info
echo -e "\033[34m\033[1m"     
cat <<EOF
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++Welcome to use system Coolect++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
ip_info=`ifconfig enp0s3|grep "broadcast"|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‘`
mark_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 -e "\033[31m"

  mysql -uroot -proot -D table2 -e "insert into system_info2 values(‘‘,‘${ip_info}‘,‘$serv_info‘,‘${cpu_info1}X${cpu_info2}‘,‘$disk_info‘,‘$mem_info‘,‘$load_info‘,‘$mark_info‘);"
else
    echo "exit"
  exit
fi


二,向数据库插入数据
#mysql -uroot -proot -e ‘use 数据库名;select * from syslog;‘|sed ‘s/-//g‘|grep -v "id"
#数据库创建
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`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

三,网页显示
<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("数据库名", $con);
        $result = mysql_query("SELECT * FROM table1");
        //在表格中输出显示结果
        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>

shell编程基础-实例

标签:ifconfig   表格   操作系统   ipad   ota   管理   cal   插入数据   free   

原文地址:http://www.cnblogs.com/sunziying/p/6390972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!