This file is indexed.

/usr/share/check_mk/web/plugins/wato/bi.py is in check-mk-multisite 1.2.8p16-1ubuntu0.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# WATO-Module for the rules and aggregations of Check_MK BI

#   .--Base class----------------------------------------------------------.
#   |             ____                        _                            |
#   |            | __ )  __ _ ___  ___    ___| | __ _ ___ ___              |
#   |            |  _ \ / _` / __|/ _ \  / __| |/ _` / __/ __|             |
#   |            | |_) | (_| \__ \  __/ | (__| | (_| \__ \__ \             |
#   |            |____/ \__,_|___/\___|  \___|_|\__,_|___/___/             |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBI(WatoMode):

    # .--------------------------------------------------------------------.
    # | Initialization and default modes                                   |
    # '--------------------------------------------------------------------'
    def __init__(self):
        WatoMode.__init__(self)

        # We need to replace the BI constants internally with something
        # that we can replace back after writing the BI-Rules out
        # with pprint.pformat
        self._bi_constants = {
            'ALL_HOSTS'          : 'ALL_HOSTS-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'HOST_STATE'         : 'HOST_STATE-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'HIDDEN'             : 'HIDDEN-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'FOREACH_HOST'       : 'FOREACH_HOST-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'FOREACH_CHILD'      : 'FOREACH_CHILD-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'FOREACH_CHILD_WITH' : 'FOREACH_CHILD_WITH-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'FOREACH_PARENT'     : 'FOREACH_PARENT-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'FOREACH_SERVICE'    : 'FOREACH_SERVICE-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'REMAINING'          : 'REMAINING-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'DISABLED'           : 'DISABLED-f41e728b-0bce-40dc-82ea-51091d034fc3',
            'HARD_STATES'        : 'HARD_STATES-f41e728b-0bce-40dc-82ea-51091d034fc3',
        }
        self._load_config()

        if not config.may("wato.bi_admin"):
            self._user_contactgroups = userdb.contactgroups_of_user(config.user_id)
        else:
            self._user_contactgroups = None # meaning I am admin
        self._create_valuespecs()

        # Most modes need a pack as context
        if html.has_var("pack"):
            self._pack_id = html.var("pack")
            if self._pack_id not in self._packs:
                raise MKGeneralException(_("Invalid URL. BI pack %s does not exist.") % html.attrencode(self._pack_id))
            self._pack = self._packs[self._pack_id]
        else:
            self._pack_id = None
            self._pack = None


    def title(self):
        title = _("Business Intelligence")
        if self._pack:
            title += " - " + html.attrencode(self._pack["title"])
        return title


    def buttons(self):
        home_button()
        if self._pack:
            html.context_button(_("All Packs"), html.makeuri_contextless([("mode", "bi_packs")]), "back")


    # .--------------------------------------------------------------------.
    # | Loading and saving                                                 |
    # '--------------------------------------------------------------------'
    def _load_config(self):
        filename = multisite_dir + "bi.mk"
        try:
            vars = { "aggregation_rules" : {},
                     "aggregations"      : [],
                     "host_aggregations" : [],
                     "bi_packs" : {},
                   }
            vars.update(self._bi_constants)
            if os.path.exists(filename):
                execfile(filename, vars, vars)
            else:
                exec(bi_example, vars, vars)

            # put legacy non-pack stuff into packs
            if (vars["aggregation_rules"] or vars["aggregations"] or vars["host_aggregations"]) and \
                "default" not in vars["bi_packs"]:
                vars["bi_packs"]["default"] = {
                    "title"             : _("Default Pack"),
                    "rules"             : vars["aggregation_rules"],
                    "aggregations"      : vars["aggregations"],
                    "host_aggregations" : vars["host_aggregations"],
                    "public"            : True,
                    "contact_groups"    : [],
                }

            self._packs = {}
            for pack_id, pack in vars["bi_packs"].items():
                # Convert rules from old-style tuples to new-style dicts
                aggregation_rules = {}
                for ruleid, rule in pack["rules"].items():
                    aggregation_rules[ruleid] = self._convert_rule_from_bi(rule, ruleid)

                aggregations = []
                for aggregation in pack["aggregations"]:
                    aggregations.append(self._convert_aggregation_from_bi(aggregation, single_host = False))
                for aggregation in pack["host_aggregations"]:
                    aggregations.append(self._convert_aggregation_from_bi(aggregation, single_host = True))

                self._packs[pack_id] = {
                    "id"             : pack_id,
                    "title"          : pack["title"],
                    "rules"          : aggregation_rules,
                    "aggregations"   : aggregations,
                    "public"         : pack["public"],
                    "contact_groups" : pack["contact_groups"],
                }


        except Exception, e:
            if config.debug:
                raise

            raise MKGeneralException(_("Cannot read configuration file %s: %s" %
                              (filename, e)))


    def save_config(self):
        make_nagios_directory(multisite_dir)
        out = create_user_file(multisite_dir + "bi.mk", "w")
        out.write(wato_fileheader())
        for pack_id, pack in sorted(self._packs.items()):
            converted_pack = self._convert_pack_to_bi(pack)
            out.write("bi_packs[%r] = %s\n\n" % (
                pack_id, self._replace_bi_constants(pprint.pformat(converted_pack, width=50))))

        # Make sure that BI aggregates are replicated to all other sites that allow
        # direct user login
        update_login_sites_replication_status()


    def _convert_pack_to_bi(self, pack):
        converted_rules = dict([
            (rule_id, self._convert_rule_to_bi(rule))
            for (rule_id, rule)
            in pack["rules"].items() ])
        converted_aggregations = []
        converted_host_aggregations = []
        for aggregation in pack["aggregations"]:
            if aggregation["single_host"]:
                append_to = converted_host_aggregations
            else:
                append_to = converted_aggregations
            append_to.append(self._convert_aggregation_to_bi(aggregation))

        converted_pack = pack.copy()
        converted_pack["aggregations"] = converted_aggregations
        converted_pack["host_aggregations"] = converted_host_aggregations
        converted_pack["rules"] = converted_rules
        return converted_pack


    def _replace_bi_constants(self, s):
        for name, uuid in self._bi_constants.items():
            while True:
                n = s.replace("'%s'" % uuid, name)
                if n != s:
                    s = n
                else:
                    break
        return s[0] + '\n ' + s[1:-1] + '\n' + s[-1]


    def _convert_aggregation_to_bi(self, aggr):
        if len(aggr["groups"]) == 1:
            conv = (aggr["groups"][0],)
        else:
            conv = (aggr["groups"],)
        node = self._convert_node_to_bi(aggr["node"])
        convaggr = conv + node
        if aggr["hard_states"]:
            convaggr = (self._bi_constants["HARD_STATES"],) + convaggr
        if aggr["disabled"]:
            convaggr = (self._bi_constants["DISABLED"],) + convaggr
        return convaggr


    def _convert_node_to_bi(self, node):
        if node[0] == "call":
            return node[1]
        elif node[0] == "host":
            return (node[1][0], self._bi_constants['HOST_STATE'])
        elif node[0] == "remaining":
            return (node[1][0], self._bi_constants['REMAINING'])
        elif node[0] == "service":
            return node[1]
        elif node[0] == "foreach_host":
            what = node[1][0]

            tags = node[1][1]
            if node[1][2]:
                hostspec = node[1][2]
            else:
                hostspec = self._bi_constants['ALL_HOSTS']

            if type(what == tuple) and what[0] == 'child_with':
                child_conditions = what[1]
                what             = what[0]
                child_tags       = child_conditions[0]
                child_hostspec   = child_conditions[1] and child_conditions[1] or self._bi_constants['ALL_HOSTS']
                return (self._bi_constants["FOREACH_" + what.upper()], child_tags, child_hostspec, tags, hostspec) \
                       + self._convert_node_to_bi(node[1][3])
            else:
                return (self._bi_constants["FOREACH_" + what.upper()], tags, hostspec) + self._convert_node_to_bi(node[1][3])
        elif node[0] == "foreach_service":
            tags = node[1][0]
            if node[1][1]:
                spec = node[1][1]
            else:
                spec = self._bi_constants['ALL_HOSTS']
            service = node[1][2]
            return (self._bi_constants["FOREACH_SERVICE"], tags, spec, service) + self._convert_node_to_bi(node[1][3])


    def _convert_aggregation_from_bi(self, aggr, single_host):
        if aggr[0] == self._bi_constants["DISABLED"]:
            disabled = True
            aggr = aggr[1:]
        else:
            disabled = False

        if aggr[0] == self._bi_constants["HARD_STATES"]:
            hard_states = True
            aggr = aggr[1:]
        else:
            hard_states = False

        if type(aggr[0]) != list:
            groups = [aggr[0]]
        else:
            groups = aggr[0]
        node = self._convert_node_from_bi(aggr[1:])
        return {
            "disabled"    : disabled,
            "hard_states" : hard_states,
            "groups"      : groups,
            "node"        : node,
            "single_host" : single_host,
        }

    # Make some conversions so that the format of the
    # valuespecs is matched
    def _convert_rule_from_bi(self, rule, ruleid):
        if type(rule) == tuple:
            rule = {
                "title"       : rule[0],
                "params"      : rule[1],
                "aggregation" : rule[2],
                "nodes"       : rule[3],
            }
        crule = {}
        crule.update(rule)
        crule["nodes"] = map(self._convert_node_from_bi, rule["nodes"])
        parts = rule["aggregation"].split("!")
        crule["aggregation"] = (parts[0], tuple(map(tryint, parts[1:])))
        crule["id"] = ruleid
        return crule

    def _convert_rule_to_bi(self, rule):
        brule = {}
        brule.update(rule)
        if "id" in brule:
            del brule["id"]
        brule["nodes"] = map(self._convert_node_to_bi, rule["nodes"])
        brule["aggregation"] = "!".join(
                    [ rule["aggregation"][0] ] + map(str, rule["aggregation"][1]))
        return brule


    # Convert node-Tuple into format used by CascadingDropdown
    def _convert_node_from_bi(self, node):
        if len(node) == 2:
            if type(node[1]) == list:
                return ("call", node)
            elif node[1] == self._bi_constants['HOST_STATE']:
                return ("host", (node[0],))
            elif node[1] == self._bi_constants['REMAINING']:
                return ("remaining", (node[0],))
            else:
                return ("service", node)

        else: # FOREACH_...

            foreach_spec = node[0]
            if foreach_spec == self._bi_constants['FOREACH_CHILD_WITH']:
                # extract the conditions meant for matching the childs
                child_conditions = list(node[1:3])
                if child_conditions[1] == self._bi_constants['ALL_HOSTS']:
                    child_conditions[1] = None
                node = node[0:1] + node[3:]

            # Extract the list of tags
            if type(node[1]) == list:
                tags = node[1]
                node = node[0:1] + node[2:]
            else:
                tags = []

            hostspec = node[1]
            if hostspec == self._bi_constants['ALL_HOSTS']:
                hostspec = None

            if foreach_spec == self._bi_constants['FOREACH_SERVICE']:
                service = node[2]
                subnode = self._convert_node_from_bi(node[3:])
                return ("foreach_service", (tags, hostspec, service, subnode))
            else:

                subnode = self._convert_node_from_bi(node[2:])
                if foreach_spec == self._bi_constants['FOREACH_HOST']:
                    what = "host"
                elif foreach_spec == self._bi_constants['FOREACH_CHILD']:
                    what = "child"
                elif foreach_spec == self._bi_constants['FOREACH_CHILD_WITH']:
                    what = ("child_with", child_conditions)
                elif foreach_spec == self._bi_constants['FOREACH_PARENT']:
                    what = "parent"
                return ("foreach_host", (what, tags, hostspec, subnode))



    # .--------------------------------------------------------------------.
    # | Valuespecs                                                         |
    # '--------------------------------------------------------------------'

    # FIXME TODO self._vs_call_rule etc. refactor to properties
    def _create_valuespecs(self):
        self._vs_call_rule = self._get_vs_call_rule()
        self._vs_host_re = self._get_vs_host_re()
        self._vs_node = self._get_vs_node()
        self._vs_aggregation = self._get_vs_aggregation()


    def _allowed_rule_choices(self):
        choices = []
        for pack_id, pack in sorted(self._packs.items()):
            if self.may_use_rules_in_pack(pack):
                for rule_id, rule in sorted(pack["rules"].items()):
                    choices.append((rule_id, "%s - %s/%s" % (rule_id, pack["title"], rule["title"])))
        return choices


    def may_use_rules_in_pack(self, pack):
        return pack["public"] or self.is_contact_for_pack(pack)


    def is_contact_for_pack(self, pack=None):
        if self._user_contactgroups == None:
            return True # I am BI admin

        if pack == None:
            pack = self._pack

        for group in self._user_contactgroups:
            if group in pack["contact_groups"]:
                return True
        return False


    def must_be_contact_for_pack(self):
        if not self.is_contact_for_pack():
            raise MKAuthException(_("You have no permission for changes in this BI pack."))


    def _validate_rule_call(self, value, varprefix):
        rule_id, arguments = value
        rule = self.find_rule_by_id(rule_id)
        rule_params = rule['params']

        if len(arguments) != len(rule_params):
            raise MKUserError(varprefix + "_1_0", _("The rule you selected needs %d argument(s) (%s), "
                                           "but you configured %d arguments.") %
                                    (len(rule_params), ', '.join(rule_params), len(arguments)))


    def _get_vs_call_rule(self):
        return Tuple(
            elements = [
                DropdownChoice(
                    title = _("Rule:"),
                    choices = self._allowed_rule_choices(),
                    sorted = True,
                ),
                ListOfStrings(
                    orientation = "horizontal",
                    size = 12,
                    title = _("Arguments:"),
                ),
            ],
            validate = lambda v, vp: self._validate_rule_call(v, vp),
        )


    def _get_vs_host_re(self):
        host_re_help = _("Either an exact host name or a regular expression exactly matching the host "
                         "name. Example: <tt>srv.*p</tt> will match <tt>srv4711p</tt> but not <tt>xsrv4711p2</tt>. ")
        return TextUnicode(
            title = _("Host:"),
            help = host_re_help,
            allow_empty = False,
        )


    def _node_call_choices(self):
        # Configuration of explicit rule call
        return [ ( "call", _("Call a Rule"), self._vs_call_rule ), ]


    # Configuration of FOREACH_...-type nodes
    def _foreach_choices(self, subnode_choices):
        return [
          ( "foreach_host", _("Create nodes based on a host search"),
             Tuple(
                 elements = [
                    CascadingDropdown(
                        title = _("Refer to:"),
                        choices = [
                            ( 'host',       _("The found hosts themselves") ),
                            ( 'child',      _("The found hosts' childs") ),
                            ( 'child_with', _("The found hosts' childs (with child filtering)"),
                                Tuple(elements = [
                                    HostTagCondition(
                                        title = _("Child Host Tags:")
                                    ),
                                    OptionalDropdownChoice(
                                        title = _("Child Host Name:"),
                                        choices = [
                                            ( None, _("All Hosts")),
                                        ],
                                        explicit = TextAscii(size = 60),
                                        otherlabel = _("Regex for host name"),
                                        default_value = None,
                                    ),
                                ]),
                            ),
                            ( 'parent',     _("The found hosts' parents") ),
                        ],
                        help = _('When selecting <i>The found hosts\' childs</i>, the conditions '
                          '(tags and host name) are used to match a host, but you will get one '
                          'node created for each child of the matched host. The '
                          'place holder <tt>$1$</tt> contains the name of the found child.<br><br>'
                          'When selecting <i>The found hosts\' parents</i>, the conditions '
                          '(tags and host name) are used to match a host, but you will get one '
                          'node created for each of the parent hosts of the matched host. '
                          'The place holder <tt>$1$</tt> contains the name of the child host '
                          'and <tt>$2$</tt> the name of the parent host.'),
                    ),
                    HostTagCondition(
                        title = _("Host Tags:")
                    ),
                    OptionalDropdownChoice(
                        title = _("Host Name:"),
                        choices = [
                            ( None, _("All Hosts")),
                        ],
                        explicit = TextAscii(size = 60),
                        otherlabel = _("Regex for host name"),
                        default_value = None,
                        help = _("If you choose \"Regex for host name\", you need to provide a regex "
                                 "which results in exactly one match group."),
                    ),
                    CascadingDropdown(
                        title = _("Nodes to create:"),
                        help = _("When calling a rule you can use the place holder <tt>$1$</tt> "
                                 "in the rule arguments. It will be replaced by the actual host "
                                 "names found by the search - one host name for each rule call."),
                        choices = subnode_choices,
                    ),
                 ]
            )
          ),
          ( "foreach_service", _("Create nodes based on a service search"),
             Tuple(
                 elements = [
                    HostTagCondition(
                        title = _("Host Tags:")
                    ),
                    OptionalDropdownChoice(
                        title = _("Host Name:"),
                        choices = [
                            ( None, _("All Hosts")),
                        ],
                        explicit = TextAscii(size = 60),
                        otherlabel = _("Regex for host name"),
                        default_value = None,
                    ),
                    TextAscii(
                        title = _("Service Regex:"),
                        help = _("Subexpressions enclosed in <tt>(</tt> and <tt>)</tt> will be available "
                                 "as arguments <tt>$2$</tt>, <tt>$3$</tt>, etc."),
                        size = 80,
                    ),
                    CascadingDropdown(
                        title = _("Nodes to create:"),
                        help = _("When calling a rule you can use the place holder <tt>$1$</tt> "
                                 "in the rule arguments. It will be replaced by the actual host "
                                 "names found by the search - one host name for each rule call. If you "
                                 "have regular expression subgroups in the service pattern, then "
                                 "the place holders <tt>$2$</tt> will represent the first group match, "
                                 "<tt>$3</tt> the second, and so on..."),
                        choices = subnode_choices,
                    ),
                 ]
            )
          )
        ]


    def _get_vs_node(self):
        # Configuration of leaf nodes
        vs_node_simplechoices = [
            ( "host", _("State of a host"),
               Tuple(
                   help = _("Will create child nodes representing the state of hosts (usually the "
                            "host check is done via ping)."),
                   elements = [ self._vs_host_re, ]
               )
            ),
            ( "service", _("State of a service"),
              Tuple(
                  help = _("Will create child nodes representing the state of services."),
                  elements = [
                      self._vs_host_re,
                      TextUnicode(
                          title = _("Service:"),
                          help = _("A regular expression matching the <b>beginning</b> of a service description. You can "
                                   "use a trailing <tt>$</tt> in order to define an exact match. For each "
                                   "matching service on the specified hosts one child node will be created. "),
                      ),
                  ]
              ),
            ),
            ( "remaining", _("State of remaining services"),
              Tuple(
                  help = _("Create a child node for each service on the specified hosts that is not "
                           "contained in any other node of the aggregation."),
                  elements = [ self._vs_host_re ],
              )
            ),
        ]


        return CascadingDropdown(
           choices = vs_node_simplechoices + self._node_call_choices() \
                  + self._foreach_choices(vs_node_simplechoices + self._node_call_choices())
        )


    def _aggregation_choices(self):
        choices = []
        for aid, ainfo in bi_aggregation_functions.items():
            choices.append((
                aid,
                ainfo["title"],
                ainfo["valuespec"],
            ))
        return choices


    def _get_vs_aggregation(self):
        return Dictionary(
            title = _("Aggregation Properties"),
            optional_keys = False,
            render = "form",
            elements = [
            ( "groups",
              ListOfStrings(
                  title = _("Aggregation Groups"),
                  help = _("List of groups in which to show this aggregation. Usually "
                           "each aggregation is only in one group. Group names are arbitrary "
                           "texts. At least one group is mandatory."),
                  valuespec = TextUnicode(),
              ),
            ),
            ( "node",
              CascadingDropdown(
                  title = _("Rule to call"),
                  choices = self._node_call_choices() + self._foreach_choices(self._node_call_choices())
              )
            ),
            ( "disabled",
              Checkbox(
                  title = _("Disabled"),
                  label = _("Currently disable this aggregation"),
              )
            ),
            ( "hard_states",
              Checkbox(
                  title = _("Use Hard States"),
                  label = _("Base state computation on hard states"),
                  help = _("Hard states can only differ from soft states if at least one host or service "
                           "of the BI aggregate has more than 1 maximum check attempt. For example if you "
                           "set the maximum check attempts of a service to 3 and the service is CRIT "
                           "just since one check then it's soft state is CRIT, but its hard state is still OK. "
                           "<b>Note:</b> When computing the availbility of a BI aggregate this option "
                           "has no impact. For that purpose always the soft (i.e. real) states will be used."),
              )
            ),
            ( "single_host",
              Checkbox(
                  title = _("Optimization"),
                  label = _("The aggregation covers data from only one host and its parents."),
                  help = _("If you have a large number of aggregations that cover only one host and "
                           "maybe its parents (such as Check_MK cluster hosts), "
                           "then please enable this optimization. It reduces the time for the "
                           "computation. Do <b>not</b> enable this for aggregations that contain "
                           "data of more than one host!"),
              ),
            ),
          ]
        )



    # .--------------------------------------------------------------------.
    # | Methods for analysing the rules and aggregations                   |
    # '--------------------------------------------------------------------'

    def aggregation_title(self, aggregation):
        rule = self.aggregation_toplevel_rule(aggregation)
        return "%s (%s)" % (rule["title"], rule["id"])


    def aggregation_toplevel_rule(self, aggregation):
        rule_id, description = self.rule_called_by_node(aggregation["node"])
        return self.find_rule_by_id(rule_id)


    def have_rules(self):
        for pack in self._packs.values():
            if pack["rules"]:
                return True
        return False


    # Returns the rule called by a node - if any
    # Result is a pair of the rule and a descriptive title
    def rule_called_by_node(self, node):
        if node[0] == "call":
            if node[1][1]:
                args = _("with arguments: %s") % ", ".join(node[1][1])
            else:
                args = _("without arguments")
            return node[1][0], _("Explicit call ") + args
        elif node[0] == "foreach_host":
            subnode = node[1][-1]
            if subnode[0] == 'call':
                if node[1][0] == 'host':
                    info = _("Called for all hosts...")
                elif node[1][0] == 'child':
                    info = _("Called for each child of...")
                else:
                    info = _("Called for each parent of...")
                return subnode[1][0], info
        elif node[0] == "foreach_service":
            subnode = node[1][-1]
            if subnode[0] == 'call':
                return subnode[1][0], _("Called for each service...")


    def pack_containing_rule(self, ruleid):
        for pack in self._packs.values():
            if ruleid in pack["rules"]:
                return pack
        return None


    def find_rule_by_id(self, ruleid):
        pack = self.pack_containing_rule(ruleid)
        if pack:
            return pack["rules"][ruleid]


    # Checks if the rule 'rule' uses either directly
    # or indirectly the rule with the id 'ruleid'. In
    # case of success, returns the nesting level
    def rule_uses_rule(self, rule, ruleid, level=0):
        for node in rule["nodes"]:
            r = self.rule_called_by_node(node)
            if r:
                ru_id, info = r
                if ru_id == ruleid: # Rule is directly being used
                    return level + 1
                # Check if lower rules use it
                else:
                    subrule = self.find_rule_by_id(ru_id)
                    l = self.rule_uses_rule(subrule, ruleid, level + 1)
                    if l:
                        return l
        return False


    def count_rule_references(self, ruleid):
        aggr_refs = 0
        for pack in self._packs.values():
            for aggregation in pack["aggregations"]:
                called_rule_id, info = self.rule_called_by_node(aggregation["node"])
                if called_rule_id == ruleid:
                    aggr_refs += 1

        level = 0
        rule_refs = 0
        for pack in self._packs.values():
            for rid, rule in pack["rules"].items():
                l = self.rule_uses_rule(rule, ruleid)
                level = max(l, level)
                if l == 1:
                    rule_refs += 1

        return aggr_refs, rule_refs, level


    def aggregation_sub_rule_ids(self, rule):
        sub_rule_ids = []
        for node in rule["nodes"]:
            r = self.rule_called_by_node(node)
            if r:
                sub_rule_ids.append(r[0])
        return sub_rule_ids



    # .--------------------------------------------------------------------.
    # | Generic rendering                                                  |
    # '--------------------------------------------------------------------'

    def url_to_pack(self, addvars):
        return html.makeuri_contextless(addvars + [("pack", self._pack_id)])


    def render_rule_tree(self, ruleid, tree_path):
        pack = self.pack_containing_rule(ruleid)
        rule = pack["rules"][ruleid]
        edit_url = html.makeuri_contextless([("mode", "bi_edit_rule"), ("id", ruleid), ("pack", pack["id"])])
        title = "%s (%s)" % (rule["title"], ruleid)

        sub_rule_ids = self.aggregation_sub_rule_ids(rule)
        if not sub_rule_ids:
            html.write('<li><a href="%s">%s</a></li>' % (edit_url, title))
        else:
            html.begin_foldable_container("bi_rule_trees", tree_path, False, title,
                                          title_url=edit_url, tree_img="tree_black")
            for sub_rule_id in sub_rule_ids:
                self.render_rule_tree(sub_rule_id, tree_path + "/" + sub_rule_id)
            html.end_foldable_container()



