summaryrefslogtreecommitdiff
blob: c452e81d5b4bf62291f2759618c93eceff86d9e3 (plain)
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
#!/usr/bin/python

#
# Convert a mirmon.state file to json
#
# This script will parse a mirmon state file, convert the content
# to json and print the json to stdout.
#
# Usage:
#   $ generate-json.py path_to_mirmon_state_file
#
# The json will be printed to stdout. In case of an error, the
# exception will be printed to stderr.
#

import json
import sys
import time

try:
    with open(str(sys.argv[1])) as file_in:
        mirrors = {}
        for raw_line in file_in:

            line = raw_line.split(" ")
            protocol = line[0].split("://")[0]
            host = line[0].split("://")[1]
            host = host.split("/")[0]

            state = {
                "Protocol" : protocol,
                "Host" : host,
                "Url" : line[0],
                "Age" : line[1],
                "StatusLastProbe" : line[2],
                "TimeLastSuccessfulProbe" : line[3],
                "ProbeHistory" : line[4],
                "StateHistory" : line[5],
                "LastProbe" : line[6].strip(),
            }

            if host not in mirrors:
                mirrors[host] = []

            mirrors[host].append(state)

        data = {}
        data["LastUpdate"] = int(time.time())
        data["Mirrors"] = mirrors
        print(json.dumps(data))

except Exception as e:
    print("Could not convert mirmon state file to json: " + str(e), file=sys.stderr)