summaryrefslogtreecommitdiffstats
path: root/Src/PyCatcher/src/rules.py
blob: 447c7b51b76d6a83faee7c81421ce5ac3d9a7e19 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from settings import Provider_list, Provider_Country_list, LAC_mapping, ARFCN_mapping, LAC_threshold, DB_RX_threshold, \
    CH_RX_threshold, Pagings_per_10s_threshold, Assignment_limit, Neighbours_threshold
from cellIDDatabase import CellIDDBStatus
import math

class RuleResult:
    OK = 'Ok'
    WARNING = 'Warning'
    CRITICAL = 'Critical'
    IGNORE = 'Ignore'

class Rule:
    is_active = False
    identifier = 'Rule'

    def check(self, arfcn, base_station_list):
        return RuleResult.CRITICAL

    def _extract_neighbours(self, arfcn, base_station_list):
        for item in base_station_list:
            if item.arfcn == arfcn:
                return item.neighbours

    def _extract_provider(self, arfcn, base_station_list):
        for item in base_station_list:
            if item.arfcn == arfcn:
                return item.provider

class ProviderRule (Rule):
    identifier = 'Provider Check'

    def check(self, arfcn, base_station_list):
        result = RuleResult.CRITICAL

        for station in base_station_list:
            if station.arfcn == arfcn:
                if station.provider in Provider_list:
                    result = RuleResult.OK
                    break
        return result

class CountryMappingRule (Rule):
    identifier = 'Country Provider Mapping'

    def check(self, arfcn, base_station_list):
        result = RuleResult.OK

        for station in base_station_list:
            if station.arfcn == arfcn:
                if station.provider in Provider_Country_list:
                    if station.country != Provider_Country_list[station.provider]:
                        result = RuleResult.CRITICAL
                else:
                    result = RuleResult.CRITICAL
        return result

class ARFCNMappingRule (Rule):
    identifier = 'ARFCN Mapping'

    def check(self, arfcn, base_station_list):
        result = RuleResult.CRITICAL

        for station in base_station_list:
            if station.arfcn == arfcn:
                if station.provider in ARFCN_mapping:
                    for lower,upper in ARFCN_mapping[station.provider]:
                        if lower <= station.arfcn <= upper:
                            result = RuleResult.OK
                            break
        return result

class LACMappingRule (Rule):
    identifier = 'LAC Mapping'

    def check(self, arfcn, base_station_list):
        result = RuleResult.CRITICAL

        for station in base_station_list:
            if station.arfcn == arfcn:
                if station.provider in LAC_mapping:
                    for lac in LAC_mapping[station.provider]:
                        if station.lac == lac:
                            result = RuleResult.OK
                            break
        return result

class UniqueCellIDRule (Rule):
    identifier = 'Unique CellID'

    def check(self, arfcn, base_station_list):
        result = RuleResult.OK
        cell_id = 0

        for station in base_station_list:
            if station.arfcn == arfcn:
                cell_id = station.cell

        for station in base_station_list:
            if station.arfcn != arfcn:
                if station.cell == cell_id:
                    result = RuleResult.CRITICAL
        return result

class LACMedianRule (Rule):
    identifier = 'LAC Median Deviation'

    def check(self, arfcn, base_station_list):
        lac_median_list = []
        provider = self._extract_provider(arfcn, base_station_list)
        lac_to_test = 0

        for item in base_station_list:
            if arfcn == item.arfcn:
                lac_to_test = item.lac
            if provider == item.provider:
                lac_median_list.append(item.lac)

        if len(lac_median_list) < 2:
            return RuleResult.IGNORE

        lac_median_list.sort()
        median = lac_median_list[int(len(lac_median_list)/2)]
        upper_bound = median + median * LAC_threshold
        lower_bound = median - median * LAC_threshold

        if lower_bound <= lac_to_test <= upper_bound:
            return RuleResult.OK
        else:
            return RuleResult.CRITICAL


class NeighbourhoodStructureRule (Rule):
    identifier = 'Neighbourhood Structure'

    def check(self, arfcn, base_station_list):
        own_provider = self._extract_provider(arfcn, base_station_list)
        own_neighbours = self._extract_neighbours(arfcn, base_station_list)
        if not len(own_neighbours):
            return RuleResult.CRITICAL
        at_least_one_neighbour_found = False
        at_least_one_indirect_neighbour = False

        for item in base_station_list:
            if item.arfcn in own_neighbours:
                at_least_one_neighbour_found = True
                break
            else:
                if item.arfcn != arfcn:
                    for foreign_neighbour_arfcn in item.neighbours:
                        if foreign_neighbour_arfcn in own_neighbours:
                            at_least_one_indirect_neighbour = True

        incoming_edges = False
        all_neighbours = []
        for station in base_station_list:
            if station.provider == own_provider:
                for neighbour in station.neighbours:
                    all_neighbours.append(neighbour)
        for neighbour_arfcn in all_neighbours:
            if neighbour_arfcn == arfcn:
                incoming_edges = True
                break

        if at_least_one_neighbour_found and incoming_edges:
            return RuleResult.OK

        if at_least_one_neighbour_found or at_least_one_indirect_neighbour:
            return RuleResult.WARNING

        return RuleResult.CRITICAL