#.
#   .--Packs---------------------------------------------------------------.
#   |                      ____            _                               |
#   |                     |  _ \ __ _  ___| | _____                        |
#   |                     | |_) / _` |/ __| |/ / __|                       |
#   |                     |  __/ (_| | (__|   <\__ \                       |
#   |                     |_|   \__,_|\___|_|\_\___/                       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBIPacks(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)
        self._contact_group_names = userdb.load_group_information().get("contact", {})


    def buttons(self):
        ModeBI.buttons(self)
        if config.may("wato.bi_admin"):
            html.context_button(_("New BI Pack"), html.makeuri_contextless([("mode", "bi_edit_pack")]), "new")


    def action(self):
        if config.may("wato.bi_admin") and html.has_var("_delete"):
            pack_id = html.var("_delete")
            pack = self._packs[pack_id]
            if pack["rules"]:
                raise MKUserError(None, _("You cannot delete this pack. It contains <b>%d</b> rules.") % len(pack["rules"]))
            c = wato_confirm(_("Confirm BI pack deletion"),
                             _("Do you really want to delete the BI pack <b>%s</b> <i>%s</i> with <b>%d</b> rules and <b>%d</b> aggregations?" %
                               (pack_id, pack["title"], len(pack["rules"]), len(pack["aggregations"]))))
            if c:
                log_mkeventd("delete-bi-pack", _("Deleted BI pack %s") % pack_id)
                del self._packs[pack_id]
                self.save_config()
            elif c == False:
                return ""


    def page(self):
        table.begin(title = _("BI Configuration Packs"))
        for pack_id, pack in sorted(self._packs.items()):
            if not self.may_use_rules_in_pack(pack):
                continue

            table.row()
            table.cell(_("Actions"), css="buttons")
            if config.may("wato.bi_admin"):
                edit_url = html.makeuri_contextless([("mode", "bi_edit_pack"), ("pack", pack_id)])
                html.icon_button(edit_url, _("Edit properties of this BI pack"), "edit")
                delete_url = html.makeactionuri([("_delete", pack_id)])
                html.icon_button(delete_url, _("Delete this BI pack"), "delete")
            rules_url  = html.makeuri_contextless([("mode", "bi_rules"), ("pack", pack_id)])
            html.icon_button(rules_url, _("View and edit the rules and aggregations in this BI pack"), "bi_rules")
            table.cell(_("ID"), pack_id)
            table.cell(_("Title"), pack["title"])
            table.cell(_("Public"), pack["public"] and _("Yes") or _("No"))
            table.cell(_("Aggregations"), len(pack["aggregations"]), css="number")
            table.cell(_("Rules"), len(pack["rules"]), css="number")
            table.cell(_("Contact Groups"), ", ".join(map(self._render_contact_group, pack["contact_groups"])))
        table.end()


    def _render_contact_group(self, c):
        display_name = self._contact_group_names.get(c, {'alias': c})['alias']
        return '<a href="wato.py?mode=edit_contact_group&edit=%s">%s</a>' % (c, display_name)


