-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-chart-v2.php
163 lines (152 loc) · 5.01 KB
/
monitor-chart-v2.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
162
163
<?php
/**
* web监控php-fpm status数据
* @author Cqiu
* @date 2019-9-25
*/
if (isset($_REQUEST['data'])) {
date_default_timezone_set("Asia/Chongqing");
header("Content-Type: text/event-stream\n\n");
$url = 'http://xxxx.com/status';
$r = [
'listen_queue' => 0,
//'max_listen_queue' => 0,
//'listen_queue_len' => 0,
'idle_processes' => 0,
'active_processes' => 0,
//'total_processes' => 0,
//'max_active_processes' => 0,
//'max_children_reached' => 0
];
// 注意:相对ajax方案,这个是单线程阻塞的!时间准确性还没有对上,目前有延迟问题。
$ctx = stream_context_create([
'http' => [
'timeout' => 2,
],
]);
while (1) {
$result = json_decode(file_get_contents($url . '?json', 0, $ctx), 1);
foreach ($result as $k => $v) {
$k = strtr($k, ' ', '_');
isset($r[$k]) && $r[$k] = $v;
}
echo 'data: ', json_encode($r), "\n\n";
ob_flush();
flush();
sleep(1);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="https://tool.lu/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.bootcss.com/highstock/6.0.3/highstock.js"></script>
</head>
<body>
<div id="container" style="min-width:400px;height:400px"></div>
<script>
var dataQueue = [];
Highcharts.setOptions({
global: {
useUTC: false
}
});
// 填充初始值,才能有宽度
var fillData = [], time = (new Date()).getTime();
for (var i = -60; i <= 0; i += 1) {
fillData.push([time + i * 1000, 0]);
}
function activeLastPointToolip(chart) {
var points = chart.series[0].points;
chart.tooltip.refresh(points[points.length - 1]);
}
var chart = Highcharts.chart('container', {
chart: {
type: 'spline',
marginRight: 10,
events: {
load: function () {
var chart = this, series = this.series;
activeLastPointToolip(chart);
setInterval(function () {
var result = dataQueue.shift();
var time = (new Date()).getTime();
//如果没有数据,时间线会拉得很长,点不可看
if (!result) {
for (var i in series) {
series[i].addPoint([time, 0], true, true);
}
return;
}
var k = 0;
for (var i in result) {
if (i !== 'start_time') {
//console.log(i, result[i] * 1);
series[k].addPoint([
time,
result[i] * 1
], true, true);//, true
k++;
}
}
//activeLastPointToolip(chart);
}, 1000);
/*
var series = this.series[0],
chart = this;
activeLastPointToolip(chart);
setInterval(function () {
var x = (new Date()).getTime(), // 当前时间
y = Math.random(); // 随机值
series.addPoint([x, y], true, true);
activeLastPointToolip(chart);
}, 1000);
*/
}
}
},
title: {
text: 'php-fpm status实时数据'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: null
}
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
series: [{
"name": "listen_queue",
"data": fillData
}, {
"name": "idle_processes",
"data": fillData
}, {
"name": "active_processes",
"data": fillData
}]
});
var evtSource = new EventSource('?data');
evtSource.onmessage = function (e) {
dataQueue.push(JSON.parse(e.data));
}
</script>
</body>
</html>