Use saltstack in Python to generate a server asset list

Source: Internet
Author: User
Tags get ip diskusage saltstack
Manually record the hardware information of each server and it has already passed. You can write and capture the information everywhere through scripts or automated tools. This article mainly uses saltstack as a tool, then, you can use the APIS provided to compile the required Python script SaltStack, which is a centralized management platform for server infrastructure and provides configuration management, remote execution, monitoring, and other functions, generally, it can be understood as the simplified version of puppet and the enhanced version of func. SaltStack is implemented based on the Python language and combined with the lightweight Message Queue (ZeroMQ) and Python third-party modules (Pyzmq, PyCrypto, Pyjinjia2, python-msgpack, and PyYAML.

By deploying the SaltStack environment, we can execute commands in batches on thousands of servers, configure centralized management, file distribution, server data collection, operating system basics, and software package management based on different business characteristics. SaltStack is a powerful tool for O & M personnel to improve work efficiency and standardize business configuration and operations.

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

Quick Start to saltstack. For details, refer to Saltstack quick start.

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

salt H-T-4 grains.item host

Get IP Address

salt zabbix grains.item ipv4

Obtain the number of CPU Cores

salt \* grains.item num_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 salt zabbix disk. 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-8import salt. client as scimport json ### 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") ### First, write the beginning, which means ret_file.write (cols + "\ n") try: for I in grains. keys (): ### comment out print grains [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" not in diskusage [I]: print "diskusage" + ":" + "have no/data disk" 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" not in diskusage [I]: disk_data = "None" else: disk_data = data_vol/1048576 ### project name empty project = "" ### identify the main running service by minion ID name, such as xx-mysql-1, run mysqlfor j in app_name: if j in hostname. 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") failed t Exception, e: print "Exception: \ n", efinally: ret_file.close ()

Use NotePad to open it.

The above content is a summary of how to generate a server asset list using saltstack in Python. I hope it will help you!

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.