#.
#   .--Edit Pack-----------------------------------------------------------.
#   |               _____    _ _ _     ____            _                   |
#   |              | ____|__| (_) |_  |  _ \ __ _  ___| | __               |
#   |              |  _| / _` | | __| | |_) / _` |/ __| |/ /               |
#   |              | |__| (_| | | |_  |  __/ (_| | (__|   <                |
#   |              |_____\__,_|_|\__| |_|   \__,_|\___|_|\_\               |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBIEditPack(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)


    def title(self):
        if self._pack:
            return ModeBI.title(self) + " - " + _("Edit BI Pack %s") % self._pack["title"]
        else:
            return ModeBI.title(self) + " - " + _("Create New BI Pack")


    def action(self):
        if html.check_transaction():
            new_pack = self._vs_pack().from_html_vars("bi_pack")
            self._vs_pack().validate_value(new_pack, 'bi_pack')
            if self._pack:
                log_pending(SYNC, None, "bi-edit-pack", _("Modified BI pack %s") % self._pack_id)
                new_pack["rules"] = self._pack["rules"]
                new_pack["aggregations"] = self._pack["aggregations"]
                new_pack["id"] = self._pack_id
            else:
                if new_pack["id"] in self._packs:
                    raise MKUserError("pack_id", _("A BI pack with this ID already exists."))
                log_pending(SYNC, None, "bi-new-pack", _("Created new BI pack %s") % new_pack["id"])
                new_pack["rules"] = {}
                new_pack["aggregations"] = []
            self._packs[new_pack["id"]] = new_pack
            self.save_config()

        return "bi_packs"


    def buttons(self):
        html.context_button(_("Abort"), html.makeuri([("mode", "bi_packs")]), "abort")


    def page(self):
        html.begin_form("bi_pack", method = "POST")
        self._vs_pack().render_input("bi_pack", self._pack)
        forms.end()
        html.hidden_fields()
        html.button("_save", self._pack and _("Save") or _("Create"), "submit")
        if self._pack:
            html.set_focus("bi_pack_p_title")
        else:
            html.set_focus("bi_pack_p_id")
        html.end_form()


    def _vs_pack(self):
        if self._pack:
            id_element = FixedValue(title = _("Pack ID"), value = self._pack_id)
        else:
            id_element = ID(
                title = _("BI pack ID"),
                help = _("A unique ID of this BI pack."),
                allow_empty = False,
                size = 12,
            )
        return Dictionary(
            title = _("BI Pack Properties"),
            optional_keys = False,
            render = "form",
            elements = [
                ( "id", id_element ),
                ( "title",
                  TextUnicode(
                      title = _("Title"),
                      help = _("A descriptive title for this rule pack"),
                      allow_empty = False,
                      size = 64,
                )),
                ( "contact_groups",
                  ListOf(
                      GroupSelection("contact"),
                      title = _("Permitted Contact Groups"),
                      help = _("The rules and aggregations in this pack can be edited by all members of the "
                               "contact groups specified here - even if they have no administrator priviledges."),
                      movable = False,
                      add_label = _("Add Contact Group"),
                )),
                ( "public",
                  Checkbox(
                      title = _("Public"),
                      label = _("Allow all users to refer to rules contained in this pack"),
                      help = _("Without this option users can only use rules if they have administrator "
                               "priviledges or are member of the listed contact groups."),
                ))
            ],
        )


