CMDB實現的四種方式
阿新 • • 發佈:2018-12-29
api pil ood .json 需要 mdb register proc his
一, Agent(基於shell命令實現)
Agent方式,可以將服務器上面的Agent程序作定時任務,定時將資產信息提交到指定API錄入數據庫
優點:速度快 缺點:需要為每臺服務器部署一個Agent程序
二,Paramiko類(SSH形式,基於Paramiko)
中控機在獲取未采集資產信息的服務器(服務器主機名,密碼),依賴於Paramiko(py模塊)通過SSH方式去獲取
優點:無Agent 缺點:速度慢
如果在服務器較少的情況下,可應用此方法
import paramiko # 創建SSH對象 ssh = paramiko.SSHClient() # 允許連接不在know_hosts文件中的主機 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 連接服務器 ssh.connect(hostname="10.0.0.10", port=22, username="root", password="123") # 執行命令 stdin, stdout, stderr = ssh.exec_command(‘df) # 獲取命令結果 result = stdout.read() # 關閉連接 ssh.close()
三, SaltStack
此方案本質上和第二種方案大致是差不多的流程,中控機在獲取到未采集資產信息的服務器(主機名),再而將命令放入一個隊列中,服務器來獲取。服務器將結果放入另一個隊列中,中控機獲取將服務信息發送到API進而錄入數據庫。
基於SaltStack的master上的pillar以及遠程執行命令實現
import salt.client local = salt.client.LocalClient() local.cmd("*", "cmd.run", ["whoami"])
優點: 快, 開發成本低, 缺點: 依賴第三方工具
四, Puppet(ruby開發)
基於Puppet的factor和report功能的實現
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
1, 自定義facotr示例
在 /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 #默認View Code
CMDB實現的四種方式