Generate a server asset inventory from Python using Saltstack

Source: Internet
Author: User
Tags get ip diskusage python script saltstack

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: http://youerning.blog.51cto.com/10513771/1708964

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

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/A0/wKioL1bUA0vyz7jLAAAyER8kKtQ523.png "title=" Clipboarasdasdd.png "alt=" wkiol1bua0vyz7jlaaayer8kktq523.png "/> 650" this.width=650; "src="/e/u261/themes/ Default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat Center; border:1px solid #ddd; "alt=" Spacer.gif "/>

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

Execution Salt Zabbix disk.usage, get the result as follows

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7C/A0/wKioL1bUA6qDqgZaAABQAUvjze8699.png "title=" Clipboard45465.png "alt=" Wkiol1bua6qdqgzaaabqauvjze8699.png "/>

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 count, operating system, data disk/data (GB), project, main application "# # # Open a. csv file so that you can write Ret_file = open ("Ret.csv", "W") # # #首先写入开头, a bit field name meaning 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 Data disk 1048576 is 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 this address          ipv4 = str (grains[i]["IPv4"]). Replace (",  ' 127.0.0.1 '", "")                 ## #因为一些历史遗留问题, This is not the hostname, but the ID name of Salt-minion, which is used to judge 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_release "]        if  "/data"  not in diskusage[i]:             disk_data =  "None"          else:            disk_data  = data_vol / 1048576                 ## #项目名为空         project =  ""         ## #通过minion  id name to determine the main operating services, such as XX-MYSQL-1, then run 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", Efinally:    ret_file.close ()

Open it in Notepad, that should be it.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7C/A1/wKiom1bUBSLCcFkDAAFuacqW6Ao882.png "title=" 1clipboard.png "alt=" Wkiom1bubslccfkdaafuacqw6ao882.png "/>

Open with Excel It should be like this

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/A0/wKioL1bUBaaQ9w7xAADq1H32mGk828.png "title=" 2clipboard.png "alt=" Wkiol1bubaaq9w7xaadq1h32mgk828.png "/>


This article is from the "Ear Note" blog, be sure to keep this source http://youerning.blog.51cto.com/10513771/1746075

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.