#.
#   .--Aggregations--------------------------------------------------------.
#   |       _                                    _   _                     |
#   |      / \   __ _  __ _ _ __ ___  __ _  __ _| |_(_) ___  _ __  ___     |
#   |     / _ \ / _` |/ _` | '__/ _ \/ _` |/ _` | __| |/ _ \| '_ \/ __|    |
#   |    / ___ \ (_| | (_| | | |  __/ (_| | (_| | |_| | (_) | | | \__ \    |
#   |   /_/   \_\__, |\__, |_|  \___|\__, |\__,_|\__|_|\___/|_| |_|___/    |
#   |           |___/ |___/          |___/                                 |
#   '----------------------------------------------------------------------'


class ModeBIAggregations(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)


    def title(self):
        return ModeBI.title(self) + " - " + _("Aggregations")


    def buttons(self):
        ModeBI.buttons(self)
        html.context_button(_("Rules"), self.url_to_pack([("mode", "bi_rules")]), "aggr")
        if self.have_rules() and self.is_contact_for_pack():
            html.context_button(_("New Aggregation"),
                                self.url_to_pack([("mode", "bi_edit_aggregation")]), "new")


    def action(self):
        nr = int(html.var("_del_aggr"))
        c = wato_confirm(_("Confirm aggregation deletion"),
            _("Do you really want to delete the aggregation number <b>%s</b>?") % (nr+1))
        if c:
            del self._pack["aggregations"][nr]
            log_pending(SYNC, None, "bi-delete-aggregation", _("Deleted BI aggregation number %d") % (nr+1))
            self.save_config()
        elif c == False: # not yet confirmed
            return ""


    def page(self):
        table.begin("bi_aggr", _("Aggregations"))
        for nr, aggregation in enumerate(self._pack["aggregations"]):
            table.row()
            table.cell(_("Actions"), css="buttons")
            edit_url = html.makeuri_contextless([("mode", "bi_edit_aggregation"), ("id", nr), ("pack", self._pack_id)])
            html.icon_button(edit_url, _("Edit this aggregation"), "edit")
            if self.is_contact_for_pack():
                delete_url = html.makeactionuri([("_del_aggr", nr)])
                html.icon_button(delete_url, _("Delete this aggregation"), "delete")
            table.cell(_("Nr."), nr + 1, css="number")
            table.cell("", css="buttons")
            if aggregation["disabled"]:
                html.icon(_("This aggregation is currently disabled."), "disabled")
            if aggregation["single_host"]:
                html.icon(_("This aggregation covers only data from a single host."), "host")
            table.cell(_("Groups"), ", ".join(aggregation["groups"]))
            ruleid, description = self.rule_called_by_node(aggregation["node"])
            edit_url = html.makeuri([("mode", "bi_edit_rule"), ("pack", self._pack_id), ("id", ruleid)])
            table.cell(_("Rule Tree"), css="bi_rule_tree")
            self.render_aggregation_rule_tree(aggregation)
            table.cell(_("Note"), description)
        table.end()


    def render_aggregation_rule_tree(self, aggregation):
        toplevel_rule = self.aggregation_toplevel_rule(aggregation)
        self.render_rule_tree(toplevel_rule["id"], toplevel_rule["id"])



