forked from diskoverdata/diskover-community
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskover_bot_module.py
executable file
·1154 lines (994 loc) · 40.3 KB
/
diskover_bot_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""diskover - Elasticsearch file system crawler
diskover is a file system crawler that index's
your file metadata into Elasticsearch.
See README.md or https://github.com/shirosaidev/diskover
for more information.
Copyright (C) Chris Park 2017-2018
diskover is released under the Apache 2.0 license. See
LICENSE for the full license text.
"""
from diskover import config, escape_chars, index_bulk_add, plugins, IS_PY3
from datetime import datetime
from scandir import scandir
import argparse
import os
import hashlib
import socket
import pwd
import grp
import time
import re
import base64
import warnings
import diskover_connections
# create Elasticsearch connection
diskover_connections.connect_to_elasticsearch()
from diskover_connections import es_conn as es
# create Reddis connection
diskover_connections.connect_to_redis()
from diskover_connections import redis_conn
# cache uid/gid names
uids = []
gids = []
owners = {}
groups = {}
def parse_cliargs_bot():
"""This is the parse CLI arguments function.
It parses command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--burst", action="store_true",
help="Burst mode (worker will quit after all work is done)")
parser.add_argument("-l", "--loglevel", default="INFO",
help="Set worker logging level to DEBUG, INFO, WARNING, ERROR (default is INFO)")
args = parser.parse_args()
return args
def get_worker_name():
"""This is the get worker name function.
It returns worker name hostname.pid .
"""
return '{0}.{1}'.format(socket.gethostname().partition('.')[0], os.getpid())
def auto_tag(metadict, tagtype, mtime, atime, ctime):
"""This is the auto tag function.
It checks diskover config for any auto tag patterns
and updates the meta dict for file or directory
to include the new tags.
"""
extpass = True
namepass = True
pathpass = True
timepass = True
if tagtype == 'file':
for pattern in config['autotag_files']:
try:
for name in pattern['name_exclude']:
if name == metadict['filename']:
return metadict
if name.startswith('*') and name.endswith('*'):
name = name.replace('*', '')
elif name.startswith('*'):
name = name + '$'
elif name.endswith('*'):
name = '^' + name
if re.search(name, metadict['filename']):
return metadict
except KeyError:
pass
try:
for path in pattern['path_exclude']:
if path == metadict['path_parent']:
return metadict
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, metadict['path_parent']):
return metadict
except KeyError:
pass
try:
for ext in pattern['ext']:
if ext == metadict['extension']:
extpass = True
break
if ext.startswith('*') and ext.endswith('*'):
ext = ext.replace('*', '')
elif ext.startswith('*'):
ext = ext + '$'
elif ext.endswith('*'):
ext = '^' + ext
if re.search(ext, metadict['extension']):
extpass = True
break
else:
extpass = False
except KeyError:
pass
try:
for name in pattern['name']:
if name == metadict['filename']:
namepass = True
break
if name.startswith('*') and name.endswith('*'):
name = name.replace('*', '')
elif name.startswith('*'):
name = name + '$'
elif name.endswith('*'):
name = '^' + name
if re.search(name, metadict['filename']):
namepass = True
break
else:
namepass = False
except KeyError:
pass
try:
for path in pattern['path']:
if path == metadict['path_parent']:
pathpass = True
break
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, metadict['path_parent']):
pathpass = True
break
else:
pathpass = False
except KeyError:
pass
timepass = time_check(pattern, mtime, atime, ctime)
if extpass and namepass and pathpass and timepass:
metadict['tag'] = pattern['tag']
metadict['tag_custom'] = pattern['tag_custom']
return metadict
elif tagtype == 'directory':
for pattern in config['autotag_dirs']:
try:
for name in pattern['name_exclude']:
if name == metadict['filename']:
return metadict
if name.startswith('*') and name.endswith('*'):
name = name.replace('*', '')
elif name.startswith('*'):
name = name + '$'
elif name.endswith('*'):
name = '^' + name
if re.search(name, metadict['filename']):
return metadict
except KeyError:
pass
try:
for path in pattern['path_exclude']:
if path == metadict['path_parent']:
return metadict
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, metadict['path_parent']):
return metadict
except KeyError:
pass
try:
for name in pattern['name']:
if name == metadict['filename']:
namepass = True
break
if name.startswith('*') and name.endswith('*'):
name = name.replace('*', '')
elif name.startswith('*'):
name = name + '$'
elif name.endswith('*'):
name = '^' + name
if re.search(name, metadict['filename']):
namepass = True
break
else:
namepass = False
except KeyError:
pass
try:
for path in pattern['path']:
if path == metadict['path_parent']:
pathpass = True
break
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, metadict['path_parent']):
pathpass = True
break
else:
pathpass = False
except KeyError:
pass
timepass = time_check(pattern, mtime, atime, ctime)
if extpass and namepass and pathpass and timepass:
metadict['tag'] = pattern['tag']
metadict['tag_custom'] = pattern['tag_custom']
return metadict
return metadict
def time_check(pattern, mtime, atime, ctime):
"""This is the time check function.
It is used by the auto_tag and cost_per_gb
functions.
"""
timepass = True
d = {'mtime': mtime, 'atime': atime, 'ctime': ctime}
for key, value in d.items():
try:
if pattern[key] > 0 and value:
# Convert time in days to seconds
time_sec = pattern[key] * 86400
file_time_sec = time.time() - value
if file_time_sec < time_sec:
timepass = False
break
except KeyError:
pass
return timepass
def cost_per_gb(metadict, fullpath, mtime, atime, ctime, doctype):
"""This is the cost per gb function.
It checks diskover config for any cost per gb patterns
and updates the meta dict for file or directory
to include the cost per gb.
"""
# by default set the costpergb to be cost per gb value from config
costpergb = config['costpergb']
# determine if we are using base2 or base10 file sizes
base = config['costpergb_base']
if base == 10:
basen = 1000
else:
basen = 1024
if doctype == 'file':
size_gb = metadict['filesize']/basen/basen/basen
metadict['costpergb'] = round(costpergb * size_gb, 2)
else: # directory
size_gb = metadict['doc']['filesize']/basen/basen/basen
metadict['doc']['costpergb'] = round(costpergb * size_gb, 2)
# if pattern lists are empty, return just cost per gb
if not config['costpergb_paths'] and not config['costpergb_times']:
return metadict
pathpass = False
timepass = False
costpergb_path = 0
costpergb_time = 0
filename = os.path.basename(fullpath)
parentdir = os.path.abspath(os.path.join(fullpath, os.pardir))
for pattern in config['costpergb_paths']:
if pathpass:
break
try:
for path in pattern['path_exclude']:
if path == parentdir or path == filename:
return metadict
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, parentdir) or re.search(path, filename):
return metadict
except KeyError:
pass
try:
for path in pattern['path']:
if path == parentdir or path == filename:
pathpass = True
costpergb_path = pattern['costpergb']
break
if path.startswith('*') and path.endswith('*'):
path = path.replace('*', '')
elif path.startswith('*'):
path = path + '$'
elif path.endswith('*'):
path = '^' + path
if re.search(path, parentdir) or re.search(path, filename):
pathpass = True
costpergb_path = pattern['costpergb']
break
else:
pathpass = False
except KeyError:
pass
for pattern in config['costpergb_times']:
timepass = time_check(pattern, mtime, atime, ctime)
if timepass:
costpergb_time = pattern['costpergb']
break
if pathpass and timepass:
if config['costpergb_priority'] == 'path':
if doctype == 'file':
metadict['costpergb'] = round(costpergb_path * size_gb, 2)
else:
metadict['doc']['costpergb'] = round(costpergb_path * size_gb, 2)
else:
if doctype == 'file':
metadict['costpergb'] = round(costpergb_time * size_gb, 2)
else:
metadict['doc']['costpergb'] = round(costpergb_time * size_gb, 2)
elif pathpass:
if doctype == 'file':
metadict['costpergb'] = round(costpergb_path * size_gb, 2)
else:
metadict['doc']['costpergb'] = round(costpergb_path * size_gb, 2)
elif timepass:
if doctype == 'file':
metadict['costpergb'] = round(costpergb_time * size_gb, 2)
else:
metadict['doc']['costpergb'] = round(costpergb_time * size_gb, 2)
return metadict
def get_owner_group_names(uid, gid, cliargs):
"""This is the get owner group name function.
It tries to get owner and group names and deals
with uid/gid -> name cacheing.
Returns owner and group.
"""
# try to get owner user name
# first check cache
if uid in uids:
owner = owners[uid]
# not in cache
else:
# check if we should just get uid or try to get owner name
if config['ownersgroups_uidgidonly'] == "true" or cliargs['crawlapi']:
owner = uid
else:
try:
# check if domain in name
if config['ownersgroups_domain'] == "true":
# check if we should remove the domain from owner
if config['ownersgroups_keepdomain'] == "true":
owner = pwd.getpwuid(uid).pw_name
else:
if config['ownersgroups_domainfirst'] == "true":
owner = pwd.getpwuid(uid).pw_name.split(config['ownersgroups_domainsep'])[1]
else:
owner = pwd.getpwuid(uid).pw_name.split(config['ownersgroups_domainsep'])[0]
else:
owner = pwd.getpwuid(uid).pw_name
# if we can't find the owner's user name, use the uid number
except KeyError:
owner = uid
# store it in cache
if not uid in uids:
uids.append(uid)
owners[uid] = owner
# try to get group name
# first check cache
if gid in gids:
group = groups[gid]
# not in cache
else:
# check if we should just get gid or try to get group name
if config['ownersgroups_uidgidonly'] == "true" or cliargs['crawlapi']:
group = gid
else:
try:
# check if domain in name
if config['ownersgroups_domain'] == "true":
# check if we should remove the domain from group
if config['ownersgroups_keepdomain'] == "true":
group = grp.getgrgid(gid).gr_name
else:
if config['ownersgroups_domainfirst'] == "true":
group = grp.getgrgid(gid).gr_name.split(config['ownersgroups_domainsep'])[1]
else:
group = grp.getgrgid(gid).gr_name.split(config['ownersgroups_domainsep'])[0]
else:
group = grp.getgrgid(gid).gr_name
# if we can't find the group's name, use the gid number
except KeyError:
group = gid
# store in cache
if not gid in gids:
gids.append(gid)
groups[gid] = group
return owner, group
def get_dir_meta(worker_name, path, cliargs, reindex_dict, statsembeded=False):
"""This is the get directory meta data function.
It gets directory metadata and returns dir meta dict.
It checks if meta data is in Redis and compares times
mtime and ctime on disk compared to Redis and if same
returns sametimes string.
"""
try:
if statsembeded:
metadata = path[1]
dirpath = path[0]
# get directory meta embeded in path
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = metadata
else:
# get directory meta using lstat
dirpath = path
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = os.lstat(dirpath)
# convert times to utc for es
mtime_utc = datetime.utcfromtimestamp(mtime).isoformat()
atime_utc = datetime.utcfromtimestamp(atime).isoformat()
ctime_utc = datetime.utcfromtimestamp(ctime).isoformat()
if cliargs['index2']:
# check if directory times cached in Redis
redis_dirtime = redis_conn.get(base64.encodestring(dirpath.encode('utf-8', errors='ignore')))
if redis_dirtime:
cached_times = float(redis_dirtime.decode('utf-8'))
# check if cached times are the same as on disk
current_times = float(mtime + ctime)
if cached_times == current_times:
return "sametimes"
# get time now in utc
indextime_utc = datetime.utcnow().isoformat()
# get owner and group names
owner, group = get_owner_group_names(uid, gid, cliargs)
filename = os.path.basename(dirpath)
parentdir = os.path.abspath(os.path.join(dirpath, os.pardir))
dirmeta_dict = {
"filename": filename,
"path_parent": parentdir,
"filesize": 0,
"items": 1, # 1 for itself
"items_files": 0,
"items_subdirs": 0,
"last_modified": mtime_utc,
"last_access": atime_utc,
"last_change": ctime_utc,
"hardlinks": nlink,
"inode": str(ino),
"owner": owner,
"group": group,
"tag": "",
"tag_custom": "",
"crawl_time": 0,
"change_percent_filesize": "",
"change_percent_items": "",
"change_percent_items_files": "",
"change_percent_items_subdirs": "",
"costpergb": "",
"worker_name": worker_name,
"indexing_date": indextime_utc,
"_type": "directory"
}
# check plugins for adding extra meta data to dirmeta_dict
for plugin in plugins:
try:
# check if plugin is for directory doc
mappings = {'mappings': {'directory': {'properties': {}}}}
plugin.add_mappings(mappings)
dirmeta_dict.update(plugin.add_meta(dirpath))
except KeyError:
pass
# add any autotags to dirmeta_dict
if cliargs['autotag'] and len(config['autotag_dirs']) > 0:
dirmeta_dict = auto_tag(dirmeta_dict, 'directory', mtime, atime, ctime)
# search for and copy over any existing tags from reindex_dict
for sublist in reindex_dict['directory']:
if sublist[0] == dirpath:
dirmeta_dict['tag'] = sublist[1]
dirmeta_dict['tag_custom'] = sublist[2]
break
except (OSError, IOError) as e:
warnings.warn("OS/IO Exception caused by: %s" % e)
return False
except Exception as e:
warnings.warn("Exception caused by: %s" % e)
raise
# cache directory times in Redis, encode path (key) using base64
if config['redis_cachedirtimes'] == 'true':
redis_conn.set(base64.encodestring(dirpath.encode('utf-8', errors='ignore')), mtime + ctime,
ex=config['redis_dirtimesttl'])
return dirmeta_dict
def get_file_meta(worker_name, path, cliargs, reindex_dict, statsembeded=False):
"""This is the get file meta data function.
It scrapes file meta and ignores files smaller
than minsize Bytes, newer than mtime
and in excluded_files. Returns file meta dict.
"""
try:
# check if stats embeded in path
if statsembeded:
metadata = path[1]
fullpath = path[0]
else:
fullpath = path
filename = os.path.basename(fullpath)
# check if file is in exluded_files list
if file_excluded(filename):
return None
extension = os.path.splitext(filename)[1][1:].lower()
if statsembeded:
# get embeded stats from path
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime, blocks = metadata
else:
# use lstat to get meta and not follow sym links
s = os.lstat(fullpath)
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = s
blocks = s.st_blocks
# Are we storing file size or on disk size
if cliargs['sizeondisk']:
size = blocks * cliargs['blocksize']
# Skip files smaller than minsize cli flag
if size < cliargs['minsize']:
return None
# Convert time in days (mtime cli arg) to seconds
time_sec = cliargs['mtime'] * 86400
file_mtime_sec = time.time() - mtime
if time_sec < 0:
# Only process files modified less than x days ago
if file_mtime_sec > (time_sec * -1):
return None
else:
# Only process files modified at least x days ago
if file_mtime_sec < time_sec:
return None
# convert times to utc for es
mtime_utc = datetime.utcfromtimestamp(mtime).isoformat()
atime_utc = datetime.utcfromtimestamp(atime).isoformat()
ctime_utc = datetime.utcfromtimestamp(ctime).isoformat()
# get owner and group names
owner, group = get_owner_group_names(uid, gid, cliargs)
# create md5 hash of file using metadata filesize and mtime
filestring = str(size) + str(mtime)
filehash = hashlib.md5(filestring.encode('utf-8')).hexdigest()
# get time
indextime_utc = datetime.utcnow().isoformat()
# get absolute path of parent directory
parentdir = os.path.abspath(os.path.join(fullpath, os.pardir))
# create file metadata dictionary
filemeta_dict = {
"filename": filename,
"extension": extension,
"path_parent": parentdir,
"filesize": size,
"owner": owner,
"group": group,
"last_modified": mtime_utc,
"last_access": atime_utc,
"last_change": ctime_utc,
"hardlinks": nlink,
"inode": str(ino),
"filehash": filehash,
"tag": "",
"tag_custom": "",
"dupe_md5": "",
"worker_name": worker_name,
"indexing_date": indextime_utc,
"_type": "file"
}
# check plugins for adding extra meta data to filemeta_dict
for plugin in plugins:
try:
# check if plugin is for file doc
mappings = {'mappings': {'file': {'properties': {}}}}
plugin.add_mappings(mappings)
filemeta_dict.update(plugin.add_meta(fullpath))
except KeyError:
pass
# add any autotags to filemeta_dict
if cliargs['autotag'] and len(config['autotag_files']) > 0:
filemeta_dict = auto_tag(filemeta_dict, 'file', mtime, atime, ctime)
# add cost per gb to filemeta_dict
if cliargs['costpergb']:
filemeta_dict = cost_per_gb(filemeta_dict, fullpath, mtime, atime, ctime, 'file')
# search for and copy over any existing tags from reindex_dict
for sublist in reindex_dict['file']:
if sublist[0] == fullpath:
filemeta_dict['tag'] = sublist[1]
filemeta_dict['tag_custom'] = sublist[2]
break
except (OSError, IOError) as e:
warnings.warn("OS/IO Exception caused by: %s" % e)
return False
except Exception as e:
warnings.warn("Exception caused by: %s" % e)
return False
return filemeta_dict
def calc_dir_size(dirlist, cliargs):
"""This is the calculate directory size worker function.
It gets a directory list from the Queue search ES for all
files in each directory (recursive) and sums their filesizes
to create a total filesize and item count for each dir,
then pdates dir doc's filesize and items fields.
"""
doclist = []
for path in dirlist:
totalsize = 0
totalitems = 1 # 1 for itself
totalitems_files = 0
totalitems_subdirs = 0
# file doc search with aggregate for sum filesizes
# escape special characters
newpath = escape_chars(path[1])
# create wildcard string and check for / (root) path
if newpath == '\/':
newpathwildcard = '\/*'
else:
newpathwildcard = newpath + '\/*'
# check if / (root) path
if newpath == '\/':
data = {
"size": 0,
"query": {
"query_string": {
"query": "path_parent: " + newpath + "*",
"analyze_wildcard": "true"
}
},
"aggs": {
"total_size": {
"sum": {
"field": "filesize"
}
}
}
}
else:
data = {
"size": 0,
"query": {
"query_string": {
'query': 'path_parent: ' + newpath + ' OR path_parent: ' + newpathwildcard,
'analyze_wildcard': 'true'
}
},
"aggs": {
"total_size": {
"sum": {
"field": "filesize"
}
}
}
}
# search ES and start scroll
res = es.search(index=cliargs['index'], doc_type='file', body=data,
request_timeout=config['es_timeout'])
# total items sum
totalitems_files += res['hits']['total']
# total file size sum
totalsize += res['aggregations']['total_size']['value']
# directory doc search (subdirs)
# check if / (root) path
if newpath == '\/':
data = {
"size": 0,
"query": {
"query_string": {
"query": "path_parent: " + newpath + "*",
"analyze_wildcard": "true"
}
}
}
else:
data = {
"size": 0,
"query": {
"query_string": {
'query': 'path_parent: ' + newpath + ' OR path_parent: ' + newpathwildcard,
'analyze_wildcard': 'true'
}
}
}
# search ES and start scroll
res = es.search(index=cliargs['index'], doc_type='directory', body=data,
request_timeout=config['es_timeout'])
# total items sum
totalitems_subdirs += res['hits']['total']
# total items
totalitems += totalitems_files + totalitems_subdirs
# update filesize and items fields for directory (path) doc
d = {
'_op_type': 'update',
'_index': cliargs['index'],
'_type': 'directory',
'_id': path[0],
'doc': {'filesize': totalsize, 'items': totalitems,
'items_files': totalitems_files,
'items_subdirs': totalitems_subdirs}
}
# add total cost per gb to doc
if cliargs['costpergb']:
d = cost_per_gb(d, path[1], path[2], path[3], path[4], 'directory')
doclist.append(d)
index_bulk_add(es, doclist, config, cliargs)
def es_bulk_add(worker_name, dirlist, filelist, cliargs, totalcrawltime=None):
starttime = time.time()
docs = dirlist + filelist
index_bulk_add(es, docs, config, cliargs)
data = {"worker_name": worker_name, "dir_count": len(dirlist),
"file_count": len(filelist), "bulk_time": round(time.time() - starttime, 6),
"crawl_time": round(totalcrawltime, 6),
"indexing_date": datetime.utcnow().isoformat()}
es.index(index=cliargs['index'], doc_type='worker', body=data)
def get_metadata(path, cliargs):
dir_source = ""
filename = escape_chars(os.path.basename(path))
parent_dir = escape_chars(os.path.abspath(os.path.join(path, os.pardir)))
fullpath = escape_chars(os.path.abspath(path))
data = {
"size": 1,
"query": {
"query_string": {
"query": "filename: " + filename + " AND path_parent: " + parent_dir
}
}
}
res = es.search(index=cliargs['index2'], doc_type='directory', body=data,
request_timeout=config['es_timeout'])
try:
dir_source = res['hits']['hits'][0]['_source']
except IndexError:
pass
data = {
"query": {
"query_string": {
"query": "path_parent: " + fullpath
}
}
}
files_source = []
res = es.search(index=cliargs['index2'], doc_type='file', scroll='1m',
size=config['es_scrollsize'], body=data, request_timeout=config['es_timeout'])
while res['hits']['hits'] and len(res['hits']['hits']) > 0:
for hit in res['hits']['hits']:
files_source.append(hit['_source'])
# get es scroll id
scroll_id = res['_scroll_id']
# use es scroll api
res = es.scroll(scroll_id=scroll_id, scroll='1m',
request_timeout=config['es_timeout'])
return dir_source, files_source
def scrape_tree_meta(paths, cliargs, reindex_dict):
global worker
tree_dirs = []
tree_files = []
totalcrawltime = 0
statsembeded = False
path_count = 0
for path in paths:
path_count += 1
starttime = time.time()
if not cliargs['dirsonly']:
root, dirs, files = path
else:
root, dirs = path
files = []
if path_count == 1:
if type(root) is tuple:
statsembeded = True
# check if stats embeded in data from diskover tree walk client or crawlapi
if statsembeded:
root_path = root[0]
dmeta = get_dir_meta(worker, root, cliargs, reindex_dict, statsembeded=True)
else:
root_path = root
dmeta = get_dir_meta(worker, root_path, cliargs, reindex_dict, statsembeded=False)
if dmeta == "sametimes":
# fetch meta data for directory and all it's files (doc sources) from index2 since
# directory times haven't changed
dir_source, files_source = get_metadata(root_path, cliargs)
datenow = datetime.utcnow().isoformat()
for file_source in files_source:
# update indexed at time
file_source['indexing_date'] = datenow
# update worker name
file_source['worker_name'] = worker
tree_files.append(('file', file_source))
if dir_source:
# update indexed at time
dir_source['indexing_date'] = datenow
# update worker name
dir_source['worker_name'] = worker
# update crawl time
elapsed = time.time() - starttime
dir_source['crawl_time'] = round(elapsed, 6)
tree_dirs.append(dir_source)
totalcrawltime += elapsed
# get meta off disk since times different in Redis than on disk
elif dmeta:
# no files in batch, get them with scandir
if cliargs['dirsonly']:
for entry in scandir(root):
if entry.is_file(follow_symlinks=False) and not file_excluded(entry.name):
files.append(entry.name)
filecount = 0
for file in files:
if statsembeded:
fmeta = get_file_meta(worker, file, cliargs, reindex_dict, statsembeded=True)
else:
fmeta = get_file_meta(worker, os.path.join(root_path, file), cliargs,
reindex_dict, statsembeded=False)
if fmeta:
tree_files.append(fmeta)
filecount += 1
# update crawl time
elapsed = time.time() - starttime
dmeta['crawl_time'] = round(elapsed, 6)
# check for empty dirs and dirsonly cli arg
if cliargs['indexemptydirs']:
tree_dirs.append(dmeta)
elif not cliargs['indexemptydirs'] and (len(dirs) > 0 or filecount > 0):
tree_dirs.append(dmeta)
totalcrawltime += elapsed
# check if doc count is more than es chunksize and bulk add to es
if len(tree_dirs) + len(tree_files) >= config['es_chunksize']:
es_bulk_add(worker, tree_dirs, tree_files, cliargs, totalcrawltime)
del tree_dirs[:]
del tree_files[:]
totalcrawltime = 0
# bulk add to es
if len(tree_dirs) > 0 or len(tree_files) > 0:
es_bulk_add(worker, tree_dirs, tree_files, cliargs, totalcrawltime)
def file_excluded(filename):
"""Return True if path or ext in excluded_files set,
False if not in the set"""
# return if filename in included list (whitelist)
if filename in config['included_files']:
return False
# check for filename in excluded_files set
if filename in config['excluded_files']:
return True
# check for extension in and . (dot) files in excluded_files
extension = os.path.splitext(filename)[1][1:].lower()
if (not extension and 'NULLEXT' in config['excluded_files']) or \
'*.' + extension in config['excluded_files'] or \
(filename.startswith('.') and u'.*' in config['excluded_files']):
return True
return False
def dupes_process_hashkey(hashkey, cliargs):
"""This is the duplicate file worker function.
It processes hash keys in the dupes Queue.
"""
from diskover_dupes import populate_hashgroup, verify_dupes, index_dupes
# find all files in ES matching hashkey
hashgroup = populate_hashgroup(hashkey, cliargs)
# process the duplicate files in hashgroup
hashgroup = verify_dupes(hashgroup, cliargs)
if hashgroup:
index_dupes(hashgroup, cliargs)