diff --git a/ArtifactoryLogParser/artifactory.py b/ArtifactoryLogParser/artifactory.py old mode 100644 new mode 100755 index 6b8ff27..c788cbf --- a/ArtifactoryLogParser/artifactory.py +++ b/ArtifactoryLogParser/artifactory.py @@ -3,48 +3,58 @@ import sys import colorama from colorama import Fore, Back, Style +import operator if len(sys.argv) !=2: print('Usage: python artifactory.py artifactory.log') sys.exit(0) - +errors = [] +warnings = [] +debug = [] readfile = sys.argv[1] -i = 0 -with open(readfile) as f: - for line in f: - if "WARN" in line: - try: - regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' - match = re.search(regex,line.encode('ascii'),flags = 0) - print Fore.RED + "WARNING:" + (match.group(6)) - - except Exception: - pass - -with open(readfile) as f: - for line in f: - if "ERROR" in line: - try: - regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' - match = re.search(regex,line.encode('ascii'),flags = 0) - print Fore.YELLOW + "ERROR:" + (match.group(6)) - - except Exception: - pass - -with open(readfile) as f: - for line in f: - if "INFO" in line: + +def readlogs(): + with open(readfile) as f: + for line in f: try: - regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' - match = re.search(regex,line.encode('ascii'),flags = 0) - print Fore.GREEN + "INFO:" + (match.group(6)) - + if "WARN" in line: + regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' + match = re.search(regex,line.encode('ascii'),flags = 0) + print Fore.RED + (match.group(6)) + warnings.append(match.group(6)[:40]) + + elif "ERROR" in line: + regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' + match = re.search(regex,line.encode('ascii'),flags = 0) + print Fore.YELLOW + (match.group(6)) + errors.append(match.group(6)[:40]) + elif "INFO" in line: + regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' + match = re.search(regex,line.encode('ascii'),flags = 0) + print Fore.GREEN + (match.group(6)) + elif "DEBUG" in line: + regex = '^([-0-9]+ [:0-9]+,[0-9]+) \[([-a-zA-Z0-9]+)\] \[([A-Z]+) *\] \(([.a-zA-Z0-9]+):([0-9]+)\) - (.*)$' + match = re.search(regex,line.encode('ascii'),flags = 0) + print Fore.MAGENTA + line + debug.append(match.group(6)[:40]) except Exception: pass - -print (Style.RESET_ALL) + warningdic = dict((x,warnings.count(x)) for x in set(warnings)) + for key, value in sorted(warningdic.iteritems(), key=lambda (k,v): (v,k)): + print Fore.RED + "%s : times --- %s" % (key, value) + + errorsdic = dict((x,errors.count(x)) for x in set(errors)) + for key, value in sorted(errorsdic.iteritems(), key=lambda (k,v): (v,k)): + print Fore.YELLOW + "%s : times --- %s" % (key, value) + + debugdic = dict((x,debug.count(x)) for x in set(debug)) + for key,value in sorted(debugdic.iteritems(),key=lambda (k,v):(v,k)): + print Fore.MAGENTA + "%s : times --- %s" % (key,value) + + + print (Style.RESET_ALL) + +readlogs() exit() - - \ No newline at end of file + diff --git a/ArtifactoryLogParser/request.py b/ArtifactoryLogParser/request.py index 28a3cc7..708b64b 100644 --- a/ArtifactoryLogParser/request.py +++ b/ArtifactoryLogParser/request.py @@ -1,21 +1,45 @@ import sys import re +import colorama +from colorama import Fore, Back, Style #match.group(1) = time(example: 20160621065454) #match.group(4) = IP Address #match.group(6) = HTTP Method(GET,POST,PUT) #match.group(7) = URL #match.group(9) = HTTP Response Code +responsecodes = [] + + if len(sys.argv) !=2: print('Usage: python request.py request.log') sys.exit(0) + readfile = sys.argv[1] -with open(readfile) as f: - for line in f: - try: - p = re.compile(ur'(\d*)\|(\d*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)') - match = re.match(p,line) - print match.group(4) + " - " + match.group(6) + " - " + match.group(9) - except Exception: - pass + +def readlogs(): + with open(readfile) as f: + for line in f: + try: + p = re.compile(ur'(\d*)\|(\d*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)') + match = re.match(p,line) + response = match.group(4) + " - " + match.group(6) + " - " + match.group(9) + if match.group(9)[:1] == "2": + print Fore.GREEN + response + responsecodes.append(match.group(9)) + elif match.group(9)[:1] == "4" or "5": + print Fore.RED + response + responsecodes.append(match.group(9)) + else: + print response + responsecodes.append(match.group(9)) + except Exception: + pass + print (Style.RESET_ALL) + responsedic = dict((x,responsecodes.count(x)) for x in set(responsecodes)) + for key, value in sorted(responsedic.iteritems(), key=lambda (k,v): (v,k)): + print "%s: %s" % (key, value) + + +readlogs() exit() \ No newline at end of file diff --git a/BuildCleaner/README.md b/BuildCleaner/README.md new file mode 100644 index 0000000..d96f93e --- /dev/null +++ b/BuildCleaner/README.md @@ -0,0 +1,3 @@ +# Build Cleaner +Deletes any builds without a set status such as 'Staged' and 'Release' +Artifactory URL and credentials need to be set first, please use an 'Admin' user \ No newline at end of file diff --git a/BuildCleaner/buildcleaner.py b/BuildCleaner/buildcleaner.py new file mode 100644 index 0000000..9da6dbe --- /dev/null +++ b/BuildCleaner/buildcleaner.py @@ -0,0 +1,44 @@ +import requests +import json +import sys +import re + +username = "admin" +password = "password" +artifactory = "https://127.0.0.1:8081/artifactory/" # change this artifactory URL +#make sure to clarify if HTTP or HTTPS + +def deletebuild(buildtodelete,idtodelete): + print buildtodelete + idtodelete + r = requests.delete(buildtodelete + idtodelete, auth= (username,password)) + +def inspectbuildjson(buildjson,buildjsonid): + #looks through every single build json in every build ID + r = requests.get(buildjson+buildjsonid, auth = (username, password)) + buildinfo = json.loads(r.text) + + if "statuses" not in buildinfo["buildInfo"] or len(buildinfo["buildInfo"]["statuses"]) == 0: + print "Deleting this build: " + buildjson + buildjsonid + deletebuild(buildjson,re.sub('/', '?buildNumbers=', buildjsonid)) + else: + print "Keeping this build: " + buildjson + buildjsonid + " - " + buildinfo["buildInfo"]["statuses"][0]["status"] + +def findbuildnumbers(buildname): + #finds all build ID's from all build projects + url = buildname + r = requests.get(url, auth = (username, password)) + build_data = json.loads(r.text) + for item in build_data["buildsNumbers"]: + inspectbuildjson(url,item["uri"]) + +def getallbuilds(): + #looks through all build projects + buildurl = "api/build" + url = artifactory + buildurl + r = requests.get(url, auth = (username, password)) + json_data = json.loads(r.text) + for item in json_data["builds"]: + findbuildnumbers(url + item["uri"]) + + +getallbuilds() \ No newline at end of file diff --git a/REST-API-Examples/RestoreTrashCan.rb b/REST-API-Examples/RestoreTrashCan.rb new file mode 100644 index 0000000..cbea160 --- /dev/null +++ b/REST-API-Examples/RestoreTrashCan.rb @@ -0,0 +1,28 @@ +require "rubygems" +require "rest_client" +require "json" +#This script restores every file inside the Trashcan +user_name = 'admin' #change this +password= 'password' #change this +repo = "auto-trashcan" +artifactory = "http://127.0.0.1:8081/artifactory/api/storage/" #change this +listfolders = "?list&deep=1&listFolders=1" +url = artifactory + repo + listfolders +site = RestClient::Resource.new(url, user_name, password) +response = site.get(:accept=>"application/json") +string = response.body +parsed = JSON.parse(string) +parsed.map + + +while true + i = 0 + trashedartifacts = parsed["files"][i]["uri"] + restoreurl = "http://127.0.0.1:8081/artifactory/api/trash/restore" + trashedartifacts + "?to=" + trashedartifacts #change my artifactory home + site2 = RestClient::Resource.new(restoreurl, user_name, password) + response2 = site2.post(:accept=>"application/json") + string2 = response2.body + puts string2 + i+=1 +end +