運維自動化路線:html
cmdb的開發須要包含三部分功能:python
執行流程:服務器的客戶端採集硬件數據,而後將硬件信息發送到API,API負責將獲取到的數據保存到數據庫中,後臺管理程序負責對服務器信息的配置和展現。shell
採集硬件信息能夠有兩種方式實現:數據庫
兩種方式的優缺點各異:方式一,優勢是不須要在每臺服務器上步一個agent,缺點是依賴於puppet,而且使用ruby開發;方式二,優勢是用於python調用shell命令,學習成本低,缺點是須要在每臺服務器上發一個agent。django
默認狀況下,puppet的client會在每半個小時鏈接puppet的master來同步數據,若是定義了report,那麼在每次client和master同步數據時,會執行report的process函數,在該函數中定義一些邏輯,獲取每臺服務器信息並將信息發送給APIjson
puppet中默認自帶了5個report,放置在【/usr/lib/ruby/site_ruby/1.8/puppet/reports/】路徑下。若是須要執行某個report,那麼就在puppet的master的配置文件中作以下配置:api
on masterruby
1
2
3
4
5
6
|
/
etc
/
puppet
/
puppet.conf
[main]
reports
=
store
#默認
#report = true #默認
#pluginsync = true #默認
|
on client服務器
1
2
3
4
5
6
7
8
|
/
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
二、應用report
Demo 2
一、建立report
在 /etc/puppet/modules 目錄下建立以下文件結構:
modules └── cmdb ├── lib │ └── puppet │ └── reports │ └── cmdb.rb └── manifests └── init.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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
1
2
3
4
5
|
/
etc
/
puppet
/
puppet.conf
[main]
reports
=
cmdb
#report = true #默認
#pluginsync = true #默認
|
方式二
使用python調用shell命令,解析命令結果並將數據發送到API
django中可使用 Django rest framwork 來實現:http://www.django-rest-framework.org/
class Blog(models.Model): title = models.CharField(max_length=50) content = models.TextField()
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')
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')) )
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' }])
後臺管理頁面須要實現對數據表的增刪改查。
問題:
一、paramiko執行sudo
1
2
3
4
|
/
etc
/
sudoers
Defaults requiretty
Defaults:cmdb !requiretty
|