def get_simple_log(self): latest_file = run_command("ls -tr " + self.log_directory + " | grep -i " + self.lookup_value + " | tail -1 ") if latest_file == "": return "defaultNotFound.txt" else: latest_txt = self.latest_file.replace( ".log", "-" + str(self.t_stamp) + ".txt") run_command("cp " + self.log_directory + self.latest_file.rstrip() + " " + self.output_directory + latest_txt) return latest_txt
def get_simple_log(self): latest_file = run_command("ls -tr " + self.log_directory + " | grep -i " + self.lookup_value + " | tail -1 ") if latest_file == "": return "defaultNotFound.txt" else: latest_txt = self.latest_file.replace(".log", "-" + str(self.t_stamp) + ".txt") run_command("cp " + self.log_directory + self.latest_file.rstrip() + " " + self.output_directory + latest_txt) return latest_txt
def __init__(self): # New time stamp needed so log file will be unique self.t_stamp = int(time.time()) # hardcoded shizzle that won't expect to ever change self.log_directory = "/var/log/httpd/" # Type of log file passed from web GUI self.lookup_value = str(sys.argv[1]) # Storage directory for the logs self.output_directory = "/var/www/htdocs/selfservice/storage/" if len(sys.argv) == 6: # Each log file uses a different logging format :/ if self.lookup_value in ["sugarcrm"]: # regex - start of line must begin with # 3 characters followed by a literal white space # 3 characters followed by a literal white space # 2 digits followed by literal space # 00:00:00 format also allows 0000:00:00 pattern = re.compile("^[a-zA-Z]{3}[\s][a-zA-Z]{3}[\s]" "[0-9]{2}[\s]" "[\d]{2,}[:][\d]{2}[:][\d]{2}") elif self.lookup_value in ["php"]: pattern = re.compile("Get the regex for php log file") else: print "stop trying to inject code" sys.exit(-1) # Get the latest log file # ls -ltr /dir/name | grep -i name | tail -1 latest_file = run_command("ls -tr " + self.log_directory + " | grep -i " + self.lookup_value + " | tail -1 ").rstrip() # Make the new name for what the file sent via web GUI will be latest_txt = latest_file.replace(".log", "-" + str(self.t_stamp) + ".txt") self.pattern = pattern self.latest_txt = latest_txt self.full_path = self.log_directory + latest_file self.same_hour = int(sys.argv[2]) == int(sys.argv[4]) self.same_minute = int(sys.argv[3]) == int(sys.argv[5]) elif len(sys.argv) == 4: self.start_date = str(sys.argv[2]) self.end_date = str(sys.argv[3]) elif len(sys.argv) == 2: # Get the latest log file # ls -ltr /dir/name | grep -i name | tail -1 self.latest_file = run_command("ls -tr " + self.log_directory + " | grep -i " + self.lookup_value + " | tail -1 ").rstrip()
def check_input(self): for path in self.file_name: path = path.translate(None, whitespace) if path.startswith("/"): if path[:35] in self.base_directory: if path.endswith("/ALL"): if os.path.exists(path[:-3]): self.get_directory(path[:-3]) else: if os.path.exists(path): self.list_of_files.append(path) else: self.file_not_found(path) elif path[:28] in self.base_directory: if path.endswith("/ALL"): if os.path.exists(path[:-3]): self.get_directory(path[:-3]) else: if os.path.exists(path): self.list_of_files.append(path) else: self.file_not_found(path) elif path in self.whitelist_files: self.list_of_files.append(path) else: self.file_not_found(path) else: if path == "ALL": self.get_directory(self.base_directory[0]) if path.endswith("/ALL"): if os.path.exists(self.base_directory[0] + path[:-3]): self.get_directory(self.base_directory[0] + path[:-3]) elif os.path.exists(self.base_directory[0] + path): self.list_of_files.append(self.base_directory[0] + path) else: self.file_not_found(path) if len(self.list_of_files) == 1: base_name = os.path.basename(str(self.list_of_files[0])) dir_name = os.path.dirname(str(self.list_of_files[0])) if self.check_blacklist(base_name): base_name += ".scrubbed" run_command("cp " + dir_name + "/" + base_name + " " + self.output_directory + base_name) print base_name else: self.create_zip()
def check_input(self): for path in self.file_name: path = path.translate(None, whitespace) if path.startswith("/"): if path[:35] in self.base_directory: if path.endswith("/ALL"): if os.path.exists(path[:-3]): self.get_directory(path[:-3]) else: if os.path.exists(path): self.list_of_files.append(path) else: self.file_not_found(path) elif path[:28] in self.base_directory: if path.endswith("/ALL"): if os.path.exists(path[:-3]): self.get_directory(path[:-3]) else: if os.path.exists(path): self.list_of_files.append(path) else: self.file_not_found(path) elif path in self.whitelist_files: self.list_of_files.append(path) else: self.file_not_found(path) else: if path == "ALL": self.get_directory(self.base_directory[0]) if path.endswith("/ALL"): if os.path.exists(self.base_directory[0]+path[:-3]): self.get_directory(self.base_directory[0]+path[:-3]) elif os.path.exists(self.base_directory[0]+path): self.list_of_files.append(self.base_directory[0]+path) else: self.file_not_found(path) if len(self.list_of_files) == 1: base_name = os.path.basename(str(self.list_of_files[0])) dir_name = os.path.dirname(str(self.list_of_files[0])) if self.check_blacklist(base_name): base_name += ".scrubbed" run_command("cp " + dir_name+"/"+base_name + " " + self.output_directory + base_name) print base_name else: self.create_zip()
def __init__(self): self.file_name = sys.argv[1].replace("\r", "\\") self.file_name = self.file_name.split("\\") self.hostname = run_command("hostname").rstrip() # Storage directory for the logs self.output_directory = "/var/www/htdocs/selfservice/storage/" self.not_found_directory = "/var/www/htdocs/sales/files-not-matched/" self.base_directory = ["/var/www/htdocs/sales/salesconnect/", "/opt/freeware/etc/httpd/conf", "/tempspace/ucd_temp/"] self.whitelist_files = ["/etc/openldap/ldap.conf", "/opt/freeware/etc/php.ini"] # New time stamp needed so log file will be unique self.t_stamp = int(time.time()) self.list_of_files = [] os.chdir(self.output_directory)
def __init__(self): self.file_name = sys.argv[1].replace("\r", "\\") self.file_name = self.file_name.split("\\") self.hostname = run_command("hostname").rstrip() # Storage directory for the logs self.output_directory = "/var/www/htdocs/selfservice/storage/" self.not_found_directory = "/var/www/htdocs/sales/files-not-matched/" self.base_directory = [ "/var/www/htdocs/sales/salesconnect/", "/opt/freeware/etc/httpd/conf", "/tempspace/ucd_temp/" ] self.whitelist_files = [ "/etc/openldap/ldap.conf", "/opt/freeware/etc/php.ini" ] # New time stamp needed so log file will be unique self.t_stamp = int(time.time()) self.list_of_files = [] os.chdir(self.output_directory)
def get_dates_zip(self): run_command("touch -t \"" + self.start_date + "\" " + self.log_directory + "touch_start") run_command("touch -t \"" + self.end_date + "\" " + self.log_directory + "touch_end") hostname = run_command("hostname").rstrip() grab_files = run_command("find " + self.log_directory + " -type f -newer " + self.log_directory + "touch_start ! -newer " + self.log_directory + "touch_end" + "| grep -i " + self.lookup_value) grab_files = grab_files.split() zf = zipfile.ZipFile(self.output_directory + hostname + str(self.t_stamp) + ".zip", mode="w") for _file in grab_files: zf.write(_file) zf.close() return hostname + str(self.t_stamp) + ".zip"
output_directory = "/var/www/htdocs/logs/storage/" lookup_value = str(sys.argv[1]).rstrip() start_hour = int(sys.argv[2]) start_minute = int(sys.argv[3]) end_hour = int(sys.argv[4]) end_minute = int(sys.argv[5]) time_stamp = int(time.time()) if lookup_value in ["sugarcrm"]: log_directory = "/var/log/httpd/" latest_file = "" latest_file = run_command("ls -tr "+log_directory+" | grep -i "+ lookup_value + " | tail -1 ").rstrip() latest_txt = latest_file.replace(".log", "-"+str(time_stamp)+".txt") full_path = log_directory+latest_file count = 0 same_hour = False same_minute = False if start_hour == end_hour: same_hour = True if start_minute == end_hour: same_minute = True count = find_line_number(full_path, start_hour, count, True,same_hour, True) count = find_line_number(full_path, start_minute, count, True, same_minute, False) end_count = find_line_number(full_path, end_hour, count, False, same_hour, True) end_count = find_line_number(full_path, end_minute, end_count, False, same_minute, False)
def get_directory(self, directory): grab_files = run_command("find " + directory + "* -prune -type f ") grab_files = grab_files.splitlines() for _file in grab_files: self.list_of_files.append(_file)
def create_file(self, start_count, end_count): run_command("sed '" + str(start_count + 1) + "," + str(end_count) + "!d;' " + self.full_path + " > " + self.output_directory + self.latest_txt) return self.latest_txt
#!/usr/bin/python import sys import time from common_functions import run_command output_directory = "/var/www/htdocs/selfservice/storage/" lookup_value = str(sys.argv[1]).rstrip() time_stamp = int(time.time()) if lookup_value in ["SO_FAR_ALL_LOGS_ARE_IN_HERE"]: pass else: log_directory = "/var/log/httpd/" latest_file = "" latest_file = run_command("ls -tr "+log_directory+" | grep -i "+ lookup_value + " | tail -1 ") if latest_file == "": print "defaultNotFound.txt" else: latest_txt = latest_file.replace(".log", "-"+str(time_stamp)+".txt") print latest_txt run_command("cp "+log_directory+latest_file.rstrip()+" "+output_directory+latest_txt)
output_directory = "/var/www/htdocs/logs/storage/" lookup_value = str(sys.argv[1]).rstrip() start_hour = int(sys.argv[2]) start_minute = int(sys.argv[3]) end_hour = int(sys.argv[4]) end_minute = int(sys.argv[5]) time_stamp = int(time.time()) if lookup_value in ["sugarcrm"]: log_directory = "/var/log/httpd/" latest_file = "" latest_file = run_command("ls -tr " + log_directory + " | grep -i " + lookup_value + " | tail -1 ").rstrip() latest_txt = latest_file.replace(".log", "-" + str(time_stamp) + ".txt") full_path = log_directory + latest_file count = 0 same_hour = False same_minute = False if start_hour == end_hour: same_hour = True if start_minute == end_hour: same_minute = True count = find_line_number(full_path, start_hour, count, True, same_hour, True) count = find_line_number(full_path, start_minute, count, True, same_minute, False) end_count = find_line_number(full_path, end_hour, count, False, same_hour,
import sys import zipfile import time from common_functions import run_command output_directory = "/var/www/htdocs/selfservice/storage/" lookup_value = str(sys.argv[1]) start_date = str(sys.argv[2]) end_date = str(sys.argv[3]) if lookup_value in ["sugarcrm"]: log_directory = "/var/log/httpd/" run_command("touch -t \""+start_date+"\" "+log_directory+"touch_start") #touch_end = run_command("touch -t \""+end_date+"\" "+log_directory+"touch_end") hostname = run_command("hostname").rstrip() grab_files = run_command("find "+log_directory+" -type f -newer " + log_directory + "touch_start ! -newer " + log_directory + "touch_end" + "| grep -i " + lookup_value) grab_files = grab_files.split() time_stamp = int(time.time()) zf = zipfile.ZipFile(output_directory+hostname+str(time_stamp)+".zip", mode="w") for _file in grab_files: zf.write(_file) zf.close() print (hostname+str(time_stamp)+".zip")