接上篇《授之以渔-运维平台发布模块一(Jenkins篇)》,今天介绍下结合着Saltstack的MasterEven,用来做发布系统的结果监控。
一、Event介绍
SaltStack 0.10版本中, 新增了Event系统, 官方在 Release Notes 对其描述如下:
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.
同时官方也在 Salt Event系统 页面中提供了监听event的例子程序, 基于其进行下Event系统学习.
我们所需要的就是监听event,然后把结果存储到Mysql,Redis之类,以便于我们后续进行处理。
二、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脚本的大体实现了过滤一些非必要信息,把fun中包含
pkg.get_repo
(验证Yum是否存在),pkg.upgrade_available
(验证Yum是否更新),pkg.install
(安装)存储到Redis中。然后我只需要去Redis中取出数据,并通过highcharts.js汇出图即可。
三、后端处理Redis数据绘图代码:
@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)
"""排除同一个主机,多个项目"""
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
"""添加检测YUM源存在时间"""
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检测更新时间"""
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安装时间"""
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))
这段代码大体实现了通过判断
pkg.get_repo
(验证Yum是否存在),pkg.upgrade_available
(验证Yum是否更新),pkg.install
(安装)返回的状态,来输出不同的status,前端会根据不同的status,绘制不同的颜色。
四、效果图:
五、前端代码: