许多开源操作系统和维护系统,例nagios、zabbix、cati等等,但是,当他们得到的时间自己的个性化操作和维护需求,始终无力!

最近的一项研究python。因此,我们认为python+django+twisted要定制一个完全个性化的操作和维护系统。

有几个基本的功能:监控、分析、报警、更甚者直接依据分析的结果进行反应操作。而以上几点通过上述的框架能够比較easy的实现。

以下上图说明:

使用freemind整理了下思路:

以下是一些代码段,完整的代码下载见文档底部:

Server:

  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. __author__ = 'dwj'
  4.  
  5. from twisted.internet.protocol import ServerFactory
  6. from twisted.protocols import basic
  7. import cx_Oracle
  8. from twisted.application import service, internet
  9.  
  10. class Mornitor_Protocol(basic.LineReceiver):
  11.  
  12. def __init__(self):
  13. #
  14. _oracle_conn=cx_Oracle.connect('xxxx', 'xxxx', '192.168.7.17/test', threaded=True)
  15. _oracle_conn.autocommit = True
  16. self.cur = _oracle_conn.cursor()
  17. self._oracle_conn=_oracle_conn
  18.  
  19. def ruku(self, line):
  20. ip=self.transport.getPeer().host
  21. #获取clientIP
  22. line=line.split(':::')
  23. #使用:::切割原始数据
  24. if line[1] in ['cpu', 'mem', 'disk', 'tcp', 'net', 'process_down']:
  25. #依据数据包头来确定使用insert还是update。当是tcp包头的时候插入,其余的更新
  26. if line[1] == 'tcp':
  27. sql = "insert into MORNITOR_BASICINFO (ipadd,time,tcp) values (\'%s\',\'%s\',\'%s\')"%(ip,line[0],line[3])
  28. print sql
  29. self.cur.execute(sql)
  30.  
  31. else:
  32. line_again = line[3].split('::')
  33. 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])
  34. print sql
  35. self.cur.execute(sql)
  36.  
  37. def connectionMade(self):
  38. print 'Connected!'
  39.  
  40. def lineReceived(self, line):
  41. print line
  42. self.ruku(line)
  43. #接受到数据之后运行入库操作。
  44. def connectionLost(self, reason='connectionDone'):
  45. self._oracle_conn.close()
  46. print 'The db is close... ok!'
  47.  
  48. class Mornitor_Factory(ServerFactory):
  49. #还没想好要初始化什么
  50. def __init__(self,service):
  51. self.service = service
  52.  
  53. protocol = Mornitor_Protocol
  54.  
  55. class Fish_Service(service.Service):
  56.  
  57. def __init__(self):
  58. pass
  59.  
  60. def startService(self):
  61. service.Service.startService(self) #什么都不做,開始服务
  62.  
  63. # def stopService(self):
  64. # return self._port.stopListening()
  65.  
  66. #配置參数
  67. port = 10000
  68. iface = '127.0.0.1'
  69.  
  70. top_server = service.MultiService() #定义服务容器
  71.  
  72. fish_server = Fish_Service() #实例化我们的服务
  73. fish_server.setServiceParent(top_server) #把自己定义的服务增加到服务容器
  74.  
  75. factory = Mornitor_Factory(Fish_Service) #工厂化服务
  76.  
  77. tcp_server = internet.TCPServer(port, factory, interface=iface) #定义tcp服务
  78. tcp_server.setServiceParent(top_server) #把tcp服务增加到服务容器
  79.  
  80. application = service.Application('Fish_Service') #给应用起个名字
  81. top_server.setServiceParent(application) #把服务容器丢到应用中去

