__main__.py 2.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
def main():
    import logging
    logging.basicConfig()
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    logging.getLogger('urllib3').setLevel(logging.CRITICAL)
    logging.getLogger('paramiko').setLevel(logging.CRITICAL)


    import argparse
    import yaml
    parser = argparse.ArgumentParser()
Chris Hines's avatar
Chris Hines committed
13
14
    parser.add_argument('--config', default='./config.yml')
    parser.add_argument('--check', type=bool, default=False)
15
16
17
18
19
20
21
22
    args = parser.parse_args()
    

    from . import statuspagewrapper
    from . import influxdbwrapper
    from .influxdbwrapper import Client as influxWrapperClient
    from . import component_tests
    from .status import Status
23
24
25
26
    
    import datetime
    global ts
    ts = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
27
28
29
30
31
32
33
34

    with open(args.config) as f:
        config = yaml.safe_load(f.read())

    influxClient = influxdbwrapper.Client.configure(config['influx'])
    spClient = statuspagewrapper.Client.configure(config['statuspageio'])

    spcomponents = {}
Chris Hines's avatar
Chris Hines committed
35
36
    for c in spClient.components.list():
        spcomponents[c['name']] = c
Chris Hines's avatar
Chris Hines committed
37
        logger.debug("found component {}".format(c['name']))
38
    
39
40
41
    for name,c in config['components'].items():
        msgs = []
        overall = Status.operational
Chris Hines's avatar
Chris Hines committed
42
43
44
        if name not in spcomponents:
            logger.error("Can't find component {}".format(name))
        if name in spcomponents and spcomponents[name]['status'] == 'under_maintenance':
45
46
47
48
            continue
        for t in c['tests']:
            testclass = component_tests.getTestClass(classname=t['class'],influxClient=influxClient,**t)
            result = testclass.test()
Chris Hines's avatar
Chris Hines committed
49
            logger.debug(result)
50
51
52
            overall = max(overall,result['status'])
            if result['status'] != Status.operational:
                msgs.append(result['msg'])
Chris Hines's avatar
Chris Hines committed
53
            if name in spcomponents:
54
                update_component(spClient,spcomponents[name],overall,"\n".join(msgs),check=args.check,description=c['desc'])
55
56
            #update_incident(spclient,spcomponents[name],overall,"\n".join(msgs),open_incidents)

57
def update_component(spclient,component,status,msg,check,description):
58
59
60
61
62
63
64
65
66
67
    from .status import Status

    if status is Status.operational:
        statusstr = 'operational'
    if status is Status.partial_outage:
        statusstr = 'partial_outage'
    if status is Status.major_outage:
        statusstr = 'major_outage'
    if status is Status.under_maintenance:
        statusstr = 'under_maintenance'
Chris Hines's avatar
Chris Hines committed
68
69
    if not check:
        if statusstr != component['status']:
70
            spclient.components.update(component.id, status = statusstr, description = "{}. Updated {}".format(description, ts))
Chris Hines's avatar
Chris Hines committed
71
72
73
74
75
76
    else:
        if statusstr == component['status']:
            print("no change {} {} {}".format(component['name'],component['status'], statusstr))
        else:
            print("change {} {} {}".format(component['name'],component['status'], statusstr))

77
78
79
80


if __name__ == '__main__':
    main()