天天看點

Python 結合Ansible 把管理資産資訊自動插入到CMDB中coding=utf-8author :wosunprint i

資産管理對于任何公司來說都是件非常重要的事情,大家不用cmdb都有點不好意思出來見人了,上了CMDB之後基礎就是資産這塊,為了不苦逼似的一個個往裡邊錄入,于是用python 結合Ansible 寫了個自動收集資産資訊,然後拿出自己需要關注的字段,自動insert 到cmdb 資料庫裡邊去。           

我們的cmdb 是開源改的,前端自定義好之後,後端的資料庫需要自己建立個表,建立表語句如下:

CREATE TABLE

ansible_host

(

id

int(11) NOT NULL AUTO_INCREMENT,

hostname

varchar(200) COLLATE utf8_bin DEFAULT NULL,

manufacturer

os

cpu_model

cpu_count

varchar(10) COLLATE utf8_bin DEFAULT NULL,

cpu_core

varchar(255) COLLATE utf8_bin DEFAULT NULL,

memory_totally

swap_totally

disk

ip

mac_address

sn

varchar(20) COLLATE utf8_bin DEFAULT NULL,

PRIMARY KEY (

id

)

) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

python 代碼:

#gobal_fun 輔助子產品,時間和資料定義在這裡邊,正式程式直接import

#!/usr/bin/env python

import datetime

import time

import sys

import MySQLdb as mysql

d1=datetime.datetime.now()

#input=open(sys.argv[1],'r')

d2=(d1-datetime.timedelta(days=1)).strftime('%Y-%m-%d')

d3=(d1-datetime.timedelta(days=1)).strftime('%d/%b/%Y')

day = time.strftime("%Y")

#outpu=open(d2+'__hourpv.csv','a+')

#outip=open('ip'+d2+'.csv','a+')

dk=time.strftime("%d/%b/%Y",time.localtime())+':00'

da=time.strftime("%d/%b/%Y",time.localtime())

conn= mysql.connect(user="root", passwd="vivame",db="falcon", charset="utf8")

cur = conn.cursor()

主程式

coding=utf-8

author :wosun

print i