#.
#   .--Rules---------------------------------------------------------------.
#   |                       ____        _                                  |
#   |                      |  _ \ _   _| | ___  ___                        |
#   |                      | |_) | | | | |/ _ \/ __|                       |
#   |                      |  _ <| |_| | |  __/\__ \                       |
#   |                      |_| \_\\__,_|_|\___||___/                       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBIRules(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)
        self._view_type = html.var("view", "list")


    def title(self):
        if self._view_type == "list":
            return ModeBI.title(self) + " - " + _("Rules")
        else:
            return ModeBI.title(self) + " - " + _("Unused Rules")


    def buttons(self):
        ModeBI.buttons(self)
        if self._view_type == "list":
            html.context_button(_("Aggregations"), self.url_to_pack([("mode", "bi_aggregations")]), "aggr")
            if self.is_contact_for_pack():
                html.context_button(_("New Rule"),     self.url_to_pack([("mode", "bi_edit_rule"), ("pack", self._pack_id)]), "new")
            html.context_button(_("Unused Rules"), self.url_to_pack([("mode", "bi_rules"), ("view", "unused")]), "unusedbirules")

        else:
            html.context_button(_("Back"), html.makeuri([("view", "list")]), "back")


    def action(self):
        self.must_be_contact_for_pack()
        if html.var("_del_rule"):
            ruleid = html.var("_del_rule")
            aggr_refs, rule_refs, level = self.count_rule_references(ruleid)
            if aggr_refs:
                raise MKUserError(None, _("You cannot delete this rule: it is still used by aggregations."))
            if rule_refs:
                raise MKUserError(None, _("You cannot delete this rule: it is still used by other rules."))
            c = wato_confirm(_("Confirm rule deletion"),
                _("Do you really want to delete the rule with "
                  "the id <b>%s</b>?") % ruleid)
            if c:
                del self._pack["rules"][ruleid]
                log_pending(SYNC, None, "bi-delete-rule", _("Deleted BI rule with id %s") % ruleid)
                self.save_config()
            elif c == False: # not yet confirmed
                return ""
            else:
                return None # browser reload


    def page(self):
        if not self._pack["aggregations"] and not self._pack["rules"]:
            new_url = self.url_to_pack([("mode", "bi_edit_rule")])
            menu_items = [
                (new_url, _("Create aggregation rule"), "new", "bi_rules",
                  _("Rules are the nodes in BI aggregations. "
                    "Each aggregation has one rule as its root."))
            ]
            render_main_menu(menu_items)
            return

        if self._view_type == "list":
            self.render_rules(_("Rules"), only_unused = False)
        else:
            self.render_rules(_("Unused BI Rules"), only_unused = True)


    def render_rules(self, title, only_unused):
        aggregations_that_use_rule = self.find_aggregation_rule_usages()

        rules = self._pack["rules"].items()
        # Sort rules according to nesting level, and then to id
        rules_refs = [ (ruleid, rule, self.count_rule_references(ruleid))
                       for (ruleid, rule) in rules ]
        rules_refs.sort(cmp = lambda a,b: cmp(a[2][2], b[2][2]) or cmp(a[1]["title"], b[1]["title"]))

        table.begin("bi_rules", title)
        for ruleid, rule, (aggr_refs, rule_refs, level) in rules_refs:
            refs = aggr_refs + rule_refs
            if not only_unused or refs == 0:
                table.row()
                table.cell(_("Actions"), css="buttons")
                edit_url = self.url_to_pack([("mode", "bi_edit_rule"), ("id", ruleid)])
                html.icon_button(edit_url, _("Edit this rule"), "edit")
                if rule_refs == 0:
                    tree_url = html.makeuri([("mode", "bi_rule_tree"), ("id", ruleid)])
                    html.icon_button(tree_url, _("This is a top-level rule. Show rule tree"), "bitree")
                if refs == 0:
                    delete_url = html.makeactionuri_contextless([("mode", "bi_rules"), ("_del_rule", ruleid), ("pack", self._pack_id)])
                    html.icon_button(delete_url, _("Delete this rule"), "delete")
                table.cell(_("Level"), level or "", css="number")
                table.cell(_("ID"), '<a href="%s">%s</a>' % (edit_url, ruleid))
                table.cell(_("Parameters"), " ".join(rule["params"]))
                table.cell(_("Title"), rule["title"])
                table.cell(_("Aggregation"),  "/".join([rule["aggregation"][0]] + map(str, rule["aggregation"][1])))
                table.cell(_("Nodes"), len(rule["nodes"]), css="number")
                table.cell(_("Used by"))
                have_this = set([])
                for (aggr_id, aggregation) in aggregations_that_use_rule.get(ruleid, []):
                    if aggr_id not in have_this:
                        pack = self.pack_containing_rule(ruleid)
                        aggr_url = html.makeuri_contextless([("mode", "bi_edit_aggregation"), ("id", aggr_id), ("pack", pack["id"])])
                        html.write('<a href="%s">%s</a><br>' % (aggr_url, html.attrencode(self.aggregation_title(aggregation))))
                        have_this.add(aggr_id)
                table.cell(_("Comment"), rule.get("comment", ""))
        table.end()


    def find_aggregation_rule_usages(self):
        aggregations_that_use_rule = {}
        for pack_id, pack in self._packs.items():
            for aggr_id, aggregation in enumerate(pack["aggregations"]):
                ruleid, description = self.rule_called_by_node(aggregation["node"])
                aggregations_that_use_rule.setdefault(ruleid, []).append((aggr_id, aggregation))
                sub_rule_ids = self._aggregation_recursive_sub_rule_ids(ruleid)
                for sub_rule_id in sub_rule_ids:
                    aggregations_that_use_rule.setdefault(sub_rule_id, []).append((aggr_id, aggregation))
        return aggregations_that_use_rule


    def _aggregation_recursive_sub_rule_ids(self, ruleid):
        rule = self.find_rule_by_id(ruleid)
        sub_rule_ids = self.aggregation_sub_rule_ids(rule)
        if not sub_rule_ids:
            return []
        result = sub_rule_ids[:]
        for sub_rule_id in sub_rule_ids:
            result += self._aggregation_recursive_sub_rule_ids(sub_rule_id)
        return result



