작업환경 (모니터링 서버 2대, 관리서버 1대)
CentOS Linux release 7.4.1708 (3대 모두 동일)
1. 메인 페이지 제작
# cat index.html <!-- 페이지 2초마다 새로고침 --> <?php header("refresh: 2;"); ?> <!DOCTYPE html> <html> <body style="text-align: center;"> <!-- 문서 타이틀 --> <head> <meta charset="utf-8"> <title>resource monitoring</title> </head> <!-- 제목설정 --> <style> h1 { background-color : #D6EAF8; margin: 10px; border: 10px dashed #283747; padding: 30px; color : skyblue; font-size :500%; } </style> <body> <h1 font size=+4 >Resource Check !!</h1> </body> <!-- 위의 제목과 내용 동일 <h1 style = " background-color : #D6EAF8; margin: 10px; border: 10px solid #283747; padding: 30px; color : skyblue; font-size :500%;"> testtest </h1> --> <!-- 페이지 내 시간 출력 설정 --> <div style="border: 3px dotted #283747; width: 440px; height: 60px; float:right; font-size:22px;font-weight:bold; margin:10px;"> <p>Date/Time: <span id="datetime"></span></p> <script> var dt = new Date(); document.getElementById("datetime").innerHTML = dt.toLocaleString(); </script> </div> </body> <!-- 모니터링 결과 --> <style type="text/css"> .tg {border-collapse:collapse;border-color:#93a1a1;border-spacing:0;} .tg td{background-color:#fdf6e3;border-color:#93a1a1;border-style:solid;border-width:0px;color:#002b36; font-family:Arial, sans-serif;font-size:20px;overflow:hidden;padding:20px 5px;word-break:normal;} .tg th{background-color:#657b83;border-color:#93a1a1;border-style:solid;border-width:0px;color:#fdf6e3; font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px;word-break:normal;} .tg .tg-2bn0{font-size:26px;text-align:center;vertical-align:top} .tg .tg-wrg0{font-size:22px;text-align:center;vertical-align:top} .tg .tg-bzci{font-size:20px;text-align:center;vertical-align:top} .tg .tg-lshq{font-family:"Trebuchet MS", Helvetica, sans-serif !important;;font-size:26px;text-align:center;vertical-align:top} .tg .tg-3tnq{background-color:#eee8d5;font-size:22px;text-align:center;vertical-align:top} .tg .tg-olr8{background-color:#eee8d5;font-size:20px;text-align:center;vertical-align:top} </style> <?php $lines = @file("data.txt") or $result = "파일을 읽을 수 없습니다."; if ($lines != null) { $result='<table class="tg" style="undefined;table-layout: fixed; width: 900px; margin:100px; position:absolute; top:40%; left:20%;"> '; $result.="<tr><th>IP</th><th>Memory 사용률</th><th>Disk 사용률</th><th>CPU 부하율</th></tr>"; for($i = 0; $i < count($lines); $i++){ $result .= "<tr>"; $arr = explode(",", $lines[$i]); for($j = 0;$j < 4;$j++){ $result .= "<td>{$arr[$j]}</td>"; } $result .= "</tr>"; } $result .= "</table>"; } ?> <p><?php echo $result;?></p> </html> |
2. 리소스 정보 받아오기
관리서버의 모니터링 페이지(index.html)는 resource.sh 스크립트를 실행하여 모니터링서버에 대한 정보를 받아와서 data.txt 에 저장한 데이터를 출력함.
모니터링서버는 root 접근 허용 및 패스워드가 동일하다는 전제에서 진행
# cat resource.sh #!/bin/bash ########################################################### 변수 CHECK PW="[패스워드]" SERVER1="[모니터링서버 IP]" SERVER2="[모니터링서버 IP]" SERVER3="[모니터링서버 IP]" mem=`free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'` disk=`df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}'` cpu=`top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}'` SERVER_CMD_1="sshpass -p $PW ssh -o StrictHostKeyChecking=no root@$SERVER1" SERVER_CMD_2="sshpass -p $PW ssh -o StrictHostKeyChecking=no root@$SERVER2" SERVER_CMD_3="sshpass -p $PW ssh -o StrictHostKeyChecking=no root@$SERVER3" FILE_DIR=[데이터가 저장될 디렉터리] ########################################################### 모니터링 서버 1 echo $SERVER1 > $FILE_DIR/data.txt.tmp $SERVER_CMD_1 free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }' >> $FILE_DIR/data.txt.tmp $SERVER_CMD_1 df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}' >> $FILE_DIR/data.txt.tmp $SERVER_CMD_1 top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}' >> $FILE_DIR/data.txt.tmp paste -sd "," $FILE_DIR/data.txt.tmp > $FILE_DIR/data.txt ########################################################### 모니터링 서버 2 echo $SERVER2 > $FILE_DIR/data.txt.tmp $SERVER_CMD_2 free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }' >> $FILE_DIR/data.txt.tmp $SERVER_CMD_2 df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}' >> $FILE_DIR/data.txt.tmp $SERVER_CMD_2 top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}' >> $FILE_DIR/data.txt.tmp paste -sd "," $FILE_DIR/data.txt.tmp >> $FILE_DIR/data.txt ########################################################### 모니터링 서버 3 #echo $SERVER3 > $FILE_DIR/data.txt.tmp #$SERVER_CMD_3 free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }' >> $FILE_DIR/data.txt.tmp #$SERVER_CMD_3 df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}' >> $FILE_DIR/data.txt.tmp #$SERVER_CMD_3 top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}' >> $FILE_DIR/data.txt.tmp #paste -sd "," $FILE_DIR/data.txt.tmp >> data.txt ########################################################### 임시 파일 삭제 rm -rf $FILE_DIR/data.txt.tmp |
# cat data.txt |
스크립트를 통해 저장된 리소스 데이터는 위와 같이 저장됨.
콤마(,)를 통해 데이터들이 테이블에 순차적으로 입력됨
3. 리소스 정보 갱신하기
crontab에서 리소스 체크 스크립트가 1분마다 돌아가가도록 실행.
crontab 실행 중 에러 발생시 resource.log 에 저장되도록 설정.
# crontab -l * * * * * su - root -c [스크립트실행 디렉터리]/resource.sh > [데이터가 저장될 디렉터리]/resource.log 2>&1 |
'개발 > HTML & PHP' 카테고리의 다른 글
오른쪽클릭, 드래그, 선택방지 (1) | 2020.11.17 |
---|---|
html 에서 자동 새로고침하기 (0) | 2020.11.05 |