Shell Programming Basics-Examples

Source: Internet
Author: User

The shell automatically collects server hardware system information, inserts a database, and displays it through a Web page.

First, the shell automatically collects server hardware system information and inserts the database. #centos under 7 operating systems

#!/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 $} ' |cut-d:-F 2 '
cpu_info1= ' cat/proc/cpuinfo |grep ' model name ' |awk-f: ' {print $} ' |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


Second, insert data into the database
#mysql-uroot-proot-e ' use database name; select * from Syslog; ' | Sed ' s/-//g ' |grep-v "id"
#数据库创建
CREATE TABLE ' SystemInfo ' (
' id ' int (one) not NULL auto_increment,
' Ip_info ' varchar (+) not NULL,//Host IP
' Serv_info ' varchar (*) not NULL,//host name
' Cpu_info ' varchar (not NULL),//cup model
' Disk_info ' varchar (a) not NULL,//disk
' Mem_info ' varchar (.) Not NULL,//memory
' Load_info ' varchar (.) Not NULL,//load
' Mark_info ' varchar (not NULL),//Remarks
PRIMARY KEY (' id '),
) Engine=myisam DEFAULT charset=latin1;

Third, the page display
<title> Server Management statistics </title>
<body>
<?
Php$con = mysql_connect ("192.168.250.190", "root", "root");
if (! $con)
{
Die (' Database connection failed: '. 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 ("Database name", $con);
$result = mysql_query ("SELECT * from table1");
Output display results in a table
echo "<table border= ' 1 ' >
<tr>
<th> Host Ip</th>
<th> Host name </th>
<th>cup Model </th>
<th> Disk </th>
<th> Memory </th>
<th> Load </th>
<th> Room </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 Programming Basics-Examples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.