通过Python使用saltstack生成服务器资产清单
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生成服务器资产清单的全部叙述,希望对大家有所帮助!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
