Generate a server asset inventory using Saltstack with Python

Source: Internet
Author: User
Saltstack is a centralized server infrastructure management platform, with configuration management, remote execution, monitoring and other functions, can generally be understood as a simplified version of the puppet and enhanced Func. Saltstack is based on the Python language implementation and is built with a lightweight Message Queuing (ZeroMQ) and Python third-party modules (PYZMQ, Pycrypto, Pyjinjia2, Python-msgpack, and Pyyaml, etc.).

By deploying the Saltstack environment, we can execute batch commands on thousands of servers, configure centralized management according to different business characteristics, distribute files, collect server data, operating system base and package management, etc., Saltstack is the operation and maintenance personnel to improve work efficiency, Standardize the business configuration and operation of the sharp weapon.

Preface: Manually go to each server hardware information and records have passed, whether through the script or Automation tools can be written to crawl everywhere, this article mainly use Saltstack as a tool, and then use its provided API to write the required Python script ~ ~

The requirements are as follows: Generate server hostname, IP address, memory, CPU cores, operating system, data disk quotas, main operating services

Saltstack Quick Start, refer to: Saltstack Quick Start Simple summary

Here the main use of saltstack grains, is saltstack Minion side generated some static information, such as CPU, memory, hostname and so on, and these are what we need

Performing a salt \* grains.items will print a whole bunch of the default crawl information, which is part of the following


Of course we only pick what we need and operate as follows

Get host Name

Salt h-t-4 Grains.item Host

Get IP Address

Salt Zabbix grains.item IPv4

Gets the number of CPU cores

Salt \* grains.item Num_cpus

And so on, according to their own needs, extract ~ ~ ~

It is worth noting that the grains information does not have hard disk information, so we also need to disk.usage this option to get the hard drive information we need

Perform salt Zabbix disk.usage to get the result as follows

Which 1k-blocks is the hard disk information we need, according to demand only the data disk/data, so the subsequent calculation of this disk quota

The final script is as follows

#coding =utf-8import salt.client as scimport json## #salt调用local = sc. Localclient () # # #目标主机指定tgt = "*" # # #获取grains, disk information grains = Local.cmd (TGT, "grains.items") Diskusage = Local.cmd (TGT, " Disk.usage ") # # #主要应用列表即文件开头app_name = [" Tomcat "," Zookeeper "," Redis "," MySQL "," nginx "]cols =" hostname, IP address, memory (GB), CPU core number, Operating system, data disk/data (GB), belongs to the project, the main application "# # #打开一个. csv file so that write Ret_file = open (" Ret.csv "," W ") # # #首先写入开头, a bit of the meaning of the field name Ret_file.write ( cols + "\ n") try:for I in Grains.keys (): # # #打印信息可注释掉print grains[i]["nodename"]print "IPv4" + ":", grains[i]["IPv4"]print "  Mem_total "+": "grains[i][" mem_total "/1024x768 + 1print" Num_cpus "+": ", grains[i][" Num_cpus "]print" Osfullname "+": " , grains[i]["Osfullname"]print "release" + ":", grains[i]["Lsb_distrib_release"]## #可能一些主机没有/ Data disk 1048576 is 1024x1024if "/data" not in Diskusage[i]:p rint "diskusage" + ":" + "has no/data disk" else:data_vol = Int (d iskusage[i]["/data" ["1k-blocks"]) print "Diskusage" + ":", data_vol/1048576 # # #去掉127.0.0.1 this address IPv4 = str (grains[i][" IPv4 "]). Replace (", ' 127.0.0.1 ', ' "") # # #因为一些历史遗留问题, here is not the hostname, but salt-minion ID name, to determine the main application hostname = grains[i]["id"]ipv4 = str (grains[i][" IPv4 "]). Replace (", ' 127.0.0.1 ' "," ") IPv4 = Ipv4.replace (", "," and ") mem = grains[i][" Mem_total "]/1024x768 + 1NUM_CPU = grains[ i]["Num_cpus"]os = grains[i]["Osfullname"] + grains[i]["lsb_distrib_release"]if "/data" not in diskusage[i]:d isk_data = "None" Else:disk_data = data_vol/1048576## #项目名为空project = "" # # #通过minion ID name to determine the primary running service, such as XX-MYSQL-1, run Mysqlfor J in App_  Name:if J in Hostname.lower (): app = Jbreakelse:app = "undefined" c = "," # # #连接并写入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") except Exception,e: Print "exception:\n", Efinally:ret_file.close ()

Open it in Notepad, that should be it.

The above is a small part of the introduction of the use of Python using Saltstack to generate a list of server assets of all the narrative, I hope to 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.