#.
#   .--Rule Tree-----------------------------------------------------------.
#   |               ____        _        _____                             |
#   |              |  _ \ _   _| | ___  |_   _| __ ___  ___                |
#   |              | |_) | | | | |/ _ \   | || '__/ _ \/ _ \               |
#   |              |  _ <| |_| | |  __/   | || | |  __/  __/               |
#   |              |_| \_\\__,_|_|\___|   |_||_|  \___|\___|               |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBIRuleTree(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)
        self._ruleid = html.var("id")


    def title(self):
        return ModeBI.title(self) + " - " + _("Rule Tree of") + " " + self._ruleid


    def buttons(self):
        ModeBI.buttons(self)
        html.context_button(_("Back"), html.makeuri([("mode", "bi_rules")]), "back")


    def page(self):
        aggr_refs, rule_refs, level = self.count_rule_references(self._ruleid)
        if rule_refs == 0:
            table.begin(sortable=False, searchable=False)
            table.row()
            table.cell(_("Rule Tree"), css="bi_rule_tree")
            self.render_rule_tree(self._ruleid, self._ruleid)
            table.end()




#.
#   .--Edit Aggregation----------------------------------------------------.
#   |                          _____    _ _ _                              |
#   |                         | ____|__| (_) |_                            |
#   |                         |  _| / _` | | __|                           |
#   |                         | |__| (_| | | |_                            |
#   |                         |_____\__,_|_|\__|                           |
#   |                                                                      |
#   |         _                                    _   _                   |
#   |        / \   __ _  __ _ _ __ ___  __ _  __ _| |_(_) ___  _ __        |
#   |       / _ \ / _` |/ _` | '__/ _ \/ _` |/ _` | __| |/ _ \| '_ \       |
#   |      / ___ \ (_| | (_| | | |  __/ (_| | (_| | |_| | (_) | | | |      |
#   |     /_/   \_\__, |\__, |_|  \___|\__, |\__,_|\__|_|\___/|_| |_|      |
#   |             |___/ |___/          |___/                               |
#   '----------------------------------------------------------------------'

class ModeBIEditAggregation(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)
        self._edited_nr = int(html.var("id", "-1")) # In case of Aggregations: index in list
        if self._edited_nr == -1:
            self._new = True
            self._edited_aggregation = { "groups" : [ _("Main") ] }
        else:
            self._new = False
            self._edited_aggregation = self._pack["aggregations"][self._edited_nr]



    def title(self):
        if self._new:
            return ModeBI.title(self) + " - " + _("Create New Aggregation")
        else:
            return ModeBI.title(self) + " - " + _("Edit Aggregation")


    def buttons(self):
        html.context_button(_("Abort"), html.makeuri([("mode", "bi_aggregations")]), "abort")


    def action(self):
        self.must_be_contact_for_pack()
        if html.check_transaction():
            new_aggr = self._vs_aggregation.from_html_vars('aggr')
            self._vs_aggregation.validate_value(new_aggr, 'aggr')
            if len(new_aggr["groups"]) == 0:
                raise MKUserError('rule_p_groups_0', _("Please define at least one aggregation group"))
            if self._new:
                self._pack["aggregations"].append(new_aggr)
                log_pending(SYNC, None, "bi-new-aggregation", _("Created new BI aggregation %d") % (len(self._pack["aggregations"])))
            else:
                self._pack["aggregations"][self._edited_nr] = new_aggr
                log_pending(SYNC, None, "bi-edit-aggregation", _("Modified BI aggregation %d") % (self._edited_nr + 1))
            self.save_config()
        return "bi_aggregations"


    def page(self):
        html.begin_form("biaggr", method = "POST")
        self._vs_aggregation.render_input("aggr", self._edited_aggregation)
        forms.end()
        html.hidden_fields()
        if self.is_contact_for_pack():
            html.button("_save", self._new and _("Create") or _("Save"), "submit")
        html.set_focus("aggr_p_groups_0")
        html.end_form()


#.
#   .--Edit Rule-----------------------------------------------------------.
#   |                _____    _ _ _     ____        _                      |
#   |               | ____|__| (_) |_  |  _ \ _   _| | ___                 |
#   |               |  _| / _` | | __| | |_) | | | | |/ _ \                |
#   |               | |__| (_| | | |_  |  _ <| |_| | |  __/                |
#   |               |_____\__,_|_|\__| |_| \_\\__,_|_|\___|                |
#   |                                                                      |
#   '----------------------------------------------------------------------'

