資源簡介
用python開發的小型的運維監控系統,使用了twisted框架
更詳細的信息見:
blog.csdn.net/smallfish1983

代碼片段和文件信息
#!/usr/bin/env?python
#coding:utf-8
__author__?=?‘dwj‘
from?twisted.protocols?import?basic
from?twisted.internet?import??protocol?defer?task
import?Get_basic_info_2?as?Huoqu
import?guardian?as?shouhu
import?time
from?twisted.application?import?service?internet
class?Monitor_Protocol(basic.LineReceiver):
????#自定義客戶端和服務端的連接協議,從basic的line繼承
????def?__init__(self):
????????#沒想好要初始化點什么
????????pass
????@staticmethod
????def?huoqu_shuju():
????????#定義一個函數獲取本機的一些狀態
????????now?=?str(time.strftime(‘%Y-%m-%d?%H:%M:%S‘))
????????
????????def?add_tag(source?tag1?tag2?=?‘none‘):
????????#定義格式化字符串函數
????????????return?‘:::‘.join([now?tag1?tag2?source])
????????????#使用:::分隔時間、簡單信息、詳細信息、原始信息
????????
????????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_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):
????#定義程序運行順序,取得信息后用callback交給發送函數發送
????????self.huoqu_shuju().addCallback(self.xunhuan)
????def?loop(self):
????#使用twist內置的循環函數定義幾秒監控數據傳送到服務端
????????l?=?task.LoopingCall(self.fasong)
????????l.start(1)
????def?connectionMade(self):
????#覆蓋協議的connectmade函數,定義于服務端的連接建立后開始循環
????????print?‘Connected!......ok!‘
????????self.loop()
????def?lineReceived(self?line):
????#必須覆蓋接受函數,否則twist會報not?importent錯誤!
????????pass
class?Moinitor_client_factory(protocol.ReconnectingClientFactory):
????
????def?__init__(self?service):
????#還沒想要要寫什么
????????self.service?=?service
????protocol?=?Monitor_Protocol
class?Client_Service(service.Service):
????def?__init__(self):
????????pass
????def?startService(self):
????????service.Service.startService(self)
#配置文件開始
port?=?10000
host?=?‘127.0.0.1‘
#守護進程
top_service?=?service.MultiService()???????????????????#定義服務容器
client_service?=?Client_Service()??????????????????????#實例化服務類
client_service.setServiceParent(top_service)???????????#把自己定義的服務丟到服務容器中
factory?=?Moinitor_client_factory(client_service)??????#定義服務工廠化
tcp_service?=?internet.TCPClient(host?port?factory)??#定義tcp連接的服務
tcp_service.setServiceParent(top_service)??????????????#把tcp服務丟到服務容器中去
application?=?service.Application(‘Fish_Service‘)??????#定義應用名字
top_service.setServiceParent(application)??????????????#把服務容器丟到應用中去
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-07-08?07:20??dwj\
?????目錄???????????0??2014-07-09?01:24??dwj\.idea\
?????目錄???????????0??2014-07-09?01:03??dwj\.svn\
?????目錄???????????0??2014-07-08?09:06??dwj\Client\
?????文件????????2164??2014-07-07?01:05??dwj\Client\Get_basic_info_2.py
?????文件??????????56??2014-07-08?05:59??dwj\Client\__init__.py
?????文件????????3220??2014-07-08?06:44??dwj\Client\client.py
?????文件????????2623??2014-07-07?01:05??dwj\Client\guardian.py
?????目錄???????????0??2014-07-09?01:03??dwj\Server\
?????文件??????????56??2014-07-07?01:05??dwj\Server\__init__.py
?????文件????????2943??2014-07-09?14:28??dwj\Server\main.py
?????目錄???????????0??2014-07-08?06:00??dwj\Server\twisted\
?????目錄???????????0??2014-07-09?01:03??dwj\codebak\
?????目錄???????????0??2014-07-09?01:03??dwj\test\
?????目錄???????????0??2014-07-08?06:00??dwj\twisted\
- 上一篇:基于python的小型搜索引擎
- 下一篇:CNN_源代碼
評論
共有 條評論