CMDB服务器管理系统【s5day88】:采集资产之Agent、SSH和Salt模式讲解

在对获取资产信息时,简述有四种方案。

1、Agent  (基于shell命令实现)

 

 

                  原理图

 

Agent方式,可以将服务器上面的Agent程序作定时任务,定时将资产信息提交到指定API录入数据库

优点:速度快 缺点:需要为每台服务器部署一个Agent程序

 

2、Paramiko类 (SSH形式,基于Paramiko模块)

中控机在获取未采集资产信息的服务器(服务器主机名,密码),依赖于Paramiko(py模块)通过SSH方式去获取

优点:无Agent  缺点:速度慢

如果在服务器较少的情况下,可应用此方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import paramiko
   
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123')
   
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
   
# 关闭连接
ssh.close()

 

3、SaltStack

  此方案本质上和第二种方案大致是差不多的流程,中控机在获取到未采集资产信息的服务器(主机名),再而将命令放入一个队列中,服务器来获取。服务器将结果放入另一个队列中,中控机获取将服务信息发送到API进而录入数据库。

基于SaltStack的master上的pillar以及远程执行命令实现

1
2
3
import salt.client
local = salt.client.LocalClient()
local.cmd('*''cmd.run', ['whoami'])

优点:快,开发成本低  缺点:依赖于第三方工具

4、Puppet(ruby语言开发)

基于Puppet的factor和report功能实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
puppet中默认自带了5个report,放置在【/usr/lib/ruby/site_ruby/1.8/puppet/reports/】路径下。如果需要执行某个report,那么就在puppet的master的配置文件中做如下配置:
 
######################## on master ###################
/etc/puppet/puppet.conf
[main]
reports = store #默认
#report = true #默认
#pluginsync = true #默认
 
 
####################### on client #####################
 
/etc/puppet/puppet.conf
[main]
#report = true #默认
   
[agent]
runinterval = 10
server = master.puppet.com
certname = c1.puppet.com
 
如上述设置之后,每次执行client和master同步,就会在master服务器的 【/var/lib/puppet/reports】路径下创建一个文件,主动执行:puppet agent  --test

1、自定义factor示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
在 /etc/puppet/modules 目录下创建如下文件结构:
 
modules
└── cmdb
    ├── lib
    │   └── puppet
    │       └── reports
    │           └── cmdb.rb
    └── manifests
        └── init.pp
 
################ cmdb.rb ################
# cmdb.rb
require 'puppet'
require 'fileutils'
require 'puppet/util'
   
SEPARATOR = [Regexp.escape(File::SEPARATOR.to_s), Regexp.escape(File::ALT_SEPARATOR.to_s)].join
   
Puppet::Reports.register_report(:cmdb) do
  desc "Store server info
    These files collect quickly -- one every half hour -- so it is a good idea
    to perform some maintenance on them if you use this report (it's the only
    default report)."
   
  def process
    certname = self.name
    now = Time.now.gmtime
    File.open("/tmp/cmdb.json",'a') do |f|
      f.write(certname)
      f.write(' | ')
      f.write(now)
      f.write("\r\n")
    end
   
  end
end
 
 
################ 配置 ################
/etc/puppet/puppet.conf
[main]
reports = cmdb
#report = true #默认
#pluginsync = true #默认

小结:

    • 采集资产信息有四种不同的形式(但puppet是具于ruby开发的)
    • API提供相关处理的接口
    • 管理平台为用户提供可视化操作

三种方法对比表

 

posted @ 2018-06-17 21:14  活的潇洒80  阅读(1084)  评论(0编辑  收藏  举报