Develop an O & M system with python + django + twistd, djangotwistd

Source: Internet
Author: User
Tags rsync

Develop an O & M system with python + django + twistd, djangotwistd

There are many open-source O & M systems, such as nagios, zabbix, and Cata. However, when you meet your own personalized O & M requirements, it is always insufficient! Recently I have been studying python, so I want to use python + django + twisted to create a completely personalized O & M system.

The O & M system has several main functions: monitoring, analysis, alarm, and more importantly, directly responding to the analysis results. The above points can be easily implemented through the above framework.

The following description:

Use freemind to sort out the following ideas:

Below are some code segments. For the complete code download, see the bottom of the document:

Server:

#! /Usr/bin/env python # coding: UTF-8 _ author _ = 'dwj' from twisted. internet. protocol import ServerFactoryfrom twisted. protocols import basicimport cx_Oraclefrom twisted. application import service, internetclass Mornitor_Protocol (basic. lineReceiver): def _ init _ (self): # _ oracle_conn = cx_Oracle.connect ('xxxx', 'xxxx', '2017. 168.7.17/test', threaded = True) _ oracle_conn.autocommit = True self. cur = _ oracle _ Conn. cursor () self. _ oracle_conn = _ oracle_conn def ruku (self, line): ip = self. transport. getPeer (). host # obtain the Client IP line = line. split (':') # Use: to split the original data if line [1] in ['cpu ', 'mem', 'disk', 'tcp ', 'net', 'process _ low']: # determine whether to use insert or update based on the data packet header. When it is a tcp packet header, insert it, other updates if line [1] = 'tcp ': SQL = "insert into MORNITOR_BASICINFO (ipadd, time, tcp) values (\' % s \', \ '% s \', \ '% s \') "% (ip, line [0], line [3]) print SQL sel F.cur.exe cute (SQL) else: line_again = line [3]. split (':') SQL = 'Update MORNITOR_BASICINFO set % s = \ '% s \', % s = \ '% s \ 'where ipadd = \' % s \ 'and time = \' % s \ ''% (line [1], line_again [0], line [2], line_again [1], ip, line [0]) print SQL self.cur.exe cute (SQL) def connectionMade (self): print 'connected! 'Def lineReceived (self, line): print line self. ruku (line) # perform the import operation after receiving the data! Def connectionLost (self, reason = 'onondone'): self. _ oracle_conn.close () print 'the db is close... OK! 'Class Mornitor_Factory (ServerFactory): # I haven't thought about how to initialize def _ init _ (self, service): self. service = service protocol = Mornitor_Protocolclass Fish_Service (service. service): def _ init _ (self): pass def startService (self): service. service. startService (self) # Do nothing, start service # def stopService (self): # return self. _ port. stopListening () # configure the parameter port = 10000 iface = '2017. 0.0.1 'top _ server = service. multiService () # define service container fish_server = Fish_Service () # instantiate our service fish_server.setServiceParent (top_server) # Add Custom Service to service container factory = Mornitor_Factory (Fish_Service) # factory service tcp_server = internet. TCPServer (port, factory, interface = iface) # define the tcp service tcp_server.setServiceParent (top_server) # Add the tcp service to the service container application = service. application ('fish _ Service') # name the application top_server.setServiceParent (Application) # Drop the Service container to the application.
Client

From twisted. protocols import basicfrom twisted. internet import protocol, defer, taskimport Get_basic_info_2 as Huoquimport guardian as shouhuimport timefrom twisted. application import service, internetclass Monitor_Protocol (basic. lineReceiver): # customize the connection protocol between the client and the server, and inherit def _ init _ (self) from basic line: # pass @ staticmethod def huoqu_shuju (): # define a function to obtain some statuses of the local machine now = str (time. strftime ('% Y-% m-% d % H: % M: % s') d Ef add_tag (source, tag1, tag2 = 'None'): # define the format string function return ':::'. join ([now, tag1, tag2, source]) # Use: To separate time, simple information, detailed information, original information tcp = add_tag (Huoqu.net _ tcp (), 'tcp ') cpu = add_tag (Huoqu. cpu (), 'cpu ', 'cpu _ detail') mem = add_tag (Huoqu. mem (), 'mem ', 'mem _ detail') disk = add_tag (Huoqu. disk_usage (), 'disk', 'disk _ detail') net = add_tag (Huoqu.net _ rate (), 'net', 'net _ detail') process = add_tag (shouhu. check _ Alive (), 'process _ low', 'process _ alived ') result = (tcp, cpu, mem, disk, net, process,) d = defer. deferred () # Use defered to return the result d. callback (result) return d def xunhuan (self, list): # define the cyclic sending function for I in list: self. sendLine (I) def fasong (self): # define the program running sequence. After obtaining the information, Use callback to send self to the sending function. huoqu_shuju (). addCallback (self. xunhuan) def loop (self): # Use the built-in loop function of twist to define how many seconds the monitoring data is transmitted to the server l = task. loopingCall (self. fasong) l. s Tart (1) def connectionMade (self): # override the connectmade function of the Protocol. It is defined as the print 'connected' loop after the server connection is established !...... OK! 'Self. loop () def lineReceived (self, line): # The acceptance function must be overwritten. Otherwise, the not importent error will be reported in twist! Passclass Moinitor_client_factory (protocol. reconnectingClientFactory): def _ init _ (self, service): # No self. service = service protocol = Monitor_Protocolclass Client_Service (service. service): def _ init _ (self): pass def startService (self): service. service. startService (self) # Start port of the configuration file = 10000 host = '2017. 0.0.1 '# daemon top_service = service. multiService () # define the service container client_service = Client_Service () # instantiate the service class client_service.setServiceParent (top_service) # Drop your own service to the service container factory = Moinitor_client_factory (client_service) # define service factory tcp_service = internet. TCPClient (host, port, factory) # define the tcp Connection service tcp_service.setServiceParent (top_service) # Drop the tcp service to the service container to go to application = service. application ('fish _ Service') # define the application name top_service.setServiceParent (Application) # Drop the Service container into the application

