서버 상태 가져와 출력하는 php코드
본문
[code]---html---
https://fonts.googleapis.com">
https://fonts.gstatic.com" crossorigin>
https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100..900&display=swap" rel="stylesheet">
body {
background-color: #1a1a1a;
color: #f9f9f9;
}
h2 {
font-size:1.5em;
}
section {
background-color: #333;
border-radius: 25px;
padding: 10px;
}
#main {
float:left;
max-width:55%;
}
#footer {
float:right;
max-width:45%;
}
.section_container {
display: flex;
justify-content: space-between;
align-items: center
}
.section_head {
}
.section_main {
}
.section_footer {
}
.domain {
}
.section_title {
}
.section_content {
}
.section_content_p {
}
.cpu_graph {
}
.bar {
width: 100%;
background-color: #f3f3f3;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
.bar span {
display: block;
height: 20px;
background-color: #2675b8;
border-radius: 4px;
transition: width .3s ease-in-out;
}
#processes {
max-width:45%
}
function updateServerStatus() {
$.getJSON('server_status.php', function(data) {
// Update the widths of the bar spans
$('.bar span').each(function() {
var id = $(this).attr('id');
if (id in data) {
$(this).css('width', data[id] + '%');
}
});
});
}
updateServerStatus();
setInterval(updateServerStatus, 3000); // 3초마다 서버 상태 업데이트
function updateServerStatus() {
$.getJSON('server_status.php', function(data) {
$('#cpu_usage').text('CPU usage: ' + data.cpu_usage + '%');
$('#memory_usage').text('Memory usage: ' + data.memory_usage + '%');
$('#disk_usage').text('Disk usage: ' + data.disk_usage + '%');
$('#processes').text(data.processes);
$('#cpu_sys').text('System usage: ' + data.cpu_sys + '%');
$('#cpu_user').text('User usage: ' + data.cpu_user + '%');
$('#cpu_iowait').text('I/O wait: ' + data.cpu_iowait + '%');
$('#cpu_steal').text('Steal: ' + data.cpu_steal + '%');
$('#cpu_cores').text('CPU cores: ' + data.cpu_cores);
$('#cpu_threads').text('CPU threads: ' + data.cpu_threads);
$('#upload').text('Upload: ' + data.upload);
$('#download').text('Download: ' + data.download);
});
}
updateServerStatus();
setInterval(updateServerStatus, 3000); // 3초마다 서버 상태 업데이트
현재 도메인을 동적으로 가져와 출력
Disk
%
---server_status.php---
// CPU 정보 가져오기
$load = sys_getloadavg();
$cpu_usage = round($load[0], 2);
$cpu_info = file('/proc/stat');
$cpu_info = explode(' ', trim($cpu_info[0]));
$cpu_info = array_slice($cpu_info, 1, 4);
$cpu_total = array_sum($cpu_info);
$cpu_idle = $cpu_info[3];
$cpu_usage = round(($cpu_total - $cpu_idle) / $cpu_total * 100, 2);
$cpu_sys = round($cpu_info[2] / $cpu_total * 100, 2);
$cpu_user = round($cpu_info[0] / $cpu_total * 100, 2);
$cpu_iowait = round($cpu_info[1] / $cpu_total * 100, 2);
$cpu_steal = round($cpu_info[3] / $cpu_total * 100, 2);
// CPU 코어 수와 스레드 수 가져오기
$cpu_cores = shell_exec("grep -P '^processor' /proc/cpuinfo|wc -l");
$cpu_threads = shell_exec("grep 'cpu cores' /proc/cpuinfo | uniq | awk '{print $4}'");
// RAM 사용량 가져오기
$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = round($mem[2]/$mem[1]*100, 2);
// 디스크 사용량 가져오기
$df = disk_free_space("/");
$dt = disk_total_space("/");
$du = $dt - $df;
$disk_usage = round($du / $dt * 100, 2);
// 현재 실행되고 있는 프로세스 가져오기
$processes = shell_exec("ps aux --sort=-%mem,-%cpu | awk '{print $11, $3, $4}'");
// 네트워크 상태 가져오기
$network = shell_exec("/sbin/ifconfig eth0 | grep 'RX bytes'|cut -d: -f2 | awk '{ print $1,$3 }'");
$network = explode(' ', trim($network));
$upload = $network[1];
$download = $network[0];
echo json_encode(array(
'cpu_usage' => $cpu_usage,
'cpu_sys' => $cpu_sys,
'cpu_user' => $cpu_user,
'cpu_iowait' => $cpu_iowait,
'cpu_steal' => $cpu_steal,
'cpu_cores' => $cpu_cores,
'cpu_threads' => $cpu_threads,
'memory_usage' => $memory_usage,
'disk_usage' => $disk_usage,
'processes' => $processes,
'upload' => $upload,
'download' => $download
));
?>[/code]
좋아요12
이 글을 좋아요하셨습니다
2kat님의 댓글
2kat 아이피 (222.♡.250.95) 작성일🥰