65
Python & SA Guixing<[email protected] >

Python and sysadmin I

Embed Size (px)

DESCRIPTION

introduce python for system administrator.

Citation preview

Page 2: Python and sysadmin I

do what?

Page 3: Python and sysadmin I

install softwareor

hardware

Page 4: Python and sysadmin I

backup

Page 5: Python and sysadmin I

repair

Page 6: Python and sysadmin I

monitor

Page 7: Python and sysadmin I

analysis

Page 8: Python and sysadmin I

adjust

Page 9: Python and sysadmin I

even more

Page 10: Python and sysadmin I

find program bugs

Page 11: Python and sysadmin I

fix it

Page 12: Python and sysadmin I

meeting

Page 13: Python and sysadmin I

HOPE FOR

Page 14: Python and sysadmin I

Automatic

Page 15: Python and sysadmin I
Page 16: Python and sysadmin I

try

Page 17: Python and sysadmin I

before try

Page 18: Python and sysadmin I

No God

Page 19: Python and sysadmin I

Not Perfect

Page 20: Python and sysadmin I

More light

Page 21: Python and sysadmin I

Continuable

Page 22: Python and sysadmin I

introduceGenerator

Page 23: Python and sysadmin I

iteration

Page 24: Python and sysadmin I

for

Page 25: Python and sysadmin I

[list] & (tuple)>>>for i in [1,2,3,4]... print i1234>>>for i in (1,2,3,4):...

Page 26: Python and sysadmin I

{dict}>>>d = {‘a’:1,’b’:2}>>>for i in d:... print i,d[i]

a 1b 2

Page 27: Python and sysadmin I

“string”>>>for i in “python”:... print ipython

Page 28: Python and sysadmin I

also file

Page 29: Python and sysadmin I

file>>>for i in open(‘example.txt’):... print i1st line2nd line

Page 30: Python and sysadmin I

iterable

Page 31: Python and sysadmin I

.__iter__(self).next(self)

Page 32: Python and sysadmin I

class countdown(object): def __init__(self,start): self.count = start def __iter__(self): return self def next(self): if self.count <= 0: raise StopIteration r = self.count self.count -= 1 return self

Page 33: Python and sysadmin I

>>>a = countdown(5)>>>for i in a:... print i...5 4 3 2 1>>>

Page 34: Python and sysadmin I

yield

Page 35: Python and sysadmin I

def d1(t): newt=[] for i in t: newt.append(i+1) return newt

def d2(t): for i in t: yield i+1

Page 36: Python and sysadmin I

yielddef countdown(n): while n > 0: yield n n -= 1

>>>a = countdown(5)>>>for i in a: print i5 4 3 2 1

Page 37: Python and sysadmin I

this is a

Generator

Page 38: Python and sysadmin I

GeneratorExpressions

Page 39: Python and sysadmin I

generator & yield(expr for i in s if condition)

#same as

for i in s: if condition: yield expr

Page 40: Python and sysadmin I

1st Problem

Page 41: Python and sysadmin I

analyzeaccess log

Page 42: Python and sysadmin I

127.0.0.1 - - [31/Mar/2009:21:53:25 +0800] "GET /apache_pb.gif HTTP/1.1" 200 2326

host referrer user [datetime] "request" status bytes

re.compile(r'(\S+) (\S+) (\S+) \[(.*?)\] "(\S+) (\S+) (\S+)" (\S+) (\S+)')

match().groups()

line = ('127.0.0.1','-','-','31/Mar/2009:21:53:25 +0800','GET','/apache_pb.gif','HTTP/1.1','200','2326')

Page 43: Python and sysadmin I

cols=('host','referrer','user','datetime','method','request','proto','status','bytes')

dict(zip(cols,line))

{'bytes': '2326', 'datetime': '31/Mar/2009:21:53:25 +0800', 'host': '127.0.0.1', 'method': '"GET', 'proto': 'HTTP/1.1', 'referrer': '-', 'request': '/apache_pb.gif', 'status': '200', 'user': '-'}

Page 44: Python and sysadmin I

RealOne

Page 45: Python and sysadmin I

lines=(dict(zip(cols,logpats.match(i).groups())) for i in open('./access_log'))

for i in lines: print i

{'status': '200', 'proto': 'HTTP/1.1', 'referrer': '-', 'request': '/', 'datetime': '31/Mar/2009:21:53:25 +0800', 'bytes': '1456', 'host': '::1', 'user': '-', 'method': 'GET'}

Page 46: Python and sysadmin I

def fmap(dictseq,col,func): for d in dictseq: d[col] = func(d[col]) yield d

log = fmap(lines,’status’,int)log = fmap(log,’bytes’,lambda s:int(s) if s != ‘-’ else 0)

{'status': 200, ...'bytes': 1456, ...}

Page 47: Python and sysadmin I

Show Time

Page 48: Python and sysadmin I

>>>print sum(r[‘bytes’] for r in log)2456

>>>print “%d %s” % max(r[‘bytes’],r[‘request’] for r in log)233 /aaa.jpg

>>>print set(r[‘host’] for r in log)

>>>print set(r[‘request’] for r in log if r[‘status’] == 404)

Page 49: Python and sysadmin I

2nd Problem

Page 50: Python and sysadmin I

findspecific files

Page 51: Python and sysadmin I

os.walk

Page 52: Python and sysadmin I

Is a Generator

Page 53: Python and sysadmin I

for p,dl,fl in os.walk(t)

#t: a top directory

#p: current path#dl: directory list in current#fl: file list in current

Page 54: Python and sysadmin I

pyfindimport osimport fnmatch

def pyfind(top,pats): for p,dl,fl in os.walk(top): for f in fnmatch.filter(fl,pats): yield os.path.join(p,f)

Page 55: Python and sysadmin I

pyfind big fileif os.path.getsize(ffp) > s*1024*1024: yield ffp

Page 56: Python and sysadmin I

os.path

Page 57: Python and sysadmin I

get statos.path.getctimeos.path.getmtimeos.path.getatimeos.path.getsize

os.stat

Page 58: Python and sysadmin I

do a testos.path.existsos.path.isabsos.path.isdiros.path.isfileos.path.islinkos.path.ismount

Page 59: Python and sysadmin I

deal with pathos.path.abspathos.path.joinos.path.basenameos.path.dirnameos.path.commonprefixos.path.splitos.path.splitext

Page 60: Python and sysadmin I

next

Page 61: Python and sysadmin I

processthreadqueue

Page 62: Python and sysadmin I

Reference

Page 64: Python and sysadmin I

Q&A

Page 65: Python and sysadmin I

Thanks