SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,一般可以理解为简化版的puppet和加强版的func。SaltStack基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。
通过部署SaltStack环境,我们可以在成千上万台服务器上做到批量执行命令,根据不同业务特性进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理等,SaltStack是运维人员提高工作效率、规范业务配置与操作的利器。
前言:人工去对每一台服务器的硬件信息并记录早已经过去了,无论通过脚本还是自动化工具都是可以进行一次编写到处抓取的,本文主要使用saltstack作为使用工具,然后利用其提供的APi编写所需的Python脚本~~
需求如下:生成服务器主机名,IP地址,内存,CPU核数,操作系统,数据盘配额,主要运行服务
saltstack快速入门,可参考:Saltstack快速入门简单汇总
这里主要用到saltstack的grains,就是saltstack minion端生成的一些静态信息,比如CPU,内存,主机名什么的,而这些就是我们所需要的
执行salt \* grains.items,会打印一大堆的默认抓取的信息,其中一部分,如下
我们当然只挑我们需要的,操作如下
获取主机名
salt H-T-4 grains.item host
获取IP地址
salt zabbix grains.item ipv4
获取CPU核数
salt \* grains.item num_cpus
以此类推,根据自己所需,提取~~~
值得注意的是,grains信息里面并没有硬盘信息,所以还需通过disk.usage这个选项,得到我们所需的硬盘信息
执行salt zabbix disk.usage,得到结果如下
其中1K-blocks即我们所需的硬盘信息,根据需求只需要数据盘/data,所以后面就会计算这个盘的配额
最终脚本如下
#coding=utf-8 import salt.client as sc import json ###salt调用 local = sc.LocalClient() ###目标主机指定 tgt = "*" ###获取grains,disk信息 grains = local.cmd(tgt,"grains.items") diskusage = local.cmd(tgt,"disk.usage") ###主要应用列表即文件开头 app_name = ["tomcat","zookeeper","redis","mysql","nginx"] cols = "主机名,IP地址,内存(GB),CPU核数,操作系统,数据盘/data(GB),所属项目,主要应用" ###打开一个.csv文件,以便写入 ret_file = open("ret.csv","w") ###首先写入开头,有点字段名的意思 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"] / 1024 + 1 print "num_cpus" + ":" , grains[i]["num_cpus"] print "osfullname" + ":" , grains[i]["osfullname"] print "release" + ":" , grains[i]["lsb_distrib_release"] ###可能一些主机没有/data数据盘1048576是1024x1024 if "/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 ###去掉127.0.0.1这个地址 ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","") ###因为一些历史遗留问题,这里取得不是主机名,而是salt-minion的id名,用以判断主要应用 hostname = grains[i]["id"] ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","") ipv4 = ipv4.replace(",","and") mem = grains[i]["mem_total"] / 1024 + 1 num_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 = "" ###通过minion ID名来判断主要运行服务,比如xx-mysql-1,则运行mysql for j in app_name: if j in hostname.lower(): app = j break else: app = "undefined" c = "," ###连接并写入 line = hostname + c + ipv4 + c + str(mem) + c + str(num_cpu) + c + str(OS) + c + str(disk_data) + c + project + c + app ret_file.write(line + "\n") except Exception,e: print "Exception:\n",e finally: ret_file.close()
用记事本打开应该是这样
以上内容是小编给大家介绍的通过Python使用saltstack生成服务器资产清单的全部叙述,希望对大家有所帮助!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor