summaryrefslogtreecommitdiffstats
path: root/tests/elasticsearch-error-format-check.py
blob: f8f091d6cdc624fb72d0306ee79cef412f0618f5 (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
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
import json
import sys
import os
def checkDefaultErrorFile():
    with open(os.environ['RSYSLOG_DYNNAME'] + ".errorfile") as json_file:
        json_data = json.load(json_file)
        indexCount =0
        replyCount=0
        for item in json_data:
            if item == "request":
                for reqItem in json_data[item]:
                    if reqItem == "url":
                        print("url found")
                        print(reqItem)
                    elif reqItem == "postdata":
                        print("postdata found")
                        indexCount = str(json_data[item]).count('\"_index\":')
                        print(reqItem)
                    else:
                        print(reqItem)
                        print("Unknown item found")
                        sys.exit(1)
              
            elif item == "reply":
                for replyItem in json_data[item]:
                    if replyItem == "items":
                        print(json_data[item][replyItem])
                        replyCount = str(json_data[item][replyItem]).count('_index')
                    elif replyItem == "errors":
                        print("error node found")
                    elif replyItem == "took":
                        print("took node found")
                    else:
                        print(replyItem)
                        print("Unknown item found")
                        sys.exit(3)

            else:
                print(item)
                print("Unknown item found")
                print("error")
                sys.exit(4)
    if replyCount == indexCount :
        return 0
    else:
        sys.exit(7)
    return 0


def checkErrorOnlyFile():
    with open(os.environ['RSYSLOG_DYNNAME'] + ".errorfile") as json_file:
        json_data = json.load(json_file)
        indexCount =0
        replyCount=0
        for item in json_data:
            if item == "request":
                print(json_data[item])
                indexCount = str(json_data[item]).count('\"_index\":')
              
              
            elif item == "url":
                print("url found")
              
          
            elif item == "reply":
                print(json_data[item])
                replyCount = str(json_data[item]).count('\"_index\":')

            else:
                print(item)
                print("Unknown item found")
                print("error")
                sys.exit(4)
    if replyCount == indexCount :
        return 0
    else:
        sys.exit(7)
    return 0

def checkErrorInterleaved():
    with open(os.environ['RSYSLOG_DYNNAME'] + ".errorfile") as json_file:
        json_data = json.load(json_file)
        indexCount =0
        replyCount=0
        for item in json_data:
            print(item)
            if item == "response":
                for responseItem in json_data[item]:
                    print(responseItem)
                    for res in responseItem:
                        print(res)
                        if res == "request":
                            print(responseItem[res])
                            indexCount = str(responseItem[res]).count('\"_index\":')
                            print("request count ", indexCount)
                        elif res == "reply":
                            print(responseItem[res])
                            replyCount = str(responseItem[res]).count('\"_index\":')
                            print("reply count ", replyCount)
                        else:
                            print(res)
                            print("Unknown item found")
                            sys.exit(9)
                    if replyCount != indexCount :
                        sys.exit(8)
          
              
              
            elif item == "url":
                print("url found")
              
          
         

            else:
                print(item)
                print("Unknown item found")
                sys.exit(4)

    return 0

def checkInterleaved():
    return checkErrorInterleaved()

if __name__ == "__main__":
    option = sys.argv[1]
    if option == "default":
        checkDefaultErrorFile()
    elif option == "erroronly":
        checkErrorOnlyFile()
    elif option == "errorinterleaved":
        checkErrorInterleaved()
    elif option == "interleaved":
        checkErrorInterleaved()
    else:
        print("Usage: <script> <default|erroronly|errorinterleaved>")
        sys.exit(6)