|
3 | 3 | ts3LogAnalyzer.py |
4 | 4 |
|
5 | 5 | Usage: |
6 | | - ts3LogAnalyzer.py <log> |
7 | | - ts3LogAnalyzer.py <log> [-d <databse>] [--hide-ip] [--debug] [--output-logging] |
| 6 | + ts3LogAnalyzer.py <database> -a <path> [--stats] [--hide-ip] [--debug] [--output-logging] |
| 7 | + ts3LogAnalyzer.py <database> (--merge <c1> <c2> | --iterative-merge) [--debug] [--output-logging] |
8 | 8 | ts3LogAnalyzer.py -h | --help |
9 | 9 | ts3LogAnalyzer.py -v | --version |
10 | 10 |
|
11 | 11 | Options: |
12 | | - -d --database <databse> Database to use or create (database.db used by default) |
13 | | - --hide-ip Don't save ips |
14 | | - --output-logging Output logging from THIS program to ts3LogAnalyzer.log |
15 | | - --debug Output debug information |
16 | | - -h --help Show this screen |
17 | | - -v --version Show version |
| 12 | + -a --analyze <path> Log file or folder to analyze |
| 13 | + -s --stats Pre-populate statistic fields in client and user |
| 14 | + --hide-ip Don't save ips |
| 15 | + --iterative-merge Auto detect merging candidates and ask to merge them |
| 16 | + --output-logging Output logging from THIS program to ts3LogAnalyzer.log |
| 17 | + --debug Output debug information |
| 18 | + -h --help Show this screen |
| 19 | + -v --version Show version |
18 | 20 | """ |
19 | 21 |
|
20 | 22 | __author__ = 'ToFran' |
@@ -50,36 +52,50 @@ def main(): |
50 | 52 | handlers = [logging.FileHandler( "ts3LogAnalyzer.log", 'w', 'utf-8')] if arguments['--output-logging'] else None \ |
51 | 53 | ) |
52 | 54 |
|
| 55 | + database = arguments['<database>'] |
| 56 | + logpath = arguments['--analyze'] |
53 | 57 | hideIp = arguments['--hide-ip'] |
54 | | - database = 'database.db' |
55 | | - if arguments['--database']: |
56 | | - database = arguments['--database'] |
| 58 | + client_id_1 = arguments['<c1>'] |
| 59 | + client_id_2 = arguments['<c2>'] |
57 | 60 |
|
58 | | - #check if databse exits |
| 61 | + import time |
| 62 | + start_time = time.time() |
| 63 | + |
| 64 | + #database |
59 | 65 | exists = os.path.exists(database) |
60 | 66 | db = sqlite3.connect(database) |
61 | 67 | if not exists: |
62 | | - setupDB() |
| 68 | + setupDB(arguments['--stats']) |
63 | 69 | logging.info(database + ' created!') |
64 | | - elif not arguments['--database']: |
65 | | - logging.warning("-d not specified, using existing " + database) |
66 | 70 |
|
67 | | - #log files |
68 | | - path = arguments['<log>'] |
| 71 | + if logpath: |
| 72 | + analyze(logpath) |
| 73 | + elif client_id_1 and client_id_2: |
| 74 | + try: |
| 75 | + client_id_1 = int(client_id_1) |
| 76 | + client_id_2 = int(client_id_2) |
| 77 | + except Exception as e: |
| 78 | + logging.critical("Invalid merge input!") |
| 79 | + sys.exit() |
| 80 | + |
| 81 | + mergeClients(client_id_1, client_id_2) |
| 82 | + |
| 83 | + db.commit() |
| 84 | + db.close() |
| 85 | + print("--- %s seconds ---" % (time.time() - start_time)) |
| 86 | + |
| 87 | +def analyze(path): |
69 | 88 | if os.path.isdir(path): |
70 | 89 | logging.debug(path + " is a folder.") |
71 | | - for f in glob.glob(arguments['<log>'] + '/*.log'): |
| 90 | + for f in glob.glob(path + '/*.log'): |
72 | 91 | analyseFile(f) |
73 | 92 | elif os.path.isfile(path): |
74 | 93 | logging.debug(path + " is a file.") |
75 | 94 | analyseFile(path) |
76 | 95 | else: |
77 | | - logging.critical(path + " does not exist! Terminating...") |
| 96 | + logging.critical(logpath + " does not exist! Terminating...") |
78 | 97 | sys.exit() |
79 | 98 |
|
80 | | - db.commit() |
81 | | - db.close() |
82 | | - |
83 | 99 | def analyseFile(filepath): |
84 | 100 | #check if logfile already analyzed |
85 | 101 | savedLog = getLog(filepath) |
@@ -224,7 +240,7 @@ def clientDisconnected(when, id, reason, logId, nickname = None): |
224 | 240 | ################# |
225 | 241 | #DATABSE |
226 | 242 |
|
227 | | -def setupDB(statsTriggers = False): |
| 243 | +def setupDB(statsTriggers = True): |
228 | 244 | with open("schema.sql", 'r') as f: |
229 | 245 | schema = f.read() |
230 | 246 | cur = db.cursor() |
@@ -320,12 +336,24 @@ def mergeClients(client_id_1, client_id_2): |
320 | 336 | return False |
321 | 337 | elif(user1): |
322 | 338 | setUser(client_id_2, user1) |
323 | | - else: |
| 339 | + elif(user2): |
324 | 340 | setUser(client_id_1, user2) |
| 341 | + else: |
| 342 | + #TODO |
| 343 | + logging.error("Not implemented!") |
| 344 | + return False |
| 345 | + logging.info("Client " + client_id_1 + " and " + client_id_2 + " merged.") |
325 | 346 | return True |
326 | 347 |
|
327 | 348 | def iterativeMerge(): |
328 | | - |
| 349 | + #Get nicknames used multiple times by multiple identeties |
| 350 | + timesUsed = 200 |
| 351 | + c_nick = db.cursor() |
| 352 | + c_nick.execute("SELECT nickname from nickname GROUP BY nickname HAVING COUNT(client) >= 2 AND SUM(used) > ? ORDER BY SUM(used) DESC", [timesUsed]) |
| 353 | + for nickname in c_nick: |
| 354 | + #c_cli = db.cursor() |
| 355 | + #c_nick.execute("SELECT client from nickname WHERE nickname = ", [timesUsed]) |
| 356 | + print (nickname) |
329 | 357 | return |
330 | 358 |
|
331 | 359 | # ---- |
|
0 commit comments