Use saltstack in Python to generate a server asset list
The hardware information of each server has been recorded manually. You can write and capture the information everywhere through scripts or automated tools. This article mainly uses saltstack as a tool, then, use the provided APi to compile the required Python script ~~
The requirements are as follows: Generate server host name, IP address, memory, number of CPU cores, operating system, data disk quota, and mainly run services
Saltstack Quick Start, see http://youerning.blog.51cto.com/10513771/1708964
Here, we mainly use grains of saltstack, which is some static information generated by saltstack minion, such as CPU, memory, host name, and so on. These are what we need.
Execute salt \ * grains. items to print a lot of default captured information, some of which are as follows:
Of course, we only need to select what we need. The operations are as follows:
Get Host Name
saltH-T-4grains.itemhost
Get IP Address
saltzabbixgrains.itemipv4
Obtain the number of CPU Cores
salt\*grains.itemnum_cpus
And so on, extract ~~~ according to your needs ~~~
It is worth noting that there is no hard disk information in the grains information, so we need to use the disk. usage option to obtain the required hard disk information.
Execute saltzabbixdisk. usage. The result is as follows:
Among them, 1K-blocks is the hard disk information we need. We only need data disks/data as needed, so we will calculate the disk quota later.
The final script is as follows:
# Coding = utf-8importsalt.clientasscimportjson ### salt call local = SC. localClient () ### specify the target host tgt = "*" ### obtain grains, and the disk information is grains = local. cmd (tgt, "grains. items ") diskusage = local. cmd (tgt, "disk. usage ") ### Main Application List: app_name = [" tomcat "," zookeeper "," redis "," mysql "," nginx "] cols =" Host Name, IP address, memory (GB), number of CPU cores, operating system, data disk/data (GB), Project, main application: open A. CSV file to write ret_file = open ("ret.csv ", "w") ### write the beginning, which indicates ret_file.write (cols + "\ n") try: foriingrains. keys (): ### comment out printgrains [I] ["nodename"] print "ipv4" + ":", grains [I] ["ipv4"] print "mem_total" + ":", grains [I] ["mem_total"]/1024 + 1 print "num_cpus" + ":", grains [I] ["num_cpus"] print "osfullname" + ":", grains [I] ["osfullname"] print "release" + ":", grains [I] ["lsb_distrib_release"] ### some hosts may not have/data disk 1048576 is 1024x1024if "/data" notindiskusage [I]: print "diskusage" + ": "+" haveno/datadisk "else: data_vol = int (diskusage [I] ["/data "] [" 1K-blocks "]) print" diskusage "+ ":", data_vol/1048576 ### remove the address ipv4 = str (grains [I] ["ipv4"]) 127.0.0.1. replace (", '2017. 0.0.1 '"," ") ### because of some historical issues, it is not the host name, but the id name of salt-minion, used to determine the main application hostname = grains [I] ["id"] ipv4 = str (grains [I] ["ipv4"]). replace (", '2017. 0.0.1 '"," ") ipv4 = ipv4.replace (", "," and ") mem = grains [I] ["mem_total"]/1024 + 1num_cpu = grains [I] ["num_cpus"] OS = grains [I] ["osfullname"] + grains [I] ["lsb_distrib_release"] if "/data" notindiskusage [I]: disk_data = "None" else: disk_data = data_vol/1048576 ### project name empty project = "" ### identify the main running service by using the minionID name, such as the xx-mysql-1, then run mysqlforjinapp_name: ifjinhostname. lower (): app = jbreakelse: app = "undefined" c = "," ### connect and write line = hostname + c + ipv4 + c + str (mem) + c + str (num_cpu) + c + str (OS) + c + str (disk_data) + c + project + c + appret_file.write (line + "\ n") using texception, e: print "Exception: \ n", efinally: ret_file.close ()
Use NotePad to open it.
In Excel, this should be the case.