Python CMDB Development

Source: Internet
Author: User
Tags call shell representational state transfer

Operation and maintenance Automation route:

The development of the CMDB needs to include three parts:

    • Acquisition of hardware data
    • Api
    • Page Management

Execution process: The client of the server collects the hardware data, then sends the hardware information to the API,API responsible for saving the acquired data to the database, and the daemon is responsible for the configuration and presentation of the server information.

Acquisition of hardware information

The acquisition of hardware information can be achieved in two ways:

    1. Using the report function in puppet
    2. Write your own agent and execute it regularly

The pros and cons of the two approaches are different: one, the advantage is that there is no need to step on each server, the disadvantage is to rely on puppet, and the use of Ruby development, mode two, the advantage is used for Python call shell command, learning cost is low, The disadvantage is that you need to send an agent on each server.

Way One

By default, Puppet's client synchronizes the data at every half-hour connection to the master of puppet, and if the report is defined, the report's process function is executed each time the client and master synchronize the data. Define some logic in the function, get each server information and send the information to the API

The default Puppet has 5 report, placed under the "/usr/lib/ruby/site_ruby/1.8/puppet/reports/" path. If a report needs to be executed, the following configuration is done in the Puppet's master configuration file:

On Master

123456 /etc/puppet/puppet.conf[main]reports =store #默认#report = true #默认#pluginsync = true #默认

On client

12345678 / etc / puppet / puppet.conf [main]    [agent] runinterval  =  10 server  =  master.puppet.com certname  =  c1.puppet.com

After the above setup, each time the client and master synchronization is performed, a file is created under the "/var/lib/puppet/reports" Path of the master server and isactively executed:puppet agent--test

Therefore, we can create our own report to achieve the acquisition of CMDB data, there are two ways to create a report.

Demo 1

1. Create report

+ View Code

2. Application Report

+ View Code

Demo 2

1. Create report

Create the following file structure under the/etc/puppet/modules directory :

Modules└──cmdb├──lib│└──puppet│└──reports│└──cmdb.rb└──manifests└──init.pp
123456789101112131415161718192021222324 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 isa good idea    to perform some maintenance on them ifyou use this report (it‘s the only    default report)."   defprocess    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    endend

2. Application Report

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

Way Two

Use Python to invoke shell commands, parse command results, and send data to API

Api
    • Rest is not technology-related, it represents a software architecture style, rest is the abbreviation for representational state transfer, and Chinese translates as "representational states transfer"
    • Rest looks at the entire network from the resource's perspective, which identifies the resources distributed across a node in the network through URLs, and the client application uses URLs to obtain representations of the resources, resulting in these applications transforming state
    • Rest is not technology-related, it represents a software architecture style, rest is the abbreviation for representational state transfer, and Chinese translates as "representational states transfer"
    • All the data, but the data obtained through the network or operation (increase and deletion), are resources, all the data as a resource is the most essential attribute of rest difference and other architectural style
    • For the resource-oriented architectural style of rest, a new architectural concept is proposed, namely: resource-oriented architecture (Roa:resource oriented Architecture)

Django can be implemented using 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 userfrom rest_framework import routers, serializers, viewsetsfrom app02 import mod Elsfrom rest_framework.decorators import Detail_route, list_routefrom rest_framework import Responsefrom 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 R Epresentation.class Blogserializer (serializers. Hyperlinkedmodelserializer): Class Meta:model = models. Blog Depth = 1 fields = (' url ', ' title ', ' content ',) # Viewsets define the View Behavior.class Blogvie WSet (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, urlfrom django.contrib import adminfrom rest_framework import Routersfrom APP02 Import apifrom APP02 import views# routers provide an easy to do by automatically determining the URL Conf.router = r Outers. 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 renderfrom rest_framework.decorators import api_viewfrom rest_framework.response Import response# Create your views here. @api_view ([' GET ', ' PUT ', ' DELETE ', ' POST ']) def index (request):    Print Request.method    Print request. DATA    
Background Management page

Background Management page need to implement the data table additions and deletions to change.

Problem:

1. Paramiko Execute sudo

1234 /etc/sudoersDefaults    requirettyDefaults:cmdb    !requiretty

  

Python CMDB Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.