###sample:
https://www.bbsmax.com/A/A7zgx2ZoJ4/
使用python+django+twistd 開發自己的操作和維護系統的一個
許多開源操作系統和維護系統,例nagios、zabbix、cati等等,但是,當他們得到的時間自己的個性化操作和維護需求,始終無力!
最近的一項研究python。因此,我們認為python+django+twisted要定制一個完全個性化的操作和維護系統。
有幾個基本的功能:監控、分析、報警、更甚者直接依據分析的結果進行反應操作。而以上幾點通過上述的框架能夠比較easy的實現。
以下上圖說明:
使用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
- #獲取clientIP
- 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_again[0],line[2],line_again[1],ip,line[0])
- print sql
- self.cur.execute(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
- protocol = Mornitor_Protocol
- class Fish_Service(service.Service):
- def __init__(self):
- pass
- def startService(self):
- service.Service.startService(self) #什么都不做,開始服務
- # def stopService(self):
- # return self._port.stopListening()
- #配置參數
- port = 10000
- iface = '127.0.0.1'
- top_server = service.MultiService() #定義服務容器
- fish_server = Fish_Service() #實例化我們的服務
- fish_server.setServiceParent(top_server) #把自己定義的服務增加到服務容器
- factory = Mornitor_Factory(Fish_Service) #工廠化服務
- tcp_server = internet.TCPServer(port, factory, interface=iface) #定義tcp服務
- tcp_server.setServiceParent(top_server) #把tcp服務增加到服務容器
- application = service.Application('Fish_Service') #給應用起個名字
- top_server.setServiceParent(application) #把服務容器丟到應用中去
Client端
- 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) #把服務容器丟到應用中去
一些自己定義監控程序是否存活的腳本:
- 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!' % k
- def 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])
django的使用眼下僅僅須要使用到admin模塊就能夠。
以下是一些代碼段:
model
- class BasicInfo(models.Model):
- ipadd = models.IPAddressField(verbose_name = u'IP地址')
- time = models.CharField(max_length=50, verbose_name = u'時間')
- 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詳情')
- mem = models.CharField(max_length=255, blank=True, verbose_name = u'內存%')
- mem_detail = models.CharField(max_length=255, blank=True, verbose_name = u'內存詳情')
- disk = models.CharField(max_length=255, blank=True, verbose_name = u'磁盤%')
- disk_detail = models.CharField(max_length=255, blank=True, verbose_name = u'磁盤詳情')
- net = models.CharField(max_length=255, blank=True, verbose_name = u'流量 bytes/s')
- net_detail = models.CharField(max_length=1000, blank=True, verbose_name = u'流量詳情')
- tcp = models.CharField(max_length=255, blank=True, verbose_name = u'tcp連接狀態')
- process_down = models.CharField(max_length=255, blank=True, verbose_name = u'DOWN-進程')
- 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>' % ('ff0000', self.process_down) #拓機的進程用紅色標識
- Process_DOWN.allow_tags = True
注冊到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)
freemind整理的思路中另一些功能沒有實現。眼下這個僅僅能算個簡單的demon吧,可是基本實現了監控的目的。歡迎大家給我留言!
以下上個django的admin界面截圖吧!
代碼下載
http://download.csdn.net/detail/qcpm1983/7611579
版權聲明:本文博客原創文章。博客,未經同意,不得轉載。
###sample
freemind思維導圖打開以后,同時也打開早前,打開過的,其他思維導圖文件,為什么會同時打開其他
windows用戶安裝時注意事項
辦法1(適合計算機和英語水平較高用戶):下載原始的FreeMind(9MB)進行安裝。安裝過程中會檢查Java,如果你的計算機沒有Java,它會提示先安裝Java。
辦法2(適合普通用戶):安裝ShareMind(38MB),你可以把ShareMind理解成加工過的FreeMind,更成熟,更易用,不再需要單獨安裝Java。
1、首先將在該頁任選一地址將FreeMind(思維腦圖)軟件包下載至本地,並解壓運行包內安裝程序。如下:
2、直接單擊【next】進入FreeMind(思維腦圖)安裝協議界面,了解協議內容並同意才可繼續安裝。同意選“i accept the...”
3、選擇【next】
4、繼續【next】
5、最后,FreeMind(思維腦圖)安裝完成單擊【finish】即可開始體驗。
FreeMind(思維腦圖)使用教程
如何使用Freemind?
1、打開FreeMind(思維腦圖),選擇【文件】——【新建】創建一個新思維導圖。
2、選擇鼠標雙擊創建的主題,修改自己喜歡的名字。
3、選中這個主題並回車添加新的分支,並命名。依次選中某個分支回車即可創建新的分支。
4、如果你想添加子主題,請選擇菜單——插入,或是快捷鍵insert。
5、最后添加需要的主題內容,如圖片、鏈接、等信息。
注意:插入圖像前,需要先保存文件。
6、如果你願意還可以修改下導圖樣式,選擇【格式】然后修改需要的節點顏色、節點樣式、節點字樣等信息。
7、修改后倒入需要的圖效果如下:是不是比較比較醒目。
FreeMind(思維腦圖)常見問題
一、FreeMind如何拖動節點?
1、選擇新建思維導圖,然后編輯需要的文字信息。
2、選中主節點回車,創建一個子節點編輯文字信息。
3、然后根據提示將鼠標直接懸放在主節點與子節點連接處,等鼠標的箭頭改變后單擊按住開始拖拽。
4、拖動節點后效果如下:
5、無法直接多個思維中心點展開,少量中文輸入法無法在 FreeMind 輸入啟動及運行速度較慢。
二、Freemind軟件在編輯中如何換行?
1、需要換行時,選擇“編輯長節點”,再按“Enter”鍵來換行
三、freemind思維導圖軟件如何插入圖片?
1、在節點上按Alt+K鍵,彈出文件框,選取你要插入的圖片,點打開,就能得到圖片了。
四、FreeMind 0.9x在P菜單中文字體過小。
解決辦法:
1.推薦:從Windows入手:桌面→右鍵→顯示屬性→外觀→高級項目。選擇「菜單」(也可直接點擊圖像的對應位置),可以看到WinXP的字體默認是Tahoma 8,將字號改為9即可。OpenOffice的此類問題亦可解決。據稱,Windows 2000不需要此設定。
2. FreeMind更換外觀:菜單→工具→首選項→外觀→外觀感覺。更換為其他外觀,比如Gtk等。
3. FreeMind更換語言:關閉FreeMind,在 c:Documents and SettingsuserID.freemindauto.properties 中加入一行 "language = en",重啟后即為英語。
XMind 與 FreeMind 的對比有什么不同之處?
分析基於Windows平台下的 FreeMind 0.90 RC3 和 XMind 3.03,結合XMind開發者所提供的信息。基本結論是:FreeMind更為強大,XMind更為華麗,XMind 與 FreeMind各有優缺點,適用於不同的人群使用。
1、相同/相通之處
- 都是免費、開源、基於 Java。
- 都滿足繪制思維導圖的基本甚至高級功能。
- 相通之處:XMind 可以導入/導出 FreeMind 格式,但有些FreeMind輸出的文件XMind不能讀取;而FreeMind不能導入/導出XMind格式。
2、XMind更適合於其它結構圖的制作
XMind在種類結構圖方面比較注重,小伙伴們可以選擇不同的結構圖展示自己思維導圖。如魚刺圖、邏輯圖、二維圖等。
3、FreeMind: 根點仍可插入父點
這點XMind就無法完成了,FreeMind工具可以在做好圖的根節點新添加節點,該功能對某些小伙伴們可能比較適用。
如果小伙伴們想要體驗XMind工具請在該處獲取鏈接地址:http://www.onlinedown.net/soft/56160.htm
###sample 1
步驟1: 設計
步驟2:
實際存儲過程如下:
create or replace procedure filesystem_check
as
vhost VARCHAR2(120);
vname VARCHAR2(120);
vkey VARCHAR2(120);
v_numdays number;
vtotal_begin_size number;
vfree_begin_size number;
vused_begin_size number;
vtotal_end_size number;
vfree_end_size number;
vused_end_size number;
vfs_growth number;
v_count number;
vfs_estimated_day number;
e_count number;
vfs_GROWTH_PER_DAY number;
vfree_percent number;
cursor v_cur is select * from (select host,name,regexp_replace(ltrim(key,'vfs.fs.size['), ',pfree]', null) key_new,free_percent from fs_percent where free_percent <35 and time=to_char(sysdate,'yyyy-mm-dd'));
begin
FOR v_rec in v_cur LOOP
/* DBMS_OUTPUT.PUT_LINE(v_rec.db_name||' db_name');
*/
DBMS_OUTPUT.ENABLE(buffer_size => null);
vhost := v_rec.host;
vname := v_rec.name;
vkey := v_rec.key_new;
vfree_percent := v_rec.free_percent;
DBMS_OUTPUT.PUT_LINE(vhost||' host ');
/*end loop;
end;*/
select count (* ) into e_count from fs_size where host=vhost and key like '%'||vkey||'%total%' and time=to_char(sysdate -3,'yyyy-mm-dd');
if e_count > 0 then
select max(size_g) into vtotal_begin_size from fs_size where host=vhost and key like '%'||vkey||'%total%' and time=to_char(sysdate-3,'yyyy-mm-dd');
DBMS_OUTPUT.PUT_LINE(vtotal_begin_size||' vtotal_begin_size ');
DBMS_OUTPUT.PUT_LINE ('peng');
select max(size_g) into vfree_begin_size from fs_size where host=vhost and key like '%'||vkey||'%free%' and time=to_char(sysdate-3,'yyyy-mm-dd');
DBMS_OUTPUT.PUT_LINE(vfree_begin_size||' vfree_begin_size ');
select max(size_g) into vused_begin_size from fs_size where host=vhost and key like '%'||vkey||'%used%' and time=to_char(sysdate-3,'yyyy-mm-dd');
select max(size_g) into vtotal_end_size from fs_size where host=vhost and key like '%'||vkey||'%total%' and time=to_char(sysdate,'yyyy-mm-dd');
select max(size_g) into vfree_end_size from fs_size where host=vhost and key like '%'||vkey||'%free%' and time=to_char(sysdate,'yyyy-mm-dd');
select max(size_g) into vused_end_size from fs_size where host=vhost and key like '%'||vkey||'%used%' and time=to_char(sysdate,'yyyy-mm-dd');
vfs_growth := vused_end_size - vused_begin_size;
v_numdays := 3;
vfs_GROWTH_PER_DAY := round((vfs_growth/v_numdays),2);
DBMS_OUTPUT.PUT_LINE(vfs_growth);
if vfs_growth > 0 then
vfs_estimated_day := round((vfree_end_size)/round(vfs_growth/v_numdays,10),2);
else
vfs_estimated_day := 999999;
end if;
DBMS_OUTPUT.PUT_LINE(CHR(10));
DBMS_OUTPUT.PUT_LINE('--------------------');
DBMS_OUTPUT.PUT_LINE('---------------------------');
DBMS_OUTPUT.PUT_LINE(CHR(10));
DBMS_OUTPUT.PUT_LINE('Summary');
DBMS_OUTPUT.PUT_LINE('========');
DBMS_OUTPUT.PUT_LINE('1) Allocated Space: '||vtotal_end_size||' GB ');
DBMS_OUTPUT.PUT_LINE('2) Used Space: '||vused_end_size||' GB');
DBMS_OUTPUT.PUT_LINE(CHR(10));
DBMS_OUTPUT.PUT_LINE('History');
DBMS_OUTPUT.PUT_LINE('========');
DBMS_OUTPUT.PUT_LINE('1) Allocated Space on '||'3 days ago '||': '||vtotal_begin_size||' GB');
DBMS_OUTPUT.PUT_LINE('2) Current Allocated Space on '||' today '||': '||vtotal_end_size||' GB');
DBMS_OUTPUT.PUT_LINE('3) Used Space on '||'3 days ago '||': '||vused_begin_size||' GB' );
DBMS_OUTPUT.PUT_LINE('4) Current Used Space on '||' today '||': '||vused_end_size||' GB' );
DBMS_OUTPUT.PUT_LINE('5) Total growth during last 3 days between '||': '||vfs_growth ||' GB');
DBMS_OUTPUT.PUT_LINE('5) growth per last 3 days between '||': '||vfs_GROWTH_PER_DAY ||' GB');
insert into fs_est values ( vhost,vname,to_char(sysdate,'yyyy-mm-dd'),vtotal_begin_size,vfree_begin_size,vused_begin_size,vtotal_end_size,vfree_end_size,vused_end_size,vfs_growth,vfs_estimated_day,vfs_GROWTH_PER_DAY,vkey,vfree_percent);
END IF;
DBMS_OUTPUT.PUT_LINE('/\/\/\/\/\/\/\/\/\/\/\/ END \/\/\/\/\/\/\/\/\/\/\/\');
DBMS_OUTPUT.PUT_LINE('---------------------------');
DBMS_OUTPUT.PUT_LINE(CHR(10));
DBMS_OUTPUT.PUT_LINE('/\/\/\/\/\/\/\/\/\/\/\/ END \/\/\/\/\/\/\/\/\/\/\/\');
END LOOP;
COMMIT;
END;