運維路線圖:

cmdb的開發需要包含三部分功能:
- 采集硬件數據
- API
- 頁面管理
執行流程:服務器的客戶端采集硬件數據,然后將硬件信息發送到API,API負責將獲取到的數據保存到數據庫中,后台管理程序負責對服務器信息的配置和展示。
采集硬件信息
采集硬件信息可以有兩種方式實現:
- 利用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
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
2、應用report
/etc/puppet/puppet.conf
[main]
reports = cmdb
#report = true #默認
#pluginsync = true #默認
Demo 2
1、創建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
2、應用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/
<ignore_js_op>
<ignore_js_op>
class Blog(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
modes.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
后台管理頁面
后台管理頁面需要實現對數據表的增刪改查。
<ignore_js_op>
問題:
1、paramiko執行sudo
/etc/sudoers
Defaults requiretty
Defaults:cmdb !requiretty
