接上篇《授之以漁-運(yùn)維平臺(tái)發(fā)布模塊一(Jenkins篇)》冕茅,今天介紹下結(jié)合著Saltstack的MasterEven驹饺,用來做發(fā)布系統(tǒng)的結(jié)果監(jiān)控。
一民镜、Event介紹
SaltStack 0.10版本中, 新增了Event系統(tǒng), 官方在 Release Notes 對(duì)其描述如下:
The Salt Master now comes equipped with a new event system. This event system has replaced some of the back end of the Salt client and offers the beginning of a system which will make plugging external applications into Salt. The event system relies on a local ZeroMQ publish socket and other processes can connect to this socket and listen for events. The new events can be easily managed via Salt's event library.
同時(shí)官方也在 Salt Event系統(tǒng) 頁(yè)面中提供了監(jiān)聽event的例子程序, 基于其進(jìn)行下Event系統(tǒng)學(xué)習(xí).
我們所需要的就是監(jiān)聽event,然后把結(jié)果存儲(chǔ)到Mysql单起,Redis之類搏嗡,以便于我們后續(xù)進(jìn)行處理。
二佣耐、Event代碼:
import salt.utils.event
import redis
def redis_conect_db6():
try:
r = redis.StrictRedis('172.18.11.126', 6379, 6)
except:
pass
return r
event = salt.utils.event.MasterEvent('/var/run/salt/master')
for data in event.iter_events(full=True):
r = redis_conect_db6()
ret = data['data']
if 'salt/job/' in data['tag']:
if ret.has_key("id") and ret.has_key("return") and ret.has_key("fun") :
if ret['fun'] != 'mine.update':
if len(ret['fun_args']) >0 and 'wwwlogs' not in ret['fun_args'][0]:
print data
print "====================================================="
if ret['fun'] == 'pkg.get_repo' and ret['return'] != {}:
r.set('%s:%s-%s'%(ret['return']['name'],ret['id'],ret['fun']), ret)
elif ret['fun'] == 'pkg.upgrade_available' :
r.set('%s:%s-%s'%(ret['fun_args'][0],ret['id'],ret['fun']), ret)
elif ret['fun'] == 'pkg.install' and ret['return'] != {}:
r.set('%s:%s-%s'%(ret['fun_args'][0],ret['id'],ret['fun']), ret)
這段event腳本的大體實(shí)現(xiàn)了過濾一些非必要信息政勃,把fun中包含
pkg.get_repo
(驗(yàn)證Yum是否存在),pkg.upgrade_available
(驗(yàn)證Yum是否更新)兼砖,pkg.install
(安裝)存儲(chǔ)到Redis中奸远。然后我只需要去Redis中取出數(shù)據(jù),并通過highcharts.js匯出圖即可讽挟。
三懒叛、后端處理Redis數(shù)據(jù)繪圖代碼:
@login_required
@my_permissionVerify
def release_status(request):
if 'id' in request.GET:
svn_name = request.GET['id']
Svn_name = Svn.objects.get(svn_name = svn_name)
r = redis_conect_db6()
svn_list = []
nodes = [{"area":0,"status":2,"no":0,"parent":[],"label":"%s" % str(Svn_name.svn_name)}]
for svn_host in (Svn_name.svn_hosts).split(','):
area_dict1 = {}
area_dict2 = {}
area_dict3 = {}
svn_list_dict = {}
area_dict1["area"] = 1
area_dict1["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict1["label"] = "%s" % str(svn_host)
area_dict1["parent"] = [{"parea":0,"pno":0}]
area_dict1["status"] = 0
nodes.append(area_dict1)
area_dict2["area"] = 2
area_dict2["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict2["label"] = "%s" % str(svn_host)
area_dict2["parent"] = [{"parea":1,"pno":int(area_dict2["no"])}]
area_dict2["status"] = 0
nodes.append(area_dict2)
area_dict3["area"] = 3
area_dict3["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict3["label"] = "%s" % str(svn_host)
area_dict3["parent"] = [{"parea":2,"pno":int(area_dict3["no"])}]
area_dict3["status"] = 0
nodes.append(area_dict3)
"""排除同一個(gè)主機(jī),多個(gè)項(xiàng)目"""
try:
if str(Svn_name) in r.get('%s:%s-%s' % (svn_name, svn_host,'pkg.get_repo')):
nodes[0] = {"area":0,"status":3,"no":0,"parent":[],"label":"%s" % str(Svn_name.svn_name)}
if nodes[0]['status'] == 3:
area_dict1["status"] = 2
try:
if json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.get_repo')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['success'] == "True":
area_dict1["status"] = 3
else:
area_dict1["status"] = 1
except:
area_dict1["status"] = 0
area_dict1["area"] = 1
area_dict1["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict1["label"] = "%s" % str(svn_host)
area_dict1["parent"] = [{"parea":0,"pno":0}]
svn_list_dict['host'] = svn_host
svn_list_dict['name'] = Svn_name
svn_list_dict["release_svn"] = Svn_name.svn_no
"""添加檢測(cè)YUM源存在時(shí)間"""
try:
svn_list_dict['get_repo_time'] = json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.get_repo')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['_stamp_localtime']
except:
svn_list_dict['get_repo_time'] = ''
nodes.append(area_dict1)
print 'nodes,',nodes[1]['status']
else:
area_dict1["status"] = 0
if nodes[1]['status'] == 3:
area_dict2["status"] = 2
try:
if json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.upgrade_available')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['success'] == "True":
area_dict2["status"] = 3
else:
area_dict2["status"] = 1
except:
area_dict2["status"] = 2
area_dict2["area"] = 2
area_dict2["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict2["label"] = "%s" % str(svn_host)
area_dict2["parent"] = [{"parea":1,"pno":int(area_dict2["no"])}]
"""添加RPM檢測(cè)更新時(shí)間"""
try:
svn_list_dict['upgrade_available'] = json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.upgrade_available')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['_stamp_localtime']
except:
svn_list_dict['upgrade_available'] = ''
nodes.append(area_dict2)
else:
area_dict2["status"] = 0
if nodes[2]['status'] == 3:
area_dict3["status"] = 2
try:
if json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.install')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['success'] == "True":
if json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.install')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['return']['%s'% svn_name]['old'] != '':
release_old = json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.install')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['return']['%s'% svn_name]['old'].split('-')[0]
else:
release_old = 0
try:
release_new = json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.install')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['return']['%s'% svn_name]['new'].split('-')[0]
except:
release_new = ''
if (int(release_old)+1 == int(release_new)) and (int(release_new) == int(Svn_name.svn_no)):
area_dict3["status"] = 3
elif int(release_old) == int(release_new):
area_dict3["status"] = 2
else:
area_dict3["status"] = 1
else:
area_dict3["status"] = 1
except:
area_dict3["status"] = 2
area_dict3["area"] = 3
area_dict3["no"] = (Svn_name.svn_hosts).split(',').index(svn_host)
area_dict3["label"] = "%s" % str(svn_host)
area_dict3["parent"] = [{"parea":2,"pno":int(area_dict3["no"])}]
"""添加RPM安裝時(shí)間"""
try:
svn_list_dict['install'] = json.loads(r.get('%s:%s-%s' % (svn_name, svn_host, 'pkg.install')).replace("'", "\"").replace('True','"True"').replace('False','"False"'))['_stamp_localtime']
svn_list_dict["release_new"] = release_new
except:
svn_list_dict['install'] = ''
nodes.append(area_dict3)
else:
area_dict3["status"] = 0
else:
pass
svn_list.append(svn_list_dict)
except Exception as err:
print 'Release_status_err:',err
return render_to_response('myrelease/release_status.html',{'svn_list':svn_list,'nodes':nodes},context_instance=RequestContext(request))
else:
return render_to_response('myrelease/release_status.html',context_instance=RequestContext(request))
這段代碼大體實(shí)現(xiàn)了通過判斷
pkg.get_repo
(驗(yàn)證Yum是否存在)耽梅,pkg.upgrade_available
(驗(yàn)證Yum是否更新)薛窥,pkg.install
(安裝)返回的狀態(tài),來輸出不同的status褐墅,前端會(huì)根據(jù)不同的status拆檬,繪制不同的顏色。
四妥凳、效果圖:
五竟贯、前端代碼:
<script type="text/javascript">
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container_drawing',
events: {
load: drawing
}
},
title: {
text: ''
}
});
});
/* 畫圖自定義函數(shù) */
function drawing(){
// Draw the flow chart
var ren = this.renderer;
var rightArrow = ['M', 0, 0, 'L', 100, 0, 'L', 95, 5, 'M', 100, 0, 'L', 95, -5];
var colors = ['#2570a1','#d71345','#decb00','#1d953f'];//四種狀態(tài)對(duì)應(yīng)的顏色:未啟動(dòng),告警,正在運(yùn)行,完成
// 分割線1
ren.path(['M', 270, 40, 'L', 270, 600]).attr({'stroke-width': 3,stroke: 'silver',dashstyle: 'dash'}).add();
// 分割線2
ren.path(['M', 520, 40, 'L', 520, 600]).attr({'stroke-width': 3,stroke: 'silver',dashstyle: 'dash'}).add();
// 分割線3
ren.path(['M', 770, 40, 'L', 770, 600]).attr({'stroke-width': 3,stroke: 'silver',dashstyle: 'dash'}).add();
// 分割線4
// ren.path(['M', 1020, 40, 'L', 1020, 600]).attr({'stroke-width': 3,stroke: 'silver',dashstyle: 'dash'}).add();
// 分割線5
// ren.path(['M', 1270, 40, 'L', 1270, 600]).attr({'stroke-width': 3,stroke: 'silver',dashstyle: 'dash'}).add();
// 區(qū)域標(biāo)題
ren.label('包構(gòu)建', 110,50).css({fontWeight: 'bold'}).add();
ren.label('驗(yàn)證源', 370,50).css({fontWeight: 'bold'}).add();
ren.label('驗(yàn)證包更新', 610, 50).css({fontWeight: 'bold'}).add();
// ren.label('前置命令', 880, 50).css({fontWeight: 'bold'}).add();
ren.label('項(xiàng)目包安裝', 880, 50).css({fontWeight: 'bold'}).add();
// ren.label('后置命令', 1380, 50).css({fontWeight: 'bold'}).add();
// 遍歷生成圖形
/**
area:區(qū)域編碼,值為:0,1,2,null.
no:節(jié)點(diǎn)序號(hào),值為:0,1,...,N,null.
label:節(jié)點(diǎn)內(nèi)容.
status:節(jié)點(diǎn)狀態(tài)值為:0為未執(zhí)行,1為錯(cuò)誤,2為執(zhí)行中,3為執(zhí)行完畢
parea:父節(jié)點(diǎn)區(qū)域編碼,值為:0,1,2,null.
pno:父節(jié)點(diǎn)序號(hào),值為:0,1,...,N,null.
*/
// var nodes = [{"area":5,"status":2,"no":0,"parent":[{"parea":4,"pno":0}],"label":"md25-qnzs-app-13.youth.cn"},{"area":4,"status":3,"no":0,"parent":[{"parea":3,"pno":0}],"label":"md25-qnzs-app-13.youth.cn"},{"area":3,"status":3,"no":0,"parent":[{"parea":2,"pno":0}],"label":"md25-qnzs-app-13.youth.cn"},{"area":2,"status":3,"no":0,"parent":[{"parea":1,"pno":0},{"parea":1,"pno":1}],"label":"md25-qnzs-app-13.youth.cn"},{"area":1,"status":1,"no":0,"parent":[{"parea":0,"pno":0},{"parea":0,"pno":1},{"parea":0,"pno":3}],"label":"md25-qnzs-app-13.youth.cn"},{"area":1,"status":0,"no":1,"parent":[{"parea":0,"pno":1},{"parea":0,"pno":2}],"label":"md25-qnzs-app-13.youth.cn"},{"area":0,"status":0,"no":0,"parent":[],"label":"md25-qnzs-app-13.youth.cn"},{"area":0,"status":1,"no":1,"parent":[],"label":"md25-qnzs-app-13.youth.cn"},{"area":0,"status":2,"no":2,"parent":[],"label":"md25-qnzs-app-13.youth.cn"},{"area":0,"status":1,"no":3,"parent":[],"label":"md25-qnzs-app-13.youth.cn"}];
var nodes = {% autoescape off %}{{ nodes}}{% endautoescape %}
for(var i=0;i<nodes.length;i++){
var node = nodes[i];
ren.label(node.label, 30+node.area*250, 80+node.no*50).attr({fill: colors[node.status],stroke: '#f2eada','stroke-width': 2,padding: 5,r: 9,width:220,height:20}).css({color: 'white',fontSize:'xx-small',textAlign:'center',fontWeight: 'bold'}).add().shadow(true);
for(var j=0;j<node.parent.length;j++){
var parent = node.parent[j];
ren.path(['M', 30+node.area*250, 75+node.no*50+20, 'L', 2+parent.parea*250+250+10, 75+parent.pno*50+20]).attr({'stroke-width': 2,stroke: '#4169E1'}).translate(0, 0).add();//
}
}
}