class ModeBIEditRule(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)
        self._ruleid = html.var("id") # In case of Aggregations: index in list
        self._new = not self._ruleid


    def title(self):
        if self._new:
            return ModeBI.title(self) + " - " + _("Create New Rule")
        else:
            return ModeBI.title(self) + " - " + _("Edit Rule") + " " + html.attrencode(self._ruleid)


    def buttons(self):
        html.context_button(_("Abort"), html.makeuri([("mode", "bi_rules")]), "abort")


    def action(self):
        self.must_be_contact_for_pack()
        if html.check_transaction():
            vs_rule = self.valuespec()
            new_rule = vs_rule.from_html_vars('rule')
            vs_rule.validate_value(new_rule, 'rule')
            if self._new:
                self._ruleid = new_rule["id"]

            if self._new and self.find_rule_by_id(self._ruleid):
                existing_rule = self.find_rule_by_id(self._ruleid)
                pack = self.pack_containing_rule(self._ruleid)
                raise MKUserError('rule_p_id',
                    _("There is already a rule with the id <b>%s</b>. "
                      "It is in the pack <b>%s</b> and as the title <b>%s</b>" % (
                            self._ruleid, pack["title"], existing_rule["title"])))
            if not new_rule["nodes"]:
                raise MKUserError(None,
                    _("Please add at least one child node. Empty rules are useless."))

            if self._new:
                del new_rule["id"]
                self._pack["rules"][self._ruleid] = new_rule
                log_pending(SYNC, None, "bi-new-rule", _("Create new BI rule %s") % self._ruleid)
            else:
                self._pack["rules"][self._ruleid].update(new_rule)
                new_rule["id"] = self._ruleid
                if self.rule_uses_rule(new_rule, new_rule["id"]):
                    raise MKUserError(None, _("There is a cycle in your rules. This rule calls itself - "
                                              "either directly or indirectly."))
                log_pending(SYNC, None, "bi-edit-rule", _("Modified BI rule %s") % self._ruleid)
            self.save_config()

        return "bi_rules"


    def page(self):
        if self._new:
            value = {}
        else:
            value = self._pack["rules"][self._ruleid]

        html.begin_form("birule", method="POST")
        self.valuespec().render_input("rule", value)
        forms.end()
        html.hidden_fields()
        if self.is_contact_for_pack():
            html.button("_save", self._new and _("Create") or _("Save"), "submit")
        if self._new:
            html.set_focus("rule_p_id")
        else:
            html.set_focus("rule_p_title")
        html.end_form()


    def valuespec(self):
        elements = [
            ( "title",
               TextUnicode(
                   title = _("Rule Title"),
                   help = _("The title of the BI nodes which are created from this rule. This will be "
                            "displayed as the name of the node in the BI view. For "
                            "top level nodes this title must be unique. You can insert "
                            "rule parameters like <tt>$FOO$</tt> or <tt>$BAR$</tt> here."),
                   allow_empty = False,
                   size = 64,
               ),
            ),

            ( "comment",
               TextUnicode(
                   title = _("Comment"),
                   help = _("An arbitrary comment of this rule for you."),
                   size = 64,
               ),
            ),
            ( "params",
              ListOfStrings(
                  title = _("Parameters"),
                  help = _("Parameters are used in order to make rules more flexible. They must "
                           "be named like variables in programming languages. For example you can "
                           "make your rule have the two parameters <tt>HOST</tt> and <tt>INST</tt>. "
                           "When calling the rule - from an aggergation or a higher level rule - "
                           "you can then specify two arbitrary values for these parameters. In the "
                           "title of the rule as well as the host and service names, you can insert the "
                           "actual value of the parameters by <tt>$HOST$</tt> and <tt>$INST$</tt> "
                           "(enclosed in dollar signs)."),
                  orientation = "horizontal",
                  valuespec = TextAscii(
                    size = 12,
                    regex = '[A-Za-z_][A-Za-z0-9_]*',
                    regex_error = _("Parameters must contain only A-Z, a-z, 0-9 and _ "
                                    "and must not begin with a digit."),
                  )
              )
            ),
            ( "aggregation",
              CascadingDropdown(
                title = _("Aggregation Function"),
                help = _("The aggregation function decides how the status of a node "
                         "is constructed from the states of the child nodes."),
                orientation = "horizontal",
                choices = self._aggregation_choices(),
              )
            ),
            ( "nodes",
              ListOf(
                  self._vs_node,
                  add_label = _("Add child node generator"),
                  title = _("Nodes that are aggregated by this rule"),
              ),
            ),
        ]


        if self._new:
            elements = [
            ( "id",
              TextAscii(
                  title = _("Unique Rule ID"),
                  help = _("The ID of the rule must be a unique text. It will be used as an internal key "
                           "when rules refer to each other. The rule IDs will not be visible in the status "
                           "GUI. They are just used within the configuration."),
                  allow_empty = False,
                  size = 12,
              ),
            )] + elements

        return Dictionary(
            title = _("General Properties"),
            optional_keys = False,
            render = "form",
            elements = elements,
            headers = [
                ( _("General Properties"),     [ "id", "title", "comment", "params" ]),
                ( _("Child Node Generation"),  [ "nodes" ] ),
                ( _("Aggregation Function"),   [ "aggregation" ], ),
            ]
        )


#.
#   .--Aggregation functions-----------------------------------------------.
#   |             _                     __                                 |
#   |            / \   __ _  __ _ _ __ / _|_   _ _ __   ___ ___            |
#   |           / _ \ / _` |/ _` | '__| |_| | | | '_ \ / __/ __|           |
#   |          / ___ \ (_| | (_| | |  |  _| |_| | | | | (__\__ \           |
#   |         /_/   \_\__, |\__, |_|  |_|  \__,_|_| |_|\___|___/           |
#   |                 |___/ |___/                                          |
#   '----------------------------------------------------------------------'

bi_aggregation_functions = {}

bi_aggregation_functions["worst"] = {
    "title"     : _("Worst - take worst of all node states"),
    "valuespec" : Tuple(
        elements = [
            Integer(
                help = _("Normally this value is <tt>1</tt>, which means that the worst state "
                         "of all child nodes is being used as the total state. If you set it for example "
                         "to <tt>3</tt>, then instead the node with the 3rd worst state is being regarded. "
                         "Example: In the case of five nodes with the states CRIT CRIT WARN OK OK then "
                         "resulting state would be WARN. Or you could say that the worst to nodes are "
                         "first dropped and then the worst of the remaining nodes defines the state. "),
                title = _("Take n'th worst state for n = "),
                default_value = 1,
                min_value = 1),
            MonitoringState(
                title = _("Restrict severity to at worst"),
                help = _("Here a maximum severity of the node state can be set. This severity is not "
                         "exceeded, even if some of the childs have more severe states."),
                default_value = 2,
            ),
        ]),
}

bi_aggregation_functions["best"] = {
    "title"     : _("Best - take best of all node states"),
    "valuespec" : Tuple(
        elements = [
            Integer(
                help = _("Normally this value is <tt>1</tt>, which means that the best state "
                         "of all child nodes is being used as the total state. If you set it for example "
                         "to <tt>2</tt>, then the node with the best state is not being regarded. "
                         "If the states of the child nodes would be CRIT, WARN and OK, then to total "
                         "state would be WARN."),
                title = _("Take n'th best state for n = "),
                default_value = 1,
                min_value = 1),
            MonitoringState(
                title = _("Restrict severity to at worst"),
                help = _("Here a maximum severity of the node state can be set. This severity is not "
                         "exceeded, even if some of the childs have more severe states."),
                default_value = 2,
            ),
        ]),
}

def vs_count_ok_count(title, defval, defvalperc):
    return Alternative(
        title = title,
        style = "dropdown",
        match = lambda x: str(x).endswith("%") and 1 or 0,
        elements = [
            Integer(
                title = _("Explicit number"),
                label=_("Number of OK-nodes"),
                min_value = 0,
                default_value = defval
            ),
            Transform(
                Percentage(
                    label=_("Percent of OK-nodes"),
                    display_format = "%.0f",
                    default_value = defvalperc),
                title = _("Percentage"),
                forth = lambda x: float(x[:-1]),
                back = lambda x: "%d%%" % x,
            ),
        ]
    )

