#!/usr/bin/env python # ----------------------------------------------------------------------- # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # ----------------------------------------------------------------------- import datetime import os import socket import subprocess import sys from optparse import OptionParser from ducc_util import DuccUtil from ducc_logger import DuccLogger logger = DuccLogger() # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # + # + gather_logs # + # + purpose: create gz of DUCC log files and other debug information # + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # ----------------------------------------------------------------------- # Extend OptionParser class class ExtendedOptionParser(OptionParser): # override epilog formatter so # that newlines are not deleted! def format_epilog(self, formatter): return self.epilog # ----------------------------------------------------------------------- # epilog for --help def get_epilog(): epilog = '' epilog = epilog+'\n' epilog = epilog+'Purpose: gather problem determination information comprising DUCC logs and configuration data into a gz file.' epilog = epilog+'\n' epilog = epilog+'\n' return epilog class GatherLogs(DuccUtil): # output directory directory = '/tmp' # output base filename filename = 'ducc-problem-determination-info' filetype = 'tar' def get_timestamp(self): dt = datetime.datetime.now() year = dt.strftime("%Y") month = dt.strftime("%b") day = dt.strftime("%d") date = year+month+day hour = dt.strftime("%H") minute = dt.strftime("%M") second = dt.strftime("%S") time = hour+minute+second timestamp = date+'.'+time return timestamp # resolve output file path def resolve_target(self): if(self.directory == None): self.directory = '/tmp' self.directory = self.directory.strip() if(self.directory == ''): self.directory = '/tmp' self.filename = self.filename+'.'+socket.gethostname()+'.'+self.get_timestamp() self.target = self.directory+'/'+self.filename self.target = self.target.replace('//','/') self.target = self.target+'.'+self.filetype if(not os.path.isdir(self.directory)): os.makedirs(self.directory) # parse command line def parse_cmdline(self): parser = ExtendedOptionParser(epilog=get_epilog()) width = 45 parser.formatter.help_position = width parser.formatter.max_help_position = width parser.add_option('-d','--directory', action='store', dest='directory', default=self.directory, help='path to directory for gz file, default='+self.directory) (options, args) = parser.parse_args() self.directory = options.directory # create tar file def create_targz(self): cmd = [] cmd.append('tar') cmd.append('--directory') cmd.append(self.DUCC_HOME) cmd.append('--exclude') cmd.append('state/database') cmd.append('--create') cmd.append('--file') cmd.append(self.target) cmd.append('apache-uima/apache-activemq/data') cmd.append('logs') cmd.append('cassandra-server/logs') cmd.append('resources') cmd.append('state') cmd.append('README') cmdline = '' for item in cmd: cmdline = cmdline+' '+item #print cmdline message = 'creating'+' '+self.target+'.gz' logger.info(message) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() cmd = [] cmd.append('gzip') cmd.append(self.target) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() sizeMB = os.path.getsize(self.target+'.gz')/(1.0*1024*1024) fmtSizeMB = "%.2f" % sizeMB message = 'size'+' '+str(fmtSizeMB)+' '+'MB' logger.info(message) def main(self, argv): self.parse_cmdline() self.resolve_target() self.create_targz() if __name__ == '__main__': instance = GatherLogs() instance.main(sys.argv[1:])