CMDB asset acquisition mode

A: Agent way

Principle: Each server agent installed on the client, asset information regularly sent to the specified database.

Advantages: speed.

Disadvantages: need more software installed on a server

. 1  Import The subprocess
 2  Import Requests
 . 3  # PIP3 the install Requests 
. 4  
. 5  # ################## data acquisition ################ ## 
. 6  # Result = subprocess.getoutput ( 'the ipconfig') 
. 7  # Result regular process of acquiring the desired data 
. 8  
. 9  # finishing asset information 
10  # data_dict = { 
. 11  #      'NIC': {}, 
12 is  #      'Disk': { }, 
13 is  #      'MEM': {} 
14  # } 
15  
16  # ################## transmission data ############## ####
17 # requests.post('http://www.127.0.0.1:8000/assets.html',data=data_dict)
agent implementations

 

 

Two: SSH way

Principle: active intermediate component assets acquired information to the server, and then add data to the database by the middleware (through API)

Pros: do not install the client server-side software

Cons: Slow

1  # on paramiko module, the install paramiko PIP3 
2  Import Requests
 . 3  Import paramiko
 . 4  
. 5  # ################## acquired host name does not obtain today ######## ########## 
. 6  # Result = requests.get ( 'HTTP: //www.127.0.0.1: 8000 / assets.html') 
. 7  # Result = [ 'c1.com', 'C2. COM '] 
. 8  
. 9  
10  # ################## connected to a remote server via paramiko, Run ################# # 
11  # Create Object SSH 
12 is SSH = paramiko.SSHClient ()
 13 is  # host connection is not allowed know_hosts file 
14 ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
 15  # connection server 
16 ssh.connect (hostname = ' 192.168.14.36 ' , Port = 22 is, username = ' wupeiqi ' , password = ' 123 ' )
 . 17  
18 is  # Run 
19  # stdin, stdout, stderr = ssh.exec_command ( 'DF') 
20 is  
21 is  # Get command results 
22 is  # result = stdout.read () 
23 is  
24  # close the connection 
25  # ssh.close () 
26 is  # Print (result) 
27 
28  # data_dict Result} = { 
29  
30  # ################## ################## transmission data 
31  # requests.post ( 'http: //www.127.0.0.1: 8000 / assets.html', data = data_dict)
SSH acquisition mode

 

 

 Three: Saltstack way

Principle: and a second similar, but put the request to the queue based on third-party software, middleware, the client receives a request to stop the queue, upon receiving the request, the server parses find the need to return to their asset information on the implementation of command and returns the result into another queue, fetching data from the intermediate queue result, and then stored in the database through the API

Advantages: speed, low development costs

Cons: dependent on third-party software

1  # 1. Install saltstack 
2  #        RPM --import https://repo.saltstack.com/yum/redhat/6/x86_64/latest/SALTSTACK-GPG-KEY.pub 
. 3  #
 . 4  #
 . 5  "" " 
. 6          Master: the install Master Salt-yum
 . 7         Master preparation:
 . 8              . A profile monitor the local IP
 . 9                  Vim / etc / Salt / Master
 10                  interface: local IP address
 . 11              B start Master.
 12 is                  /etc/init.d/salt-master Start
 13 is  
14  
15          the Slave: Salt-yum the install Minion
 16          the Slave prepared:
17              . A configuration file, connect the master
 18                  vim / etc / Salt / Minion
 19                  master: remote master address
 20              b start Slave.
 21                  /etc/init.d/salt-minion Start
 22  
23  2. Create a relationship
 24      View
 25      Master : Salt -L-Key
 26 is          accepted Keys:
 27          Denied Keys:
 28          unaccepted Keys:
 29              c1.com
 30              c2.com
 31 is              c3.com
 32          the Rejected Keys:
 33 is      accepted
 34     Master:salt-key -a c1.com
35         Accepted Keys:
36             c1.com
37             c2.com
38         Denied Keys:
39         Unaccepted Keys:
40             c3.com
41         Rejected Keys:
42 
43 
44 3. 执行命令
45     master:
46         salt 'c1.com' cmd.run  'ifconfig'
47 
48     import salt.client
49     local = salt.client.LocalClient()
50     result = local.cmd('c2.salt.com', 'cmd.run', ['ifconfig'])
51 
52 """
53 # ################## get today is not collecting hostname ################## 
54  # the Result = requests.get ( 'HTTP: //www.127.0.0.1: 8000 / assets.html') 
55  # Result = [ 'c1.com', 'c2.com'] 
56 is  
57 is  
58  # ########## ######## remote server Run ################## 
59  # Import The subprocess 
60  # Result = subprocess.getoutput ( "Salt 'c1.com' cmd .run 'the ifconfig' ") 
61 is  #
 62 is  # Import salt.client 
63 is  # local salt.client.LocalClient = () 
64  # Result = local.cmd ( 'c2.salt.com', 'cmd.run', [ ' ifconfig '])
65 
66 
67 # ################## send data ################## 
68  # requests.post ( 'HTTP: // www.127.0.0.1: 8000 / assets.html ', data = data_dict)
salt Stack

 

 Four: puppet

Principle: the server sends data to the middleware, middleware API data stored in the database.

Advantages: automatically provides data to the middleware server

Disadvantages: the use of middleware puppet ruby ​​development

 

Guess you like

Origin www.cnblogs.com/sun-10387834/p/12585349.html