-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzeDNS.py
96 lines (61 loc) · 1.93 KB
/
analyzeDNS.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
#!/usr/bin/python3
import boto3
import datetime
#from subprocess import call
import os
debug_on = True
today = datetime.date.today()
bucket_name = 'halimer-dns-analytics'
log_folder_name = '/var/log/'
tmp_dir = '/tmp/'
prefix = 'd=' + str(today) + '/'
pihole_log_file = 'pihole.log'
clean_log_name = 'clean_dns.log'
#print clean_log_name
def debug(title,item):
if(debug_on):
print(title)
print(item)
return
def get_private_words():
with open('donotanalyze.txt') as private_txt:
private_txt.seek(0)
private_raw = private_txt.read()
print(private_raw)
split_word_list = private_raw.split('\n')
private_word_list =[]
for word in split_word_list:
if word != '':
private_word_list.append(word)
return private_word_list
def remove_private_words():
words_to_scrub = get_private_words()
current_log_file=pihole_log_file
number_of_words = len(words_to_scrub)
debug("Number of Blacklist Words",number_of_words)
word_counter = 0
grep_string="egrep -v '"
for word in words_to_scrub:
grep_string+=word
word_counter += 1
if(word_counter < len(words_to_scrub)):
grep_string+='|'
grep_string += "' " + log_folder_name + current_log_file +" > " + tmp_dir + clean_log_name
print(grep_string)
os.system(grep_string)
def delete_old_log_files():
os.system('pihole -f')
os.system('rm ' + tmp_dir + clean_log_name)
def upload_log_to_s3():
client = boto3.client('s3')
s3 = boto3.resource('s3')
response = s3.meta.client.upload_file(tmp_dir + clean_log_name,bucket_name, prefix + clean_log_name)
debug("Response is :", response)
words = get_private_words()
debug("Words",words)
number_of_words = len(words)
debug("Number of Blacklist Words",number_of_words)
debug("Prefix is: ", prefix)
remove_private_words()
upload_log_to_s3()
delete_old_log_files()