Day: June 19, 2014

  • วิธีการเขียน Script ตรวจสอบ Server Performance สำหรับเครื่อง Linux Server

    ขอนำเสนอ PHP Script และ Terminal linux program สำหรับใช้ Monitor Server Performance สำหรับ Ubuntu Linux ดังนี้

    คำสั่งตรวจสอบ Hostname

    hostname -A

    วิธีตรวจสอบ Traffic จาก ifconfig ด้วย PHP5 Script

    <?php
    //Net Traffic
    exec("ifconfig | grep 'RX bytes'",$net);
    $neteth0 = explode(":",$net[0]);
    $netin1 = explode(" ",$neteth0[1]);
    $netin = $netin1[0];
    $netout1 = explode(" ",$neteth0[2]);
    $netout = $netout1[0];
    unset($net);
    unset($neteth0);
    unset($netin1);
    unset($netout1);
    exec("cat net.db",$nethistory);
    $hnet = explode(":",$nethistory[0]);
    $diff = time()-$hnet[0];
    $cnetin = round(($netin-$hnet[1])/$diff,0);
    $cnetout = round(($netout-$hnet[2])/$diff,0);
    unset($nethistory);
    exec("echo ".time().":".$netin.":".$netout." > net.db");
    echo date("d-m-Y H:i:s",time())." Traffic In : ".$cnetin.", Traffic Out : ".$cnetout;
    ?>

    วิธีการตรวจสอบ CPU จาก /proc/cpuinfo ด้วย PHP5 Script

    <?php
    //CPU
    exec("cat /proc/cpuinfo | grep MHz | head -1",$clockrate);
    exec("cat /proc/cpuinfo | grep MHz | wc -l",$cpucount);
    $clockrate1 = explode(":",$clockrate[0]);
    $maxcpu = trim($clockrate1[1])*$cpucount[0];
    unset($clockrate);
    unset($clockrate1);
    unset($cpucount);
    exec("vmstat 1 2 | tail -1 | awk '{ print $15 }'",$pcpufree);
    $pcpuuse = 100-$pcpufree[0];
    $cpuuse = round(($pcpuuse*$maxcpu)/100,0);
    unset($pcpufree);
    echo "MAX CPU Speed : ".$maxcpu." MHz\n";
    echo "CPU Use : ".$cpuuse." MHz (".$pcpuuse." %)\n";
    ?>

    วิธีการตรวจสอบ Memory จาก free -m ด้วย PHP5 Script

    <?php
    //MEM
    exec("free -m | grep Mem: | awk '{print $2\":\"$3}'",$ram);
    $ramraw = explode(":",$ram[0]);
    $ramtotal = $ramraw[0];
    $ramuse = $ramraw[1];
    $pramuse = round(($ramuse/$ramtotal)*100,0);
    unset($ram);
    unset($ramraw);
    echo "MAX RAM : ".$ramtotal." MB\n";
    echo "MEMORY Use : ".$ramuse." MB (".$pramuse." %)\n";
    ?>

    คำสั่งตรวจสอบ Connection (ยกตัวอย่างเฉพาะ port 80)

    netstat -na | grep 192.168.99.1:80 | wc -l

    คำสั่งตรวจสอบ Unique IP (ยกตัวอย่างเฉพาะ port 80)

    netstat -na | grep 192.168.99.1:80 | sed 's/::ffff://g' | awk '{print $5}' | cut -f 2 -d ' ' | cut -f 1 -d ':' | sort | uniq | wc -l

    จะเห็นได้ว่าเราสามารถเขียน script monitor ได้หลากหลายมุมมอง ได้ตามต้องการเลยครับ

  • การสร้างระบบ Load Balance Web Server ด้วยวิธีการ URL Redirect

    เราสามารถสร้างระบบ Load Balance Web Server ด้วยวิธีการ URL Redirect
    โดยสามารถสร้างด้วยภาษาใดก็ได้ แต่จะขอยกตัวอย่างด้วยภาษา PHP ได้ง่าย ๆ ดังนี้

    ตัวอย่างไฟล์ server-status ที่ได้ทำการ monitor web server มาเรียบร้อยแล้ว

    webserver1.testlab UP
    webserver2.testlab DOWN
    

    – เราสามารถใช้คำสั่งดึงไฟล์เพื่อคัดเฉพาะ Web Server ที่สามารถใช้งานได้จริง ๆ ดังนี้

    exec("cat /tmp/server-status | grep UP",$redirect);

    – จากนั้นจะทำการ นำรายการ server ทั้งหมดซึ่งอยู่ในตัวแปร $redirect มาทำการสุ่ม ดังนี้

    $random = rand(0,(count($redirect)-1);

    – ก็จะทำเอาเลข server ที่ random ได้ไปเข้ากระบวนการ Redirect ดังนี้

    $server = explode(" ",$redirect[$random]);
    header('HTTP/1.1 301 Moved Permanently');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
    header('Location: https://'.$server[0].'/');
    unset($server);
    

    ในกรณีที่ต้องการสร้างหน้ารอให้ทำการตรวจสอบว่าในกรณีที่ไม่มีเครื่องให้บริการให้แสดงข้อความ ตัวอย่างดังนี้

    if(count($redirect)==0){
       echo "Server Unavailable or Maintenance Period";
    }

    ดังที่กล่าวมาทั้งหมดข้างต้นสามารถรวมเป็น script ไฟล์ PHP ได้ดังนี้

    <?php
    exec("cat /tmp/server-status | grep UP",$redirect);
    if(count($redirect)==0){
       echo "Server Unavailable or Maintenance Period";
    }else{
       $random = rand(0,(count($redirect)-1));
       $server = explode(" ",$redirect[$random]);
       header('HTTP/1.1 301 Moved Permanently');
       header('Cache-Control: no-store, no-cache, must-revalidate');
       header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
       header('Location: http://'.$server[0].'/');
       unset($server);
    }
    unset($redirect);
    ?>

    *สามารถอ่านวิธีสร้างไฟล์สำหรับ monitor web serverได้จาก บทความนี้ http://sysadmin.psu.ac.th/2014/06/19/monitor-web-server-wget-shell-script-daemon/

  • การตรวจสอบสถานะการให้บริการ Web Server ด้วย WGET และสร้าง Shell Script เพื่อตรวจสอบอัตโนมัติ

    ขอนำเสนอวิธีการตรวจสอบสถานะการให้บริการด้วยคำสั่ง 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 ได้ตามสะดวกครับ