monit.jsp 16.7 KB
<%@page language="java" contentType="text/html;charset=utf-8" %>
<%@page import="com.ui.contants.ProjectConstant"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap-datetimepicker.css"/>
    <link href="<%=basePath %>js/bootstrap-plugin/css/bootstrap.table.css" rel="stylesheet" media="screen"/>
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap-responsive.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/fullcalendar.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.main.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.grey.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/jquery-ui.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/uniform.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/select2.css"/>
    <link rel="stylesheet" href="<%=basePath %>js/jstree/themes/proton/style.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/select2.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/yoho.css"/>
    <script src="<%=basePath %>js/excanvas.min.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jquery-1.12.0.min.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jquery-ui.custom.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>/js/bootstrap.min.js"></script>
    <script src="<%=basePath %>/js/unicorn.js"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/moment-with-locales.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/bootstrap-datetimepicker.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/global.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.pagination.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.table.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.dialog.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.panel.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.alerts.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.accordion.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.breadcrumb.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.validate.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/layer/layer.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.select.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jstree/jstree.min.js"></script>
    <script src="<%=basePath %>js/jquery.toaster.js"></script>
    <script>
        var contextPath = '<%=basePath %>';
    </script>
    <title>YOHO!运维</title>
</head>
<body>

<!-- 头部 -->
<div id="head">
</div>
<!-- 右侧具体内容 -->
<div id="content">

    <div id="breadcrumb">
        <a href="#" title="Go to Home" class="tip-bottom"><i
                class="icon-home"></i> Home</a> <a href="#" class="current">Monit</a>
    </div>

    <div class="container-fluid">
        <div class="widget-box">
            <div class="widget-title">
                <h5>Monit信息查询</h5>
            </div>
            <div class="widget-content nopadding">
                <div class="widget-title" style="height: 53px;">
                    <div>
                        <div class="form-inline" role="form" id="inBoxQueryDiv"
                             style=" margin-top: 12px;margin-left: 25px;float: left;">
                            <div class="input-group" style="float: left; margin-left: 20px">
                                <span class="input-group-addon">IP:</span>
                                <input type="text" id="hostIp" name="hostIp" class="form-control" placeholder=""/>
                            </div>
                            <div class="input-group" style="float: left;margin-left: 20px">
                                <span class="input-group-addon">标签:</span>
                                <input type="text" id="tag" name="hostIp" class="form-control" placeholder=""/>
                            </div>
                            <button id="searchHostInfoBtn" class="btn btn-primary" style="margin-left: 18px;">搜索
                            </button>
                        </div>
                    </div>
                </div>
                <div id="monitorTable">
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="serviceModal" tabindex="-1" role="dialog" aria-labelledby="serviceModalLable"
         aria-hidden="true"
         style="height: 1000px;overflow :auto">
        <div class="modal-dialog" style="width: 1500px">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="serviceModalTitle"></h4>
                </div>
                <div class="modal-body">
                    <div id="body_div">
                        <div class="blanksegmentation"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal -->
        </div>
    </div>

</div>


<script src="<%=basePath %>script/common/genarate_left_panel.js?v=<%=ProjectConstant.MENU_VERSION %>"></script>
<script>
    $("#li_monitor").addClass("active open");
    $("#li_monit").addClass("active");
