本文主要介绍Python自动化运维的常用模块,比如:XlsxWriter、rrdtool等。
1. 用XlsxWriter模块创建报表
- 安装xlsxwriter模块
1 | # pip install xlsxwriter 推荐使用pip安装 |
- xlsxwriter模块使用
1 | #!/usr/bin/env python |
2. 绘图大师rrdtool
- rrdtool绘图
创建rrd1
2
3
4
5
6
7
8
9
10
11
12#!/usr/bin/python
import rrdtool
rrdb=rrdtool.create('rest.rrd','--step','60','--start','1369982786',
'DS:input:GAUGE:120:U:U',
'DS:output:GAUGE:120:U:U',
'RRA:LAST:0.5:1:600',
'RRA:AVERAGE:0.5:5:600',
'RRA:MAX:0.5:5:600',
'RRA:MIN:0.5:5:600')
if rrdb:
print rrdtool.error()
rrd插入数据1
2
3
4
5
6
7
8
9
10
11#!/usr/bin/python
import time
import psutil
import rrdtool
for keys in psutil.network_io_counters(pernic=True):
if keys == 'em1':
sent=psutil.network_io_counters(pernic=True)[keys][0]
recv=psutil.network_io_counters(pernic=True)[keys][1]
up=rrdtool.updatev('rest.rrd','N:%d:%d' % (sent,recv))
print up
根据rrd绘图1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#!/usr/bin/python
import rrdtool
rrdtool.graph('rest.png','--start','1369983960',
'--title','my rrd graph test',
'--vertical-label','bits',
'DEF:input=rest.rrd:input:LAST',
'DEF:output=rest.rrd:output:LAST',
'LINE1:input#0000FF:In traffic',
'LINE1:output#00FF00:Out traffic\\r',
'CDEF:bytes_in=input,8,*',
'CDEF:bytes_out=output,8,*',
'COMMENT:\\n',
'GPRINT:bytes_in:LAST:LAST in traffic\: %6.2lf %Sbps',
'COMMENT: ',
'GPRINT:bytes_out:LAST:LAST out traffic\: %6.2lf %Sbps')
3. 利用Python-nmap实现高效端口扫描
1 | #!/usr/bin/env python |