Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1704099
  • 博文数量: 117
  • 博客积分: 2559
  • 博客等级: 少校
  • 技术积分: 4385
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-13 20:08
个人简介

作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.

文章分类

全部博文(117)

文章存档

2014年(1)

2013年(25)

2012年(13)

2011年(77)

2010年(1)

分类: 系统运维

2011-01-20 14:27:54

  1. #!/usr/bin/python

  2. import os,sys,time,re,threading,MySQLdb

  3. now=time.strftime('%Y%m%d')
  4. log_02='/tmp/tongbu_stats_%s.csv' % (now)

  5. dict_cmd={
  6. 'test1':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
  7. 'test2':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
  8. #'test3':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
  9. }

  10. def write_title(log_stat):
  11.     file_log=open(log_stat,'a')
  12.     file_log.write('start_time,end_time,module,Number of files,Number of files transferred,Total file size(bytes),Total transferred file size(bytes)\n')
  13.     file_log.close()

  14. def write_notes(cmd,log_stat,nam):
  15.     fle=open(log_stat,'a')
  16. # db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
  17. # db_curs=db_conn.cursor()
  18.     try:
  19.         s_time=time.strftime('%H:%M:%S')
  20.         cmd_put=os.popen(cmd)
  21.         list_tmp=[]
  22.         re_p=re.compile('(Number of files:|Number of files transferred:|Total file size:|Total transferred file size:) (\d+)?')
  23.         for each_line1 in cmd_put:
  24.             for each_line2 in re_p.finditer(each_line1):
  25.                 if each_line2 is not None:
  26.                     list_tmp.append(each_line2.group(2))
  27.                 else:
  28.                     pass
  29.         e_time=time.strftime('%H:%M:%S')
  30.     cmd_stat='%s,%s,%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,list_tmp[0],list_tmp[1],list_tmp[2],list_tmp[3],os.linesep)
  31.         cmd_put.close()
  32.         fle.write(cmd_stat)
  33.         db_curs.execute("INSERT INTO bak_stat \
  34.         (date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','')" \
  35.         % (now,s_time,e_time,nam,list_tmp[0],list_tmp[1],list_tmp[2],list_tmp[3]) )
  36.     except:
  37.         cmd_stat='%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,'error','error','error','error'+os.linesep)
  38.         fle.write(cmd_stat)
  39.         db_curs.execute("INSERT INTO bak_stat (date_ymd,s_time,e_time,mod_name,a_error) VALUES('%s','%s','%s','%s','error')" % (now,s_time,e_time,nam) )
  40.     fle.close()
  41. # db_curs.close()
  42. # db_conn.close()

  43. if __name__ == '__main__':
  44.     write_title(log_02)
  45.     db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
  46.     db_curs=db_conn.cursor()
  47.     threads = {}
  48.     for i in dict_cmd:
  49.         t=threading.Thread(target=write_notes,args=(dict_cmd[i],log_02,i))
  50. # print 'exec',i
  51.         threads[i]=t
  52. # print threads.items()
  53.     for i in dict_cmd:
  54.         threads[i].start()
  55.         time.sleep(2)
  56.     for i in dict_cmd:
  57.         threads[i].join()
  58.     db_curs.close()
  59.     db_conn.close()
 
部分代码解释:
#!/usr/bin/python
import os,sys,time,re,threading,MySQLdb
now=time.strftime('%Y%m%d')
log_02='/tmp/tongbu_stats_%s.csv' % (now)
dict_cmd={
'test1':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
'test2':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
#'test3':'/usr/bin/rsync -tzrlp --stats /tmp/test1 /tmp/test2010',
}
#执行命令也用字典的形式,“键”相当于模块名,用于标识
def write_title(log_stat):
    file_log=open(log_stat,'a')
    file_log.write('start_time,end_time,module,Number of files,Number of files transferred,Total file size(bytes),Total transferred file size(bytes)\n')
    file_log.close()
def write_notes(cmd,log_stat,nam):
    fle=open(log_stat,'a')
#    db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
#    db_curs=db_conn.cursor()
    try:
        s_time=time.strftime('%H:%M:%S')
        cmd_put=os.popen(cmd)
        list_tmp=[]
        re_p=re.compile('(Number of files:|Number of files transferred:|Total file size:|Total transferred file size:) (\d+)?')
#cmd,log_stat,nam为形参,注意区分下面的实参,i
#(\d+)?表示,\d+表示任意数字,至少有一个,
#特殊符号*,+,?它们可以用于匹配字符串模式出现一次、多次、或未出现的情况。
#星号或称星号操作符匹配它左边那个正则表达式出现零次或零次以上的情况
#加号(+)操作符匹配它左边那个正则表达式模式至少出现一次的情况(它也被称为正闭包操作符)
#问号操作符( ? )匹配它左边那个正则表达式模式出现零次或一次的情况
 for each_line1 in cmd_put:
            for each_line2 in re_p.finditer(each_line1):
                if each_line2 is not None:
                    list_tmp.append(each_line2.group(2))
                else:
                    pass
#用了两次循环,第一次把命令结果迭代赋给each_line1变量,第二次,调用正则表达式,把匹配值赋给each_line2变量
#finditer(pattern,string[, flags])在字符串中,查找匹配项,返回迭代器;对于每个匹配,该迭代器返回一个匹配对象。
#避免程序抛出错误,增加一个判断。
        e_time=time.strftime('%H:%M:%S')
 cmd_stat='%s,%s,%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,list_tmp[0],list_tmp[1],list_tmp[2],list_tmp[3],os.linesep)
        cmd_put.close()
        fle.write(cmd_stat)
        db_curs.execute("INSERT INTO bak_stat \
        (date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','')" \
        % (now,s_time,e_time,nam,list_tmp[0],list_tmp[1],list_tmp[2],list_tmp[3]) )
    except:
        cmd_stat='%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,'error','error','error','error'+os.linesep)
        fle.write(cmd_stat)
        db_curs.execute("INSERT INTO bak_stat (date_ymd,s_time,e_time,mod_name,a_error) VALUES('%s','%s','%s','%s','error')" % (now,s_time,e_time,nam) )
#try...except,异常处理
    fle.close()
#    db_curs.close()
#    db_conn.close()
if __name__ == '__main__':
    write_title(log_02)
    db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
    db_curs=db_conn.cursor()
#避免MySQL连接的wait_timeout超时,统计数据无法记录到数据库中
    threads = {}
    for i in dict_cmd:
        t=threading.Thread(target=write_notes,args=(dict_cmd[i],log_02,i))
#        print 'exec',i
        threads[i]=t
#        print threads.items()
    for i in dict_cmd:
        threads[i].start()
        time.sleep(2)
    for i in dict_cmd:
        threads[i].join()
    db_curs.close()
    db_conn.close()
#创建一个Thread 的实例,传给它一个函数target,参数args
#threading是多线程
#三个循环,将线程都存在threads{}字典中;依次开始;程序挂起,直到线程结束。
 
阅读(6760) | 评论(0) | 转发(0) |
0

上一篇:mysqldump备份数据库

下一篇:多个主机传输

给主人留下些什么吧!~~