“Putty + mRemoteng 를 주로 사용하다 Xshell 을 사용하면서 동시에 Linux 서버를 모니터링할 수 있는 표준화된 스크립트가 있었으면 하는 요구사항으로 모니터링 스크립트를 작성했습니다. CentOS, Ubuntu 일부에서 사용할 수 있도록 작성했습니다. “
* 요구사항 *
1) 스크립트 실행 시 파라미터를 함께 입력하여 모니터링 정보의 전환속도, 정보의 Depth, 로그 기록 여부를 설정합니다.
2) 가능한 하나의 스크립트로 CentOS 버전과 상관없이 주요 정보를 모니터링 합니다.
3) Linux 기본 기본 제공되는 명령어를 최대한 활용한다.
4) 스크립트 상단에 Config 설정으로 서버환경에 따른 유연성 있는 스크립트를 작성합니다.
5) 모니터링 정보는 다음과 같습니다.
? 서버 일반 정보 : OS 버전, Uptime, OS 설치일자, Hosted or VM 구분, PID 1번
? 하드웨어 리소스 정보 : 디스크사용량, 레이드 구성정보, cpu정보, 메모리
? 네트워크 정보 : IP, DNS, 방화벽 활성화여부, 소켓상태(LISTEN, ESTABLISHED),
? 설치된 주요 서버 프로그램 및 프로세스(httpd, mysqld, samba, qmail, glusterfs, 등)
* 테스트환경 *
CentOS 7 VM, Lubuntu 15.10 VM
1. 모니터링 주요 명령어
1) 서버 일반정보 ? OS 버전, OS 설치일자, Uptime, PID 1번, Hosted or VM
#### OS 버전 확인 명령어
[root@localhost ~]# find /etc/*-release | xargs cat | grep 'PRETTY_NAME' | awk -F = '{print $2}' | tr -d '"'
CentOS Linux 7 (Core)
#### OS 설치일자 확인방법 1
[root@localhost scripts]# ll --time-style full-iso /root/anaconda-ks.cfg | awk '{print $6}'
2015-09-20
#### OS 설치일자 확인방법 2
[root@localhost ~]# iDIR=("/root/install.log" "/root/anaconda-ks.cfg" "/var/log/installer/syslog")
[root@localhost ~]# for no in ${!iDIR[@]}; do ll --time-style full-iso ${iDIR[$no]} | awk '{print $6}';done
ls: cannot access /root/install.log: 그런 파일이나 디렉터리가 없습니다
2015-09-20
ls: cannot access /var/log/installer/syslog: 그런 파일이나 디렉터리가 없습니다
#### Uptime 확인
[root@localhost ~]# uptime
#### PID 1번
[root@localhost ~]# ps -p 1 | tail -1 | awk '{print $4}'
systemd
#### 호스트인지 VM 인지 확인
[root@localhost ~]# lscpu | grep 'Hypervisor' | awk -F : '{print $2}' | tr -d " "
KVM
[root@localhost ~]# virt-what
virtualbox
kvm
2) 하드웨어 리소스 정보 ? CPU 정보, 메모리, 디스크 파티션
#### CPU 모델명
[root@localhost ~]# lscpu | grep 'Model name' | awk -F : '{print $2}' | tr -d " "
Intel(R)Core(TM)i3CPU530@2.93GHz
#### 메모리 정보
[root@localhost ~]# cat /proc/meminfo | grep 'MemTotal\|MemFree'
MemTotal: 501080 kB
MemFree: 343668 kB
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 489 44 335 4 109 363
Swap: 1023 0 1023
#### 디스크 파티션 정보
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 15G 1.1G 14G 8% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4.4M 241M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda1 497M 160M 337M 33% /boot
3) 네트워크정보 ? IP주소, DNS, 방화벽 ,
#### IP 주소
[root@localhost ~]# ip a | grep 'inet' | grep brd | awk '{print $2}' | awk -F / '{print $1}'
192.168.11.111
#### DNS 주소
[root@localhost ~]# cat /etc/resolv.conf | grep nameserver
nameserver 164.124.101.2
#### 방화벽 활성화 여부
[root@localhost ~]# iptables -L -n | grep 'INPUT\|OUTPUT'
Chain INPUT (policy ACCEPT)
Chain OUTPUT (policy ACCEPT)
#### 네트워크 TCP Linten 정보
[root@localhost ~]# ss -atn | grep 'LISTEN'
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
#### 네트워크 ESTABLISH 세션 수
[root@localhost ~]# ss -atn | grep 'ESTAB' | wc -l
4) 서버 프로그램, 프로세스 정보
#### 설치된 주요 프로그램 정보
[root@localhost scripts]# App="httpd\|samba\|mysql\|mariadb\|gluster\|vsftpd\|tomcat\|nginx"
[root@localhost scripts]# rpm -qa | grep $App
mariadb-libs-5.5.44-1.el7_1.x86_64
httpd-tools-2.4.6-40.el7.centos.x86_64
httpd-2.4.6-40.el7.centos.x86_64
## Ubuntu 의 경우
root@hostname:~# dpkg -l | grep $App
[root@localhost ~]# ps -aux | grep $App | awk '{print $1, $11}' | grep -v 'grep' | uniq
root /usr/sbin/httpd
apache /usr/sbin/httpd
2. monitor.sh
#!/bin/bash
################### Config ##################
srv_idx=1
PROMPT=1 # 0: Default Parameter 1: Input Parameter
iDIR=("/root/install.log" "/root/anaconda-ks.cfg" "/var/log/installer/syslog")
App="httpd\|samba-common\|mysqld\|mariadb\|bind\|gluster\|vsftpd\|php-common"
rPS="httpd\|smbd\|mysqld\|bind\|gulster\|vsftpd\|tomcat\|snmpd\|rsync\|openfire\|asterisk\|fmscore\|qmail"
L1="====================="
L2="---"
L_Rep=/scripts/M_Report/`date +%Y%m%d_%H%M`
URI=http://192.168.11.111/~test/report/srvreport_insert.php
if [ ! -d /scripts/M_Report ];then
echo -e "Error... Make the directory!! /scripts/M_Report"
exit
fi
if [ "$PROMPT" = "1" ];then
echo "Input Change_Time (1 to 9)"
read C_T
echo "Do you want to send the report? (y or n)"
read TRANSFER
else
C_T=3
TRANSFER=y
fi
#############################################
##### 1. Server Information
clear
SRVINFO ()
{
OS=`find /etc/*-release | xargs cat | grep 'PRETTY_NAME' | awk -F = '{print $2}' | tr -d '"'`
if [ -z "$OS" ]
then
OS=`find /etc/*-release | xargs cat | uniq`
fi
KERNEL=`uname -r`
for no in ${!iDIR[@]}
do
if [ -e ${iDIR[$no]} ]
then
iDATE=`ls -al --time-style full-iso ${iDIR[$no]} | awk '{print $6}'`
fi
done
UPTIME=`uptime`
PID1=`ps -p 1 | tail -1 | awk '{print $4}'`
HV=`lscpu | grep 'Hypervisor' | awk -F : '{print $2}' | tr -d " "`
if [ -z "$HV" ]
then
HV=Hosted
fi
TIME=`date`
echo -e $L1" 1. Server Information"$L1 | tee $L_Rep.txt
echo -e $L2" 1) OS " | tee -a $L_Rep.txt
echo -e "$OS\n" | tee -a $L_Rep.txt
echo -e $L2" 2) Kernel " | tee -a $L_Rep.txt
echo -e "$KERNEL\n" | tee -a $L_Rep.txt
echo -e $L2" 3) Install Date " | tee -a $L_Rep.txt
echo -e "$iDATE\n" | tee -a $L_Rep.txt
echo -e $L2" 4) Uptime " | tee -a $L_Rep.txt
echo -e "$UPTIME\n" | tee -a $L_Rep.txt
echo -e $L2" 5) PID 1 " | tee -a $L_Rep.txt
echo -e "$PID1\n" | tee -a $L_Rep.txt
echo -e $L2" 6) Hosted or VM " | tee -a $L_Rep.txt
echo -e "$HV\n" | tee -a $L_Rep.txt
echo -e $L2" 7) Server Date " | tee -a $L_Rep.txt
echo -e "$TIME\n" | tee -a $L_Rep.txt
echo -e $L1"======================"$L1
}
SRVINFO
sleep $C_T;clear
#### 2. H/W Information
HWINFO ()
{
CPU=`lscpu | grep 'Model name' | awk -F : '{print $2}' | tr -d " "`
if [ -z "$CPU" ]
then
CPU=`cat /proc/cpuinfo | grep "model name" | uniq | awk -F : '{print $2}'`
fi
MEM=`cat /proc/meminfo | grep 'MemTotal\|MemFree'`
FREE=`free -h`
DFH=`df -h`
echo -e $L1" 2. H/W Information"$L1 | tee -a $L_Rep.txt
echo -e $L2" 1) CPU " | tee -a $L_Rep.txt
echo -e "$CPU\n" | tee -a $L_Rep.txt
echo -e $L2" 2) Memory " | tee -a $L_Rep.txt
echo -e "$MEM\n" | tee -a $L_Rep.txt
echo -e $L2" 3) Memory info " | tee -a $L_Rep.txt
echo -e "$FREE\n" | tee -a $L_Rep.txt
echo -e $L2" 4) Disk Partion info " | tee -a $L_Rep.txt
echo -e "$DFH\n" | tee -a $L_Rep.txt
echo -e $L1"==================="$L1
}
HWINFO
sleep $C_T;clear
#### 3. Network Information
NETINFO ()
{
IP=`ip a | grep 'inet' | grep brd | awk '{print $2}' | awk -F / '{print $1}'`
DNS=`cat /etc/resolv.conf | grep nameserver`
FW=`iptables -L -n | grep 'INPUT\|OUTPUT'`
TCP=`ss -atn | grep 'LISTEN'`
SS=`ss -atn | grep 'ESTAB' | wc -l`
echo -e $L1" 3. Network Information"$L1 | tee -a $L_Rep.txt
echo -e $L2" 1) IP Address " | tee -a $L_Rep.txt
echo -e "$IP\n" | tee -a $L_Rep.txt
echo -e $L2" 2) DNS " | tee -a $L_Rep.txt
echo -e "$DNS\n" | tee -a $L_Rep.txt
echo -e $L2" 3) Firewall" | tee -a $L_Rep.txt
echo -e "$FW\n" | tee -a $L_Rep.txt
echo -e $L2" 4) TCP Listen " | tee -a $L_Rep.txt
echo -e "$TCP\n" | tee -a $L_Rep.txt
echo -e $L2" 5) ESTABLISHED Session " | tee -a $L_Rep.txt
echo -e "$SS\n" | tee -a $L_Rep.txt
echo -e $L1"======================="$L1
}
NETINFO
sleep $C_T;clear
#### 4. Application Information
APPINFO ()
{
RPM=`which rpm`
RPM=$?
if [ $RPM != 0 ]
then
iAPP=`dpkg -l | grep $App`
else
iAPP=`rpm -qa | grep $App`
fi
PS=`ps -ef | grep $rPS | awk '{print $1, $8}' | grep -v 'grep' | uniq`
PSC=`ps -ef | wc -l`
TopPS=`top b -n1 | head -12 | tail -6`
VMSTAT=`vmstat`
echo -e $L1" 4. App and ps Information"$L1 | tee -a $L_Rep.txt
echo -e $L2" 1) Installed Application " | tee -a $L_Rep.txt
echo -e "$iAPP\n" | tee -a $L_Rep.txt
echo -e $L2" 2) Process" | tee -a $L_Rep.txt
echo -e "$PS\n" | tee -a $L_Rep.txt
echo -e $L2" 3) No of Process" | tee -a $L_Rep.txt
echo -e "$PSC\n" | tee -a $L_Rep.txt
echo -e $L2" 4) Top 5 Process" | tee -a $L_Rep.txt
echo -e "$TopPS\n" | tee -a $L_Rep.txt
echo -e $L2" 5) vmstat" | tee -a $L_Rep.txt
echo -e "$VMSTAT\n" | tee -a $L_Rep.txt
echo -e $L1"========================"$L1
}
APPINFO
sleep $C_T;clear
#### 5. Etc Information
ETC()
{
SYSAcc=`cat /etc/passwd | awk -F: '$3 > 500 {print $1}' | wc -l`
CROND=`crontab -l`
echo -e $L1" 5. ETC Information"$L1 | tee -a $L_Rep.txt
echo -e $L2" 1) No of Accounts " | tee -a $L_Rep.txt
echo -e "$SYSAcc\n" | tee -a $L_Rep.txt
echo -e $L2" 2) Crontab List" | tee -a $L_Rep.txt
echo -e "$CROND\n" | tee -a $L_Rep.txt
echo -e $L1"========================"$L1 | tee -a $L_Rep.txt
}
ETC
sleep $C_T;clear
#### Remote transmission
if [ "$TRANSFER" = "y" ];then
REPORT=`cat $L_Rep.txt`
curl -d "srv_idx=$srv_idx&report=$REPORT" $URI
echo -e "Success Transfer!! " $URI
else
exit
fi
3. srvreport_insert.php
<?php
include('../conf/common.php');
$srv_idx = $_POST['srv_idx'];
$report = $_POST['report'];
$idate = date("Y/m/d H:i:s");
$stmt = $mysqli->prepare("insert into srv_report (srv_idx,report,YMD) values (?,?,?)");
$stmt->bind_param('iss',$srv_idx,$report,$idate);
$stmt->execute();
$stmt->affected_rows;
$stmt->close();
$mysqli->close();
?>

댓글목록
등록된 댓글이 없습니다.