</script>
<script>
    //加载页面 初始化页面
    $(
            queryAllMonitorService()
    );


    //设置定时器,一分钟刷新一次页面
    /*timename = setInterval("queryAllMonitorService();", 60 * 1000);*/

    //查询monitor所有服务信息,并且刷新页面展示
    function queryAllMonitorService() {
        $("#monitorTable").table(
                {
                    url: contextPath + "/monit/service/allMonit",
                    method: 'post',
                    dataType: 'json',
                    data: {},
                    striped: true,
                    panelClass: "panel-success",
                    loadFilter: function (data) {
                        console.log(data.data)
                        return defaultLoadFilter(data);
                    },
                    columns: [
                        {
                            title: "IP",
                            field: "ip"
                        },
                        {
                            title: "标签",
                            field: "tags"
                        },
                        {
                            title: "详情",
                            formatter: function (value, rowData, rowIndex) {
                                var div = $("<div>");

                                //详情
                                var editBtn = $("<button data-target='#updateModal' data-toggle='modal'>").addClass("btn btn-xs btn-success").html("详情").appendTo(div);
                                editBtn.click(function () {
                                    searchService(rowData.monitId);
                                });
                                return div;
                            }
                        }
                    ]
                }
        );
    }

    function searchService(monitId) {
        $.ajax({
            url: contextPath + '/monit/service/searchService',
            type: 'POST',
            dataType: 'json',
            data: {
                monitorId: monitId
            },
            success: function (resp) {

                console.log("search rep:" + resp);

                genarateMonitorDashboard(resp.data);

                $("#serviceModalTitle").text("Monit 详情");

                $("#serviceModal").modal('show');
            },
            error: function (e) {

                localAlert("查询失败", "查询失败,请稍后再试:" + e.data);

                $("#serviceModal").modal("hide");

                $("#monitorTable").table("load");
            }
        });
    }


    $("#searchHostInfoBtn").click(
            function () {
                var ip = $("#hostIp").val();
                var tag = $("#tag").val();

                $("#monitorTable").table("load", {
                    query: ip + ";" + tag
                });
            }
    );

    //生成monitor dashboard展示页面
    function genarateMonitorDashboard(data) {

        var innerHTML = "";
        var info = data;
        console.log("monitorId: " + info.monitorId);
        innerHTML += "<div class='blanksegmentation'></div>";
        innerHTML += "<div class='panel panel-default'>";

        //genarate panel header httpInfo
        innerHTML += genarateHttpInfoDiv(info.httpInfo, info.monitorId);
        //genarate panel body serviceInfos events
        innerHTML += genarateServiceInfosTables(info.servicesInfo, info.eventsInfo, info.monitorId);

        innerHTML += "</div>";

        document.getElementById("body_div").innerHTML = innerHTML;
    }

    //HttpInfo panel header
    function genarateHttpInfoDiv(httpInfo, monitorId) {
        console.log("httpInfo: " + httpInfo);
        var innerHTML = "<div class='panel-heading'>";
        innerHTML += genarateSpanText("address: " + httpInfo.address, 20);
        innerHTML += genarateSpanText("port: " + httpInfo.port, 20);
        innerHTML += genarateSpanText("ssl: " + httpInfo.ssl, 20);
        innerHTML += "<a href=\"javascript:deleteBtnOnClicked(\'" + monitorId + "\')\"><span class='glyphicon glyphicon-trash' style='color: #FF0000;font-size:18px'></span></a>";
        innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;";
        innerHTML += "<a href=\"javascript:updateBtnOnClicked(\'" + monitorId + "\')\"><span class='glyphicon glyphicon-upload' style='color: #52ff90;font-size:18px'></span></a>";
        innerHTML += "</div>";
        return innerHTML;
    }

    //serviceInfos Table events Table
    function genarateServiceInfosTables(serviceInfos, events, monitorId) {

        var innerHTML = "<div class='panel-body'>";

        //genarate serviceInfo table<th>
        innerHTML += "<table class='table table-bordered mb30'>";
        innerHTML += "<thead><tr><th style='width: 25%;'>serviceName</th><th style='width: 20%;'>serviceStatus</th><th style='width: 10%;'>system</th>";
        innerHTML += "<th style='width: 15%;'>start</th><th style='width: 15%;'>stop</th><th style='width: 15%;'>restart</th></tr></thead><tbody>";

        //genarate serviceInfo table<tr>
        for (var i in serviceInfos) {
            var service = serviceInfos[i];
            innerHTML += "<tr>"
            innerHTML += "<td>" + service.serviceName + "</td>";

            //status == NORMAL status font turn green, start button unclickable, and the others potition, turn red stop button unclickable
            if (serviceInfos[i].serviceStatus == "NORMAL") {
                innerHTML += "<td style='color:#008000;font-weight:bold'>" + service.serviceStatus + "</td>";
                innerHTML += "<td>" + service.system + "</td>";
                innerHTML += "<td><button class='btn btn-success btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'start')\">Start</button></td>";
                innerHTML += "<td><button class='btn btn-danger btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'stop')\">Stop</button></td>";
                innerHTML += "<td><button class='btn btn-success btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'restart')\">Restart</button></td>";
            } else {
                innerHTML += "<td style='color:#FF0000;font-weight:bold'>" + serviceInfos[i].serviceStatus + "</td>";
                innerHTML += "<td>" + serviceInfos[i].system + "</td>";
                innerHTML += "<td><button class='btn btn-success btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'start')\">Start</button></td>";
                innerHTML += "<td><button class='btn btn-danger btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'stop')\">Stop</button></td>";
                innerHTML += "<td><button class='btn btn-success btn-rounded' onclick=\"actionBtnOnClicked('" + monitorId + "','" + service.serviceName + "', 'restart')\">Restart</button></td>";
            }
            innerHTML += "</tr>";
        }
        innerHTML += "</tbody></table>";

        //genarate events table<th>
        innerHTML += "<table class='table table-bordered mb30'>";
        innerHTML += "<thead><tr><th>collected_sec</th><th>service</th><th>type</th>";
        innerHTML += "<th>message</th></tr></thead><tbody>";
        for (var i in events) {
            var event = events[i];
            innerHTML += "<tr>"
            innerHTML += "<td>" + event.collected_sec + "</td>";
            innerHTML += "<td>" + event.service + "</td>";
            innerHTML += "<td>" + event.type + "</td>";
            innerHTML += "<td>" + event.message + "</td>";
            innerHTML += "</tr>";
        }
        innerHTML += "</tbody></table>"

        innerHTML += "</div>";
        return innerHTML;
    }

    //生成data的json字符串
    function buildBody(monitorId, serviceName, actionType) {

        var actionItem = new Object();
        actionItem['hostId'] = monitorId;
        actionItem['service'] = serviceName;
        actionItem['action'] = actionType;

        var actions = new Array();
        actions.push(actionItem);

        var body = new Object();
        body['actions'] = actions;
        return JSON.stringify(body);
    }

    //启停按钮的动作方法
    function actionBtnOnClicked(monitorId, serviceName, actionType) {

        var body = buildBody(monitorId, serviceName, actionType);
        console.log(body);

        if (actionType == "delete") {
            deleteBtnOnClicked(monitorId);
            return;
        }

        $.ajax({
            "url": "/monit/hosts/oneAction",
            "type": 'POST',
            "dataType": "json",
            "data": {
                hostId: monitorId,
                service: serviceName,
                action: actionType
            },
            success: function (resp) {
                searchService(monitorId);
            },
            error: function (e) {
                $("#serviceModal").modal("hide");

                queryAllMonitorService();
            }
        });
    }

    //delete按钮的动作方法
    function deleteBtnOnClicked(monitorId) {
        var url = "/monit/hosts/delete/" + monitorId;
        console.log(url)
        $.ajax({
            "url": url,
            "type": 'POST',
            "dataType": "json",
            success: function (data) {

                $("#serviceModal").modal("hide");

                queryAllMonitorService();
            },
            error: function (e) {

                $("#serviceModal").modal("hide");

                queryAllMonitorService();
            }
        });
    }


    //delete按钮的动作方法
    function updateBtnOnClicked(monitorId) {
        searchService(monitorId);
    }

    function genarateSpanText(text, size) {
        return "<span style='font-size:" + size + "px;margin-right:20px'>" + text + "</span>";
    }

</script>
</body>
</html>