Client端

  1. from twisted.protocols import basic
  2. from twisted.internet import protocol, defer, task
  3. import Get_basic_info_2 as Huoqu
  4. import guardian as shouhu
  5. import time
  6. from twisted.application import service, internet
  7.  
  8. class Monitor_Protocol(basic.LineReceiver):
  9. #自定义客户端和服务端的连接协议。从basic的line继承
  10.  
  11. def __init__(self):
  12. #
  13. pass
  14.  
  15. @staticmethod
  16. def huoqu_shuju():
  17. #定义一个函数获取本机的一些状态
  18. now = str(time.strftime('%Y-%m-%d %H:%M:%S'))
  19.  
  20. def add_tag(source, tag1, tag2 = 'none'):
  21. #定义格式化字符串函数
  22. return ':::'.join([now, tag1, tag2, source])
  23. #使用:::分隔时间、简单信息、具体信息、原始信息
  24.  
  25. tcp = add_tag(Huoqu.net_tcp(), 'tcp')
  26. cpu = add_tag(Huoqu.cpu(), 'cpu', 'cpu_detail')
  27. mem = add_tag(Huoqu.mem(), 'mem', 'mem_detail')
  28. disk = add_tag(Huoqu.disk_usage(), 'disk', 'disk_detail')
  29. net = add_tag(Huoqu.net_rate(), 'net', 'net_detail')
  30. process = add_tag(shouhu.check_alive(), 'process_down', 'process_alived')
  31. result = (tcp, cpu, mem, disk, net, process, )
  32. d = defer.Deferred()
  33. #使用defered返回结果
  34. d.callback(result)
  35. return d
  36.  
  37. def xunhuan(self, list):
  38. #定义循环发送函数
  39. for i in list:
  40. self.sendLine(i)
  41.  
  42. def fasong(self):
  43. #定义程序执行顺序,取得信息后用callback交给发送函数发送
  44. self.huoqu_shuju().addCallback(self.xunhuan)
  45.  
  46. def loop(self):
  47. #使用twist内置的循环函数定义几秒监控数据传送到服务端
  48. l = task.LoopingCall(self.fasong)
  49. l.start(1)
  50.  
  51. def connectionMade(self):
  52. #覆盖协议的connectmade函数。定义于服务端的连接建立后開始循环
  53. print 'Connected!......ok!'
  54. self.loop()
  55.  
  56. def lineReceived(self, line):
  57. #必须覆盖接受函数,否则twist会报not importent错误!
  58.  
  59. pass
  60.  
  61. class Moinitor_client_factory(protocol.ReconnectingClientFactory):
  62.  
  63. def __init__(self, service):
  64. #还没想要要写什么
  65. self.service = service
  66. protocol = Monitor_Protocol
  67.  
  68. class Client_Service(service.Service):
  69.  
  70. def __init__(self):
  71. pass
  72.  
  73. def startService(self):
  74. service.Service.startService(self)
  75.  
  76. #配置文件開始
  77. port = 10000
  78. host = '127.0.0.1'
  79.  
  80. #守护进程
  81. top_service = service.MultiService() #定义服务容器
  82.  
  83. client_service = Client_Service() #实例化服务类
  84. client_service.setServiceParent(top_service) #把自定义的服务丢到服务容器中
  85.  
  86. factory = Moinitor_client_factory(client_service) #定义服务工厂化
  87.  
  88. tcp_service = internet.TCPClient(host, port, factory) #定义tcp连接的服务
  89. tcp_service.setServiceParent(top_service) #把tcp服务丢到服务容器中去
  90.  
  91. application = service.Application('Fish_Service') #定义应用名字
  92. top_service.setServiceParent(application) #把服务容器丢到应用中去

一些自己定义监控程序是否存活的脚本:

  1. program = {'nginx': ['/opt/nginx/logs/nginx.pid', '/opt/nginx/sbin/nginx'],
  2. 'rsync-C': ['/var/run/rsyncd.pid', 'rsync --daemon'],
  3. }
  4.  
  5. def main():
  6. for k in program:
  7. a = get_pid(k, program[k][0])
  8. if isinstance(a, tuple):
  9. print '%s is not running!' % k
  10. print 'Start the program by Horland_guardian!'
  11. subprocess.call(program[k][1], shell=True)
  12. else:
  13. print 'The %s is running!' % k
  14.  
  15. def check_alive():
  16. l_lived = []
  17. l_downed = []
  18. for k in program:
  19. a = get_pid(k, program[k][0])
  20. if isinstance(a, tuple):
  21. l_downed.append(k)
  22. else:
  23. l_lived.append(k)
  24. process_alived = ' '.join(l_lived)
  25. process_down = ' '.join(l_downed)
  26.  
  27. return '::'.join([process_down, process_alived])

django的使用眼下仅仅须要使用到admin模块就能够。

以下是一些代码段:

model

  1. class BasicInfo(models.Model):
  2. ipadd = models.IPAddressField(verbose_name = u'IP地址')
  3. time = models.CharField(max_length=50, verbose_name = u'时间')
  4. cpu = models.CharField(max_length=255, blank=True, verbose_name = u'CPU%')
  5. cpu_detail = models.CharField(max_length=255, blank=True, verbose_name = u'CPU详情')
  6. mem = models.CharField(max_length=255, blank=True, verbose_name = u'内存%')
  7. mem_detail = models.CharField(max_length=255, blank=True, verbose_name = u'内存详情')
  8. disk = models.CharField(max_length=255, blank=True, verbose_name = u'磁盘%')
  9. disk_detail = models.CharField(max_length=255, blank=True, verbose_name = u'磁盘详情')
  10. net = models.CharField(max_length=255, blank=True, verbose_name = u'流量 bytes/s')
  11. net_detail = models.CharField(max_length=1000, blank=True, verbose_name = u'流量详情')
  12. tcp = models.CharField(max_length=255, blank=True, verbose_name = u'tcp连接状态')
  13. process_down = models.CharField(max_length=255, blank=True, verbose_name = u'DOWN-进程')
  14. process_alived = models.CharField(max_length=255, blank=True, verbose_name = u'Process_UP')
  15.  
  16. def Process_DOWN(self):
  17. return '<span style="color: #%s;">%s</span>' % ('ff0000', self.process_down) #拓机的进程用红色标识
  18. Process_DOWN.allow_tags = True

注冊到admin

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

freemind整理的思路中另一些功能没有实现。眼下这个仅仅能算个简单的demon吧,可是基本实现了监控的目的。欢迎大家给我留言!

以下上个django的admin界面截图吧!

代码下载

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

版权声明:本文博客原创文章。博客,未经同意,不得转载。