zoukankan      html  css  js  c++  java
  • python脚本实战1:rsync客户端推送

    前段时间想有空写个rsync client脚本 需要收集一下rsync执行结果,正好昨天部门其他team有这个需求,今天抽时间写了一个。

     1 #!/bin/env python26
     2 #-*- coding: utf-8 -*-
     3 '''
     4 Created on 2012-11-9
     5 
     6 @author: Jin
     7 
     8 '''
     9 
    10 import os
    11 import sys
    12 import subprocess
    13 import logging
    14 
    15 #rsync config
    16 localdir  = '/tmp/testdir/'
    17 rsync_exe = '/usr/bin/rsync '
    18 pwdfile   = '/etc/rsync/rsync.passwd'
    19 argv      = '-vzrtopg --delete --password-file='+pwdfile
    20 USER      = 'rsync_store'
    21 IP        = '@1.1.1.1'
    22 MODULE    = '::store'
    23 
    24 #vars
    25 homedir   = '/home/rsyncstore/'
    26 logfile   = os.path.basename(sys.argv[0])+'.log'
    27 logpath   = homedir+logfile
    28 
    29 #logconfig
    30 logging.basicConfig(level=logging.DEBUG,
    31                     format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
    32                     datefmt='%Y-%m-%d %H:%M',
    33                     filename=logpath,
    34                     filemode='a')
    35 
    36 console = logging.StreamHandler()
    37 console.setLevel(logging.INFO)
    38 formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    39 console.setFormatter(formatter)
    40 logging.getLogger('').addHandler(console)
    41 
    42 
    43 def is_run(processname):
    44     '''Get processname status'''
    45     cmd='/usr/bin/pgrep '+ processname+' > /dev/null 2>&1'
    46     try:
    47         #pstat=subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE)
    48         pstat=subprocess.Popen(cmd,shell=True)
    49     except StandardError,e:
    50         logging.error("Get run status failed:: %d (%s) ,exit run!" % (e.errno, e.strerror))
    51         sys.exit(1)
    52     else:
    53         pstat.wait()
    54         if pstat.returncode == 0:
    55             return True
    56         else:
    57             return False
    58         
    59 
    60 def rsync_store():
    61     '''Put localfile to rsync server'''
    62     cmd=rsync_exe+argv+' '+localdir+' '+USER+IP+MODULE
    63     if is_run('rsync'):
    64         logging.warning("rsync is running!")
    65     else:
    66         print 'to run'
    67         try:
    68             pstat=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
    69         except StandardError,e: 
    70             logging.error("Run script failed:: %d (%s) ,exit run!" % (e.errno, e.strerror))
    71             sys.exit(1)
    72         else:
    73             pstat.wait()
    74             if pstat.returncode == 0:
    75                 logging.info("Run script Successful,exit code is %s" % pstat.returncode)
    76                 lines=pstat.stdout.readlines()
    77                 dellist=[]
    78                 rsync_dict={}
    79                 for line in lines:
    80                     if 'deleting' in line:
    81                         dellist.append(line.split()[1])
    82                 delcount=len(dellist)
    83                 addlist=[i.rstrip('\n') for i in lines[delcount+2:-3]]
    84                 addcount=len(addlist)
    85                 speed=' '.join(lines[-2].split()[-2:])
    86                 logging.info("Result,delcount:%d,dellist:%s,addcount:%d,addlist:%s,speed:%s" % (delcount,dellist,addcount,addlist,speed))
    87                     
    88             else:
    89                 logging.error("Run script End,exit code is %s,With reason <%s>" % (pstat.returncode,pstat.stderr.read().rstrip('\n')))
    90                 
    91 
    92 if __name__=='__main__':
    93     rsync_store()
  • 相关阅读:
    USACO 4.1 Fence Rails
    POJ 1742
    LA 2031
    uva 10564
    poj 3686
    LA 3350
    asp.net MVC 3多语言方案--再次写, 配源码
    使用Log4net记录日志
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    为什么要使用反射机制
  • 原文地址:https://www.cnblogs.com/diege/p/2762992.html
Copyright © 2011-2022 走看看