monit.js
7.84 KB
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
$(
//加载页面 初始化页面
queryAllMonitorService()
);
//设置定时器,一分钟刷新一次页面
/*timename = setInterval("queryAllMonitorService();", 60 * 1000);*/
//查询monitor所有服务信息,并且刷新页面展示
function queryAllMonitorService() {
$("#monitorTable").table(
{
url: contextPath + "/monit/service/allMonit",
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);
});
return div;
}
}
],
/* data: data*/
});
}
function searchService(rowData) {
$.ajax({
"url": "/monit/service/searchService",
"type": 'POST',
"dataType": "json",
"data": {
monitorId: rowData.monitorId
},
success: function (resp) {
console.log(resp);
genarateMonitorDashboard(resp.data);
},
error: function (e) {
localAlert("Error", "操作失败:" + e.data);
$("#monitorTable").reload();
}
});
}
$("#searchHostInfoBtn").click(
function () {
}
);
//生成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 += "</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) {
console.log(resp);
queryAllMonitorService();
},
error: function (e) {
localAlert("Error", "操作失败:" + e.data);
queryAllMonitorService();
}
});
}
//delete按钮的动作方法
function deleteBtnOnClicked(monitorId) {
var url = "/monit/hosts/delete/" + monitorId;
console.log(url)
$.ajax({
"url": url,
"type": 'POST',
"dataType": "json",
success: function (data) {
queryAllMonitorService();
},
error: function (e) {
queryAllMonitorService();
}
});
}
function genarateSpanText(text, size) {
return "<span style='font-size:" + size + "px;margin-right:20px'>" + text + "</span>";
}