ขอนำเสนอวิธีการตรวจสอบสถานะการให้บริการด้วยคำสั่ง wget และการสร้าง Shell Script เพื่อให้ทำงานได้อย่างอัตโนมัติ
การตรวจสอบสถานะการให้บริการด้วย WGET
วิธีติดตั้ง wget สำหรับ ubuntu 14.04
สามารถติดตั้งโดยใช้คำสั่ง (โดยปกติจะลงมาให้อยู่แล้ว)
# sudo apt-get install -y wget
วิธีติดตั้ง wget สำหรับ windows
สามารถ Download ได้ที่
http://downloads.sourceforge.net/project/gnuwin32/wget/1.11.4-1/wget-1.11.4-1-setup.exe
สามารถเปิด command prompt รันได้ที่ path นี้ (ยกตัวอย่างจาก windows 64 bit)
C:\Users\xxx>cd C:\Program Files (x86)\GnuWin32\bin C:\Program Files (x86)\GnuWin32\bin>wget SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc wget: missing URL Usage: wget [OPTION]... [URL]... Try `wget --help' for more options.
ตัวอย่างคำสั่งที่ใช้สำหรับโหลด html URL ที่ต้องการตรวจสอบ
wget -nv --timeout 2 --connect-timeout=10 -t 1 http://webserver1.testlab -O /tmp/webserver1-tmp -nv : ใช้สำหรับปิดการแสดงผล แต่ยังแสดงผล error และข้อมูลเบื้องต้น --timeout [sec] : ใช้สำหรับกำหนดค่า timeout ในการเชื่อมต่อไปยัง web server --connect-timeout [sec] : ใช้กำหนดเวลาในกรณีที่การเชื่อมต่อไม่สิ้นสุดในเวลาที่กำหนด -t [sec] : จำนวนครั้งในการ retry connection -O : บันทึกผลลัพธ์ไปยังไฟล์
ในกรณีที่ต้องการโหลด html URL ประเภท https ให้ทำการข้ามการตรวจสอบ certificate ด้วยตัวอย่างคำสั่งดังนี้
wget -nv --timeout 2 --connect-timeout=10 -t 1 --no-check-certificate https://webserver1.testlab -O /tmp/webserver1-https-tmp
หลังจากได้ไฟล์ผลลัพธ์ของหน้า html website เป้าหมายแล้ว สามารถใช้โปรแกรม grep เพื่อตรวจสอบว่าปรากฎข้อความที่แสดงว่าหน้า web ทำงานปกติหรือไม่ เช่น
grep WEBSERVER /tmp/webserver1-tmp
หลังจากนั้นเราสามารถประยุกต์เพื่อเขียน Shell Script ง่าย ๆ ที่สามารถตรวจสอบ Web Server เพื่อดูว่า Web ปกติหรือไม่ในขั้นตอนถัดไป
วิธีการเขียน Shell Script เพื่อตรวจสอบการใช้งานแบบอัตโนมัติ
Shell Script สำหรับตรวจสอบ Web Server
– ทำการสร้างไฟล์ /home/[user]/checkweb.sh
nano /home/testlab/checkweb.sh
– สร้าง script ข้อความดังนี้
*ตัวอย่างรูปแบบ sh shell
#!/bin/sh
url1='webserver1.testlab'
url2='webserver2.testlab'
searchtxt='WEBSERVER'
#URL PATH
path='/'
while(true);
do
cp /dev/null /tmp/server-status
for i in $(seq 1 2)
do
eval wget -q --timeout 2 --connect-timeout=10 -t 1 http://\${url${i}}$path -O /tmp/\${url${i}}-tmp
eval echo "Get URL : http://\${url${i}}$path"
status=$(eval grep $searchtxt /tmp/\${url${i}}-tmp | wc -l)
if [ $status -ge "1" ]; then
eval echo \${url${i}}" : STATUS = UP"
eval echo \${url${i}}" UP" >> /tmp/server-status
else
eval echo \${url${i}}" : STATUS = DOWN"
eval echo \${url${i}}" DOWN" >> /tmp/server-status
fi
done
echo "Sleep 15 secs....."
sleep 15
/usr/bin/clear
done
*ตัวอย่างรูปแบบ bash shell
#!/bin/bash
url[1]='webserver1.testlab'
url[2]='webserver2.testlab'
searchtxt='WEBSERVER'
#URL PATH
path='/'
while(true);
do
cp /dev/null /tmp/server-status
for i in {1..2}
do
wget -q --timeout 2 --connect-timeout=10 -t 1 http://${url[$i]}$path -O /tmp/${url[$i]}-tmp
echo "Get URL : http://${url[$i]}$path"
status=$(grep $searchtxt /tmp/${url[$i]}-tmp | wc -l)
if [ $status -ge "1" ]; then
echo ${url[$i]}" : STATUS = UP"
echo ${url[$i]}" UP" >> /tmp/server-status
else
echo ${url[$i]}" : STATUS = DOWN"
echo ${url[$i]}" DOWN" >> /tmp/server-status
fi
done
echo "Sleep 15 secs....."
sleep 15
/usr/bin/clear
done
– ทำการเปลี่ยน permission ให้สามารถ execute ได้
chmod 755 checkweb.sh
– ทดสอบ execute script จะได้ข้อความดังนี้
testlab@lbs:~$ ./checkweb.sh Get URL : http://webserver1.testlab/ webserver1.testlab : STATUS = UP Get URL : http://webserver2.testlab/ webserver2.testlab : STATUS = UP Sleep 15 secs.....
สามารถนำ script ดังกล่าวไปปรับเปลี่ยนเพื่อใช้ในการรันเป็น daemon ได้ตามสะดวกครับ

Leave a Reply