CMDB概述(二)

運維自動化路線:python

cmdb的開發須要包含三部分功能:shell

  ·採集硬件數據數據庫

  ·APIdjango

  ·頁面管理json

執行流程:服務器的客戶端採集硬件數據,而後將硬件信息發送到API,API負責將獲取到的數據保存到數據庫中,後臺管理程序負責對服務器信息的配置和展現。
api

採集硬件信息ruby

採集硬件信息能夠有兩種方式實現:服務器

  一、利用puppet中的report功能
  二、本身寫agent,定時執行網絡

兩種方式的優缺點各異:架構

  方式一

    優勢是不須要在每臺服務器上步一個agent

    缺點是依賴於puppet,而且使用ruby開發

  方式二

    優勢是用於python調用shell命令,學習成本低

    缺點是須要在每臺服務器上發一個agent

方式一

默認狀況下,puppet的client會在每半個小時鏈接puppet的master來同步數據若是定義了report,那麼在每次client和master同步數據時,會執行report的process函數,在該函數中定義一些邏輯,獲取每臺服務器信息並將信息發送給API

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

因此,咱們能夠建立本身的report來實現cmdb數據的採集,建立report也有兩種方式

Demo 1

一、建立report

/usr/lib/ruby/site_ruby/1.8/puppet/reports/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

二、應用report

/etc/puppet/puppet.conf
[main]
reports = cmdb
#report = true #默認
#pluginsync = true #默認

Demo 2

一、建立report

在 /etc/puppet/modules 目錄下建立以下文件結構:

modules
└── cmdb
    ├── lib
    │   └── puppet
    │       └── reports
    │           └── cmdb.rb
    └── manifests
        └── init.pp

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

二、應用report

/etc/puppet/puppet.conf
[main]
reports = cmdb
#report = true #默認
#pluginsync = true #默認

方式二

使用python調用shell命令,解析命令結果並將數據發送到API

API

  ·REST與技術無關,表明的是一種軟件架構風格,REST是Representational State Transfer的簡稱,中文翻譯爲「表徵狀態轉移」

  ·REST從資源的角度類審視整個網絡,它將分佈在網絡中某個節點的資源經過URL進行標識,客戶端應用經過URL來獲取資源的表徵,得到這些表徵致               使這些應用轉變狀態

  ·REST與技術無關,表明的是一種軟件架構風格,REST是Representational State Transfer的簡稱,中文翻譯爲「表徵狀態轉移」

  ·全部的數據,不過是經過網絡獲取的仍是操做(增刪改查)的數據,都是資源,將一切數據視爲資源是REST區別與其餘架構風格的最本質屬性

  ·對於REST這種面向資源的架構風格,有人提出一種全新的結構理念,即:面向資源架構(ROA:Resource Oriented Architecture)

django中可使用 Django rest framwork 來實現:http://www.django-rest-framework.org/

class Blog(models.Model):
    
    title = models.CharField(max_length=50)
    content = models.TextField()
models.py
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from app02 import models
from rest_framework.decorators import detail_route, list_route
from rest_framework import response
from django.shortcuts import HttpResponse

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')

# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    
    
# Serializers define the API representation.
class BlogSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Blog
        depth = 1
        fields = ('url','title', 'content',)
    
    
# ViewSets define the view behavior.
class BLogViewSet(viewsets.ModelViewSet):
    queryset = models.Blog.objects.all()
    serializer_class = BlogSerializer
    
    @list_route()
    def detail(self,request):
        print request
        #return HttpResponse('ok')
        return response.Response('ok')
api.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from rest_framework import routers
from app02 import api
from app02 import views


# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', api.UserViewSet)
router.register(r'blogs', api.BLogViewSet)


urlpatterns = patterns('',    
    url(r'^', include(router.urls)),
    url(r'index/', views.index),
    #url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
urls.py
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
# Create your views here.


@api_view(['GET', 'PUT', 'DELETE','POST']) 
def index(request):
    print request.method
    print request.DATA
    return Response([{'asset': '1','request_hostname': 'c1.puppet.com' }])
views.py

後臺管理頁面

後臺管理頁面須要實現對數據表的增刪改查。

相關文章
相關標籤/搜索