Use shell script to "synthesize, centralize" to view common hardware and software information of Linux server

Source: Internet
Author: User

As an OPS person, it is essential to understand the hardware-related information of each server and the system-related configuration. Although not difficult, but very fragmented, information concentration is not high. So do a set of centralized display of the above information "platform" (in fact, no platform, collect-organize-show only), its principle by a "monitoring machine" (so call it) run the main script, according to the record server IP file loop processing each server; cooperate with "monitored machine" On the two awk files (awk rules), the information is collected uniformly to the "monitor machine" and then presented by the Web server, the interface is roughly the following surface legend.

#注: Web server needs to have directory access enabled

Home page, all the servers are displayed as a directory containing information about the server in the directory.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/66/wKioL1X8McCjRIqFAADoR30YaUA273.jpg "title=" 1.png " alt= "Wkiol1x8mccjriqfaador30yaua273.jpg"/>

After selecting a server, go to Level two page and record the task schedule/hardware Information/firewall settings/root-shell The directory that I used to store the shell script/sys_conf as the system common configuration file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/66/wKioL1X8MmGgp86iAAElZbahbV8281.jpg "title=" 2.png " alt= "Wkiol1x8mmggp86iaaelzbahbv8281.jpg"/>

Among them, Devinfo.txt is the hardware information after finishing in the meeting, such as. Personally feel "memory" display effect is I am more proud of, can clearly see how many slots, plug in a few, the existing memory of a single capacity/frequency/factory code, this information in the increase or replacement of memory is very useful. In addition, the disk information with the LSBLK command is more concise and intuitive

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/66/wKioL1X8M13gITmSAAPRibXrMBc252.jpg "title=" 3.png " alt= "Wkiol1x8m13gitmsaapribxrmbc252.jpg"/>

Sys_conf directory, the system has been retained for three days since the common configuration file, including the following files

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/66/wKioL1X8PAfT2yakAAJIPEJLN8k622.jpg "title=" 5.png " alt= "Wkiol1x8paft2yakaajipejln8k622.jpg"/>

See here, if you feel a little use of classmates can then go down to see how to achieve. This script has been tested on both CENTOS6 and 7.

My "monitor" is in the intranet, through SSH keyless key way can be logged to the line of the last server (because it is unavoidable to read more than one server in bulk operation, it is necessary to configure an intranet can be a key-free login online server).

The main script runs at the "Monitor" timer and obtains the hostname and IP of Mr. Server through the ips.txt file. Ips.txt is counted in other ways (this is a variety, the worst is manually editing this) in the following format: web1-x.x.x.x, one per line . The main script gets the information with the remote execution command, where two awk files, Servername.awk and Mem.awk (called by awk-f, are used to obtain and format the output "machine model string" and "Memory" information) on each "monitored machine". How to upload these files to the server in batches is not covered in this article.

Finally, all information is aggregated in the/server_info directory of the "Monitor" and the Web server root is pointed to.

Main script content: cat/root/shells/serverinfo.sh

#!/bin/sh# the hardware information of each server into text and then web-server to display for server_ip in  ' cat /root/shells/ Ips.txt '   #记录局域网的所有主机ip和主机名   do  ip= ' echo  $server _ip|awk -f  '-'   ' { print $2} '     #获取各server  ip    ## #第一次运行该脚本, it is best to add a yum to the following remote commands section  -y install dmidecode util-linux-ng > /dev/null  ## #以防命令不存在    ssh [email protected] $ip   "cat /etc/issue|sed -n  ' 1p '  > / devinfo/devinfo.txt;           uname -a|cut - d  '   '  -f 2|sed s/^/hostname: '   '/ >> /devinfo/devinfo.txt;            uname -a|cut -d  '   '  -f  3|sed s/^/kernel: '   '/ >> /devinfo/devinfo.txt;            dmidecode -t system|awk -f /root/shells/servername.awk >> /devinfo/ devinfo.txt;           echo -e  ' \ n--------CPU :-------'  >> /devinfo/devinfo.txt;            lscpu |egrep -v  ' order| vendor| model| Stepping| bogomips|family| on-line|op-mode| Cpu\ socket| Numa\ node0 '  >> /devinfo/devinfo.txt;            echo -e  ' \ n------Memory:------'  >> /devinfo/devinfo.txt;            dmidecode -t memory|grep -A7  physical >> /devinfo/devinfo.txt;            dmidecode -t memory|grep -e  "size.*[0-9]"  -a8|awk -f /root/shells/ mem.awk >> /devinfo/devinfo.txt;           echo -e  ' \ n-------Disk:-------'  >> /devinfo/devinfo.txt;            lsblk >> /devinfo/devinfo.txt;            crontab -l > /devinfo/crontab.txt;            iptables -l -n > /devinfo/iptables.txt "   mkdir /server_info/$server _ip &> /dev/null  scp [email protected] $ip :/devinfo/* /server_info/$server _ip/  ## #从/tmp/$ip   Copy system level profile   backup to   time= ' Date +%f '   mkdir -p /server_info/$server _ip/sys_conf/$time   cp  / tmp/$ip/* /server_info/$server _ip/sys_conf/$time/  ## #保留两天的sys_conf即可   find / server_info/$server _ip/sys_conf -type d -mtime +1|xargs rm -rf    ## #将生产各server/root/ shells  script synchronization to do backup   rsync -av --delete [email protected] $ip:/root/shells/  /server_info/$server _ip/root-shells    echo -e  "\e[1;31m  $server _ip  is done \e[0m "  done

The contents of the two awk files referenced in the main script are as follows

[[email protected] ~]# cat Shells/servername.awk $0~/product.*/{printf "%s", "\nserver Model:" $ (NF-1) "" $NF}$0~/serial.*/{ printf "%s", $ "\ n"}[[email protected] ~]# cat Shells/mem.awk $0~/size.*[0-9]/{printf "%s", $ "\ T"} $0~/speed.*[0-9]/{PR intf "%s", $ "\t\t"} $0~/manufacturer.*/{print "Factory code:" $ $}

Hope this script can give other students a little help, also not waste the spirit of open source.

In fact, this is just the spirit of "let operation and maintenance simplification" big thought of a small embodiment, this set of small platform can exist independently, but also with other platforms integration. For example, I am now working on a company's internal dimensional plane platform (with limited technology, less-than-simple ones) that integrates daily from Dev to test, on-line, log analytics, and the Server_info platforms you share above.

Use shell script to "synthesize, centralize" to view common hardware and software information of Linux server

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.