-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth-checker.py
77 lines (66 loc) · 2.59 KB
/
health-checker.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
import csv
import argparse
from time import sleep
from bs4 import BeautifulSoup
from os.path import isfile
from config import Config
from resp import Response
from requests import get
from json import loads, dumps
def argument_parser():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--config', help='config file for health check.')
group.add_argument('--generate', dest='generate',
action='store_true', help='generate example config file.')
parser.add_argument('--force', dest='force',
action='store_true', help='force operation.')
args = parser.parse_args()
if args.generate == True:
exist = isfile('./config.json')
if exist and args.force != True:
print('config.json file is exist. use --force argument for overwrite.')
return
generate_example_config()
print('example config.json file generated.')
elif args.config:
config = Config(
**loads(open(args.config, 'r', encoding='utf-8').read()))
resps = process_config(config)
output(config, resps)
def output(config, resps):
if config.output == None:
return
if config.output == 'json':
file = open(f'logs.json', 'w', encoding='utf-8')
serialized_resps = [resp.__dict__ for resp in resps]
file.write(dumps(serialized_resps, indent=4))
elif config.output == 'csv':
file = csv.writer(open('logs.csv', 'w', encoding='utf-8'),)
file.writerow(['url', 'status_code', 'elapsed'])
for resp in resps:
file.writerow([resp.url, resp.status_code, resp.elapsed])
def process_config(config):
resp = get(config.url)
soup = BeautifulSoup(resp.content, 'html.parser')
urls = soup.find_all('loc')
if config.debug:
print(f'{len(urls)} url will be tested.')
resps = []
for url in urls:
if config.delay > 0:
sleep(config.delay)
resp = get(url.text)
innerSoup = BeautifulSoup(resp.content, 'html.parser')
if len(innerSoup.find_all('loc')) > 0:
config.url = url.text
process_config(config)
if config.debug:
print(url.text, resp.status_code, resp.elapsed)
resps.append(Response(resp.url, resp.status_code, str(resp.elapsed)))
return resps
def generate_example_config():
config = Config('http://www.mysite.com/sitemap.xml')
json = open('./config.json', 'w', encoding='utf-8')
json.writelines(dumps(config.__dict__, indent=4, sort_keys=True))
argument_parser()