Python脚本监控线上AMQ Number of Pending Messages数量
需求:用python腳本統(tǒng)計(jì)線上AMQ的Number of Pending Messages數(shù)量,當(dāng)達(dá)到10的時(shí)候發(fā)送Email報(bào)警郵件
腳本實(shí)現(xiàn)邏輯如下:
1)用戶名密碼登陸到WEB頁面
2)獲取WEB頁面的源代碼
3)用正則表達(dá)式匹配AMQ的Number of Pending Messages值
4)判斷是否達(dá)到報(bào)警閥值
5)如果達(dá)到報(bào)警閥值,發(fā)送Email報(bào)警郵件
在登陸AMQ頁面的時(shí)候用Fiddler工具(www.fiddler2.com)查看HTTP請(qǐng)求的詳細(xì)過程。
Fiddler工具的Headers可以查看HTTP請(qǐng)求的Header頭信息。
Fiddler工具的Auth可以看到客戶端發(fā)送過來的用戶名和密碼信息。
Fiddler工具的RAW可以查看詳細(xì)的消息結(jié)構(gòu)?
在Headers頭中看到的信息Authorization: Basic?YWRtaW46YWRtaW5AdGNsdWI=?紅色的部分其實(shí)是HTTP認(rèn)證的用戶名和密碼,是通過base64加密的,在python通過base64可以解密出用戶名和密碼,和Auth里面看到的一致。
| 1 2 3 4 5 6 7 8 | >>>?import?base64 >>> s?=?'YWRtaW46YWRtaW5AdGNsdWI=' >>>?import?tab >>>?import?base64?? >>> s?=?'YWRtaW46YWRtaW5AdGNsdWI=' >>>?print?base64.b64 >>>?print?base64.b64decode(s) admin:admin@tclub |
在Python腳本中,發(fā)送http請(qǐng)求的時(shí)候把fiddler分析出來的這個(gè)“Authorization: Basic YWRtaW46YWRtaW4=”加上就OK了,就是把用戶名密碼轉(zhuǎn)換成base64加到header頭里面。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/env python #sfzhang131029 #encoding:utf-8 import?re import?os import?sys import?time import?base64 import?urllib2 date?=?time.strftime('%Y-%m%d-%H:%M') login_url?=?'http://192.168.1.244:8161/admin/xml/queues.jsp' req?=?urllib2.Request(login_url) Username?=?'admin' Password?=?'admin@tclub' max_size?=?0 #把用戶名密碼轉(zhuǎn)換成Base64編碼 base64_info?=?base64.encodestring( ????????????????'%s:%s'?%?(Username, Password))[:-1] print?base64_info auth_header?=??"Basic %s"?%?base64_info #把Fiddler分析出來"Authorization: Basic YWRtaW46YWRtaW5AdGNsdWI="添加到HTTP請(qǐng)求頭里面 req.add_header("Authorization", auth_header) try: ????handle?=?urllib2.urlopen(req) except?IOError, e: ????print?"your username or password is wrong" ????sys.exit(1) html?=?handle.read() #用正則表達(dá)匹配AMQ"Number of Pending Messages"數(shù)值 r1?=?r'size="[0-9]*"' size_list?=?re.findall(r1,html) #print size_list #找出size最大值 for?i?in?size_list: ????tmp?=?int(i.split('=')[1].replace('"','')) ????if?tmp > max_size: ????????max_size?=?tmp print?max_size mail_CMD?=?"echo '%s The number of apache amq entry connections is more then %d' and check! |mail -s 'Apache amq alarm' zhangshifeng@tclub.cn"?%?(date,max_size) #用if判斷如果超出閥值,則發(fā)送報(bào)警郵件 if?max_size <?10: ????print?"%s The apache amq is ok!"?%?date else: ????print??"%s The number of apache amq entry connections is more then %d and check!"?%?(date,max_size) ????os.system(mail_CMD) |
總結(jié):Fiddler是最強(qiáng)大最好用的Web調(diào)試工具之一,它能記錄所有客戶端和服務(wù)器的http和https請(qǐng)求,允許你監(jiān)視,設(shè)置斷點(diǎn),甚至修改輸入輸出數(shù)據(jù),Fiddler無論對(duì)開發(fā)人員或者測試人員來說,都是非常有用的工具。
本文轉(zhuǎn)自 sfzhang 51CTO博客,原文鏈接:http://blog.51cto.com/sfzhang88/1316789,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Python脚本监控线上AMQ Number of Pending Messages数量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 店招模块终于可以进行后台换图片了
- 下一篇: 【教程】minicom使用教程