Many open source operating systems and maintenance systems, such as Nagios, Zabbix, CATI, etc., but, when they get the time to their own personalized operation and maintenance needs, always powerless!
A recent study of Python. Therefore, we believe that python+django+twisted to customize a fully personalized operation and maintenance system.
There are several basic functions: monitoring, analysis, alarm, and more directly based on the results of the analysis of the reaction operation. And the above points through the above framework can be relatively easy to achieve.
The following instructions:
Use Freemind to organize the following ideas:
Here are some code snippets, complete code downloads at 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 ', ' 192.168.7.17/test ', thre aded=true) _oracle_conn.autocommit = True self.cur = _oracle_conn.cursor () self._oracle_conn=_oracle_ Conn def ruku (self, line): Ip=self.transport.getpeer (). Host #获取clientIP Line=line.split ('::: ') #使用::: Cut raw data if line[1] in [' CPU ', ' mem ', ' Disk ', ' TCP ', ' net ', ' Process_down '): #依据数据包头来确定使用insert还是upd Ate. When the TCP header is inserted, the rest of the update if line[1] = = ' TCP ': sql = "INSERT into Mornitor_basicinfo (ipadd,time,tcp) v Alues (\ '%s\ ', \ '%s\ ', \ '%s\ ') "% (ip,line[0],line[3]) print SQL Self.cur.execute (SQL) Else:line_agAin = Line[3].split (':: ') sql = ' Update mornitor_basicinfo set%s=\ '%s\ ',%s=\ '%s\ ' where ipadd=\ '%s\ ' and t Ime=\ '%s\ '% (line[1],line_again[0],line[2],line_again[1],ip,line[0]) print SQL Self.cur.exec Ute (SQL) def connectionmade (self): print ' connected! ' def linereceived (self, line): Print line Self.ruku (line) #接受到数据之后运行入库操作. def connectionlost (self, reason= ' Connectiondone '): Self._oracle_conn.close () print ' The DB is close ... ok! ' Class Mornitor_factory (serverfactory): #还没想好要初始化什么 def __init__ (self,service): Self.service = Service Prot Ocol = Mornitor_protocolclass Fish_service (Service. Service): Def __init__ (self): Pass def StartService (self): service. Service.startservice (self) #什么都不做, start service # def stopservice (self): # return Self._port.stoplistening () #配置參数port = 10000iface = ' 127.0.0.1 ' top_server = service. Multiservice () #定义服务容器fish_server = Fish_service () #实例化我们的服务fish_server. Setserviceparent (To P_server) #把自己定义的服务增加到服务容器factory = Mornitor_factory (fish_service) #工厂化服务tcp _server = Internet. TCPServer (port, Factory, Interface=iface) #定义tcp服务tcp_server. Setserviceparent (top_server) #把tcp服务增 Add to service container application = Services. Application (' Fish_service ') #给应用起个名字top_server. Setserviceparent (application) #把服务容器丢到 In the application to
Client Side
From twisted.protocols import basicfrom twisted.internet Import protocol, defer, Taskimport get_basic_info_2 as Huoquimpo RT Guardian as Shouhuimport timefrom twisted.application Import Service, Internetclass Monitor_protocol (Basic. Linereceiver): #自定义客户端和服务端的连接协议. Inherit def __init__ from basic line: # Pass @staticmethod def huoqu_shuju (): #定义一个函数获取本机的一些状态 now = str (time.strftime ('%y-%m-%d%h:%m:%s ')) def add_tag (source, tag1, Tag2 = ' None '): #定义格式化 String function return '::: '. Join ([Now, Tag1, Tag2, source]) #使用::: Separation time, simple information, specific information, raw information TCP = AD D_tag (Huoqu.net_tcp (), ' TCP ') CPU = Add_tag (HUOQU.CPU (), ' CPU ', ' cpu_detail ') mem = Add_tag (Huoqu.mem (), ' Me M ', ' 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_down ', ' process_alived ') result = (TCP , CPU, mem, disk, net, process,) d = defer. Deferred () #使用defered返回结果 d.callback (Result) return D def xunhuan (self, list): #定义循环发送函数 For I in List:self.sendLine (i) def fasong (self): #定义程序执行顺序, send Self.huoqu_shu with callback to send function after obtaining information Ju (). Addcallback (Self.xunhuan) def loop (self): #使用twist内置的循环函数定义几秒监控数据传送到服务端 L = task. Loopingcall (Self.fasong) L.start (1) def connectionmade (self): #覆盖协议的connectmade函数. Start looping print ' Connected!...... ok! ' When the connection to the server is established Self.loop () def linereceived (self, line): #必须覆盖接受函数, otherwise twist will report not importent error!Passclass moinitor_client_factory (protocol. Reconnectingclientfactory): Def __init__ (self, Service): #还没想要要写什么 self.service = Service protocol = M Onitor_protocolclass Client_service (Service. Service): Def __init__ (self): Pass def StartService (self): service. Service.startservice (self) #配置文件開始port = 10000host = ' 127.0.0.1 ' #守护进程top_service = service. Multiservice () #定义服务容器client_service = Client_service () #实例化服务类client_service. Setse Rviceparent (top_service) #把自定义的服务丢到服务容器中factory = Moinitor_client_factory (client_service) #定义服务工厂化tcp_servi CE = Internet. TCPClient (host, port, Factory) #定义tcp连接的服务tcp_service. Setserviceparent (Top_service) #把tcp服务丢到服务容器中去applicati On = service. Application (' Fish_service ') #定义应用名字top_service. Setserviceparent (application) #把服务容器丢到应用中去
Some scripts that define the monitor's survival:
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): C5/>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_do Wned.append (k) else: l_lived.append (k) process_alived = ". Join (l_lived) Process_down = '. Join (l_downed) return ':: '. Join ([Process_down, process_alived])
The use of Django is now only possible with the admin module.
Here are some code snippets:
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 ' Flow 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 sty le= "COLOR: #%s;" >%s</span> '% (' ff0000 ', self.process_down) #拓机的进程用红色标识 process_down.allow_tags = True
Register to 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)
Other functions are not realized in the thought of freemind arrangement. Now this can only be a simple demon, but basically achieve the purpose of monitoring. Welcome to My message!
The following is the last Django Admin interface!
Code download
http://download.csdn.net/detail/qcpm1983/7611579
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Use PYTHON+DJANGO+TWISTD to develop one of your own operations and maintenance systems