-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax.php
161 lines (157 loc) · 7.14 KB
/
ajax.php
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
<?php
require_once 'config.php';
switch ($_REQUEST['type']) {
case 'server':
// 获取服务器列表
$sql = "SELECT `id`, `name`, `ip_addr`, `last_update`, `cpu`, `mem` FROM `hosts`";
$result = $db_con->query($sql);
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$last = $row['last_update'];
$cpu = $row['cpu'];
$mem = $row['mem'];
if ($row['cpu'] > 70) {
$status = 'warning';
$status_tip = '服务器压力较大';
$status_tip_end = '压力较大' . "<script type=\"text/javascript\">mdui.snackbar({
message: '$name 压力较大',
position: 'bottom',
buttonText: '查看',
onButtonClick: function(){
$('html,body').animate({scrollTop:$('#server-$id').offset().top});
}
});</script>";
} elseif ($row['mem'] > 70) {
$status = 'warning';
$status_tip = '服务器内存占用较高';
$status_tip_end = '内存占用较高' . "<script type=\"text/javascript\">mdui.snackbar({
message: '$name 服务器内存占用较高',
position: 'bottom',
buttonText: '查看',
onButtonClick: function(){
$('html,body').animate({scrollTop:$('#server-$id').offset().top});
}
});</script>";
} else {
$status = 'check';
$status_tip = "负载正常";
$status_tip_end = '正常';
}
echo <<<EOF
<!-- Start Server-$id -->
<div id="server-$id-status">
<h2 class="mdui-text-color-blue mdui-text-center text server-name" id="server-$id">$name</h2>
<div id="server-$id-status-table">
<div class="mdui-table-fluid">
<table class="mdui-table mdui-table-hoverable">
<thead>
<tr>
<th></th>
<th>CPU占用</th>
<th>内存</th>
<th>最后提交</th>
<th>负载情况</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td id="server-$id-status-cpu">$cpu%</td>
<td id="server-$id-status-memory">$mem%</td>
<td id="server-$id-status-lastupdate">$last</td>
<td id="server-$id-status-available"><i mdui-tooltip="{content: '$status_tip', position: 'bottom'}" class="mdui-icon material-icons">$status</i> $status_tip_end</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="server-$id-status-image" class="server-status-image">
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
var title = {
text: '服务器资源占用图表'
};
var yAxis = {
title: {
text: '资源占用(%)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
};
var tooltip = {
valueSuffix: '%'
};
var legend = {
layout: 'vertical',
align: 'right',
verticalAlign: 'left',
borderWidth: 0
};
var series = [{
name: 'CPU',
data: [
EOF;
// 取最后十条记录
$sql1 = "SELECT `cpu` FROM `resource_stat` WHERE `by_host` = $id ORDER BY `submit_time` DESC LIMIT 10";
$result1 = $db_con->query($sql1);
while ($row1 = mysqli_fetch_array($result1)) {
echo $row1['cpu'];
echo ',';
}
echo ']
},{name: \'内存\',
data: [';
$sql5 = "SELECT `memory` FROM `resource_stat` WHERE `by_host` = $id ORDER BY `submit_time` DESC LIMIT 10";
$result5 = $db_con->query($sql5);
while ($row5 = mysqli_fetch_array($result5)) {
echo $row5['memory'];
echo ',';
}
echo ']}];';
echo "
var xAxis = {
categories: [";
$sql2 = "SELECT * FROM `resource_stat` WHERE `by_host` = $id ORDER BY `resource_stat`.`submit_time` DESC LIMIT 10";
$result2 = $db_con->query($sql2);
while ($row2 = mysqli_fetch_array($result2)) {
echo "'";
echo $row2['submit_time'];
echo "',";
}
echo ']' . PHP_EOL . '};';
echo '
var json = {};
json.title = title;
json.xAxis = xAxis;
json.yAxis = yAxis;
json.tooltip = tooltip;
json.legend = legend;
json.series = series;
';
echo " $('#server-$id-status-image').highcharts(json);";
echo "})</script>
<!-- End Server-$id -->";
}
break;
case 'menu':
// 获取菜单
$sql = "SELECT `id`, `name` FROM `hosts`";
$result = $db_con->query($sql);
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
echo <<<EOF
<li class="mdui-list-item mdui-ripple"><i class="mdui-list-item-icon mdui-icon material-icons"></i>
<div class="mdui-list-item-content"><a href="#server-$id">$name</a></div>
</li>
EOF;
}
default:
break;
}