Some scripts that customize whether the monitor program is alive:

program = {'nginx': ['/opt/nginx/logs/nginx.pid', '/opt/nginx/sbin/nginx'],            'rsync-C': ['/var/run/rsyncd.pid', 'rsync --daemon'],            }def main():    for k in program:        a = get_pid(k, program[k][0])        if isinstance(a, tuple):            print '%s is not running!' % k            print 'Start the program by Horland_guardian!'            subprocess.call(program[k][1], shell=True)        else:            print 'The %s is running!' % kdef check_alive():    l_lived = []    l_downed = []    for k in program:        a = get_pid(k, program[k][0])        if isinstance(a, tuple):            l_downed.append(k)        else:            l_lived.append(k)    process_alived = ' '.join(l_lived)    process_down = ' '.join(l_downed)    return '::'.join([process_down, process_alived])


Currently, you only need to use the admin module to use django.

The following code snippet:

Model

Class BasicInfo (models. model): ipadd = models. IPAddressField (verbose_name = u 'IP address') time = models. charField (max_length = 50, verbose_name = u 'time') cpu = models. charField (max_length = 255, blank = True, verbose_name = u'cpu % ') cpu_detail = models. charField (max_length = 255, blank = True, verbose_name = u 'cpu details') mem = models. charField (max_length = 255, blank = True, verbose_name = u'memory % ') mem_detail = models. charField (max_length = 255, blank = True, verbose_name = u'memory details') disk = models. charField (max_length = 255, blank = True, verbose_name = u'disk % ') disk_detail = models. charField (max_length = 255, blank = True, verbose_name = u'disk details') net = models. charField (max_length = 255, blank = True, verbose_name = u'traffic bytes/s') net_detail = models. charField (max_length = 1000, blank = True, verbose_name = u'traffic details') tcp = models. charField (max_length = 255, blank = True, verbose_name = u 'tcp connection status') process_down = models. charField (max_length = 255, blank = True, verbose_name = u'down-Process') process_alived = models. charField (max_length = 255, blank = True, verbose_name = u'process _ up') def Process_DOWN (self): return '<span style = "color: # % s; "> % s </span> '% ('ff000000', self. process_down) # Process_DOWN.allow_tags is marked in red by the extended process.

Register with admin

class BasicInfo_admin(admin.ModelAdmin):    list_display = ('time', 'cpu', 'cpu_detail', 'mem', 'mem_detail', 'disk', 'disk_detail', 'net', 'net_detail', 'tcp', 'Process_DOWN', 'process_alived')    list_filter = ('ipadd', )admin.site.register(BasicInfo, BasicInfo_admin)

There are still some functions not implemented in the freemind sorting idea. Currently, this can only be regarded as a simple demon, but it basically achieves the purpose of monitoring. You are welcome to leave a message!

Next is the admin interface of django!


Code download

Http://download.csdn.net/detail/qcpm1983/7611579



Python has many web frameworks, such as Django and webpy, but which one has the strongest comprehensive strength?

Django and web. py is more powerful than django, but I need to explain: I feel django is too big. It is a waste of time and energy to study it. However, if you use it for development based on his teaching materials, it is easy and labor-saving, and suitable for the whole-site development. However, once special requirements are met, it is difficult to modify them. You have to study its original code, such as admin and input interface, simple use is cool, but customization is too troublesome; web. py is suitable for fast development with a single goal. Although it is not full of django, It is very fast to develop with previous web experience.

What is the rapid development of django/python reflected in? How fast is it?

Django conforms to the mvc pattern, but in django it is called mtv, that is, the model, template, view, and django philosophy. At present, I understand that django is simple, concise, and coupled, I used it to write a blog. The biggest thing I realized was that his general view gave a lot of help, and the code was much less. django's built-in components, such as comments and comments, it's easy to use, and you don't have to write code. django is open-source and can be easily implemented in multiple countries and languages. You can read django book and a free official django document, well understood, there should be something you want.

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.