[toc]
业务服务监控详解
业务服务监控是运维体系中最重要的环节,是保证业务服务质量的关键手段。如何更有效地实现业务服务
是每个运维人员应该思考的问题,不同业务场景需定制不同的监控策略。Python在监控方面提供了大量
的第三方式具,可以帮助我们快速、有效地开发企业级服务监控平台,为我们的业务保驾护航。
文件内容差异对比方法
通过difflib模块实现文件内容差异比对。difflib作为Python的标准库模块,无需安装,作用是对比
文本之间的差异,且支持输出可读性较强的HTML档,与linux下的diff命令相似。 我们可以使用
difflib对比代码、配置文件的差别,在版本控制方在是非常有用的。python2.3或者更高版本默认
自带difflib模块
两个字符串的差异对比
通过使用difflib模块实现两个字符串的差异对比,然后以版本控制风格进行输出
➜ test cat diff.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import difflib
text1 = """text1:
This module provides classes and funcitons for compring sequences.
including HTML and context and unified diffs.
difflib document v7.4
add sring
"""
text1_lines = text1.splitlines() #以行进行分隔,以便进行对比
text2 = """text2
This module provides classes and functions for Comparing sequences.
including HTML and context and unified diffs.
difflib document v7.5
"""
text2_lines = text2.splitlines()
d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines) # 采用compare方法对字符串进行比较
print '\n' .join(list(diff))
# 示例结果
➜ test python diff.py
- text1:
+ text2
- This module provides classes and funcitons for compring sequences.
? - ^
+ This module provides classes and functions for Comparing sequences.
? + ^ + +
including HTML and context and unified diffs.
- difflib document v7.4
? ^
+ difflib document v7.5
? ^
- add sring
➜ test
符号说明
'_' '包含在第一个序列行中,但不包含在第二个序列行'
'+' '包含在第二个序列行中,但不包含在第一个序列行'
'' '两个序列行一致'
'?' '标志两个序列行存在增量差异'
'^' '标志出两个序列行存在的差异字符'
生成美观的对比HTML格式文档
采用HtmlDiff()类的make_file() 方法就可以生成美观的HTML文档
d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines)
print '\n' .join(list(diff))
替换成:
d = difflib.HtmlDiff()
print d.make_file(text1_lines, text2_lines)
对比Nginx配置文件差异
当我们维护多个Nginx配置时,时常会对比不同版本的配置文件的差异,使用运维人员更加清晰地
了解不同版本迭代的更新项,实现的思路是读取两个需对比的配置文件,再以换行符作为分隔符
调用 difflib.HtmlDiff() 生成HTML格式的差异文档
scp diff_nginx.py root@www.ssjinyao.com:/root/
diff_nginx.py 100% 1035 1.0KB/s 00:00
➜ test ssh root@www.ssjinyao.com
Last failed login: Thu Jan 18 15:55:31 CST 2018 from 140.205.201.39 on ssh:notty
There were 48 failed login attempts since the last successful login.
Last login: Tue Jan 16 10:06:45 2018 from 111.198.29.81
Welcome to Alibaba Cloud Elastic Compute Service !
[root@iz2zearhdmowvugecyh820z ~]# vim diff_nginx.py
[root@iz2zearhdmowvugecyh820z ~]# python diff_nginx.py /etc/nginx/conf.d/leleol.conf /etc/nginx/conf.d/hexo.conf > /var/www/ssjinyao/diff_nginx.html
[root@iz2zearhdmowvugecyh820z ~]# cat diff_nginx.py
#!/bin/bash/env python
# -*- coding: utf-8 -*-
import difflib
import sys
try:
textfile1 = sys.argv[1] #第一个配置文件路径参数
textfile2 = sys.argv[2] #第二个配置文件路径参数
except Exception, e:
print "Error:" + str(e)
print "Usage: diff_nginx.py filename1 filename2"
sys.exit()
def readfile(filename): # 文件读取分隔函数
try:
fileHandle = open (filename, 'rb' )
text=fileHandle.read() .splitlines() #读取后以行进行分隔
fileHandle.close()
return text
except IOError as error:
print ('Read file Error:' +str(error))
sys.exit()
if textfile1 == "" or textfile2 == "":
print "Usage: diff_nginx.py filename1 filename2 "
sys.exit()
text1_lines = readfile(textfile1) #调用readfile函数,获取分隔后的字符串
text2_lines = readfile(textfile2)
d = difflib.HtmlDiff() #创建HtmlDiff()类对象
print d.make_file(text1_lines,text2_lines) #通过make_file方法输出HTML的对比结果
https://www.ssjinyao.com/diff_nginx.html