Generate a server asset inventory from Python using Saltstack

Source: Internet
Author: User
Tags diskusage saltstack

(the following codeLinuxTest Success)

linux-node0.oldboyedu.com 192.168.1.30 installation salt-master,salt-minion

linux-node1.oldboyedu.com 192.168.1.31 installation salt-minion

The main use here Saltstack of the Grains, is that Saltstack Minion static information generated by the terminal, such as CPU , memory, hostname, whatever, and that's all we need.

Execution 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 linux-node1.oldboyedu.com Grains.item Host

Get IP Address

Salt linux-node1.oldboyedu.com grains.item IPv4

Get CPU Number of cores

Salt \* grains.item Num_cpus

and so on, depending on what you need, extract ~~~

It is important to note that Grains There is no hard drive information inside the message, so you need to pass Disk.usage This option to get the hard drive information we need

Execution Salt linux-node1.oldboyedu.com disk.usage

where 1k-blocks is the hard drive information that we need, it only needs a disk per demand , so the quota for this disk is calculated later.

11.py Code:

#coding =utf-8

Import Salt.client as SC

Import JSON

# # #salt called

Local = SC. Localclient ()

### Target host designation

TGT = "*"

### Get Grains , Disk Information

Grains = Local.cmd (TGT, "Grains.items")

Diskusage = Local.cmd (TGT, "Disk.usage")

### The main application list is the beginning of the file

App_name = ["Tomcat", "Zookeeper", "Redis", "MySQL", "Nginx"]

cols = "Hostname,ip Address,ram (GB), CPU Num,operation,data/(GB), project"

### Open a . csv file to write to

Ret_file = open ("Ret.csv", "W")

### first write the beginning, a bit of the meaning of the field name

Ret_file.write (cols + "\ n")

Try

For I in Grains.keys ():

### Print information can be commented 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_id"]

### In case the host does not / Data Disk 1048576 is a 1024x102

If "/" Not in Diskusage[i]:

Print "Diskusage" + ":" + "with No/data disk"

Else

Data_vol = Int (diskusage[i]["/" ["1k-blocks"])

Print "Diskusage" + ":", data_vol/1048576

### Remove 127.0.0.1 this address

IPv4 = str (grains[i]["IPv4"]). Replace (", ' 127.0.0.1 '", "")

### because of some historical problems, the acquisition is not a hostname, but salt-minion of the ID Used 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"]/1024 + 1

NUM_CPU = grains[i]["Num_cpus"]

OS = grains[i]["Osfullname"] + grains[i]["lsb_distrib_id"]

If "/" Not in Diskusage[i]:

Disk_data = "None"

Else

Disk_data = data_vol/1048576

### through Minion ID to determine the main operating services, such as xx-mysql-1 , you run MySQL

For J in App_name:

If J in Hostname.lower ():

App = J

Break

Else

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 + App

Ret_file.write (line + "\ n")

Except Exception,e:

Print "exception:\n", E

Finally

Ret_file.close ()

Execution Salt \* grains.items, found "lsb_distrib_id" instead of the original "Lsb_distrib_release" , so it used a "lsb_distrib_id"

[[email protected] ~]# python 11.py

Linux-node0

IPv4: [' 127.0.0.1 ', ' 192.168.1.30 ', ' 192.168.1.38 ']

Mem_total:1

Num_cpus:1

Osfullname:centos Linux

Release:centos Linux

Diskusage:16

Linux-node1

IPv4: [' 127.0.0.1 ', ' 192.168.1.31 ', ' 192.168.122.1 ']

Mem_total:3

Num_cpus:1

Osfullname:centos Linux

Release:centos Linux

Diskusage:16

generated in the same directory after execution Ret.csv file, open as follows:


Generate a server asset inventory from Python using Saltstack

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.