bi_aggregation_functions["count_ok"] = {
    "title"     : _("Count the number of nodes in state OK"),
    "valuespec" : Tuple(
        elements = [
            vs_count_ok_count(_("Required number of OK-nodes for a total state of OK:"), 2, 50),
            vs_count_ok_count(_("Required number of OK-nodes for a total state of WARN:"), 1, 25),
        ]),
}

#.
#   .--Example Configuration-----------------------------------------------.
#   |               _____                           _                      |
#   |              | ____|_  ____ _ _ __ ___  _ __ | | ___                 |
#   |              |  _| \ \/ / _` | '_ ` _ \| '_ \| |/ _ \                |
#   |              | |___ >  < (_| | | | | | | |_) | |  __/                |
#   |              |_____/_/\_\__,_|_| |_| |_| .__/|_|\___|                |
#   |                                        |_|                           |
#   |                                                                      |
#   '----------------------------------------------------------------------'

bi_example = '''
aggregation_rules["host"] = (
  "Host $HOST$",
  [ "HOST" ],
  "worst",
  [
      ( "general",      [ "$HOST$" ] ),
      ( "performance",    [ "$HOST$" ] ),
      ( "filesystems",  [ "$HOST$" ] ),
      ( "networking",   [ "$HOST$" ] ),
      ( "applications", [ "$HOST$" ] ),
      ( "logfiles",     [ "$HOST$" ] ),
      ( "hardware",     [ "$HOST$" ] ),
      ( "other",        [ "$HOST$" ] ),
  ]
)

aggregation_rules["general"] = (
  "General State",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", HOST_STATE ),
      ( "$HOST$", "Uptime" ),
      ( "checkmk",  [ "$HOST$" ] ),
  ]
)

aggregation_rules["filesystems"] = (
  "Disk & Filesystems",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "Disk|MD" ),
      ( "multipathing", [ "$HOST$" ]),
      ( FOREACH_SERVICE, "$HOST$", "fs_(.*)", "filesystem", [ "$HOST$", "$1$" ] ),
      ( FOREACH_SERVICE, "$HOST$", "Filesystem(.*)", "filesystem", [ "$HOST$", "$1$" ] ),
  ]
)

aggregation_rules["filesystem"] = (
  "$FS$",
  [ "HOST", "FS" ],
  "worst",
  [
      ( "$HOST$", "fs_$FS$$" ),
      ( "$HOST$", "Filesystem$FS$$" ),
      ( "$HOST$", "Mount options of $FS$$" ),
  ]
)

aggregation_rules["multipathing"] = (
  "Multipathing",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "Multipath" ),
  ]
)

aggregation_rules["performance"] = (
  "Performance",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "CPU|Memory|Vmalloc|Kernel|Number of threads" ),
  ]
)

aggregation_rules["hardware"] = (
  "Hardware",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "IPMI|RAID" ),
  ]
)

aggregation_rules["networking"] = (
  "Networking",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "NFS|Interface|TCP" ),
  ]
)

aggregation_rules["checkmk"] = (
  "Check_MK",
  [ "HOST" ],
  "worst",
  [
       ( "$HOST$", "Check_MK|Uptime" ),
  ]
)

aggregation_rules["logfiles"] = (
  "Logfiles",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "LOG" ),
  ]
)
aggregation_rules["applications"] = (
  "Applications",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", "ASM|ORACLE|proc" ),
  ]
)

aggregation_rules["other"] = (
  "Other",
  [ "HOST" ],
  "worst",
  [
      ( "$HOST$", REMAINING ),
  ]
)

host_aggregations += [
  ( DISABLED, "Hosts", FOREACH_HOST, [ "tcp" ], ALL_HOSTS, "host", ["$1$"] ),
]
'''

#.
#   .--Declarations--------------------------------------------------------.
#   |       ____            _                 _   _                        |
#   |      |  _ \  ___  ___| | __ _ _ __ __ _| |_(_) ___  _ __  ___        |
#   |      | | | |/ _ \/ __| |/ _` | '__/ _` | __| |/ _ \| '_ \/ __|       |
#   |      | |_| |  __/ (__| | (_| | | | (_| | |_| | (_) | | | \__ \       |
#   |      |____/ \___|\___|_|\__,_|_|  \__,_|\__|_|\___/|_| |_|___/       |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |  Integrate all that stuff into WATO                                  |
#   '----------------------------------------------------------------------'

config.declare_permission("wato.bi_rules",
    _("Business Intelligence Rules and Aggregations"),
    _("Use the WATO BI module, create, modify and delete BI rules and aggregations in packs that you are a contact of"),
     [ "admin", "user" ])

config.declare_permission("wato.bi_admin",
    _("Business Intelligence Administration"),
    _("Edit all rules and aggregations for Business Intelligence, create, modify and delete rule packs."),
     [ "admin" ])

modes.update({
    "bi_packs"           : (["bi_rules"], ModeBIPacks),
    "bi_edit_pack"       : (["bi_rules", "bi_admin"], ModeBIEditPack),
    "bi_rules"           : (["bi_rules"], ModeBIRules),
    "bi_aggregations"    : (["bi_rules"], ModeBIAggregations),
    "bi_rule_tree"       : (["bi_rules"], ModeBIRuleTree),
    "bi_edit_rule"       : (["bi_rules"], ModeBIEditRule),
    "bi_edit_aggregation": (["bi_rules"], ModeBIEditAggregation),
})


#.
#   .--Rename Hosts--------------------------------------------------------.
#   |   ____                                   _   _           _           |
#   |  |  _ \ ___ _ __   __ _ _ __ ___   ___  | | | | ___  ___| |_ ___     |
#   |  | |_) / _ \ '_ \ / _` | '_ ` _ \ / _ \ | |_| |/ _ \/ __| __/ __|    |
#   |  |  _ <  __/ | | | (_| | | | | | |  __/ |  _  | (_) \__ \ |_\__ \    |
#   |  |_| \_\___|_| |_|\__,_|_| |_| |_|\___| |_| |_|\___/|___/\__|___/    |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |  Class just for renaming hosts in the BI configuration.              |
#   '----------------------------------------------------------------------'

class BIHostRenamer(ModeBI):
    def __init__(self):
        ModeBI.__init__(self)

    def rename_host(self, oldname, newname):
        renamed = 0
        for pack in self._packs.values():
            renamed += self.rename_host_in_pack(pack, oldname, newname)
        if renamed:
            self.save_config()
            return [ "bi" ] * renamed
        else:
            return []


    def rename_host_in_pack(self, pack, oldname, newname):
        renamed = 0
        for aggregation in pack["aggregations"]:
            renamed += self.rename_host_in_aggregation(aggregation, oldname, newname)
        for rule in pack["rules"].values():
            renamed += self.rename_host_in_rule(rule, oldname, newname)
        return renamed


    def rename_host_in_aggregation(self, aggregation, oldname, newname):
        node = aggregation["node"]
        if node[0] == 'call':
            if rename_host_in_list(aggregation["node"][1][1], oldname, newname):
                return 1
        return 0


    def rename_host_in_rule(self, rule, oldname, newname):
        renamed = 0
        nodes = rule["nodes"]
        for nr, node in enumerate(nodes):
            if node[0] in [ "host", "service", "remaining" ]:
                if node[1][0] == oldname:
                    nodes[nr] = (node[0], ( newname, ) + node[1][1:])
                    renamed = 1
            elif node[0] == "call":
                if rename_host_in_list(node[1][1], oldname, newname):
                    renamed = 1
        return renamed