13916729435 发表于 2018-10-26 11:59:43

MongoDB 日志切换(Rotate Log Files)实战

#!/bin/env python  
import sys
  
import os
  
import commands
  
import datetime,time
  
#get mongo pid
  
mongo_pid = commands.getoutput("/sbin/pidof mongod")
  
print mongo_pid
  
#send Sig to mongo
  
if mongo_pid != '':
  
cmd = "/bin/kill -USR1 %s" %(mongo_pid)
  
print cmd
  
mongo_rotate = commands.getoutput(cmd)
  
else:
  
print "mongod is not running..."
  
#clean log which > 7 days
  
str_now = time.strftime("%Y-%m-%d")
  
dat_now = time.strptime(str_now,"%Y-%m-%d")
  
array_dat_now = datetime.datetime(dat_now,dat_now,dat_now)
  
lns = commands.getoutput("/bin/ls --full-time /var/log/mongodb/|awk '{print $6, $9}'")
  
for ln in lns.split('\n'):
  
ws = ln.split()
  
if len(ws) != 2:
  
continue
  
ws1 = time.strptime(ws,"%Y-%m-%d")
  
ws2 = datetime.datetime(ws1,ws1,ws1)
  
if (array_dat_now - ws2).days > 7:
  
v_del = commands.getoutput("/bin/rm -rf /var/log/mongodb//%s" % (ws))


页: [1]
查看完整版本: MongoDB 日志切换(Rotate Log Files)实战