方式一
默认情况下,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
/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
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
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',)
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)
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
# Create your views here.