开源的运维系统不少,比如nagios、zabbix、cati等等,但是遇到自己个性化的运维需求的时候,总是显的力不从心!最近在学习python,所以就考虑用python+django+twisted来定做一个完全个性化的运维系统。
运维系统有几个主要的功能:监控、分析、报警、更甚者直接根据分析的结果进行反应操作。而以上几点通过上述的框架可以比较容易的实现。
下面上图说明:
使用freemind整理了下思路:
下面是一些代码段,完整的代码下载见文档底部:
Server:
#!/usr/bin/env python
#coding:utf-8
__author__ = 'dwj'
from twisted.internet.protocol import ServerFactory
from twisted.protocols import basic
import cx_Oracle
from twisted.application import service, internet
class Mornitor_Protocol(basic.LineReceiver):
def __init__(self):
#
_oracle_conn=cx_Oracle.connect('xxxx', 'xxxx', '192.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
#获取客户端IP
line=line.split(':::')
#使用:::分割原始数据
if line[1] in ['cpu', 'mem', 'disk', 'tcp', 'net', 'process_down']:
#根据数据包头来确定使用insert还是update,当是tcp包头的时候插入,其余的更新
if line[1] == 'tcp':
sql = "insert into MORNITOR_BASICINFO (ipadd,time,tcp) values (\'%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 time=\'%s\''%(line[1],line_