Network Change Detected
"
405 | ).css({
406 | "display": "block",
407 | }).delay(5000).fadeOut();
408 | }
409 |
410 | error = (error) => {
411 | errorHandler(error)
412 | }
413 |
414 | });
415 | }
416 |
417 | function startEventListeners(network, networkData, settings) {
418 |
419 | var font_size = 0;
420 |
421 | // function launches all event listeners for the network, and buttons.
422 |
423 | network.on("doubleClick", function (params) { //double click on node listener
424 | if (params.nodes[0] != undefined) {
425 | $('.fabs').hide();
426 |
427 | let hostMonitoringAddress = '';
428 |
429 | if (location.href.indexOf('/icingaweb2') > 1) {
430 | console.log("YOUR MOTHER WAS A HAMPSTER")
431 | hostMonitoringAddress = '/icingaweb2/monitoring/host/show?host='
432 | } else {
433 |
434 | hostMonitoringAddress = '/monitoring/host/show?host=';
435 | }
436 | location.href = './network#!' + hostMonitoringAddress + params.nodes[0]; //redirect to host info page.
437 |
438 | }
439 | });
440 |
441 | network.on("selectNode", function (params) { //on selecting node, background of label is made solid white for readabillity.
442 | var clickedNode = network.body.nodes[params.nodes[0]];
443 | font_size = clickedNode.options.font.size;
444 | clickedNode.setOptions({
445 | font: {
446 | size: 30,
447 | background: 'white',
448 | }
449 | });
450 | });
451 |
452 | network.on("deselectNode", function (params) { //on node deselect, label set back to transparent.
453 |
454 | var clickedNode = network.body.nodes[params.previousSelection.nodes[0]];
455 | clickedNode.setOptions({
456 | font: {
457 | size: font_size,
458 | background: 'none',
459 | }
460 | });
461 |
462 | });
463 |
464 | $('#edit-btn').click(function (params) { //on edit
465 |
466 |
467 | $('#notifications').append().html('
Editing Node Positions');
468 |
469 | network.setOptions({ //unlock nodes for editing
470 | nodes: {
471 | fixed: false
472 | },
473 | });
474 |
475 | $('.edit-fab').toggleClass('scale-out'); // show secondary FABs
476 | if ($('.edit-fab').hasClass('scale-out')) { //if already scaled out, second click hides secondary FABs and locks nodes
477 | network.setOptions({
478 | nodes: {
479 | fixed: true
480 | }
481 | });
482 | }
483 | });
484 |
485 |
486 | $('#edit-btn-delete').click(function () {
487 |
488 | if (confirm("Reset All Network Positions?")) {
489 |
490 |
491 |
492 | success = () => {
493 | setTimeout(function () {
494 |
495 | window.location.replace("./network"); //on succes redirect to network.
496 |
497 | }, 2000);
498 | }
499 |
500 | error = (error) => {
501 | errorHandler(error);
502 | }
503 |
504 | var promise = storeNodePositions('RESET').then(success, error);
505 | }
506 |
507 | });
508 |
509 | $('#edit-btn-save').click(function () { //on save
510 |
511 | network.setOptions({
512 | nodes: {
513 | fixed: true
514 | }
515 | });
516 |
517 | network.storePositions(); //visjs function that adds X, Y coordinates of all nodes to the visjs node dataset that was used to draw the network.
518 |
519 | success = () => {
520 | $('#notifications').append().html('
Nodes Positions Saved');
521 |
522 | }
523 |
524 | error = (error) => {
525 | errorHandler(error);
526 | }
527 |
528 |
529 | var promise = storeNodePositions(networkData.nodes._data).then(success, error);
530 |
531 | });
532 |
533 | $('#edit-btn-fullscreen').click(() => {
534 |
535 | if (settings.fullscreen_mode === 'network') {
536 | window.location.replace("./network?showFullscreen");
537 | } else {
538 | window.location.replace("./statusGrid?showFullscreen")
539 | }
540 |
541 | });
542 |
543 | if (settings['enable_director'] === true) {
544 |
545 | $('#dependency-fabs').show();
546 |
547 | $('#dependency-btn').click(() => {
548 |
549 | network.setOptions({
550 | nodes: {
551 | fixed: false
552 | }
553 | });
554 |
555 | $('.dependency-fab').toggleClass('scale-out'); // show secondary FABs
556 |
557 |
558 | if (!$('.edit-fab').hasClass('scale-out')) { //if already scaled out, second click hides secondary FABs and locks nodes
559 | $('.edit-fab').toggleClass('scale-out');
560 | }
561 |
562 | $('#notifications').append().html('
Editing Dependencies');
563 |
564 | if (!settings.default_dependency_template) {
565 | alert('No Default Director Dependency Template Selected, Please Create or Select One.');
566 | window.location.replace("./settings");
567 | }
568 |
569 | $("#notification").html(
570 | "
Editing Dependencies (Child -----> Parent)
"
571 | ).css({
572 | "display": "block",
573 | })
574 |
575 | network.setOptions({
576 | edges: {
577 | arrows: {
578 | from: true
579 | }
580 | }
581 | });
582 |
583 | network.off('doubleClick');
584 |
585 | network.off('selectNode');
586 |
587 | network.off('deselectNode');
588 |
589 | $('#dependency-btn').off();
590 |
591 | $('#edit-btn').off();
592 |
593 | startDependencyModeListeners(networkData, network, settings);
594 |
595 | });
596 | }
597 |
598 | }
599 |
600 | function startDependencyModeListeners(networkData, network, settings) {
601 |
602 | var dependencies = [];
603 |
604 | var dependency = [];
605 |
606 | var updatedNodes = [];
607 |
608 | var container = document.getElementById('dependency-network');
609 |
610 |
611 | network.on("selectNode", function (params) { //on selecting node, background of label is made solid white for readabillity.
612 |
613 |
614 | var selectedNode = network.body.nodes[params.nodes[0]];
615 |
616 | dependency.push(selectedNode.id);
617 |
618 | if (dependency.length === 2) {
619 |
620 | drawnDependency = {
621 | "object_name": dependency[0] + " __to__ " + dependency[1],
622 | "object_type": "apply",
623 | "assign_filter": "host.name=%22" + dependency[0] + "%22",
624 | "imports": [settings.default_dependency_template],
625 | "apply_to": "host",
626 | "parent_host": dependency[1],
627 | }
628 |
629 | networkData.edges.update({
630 | id: drawnDependency.object_name,
631 | from: dependency[1],
632 | to: dependency[0]
633 | });
634 |
635 | dependencies.push(drawnDependency);
636 |
637 | dependency = [];
638 |
639 | }
640 |
641 | });
642 |
643 | network.on("click", function (params) {
644 | if (params.nodes[0] === undefined) {
645 | dependency = [];
646 | }
647 | });
648 |
649 | $('#dependency-btn').click(() => {
650 |
651 | $('.dependency-fab').toggleClass('scale-out'); // show secondary FABs
652 | if ($('.dependency-fab').hasClass('scale-out')) { //if already scaled out, second click hides secondary FABs and locks nodes
653 | network.setOptions({
654 | nodes: {
655 | fixed: true
656 | },
657 | edges: {
658 | arrows: {
659 | from: false
660 | }
661 | }
662 | });
663 | }
664 |
665 | network.off('doubleClick');
666 |
667 | network.off('selectNode');
668 |
669 | network.off('deselectNode');
670 |
671 | $('#dependency-btn').off();
672 |
673 | startEventListeners(network, networkData, settings);
674 |
675 |
676 | });
677 |
678 | $('#edit-btn').click(() => {
679 |
680 | $('#notifications').append().html('Editing Node Positions');
681 |
682 | $('.dependency-fab').toggleClass('scale-out');
683 | network.setOptions({
684 | nodes: {
685 | fixed: false
686 | },
687 | edges: {
688 | arrows: {
689 | from: false
690 | }
691 | }
692 | });
693 |
694 | $('.edit-fab').toggleClass('scale-out')
695 |
696 | network.off('doubleClick');
697 |
698 | network.off('selectNode');
699 |
700 | network.off('deselectNode');
701 |
702 | $('#dependency-btn').off();
703 | $('#edit-btn').off();
704 |
705 | startEventListeners(network, networkData, settings);
706 |
707 | })
708 |
709 |
710 | $('#dependency-btn-undo').click(() => {
711 |
712 | if (dependencies.length === 0) {
713 | alert("Nothing to Undo");
714 | return;
715 | }
716 |
717 | removedDependency = dependencies.pop();
718 |
719 |
720 | networkData.edges.remove({
721 | id: removedDependency.object_name
722 | });
723 |
724 | });
725 |
726 |
727 |
728 | $('#dependency-btn-save').click(function () { //on save
729 |
730 |
731 | importDependencies(dependencies);
732 |
733 |
734 | network.storePositions(); //visjs function that adds X, Y coordinates of all nodes to the visjs node dataset that was used to draw the network.
735 |
736 | $.ajax({ //ajax request to store into DB
737 | url: "./dependency_plugin/module/storeNodePositions",
738 | type: 'POST',
739 | data: {
740 | json: JSON.stringify(networkData.nodes._data)
741 | },
742 | });
743 |
744 | });
745 |
746 | }
747 |
748 | function importDependencies(dependencies) {
749 |
750 |
751 | for (i = 0; i < dependencies.length; i++) {
752 |
753 | $.ajax({
754 | url: "./director/dependency",
755 | type: 'POST',
756 | headers: {
757 | 'Accept': 'application/json'
758 | },
759 | data: JSON.stringify(dependencies[i]),
760 | success: () => {
761 | deployChanges();
762 | },
763 | error: function (data) {
764 | console.log(data);
765 | alert('Adding dependency Unsuccessful:\n\n' + data.responseJSON['message']);
766 | return;
767 | }
768 | });
769 |
770 | }
771 |
772 |
773 |
774 |
775 | }
776 |
777 | function deployChanges() {
778 |
779 | $.ajax({
780 | url: "./director/config/deploy",
781 | type: 'POST',
782 | headers: {
783 | 'Accept': 'application/json'
784 | },
785 | success: function (data) {
786 |
787 | $('#notifications').append().html('Dependencies Saved Successfully');
788 | }
789 |
790 | });
791 |
792 | }
793 |
794 | function Host(hostData) {
795 |
796 | //function accepts raw host data pulled from icinga 2 api, and formats it into a more usable format
797 | //while providing functions to add dependencies and position
798 |
799 | determineStatus = (state, wasReachable) => {
800 |
801 | if (state === 0) {
802 | return 'UP';
803 | } else if (state === 1 && !wasReachable) {
804 |
805 | return 'UNREACHABLE'
806 |
807 | } else {
808 |
809 | return 'DOWN';
810 | }
811 |
812 | }
813 |
814 | this.name = '' || hostData.name;
815 | this.status = determineStatus(hostData.state, hostData.last_reachable);
816 | this.description = '' || hostData.display_name;
817 | this.hasDependencies = false;
818 | this.parents = [];
819 | this.isLargeNode = false;
820 | this.group = '' || hostData.groups;
821 | this.children = [];
822 | this.position = {
823 | x: 0,
824 | y: 0
825 | };
826 | this.hasPositionData = false;
827 |
828 | this.addParent = (parent) => {
829 | this.parents.push(parent);
830 | this.hasDependencies = true;
831 | }
832 |
833 | this.addChild = (Child) => {
834 | this.children.push(Child);
835 | this.hasDependencies = true;
836 |
837 | if (this.children.length > 3) {
838 | this.isLargeNode = true;
839 | }
840 | }
841 |
842 | this.setPositionData = (data) => {
843 | this.position.x = data.node_x;
844 | this.position.y = data.node_y;
845 | this.hasPositionData = true;
846 | }
847 | }
848 |
849 | function HostArray() {
850 |
851 | this.hostObject = {};
852 |
853 | this.isNewNetwork = true; //if there any node has position data
854 |
855 | this.length = 0;
856 |
857 | this.addHost = (hostData) => {
858 | this.hostObject[hostData.name] = new Host(hostData)
859 |
860 | this.length++;
861 | }
862 |
863 | this.addDependency = (dependency) => {
864 |
865 | childName = dependency.child_host_name;
866 |
867 | parentName = dependency.parent_host_name;
868 |
869 | if (isInHosts(parentName)) {
870 |
871 | this.hostObject[parentName].addChild(childName);
872 |
873 | }
874 |
875 | if (isInHosts(childName)) {
876 |
877 | this.hostObject[childName].addParent(parentName);
878 | }
879 |
880 | }
881 |
882 | this.addPosition = (positionData) => {
883 |
884 | name = positionData.node_name;
885 |
886 | if (isInHosts(name)) {
887 |
888 | this.hostObject[name].setPositionData(positionData);
889 |
890 | this.isNewNetwork = false;
891 |
892 | }
893 |
894 | }
895 |
896 | isInHosts = (name) => {
897 |
898 | return (this.hostObject[name] != undefined);
899 | }
900 |
901 |
902 | }
--------------------------------------------------------------------------------