class PureNeighbourhoodRule (Rule):
    identifier = 'Pure Neighbourhoods'

    def check(self, arfcn, base_station_list):

        neighbours = self._extract_neighbours(arfcn, base_station_list)
        provider = self._extract_provider(arfcn, base_station_list)
        all_neighbours_pure = True
        for item in base_station_list:
            if item.arfcn in neighbours:
                if not item.provider == provider:
                    all_neighbours_pure = False

        if all_neighbours_pure:
            return RuleResult.OK
        else:
            return RuleResult.CRITICAL


class DiscoveredNeighboursRule (Rule):
    identifier = 'Discovered Neighbours'

    def check(self, arfcn, base_station_list):

        neighbours = self._extract_neighbours(arfcn, base_station_list)
        found = 0
        for item in base_station_list:
            if item.arfcn in neighbours:
                found += 1

        if Neighbours_threshold < 0:
            return RuleResult.IGNORE

        if 0 <= Neighbours_threshold <=1:
            if (float(found) / float(neighbours)) >= Neighbours_threshold:
                return RuleResult.OK
            else:
                return RuleResult.CRITICAL
        else:
            if found >= int(Neighbours_threshold):
                return RuleResult.OK
            else:
                return RuleResult.CRITICAL

class LocationAreaDatabaseRule(Rule):
    identifier = 'Local Area Database'
    def __init__(self):
        self.location_database_object = None

    def check(self, arfcn, base_station_list):
        if not self.location_database_object:
            return RuleResult.IGNORE
        for item in base_station_list:
            if item.arfcn == arfcn:
                result = self.location_database_object.get_station(item.cell)
                if not result:
                    return RuleResult.CRITICAL
                rxmin = result.rxmin
                rxmax = result.rxmax
                rxmin_thresh = rxmin - math.fabs(rxmin * DB_RX_threshold)
                rxmax_thresh = rxmax + math.fabs(rxmax * DB_RX_threshold)

                if rxmin_thresh <= float(item.rxlev) <= rxmax_thresh:
                    return RuleResult.OK
                else:
                    return RuleResult.CRITICAL

class CellIDDatabaseRule (Rule):
    identifier = 'CellID Database'

    def check(self, arfcn, base_station_list):
        for item in base_station_list:
            if item.arfcn == arfcn:
                if item.db_status == CellIDDBStatus.NOT_LOOKED_UP:
                    return RuleResult.IGNORE
                if item.db_status == CellIDDBStatus.CONFIRMED:
                    return RuleResult.OK
                else:
                    return RuleResult.CRITICAL

class LACChangeRule (Rule):
    identifier = 'LAC Change Rule'

    def __init__(self):
        self._old_lac = {}

    def check(self, arfcn, base_station_list):
        for item in base_station_list:
            if item.arfcn == arfcn:
                if self._old_lac.has_key(arfcn):
                    lac, old_scanned, old_result = self._old_lac[arfcn]
                    if item.times_scanned > 1:
                        if item.times_scanned > old_scanned:
                            #print 'evaluating lac change on %d(%d): old lac %d / new lac %d'%(item.times_scanned,arfcn, lac, item.lac)
                            if item.lac == lac:
                                self._old_lac[arfcn] = item.lac, item.times_scanned, RuleResult.OK
                                #print '     return ok'
                                return RuleResult.OK
                            else:
                                self._old_lac[arfcn] = lac, item.times_scanned, RuleResult.CRITICAL
                                #print '     return critical'
                                return RuleResult.CRITICAL
                        else:
                            return old_result
                    else:
                        return old_result
                else:
                    self._old_lac[arfcn] = item.lac, item.times_scanned, RuleResult.IGNORE
                    return RuleResult.IGNORE

class RxChangeRule (Rule):
    identifier = 'rx Change Rule'

    def __init__(self):
        self._old_rx = {}

    def check(self, arfcn, base_station_list):
        for item in base_station_list:
            if item.arfcn == arfcn:
                if self._old_rx.has_key(arfcn):
                    rx, old_scanned, old_result = self._old_rx[arfcn]
                    if item.times_scanned > 1:
                        if item.times_scanned > old_scanned:
                            #print 'evaluating rx change on %d(%d): old rx %d / new rx %d'%(item.times_scanned,arfcn, rx, item.rxlev)
                            lower_bound = rx - math.fabs(rx * CH_RX_threshold)
                            upper_bound = rx + math.fabs(rx * CH_RX_threshold)
                            #print '     thresholds: %d/%d'%(lower_bound, upper_bound)
                            if lower_bound <= item.rxlev <= upper_bound:
                                self._old_rx[arfcn] = item.rxlev, item.times_scanned, RuleResult.OK
                                #print '     return ok'
                                return RuleResult.OK
                            else:
                                self._old_rx[arfcn] = item.rxlev, item.times_scanned, RuleResult.CRITICAL
                                #print '     return critical '
                                return RuleResult.CRITICAL
                        else:
                            return old_result
                    else:
                        return old_result
                else:
                    self._old_rx[arfcn] = item.rxlev, item.times_scanned, RuleResult.IGNORE
                    return RuleResult.IGNORE

class PCHRule (Rule):
    identifier = 'PCH Scan'

    def check(self, arfcn, base_station_list):
        for item in base_station_list:
            if arfcn == item.arfcn:
                if not item.pch_scan_done:
                    return RuleResult.IGNORE
                else:
                    if item.imm_ass_non_hop > 0:
                        return RuleResult.CRITICAL
                    if item.pagings >= Pagings_per_10s_threshold and item.imm_ass_hop >= Assignment_limit:
                        return RuleResult.OK
                    else:
                        return RuleResult.CRITICAL