WorkShop : Server Monitoring

“อาว Server ตายตอนไหนไม่เห็นจะรู้เลย โหลดเยอะละม้าง หรือไม่ก็แรมหมด เสถียรไหมไม่รู้สิ อาวเมื่อคืน disk หมดหรอกเหรอ สงสัย Backup อยู่ม้าง” ถ้าเรามีระบบ Monitor ที่ดีพบคงสามารถตอบคำถามเหล่านี้ได้ง่ายขึ้น และไม่ต้องเจอเหตุการณ์เดิม ๆ ซ้ำ ๆ อีกต่อไป มาถึงอีก 1 workshop เคยเขียนไปแล้วรอบหนึ่ง แต่ตอนนั้นสอนแค่ครึ่งวัน ได้แค่ลง Nagiosql ยังไม่ได้ทำอะไรเป็นชิ้นเป็นอันสักเท่าไหร่ รอบนี้เลยจัดเต็ม 2 วัน ถ้ามีเนื้อหาผิดพลาดประการใดแจ้งได้เลยครับ เดี๋ยวจะแก้ไขให้ครับ (งานร้อน ^.^) Workshop Outline ftp://ftp.psu.ac.th/pub/psu-monitor/workshop-outline.pdf VirtualBox Installation เครื่องที่ใช้ในการทดสอบ (Oracle VM VirtualBox) *แนะนำให้เปิดกับโปรแกรม version ล่าสุด Monitor ** User : workshop , Password : 123456 Linux-Server ** User : monitor , Password : 123456 MS-Server ** User : administrator, Password : 123456 รายละเอียด URL Monitor Linux-Server MS-Server http://ftp.psu.ac.th/pub/psu-monitor/workshop.ova โดยจะแบ่งเป็น 11 ตอนโดยแยกเป็น 11 Blog ดังนี้ ตอนที่ ชื่อตอน ตอนที่ 1 การติดตั้งและการตั้งค่าเบื้องต้น Nagios บน Ubuntu 14.04 LTS ตอนที่ 2 การติดตั้ง NagiosQL บน Ubuntu 14.04 LTS ตอนที่ 3 วิธีการตั้งค่าและการใช้งาน NagiosQL บน Ubuntu 14.04 LTS ตอนที่ 4 การติดตั้งและการตั้งค่าเบื้องต้น NagiosGraph บน Ubuntu 14.04 LTS ตอนที่ 5 วิธีการตั้งค่า NRPE เพื่อใช้งานกับ NagiosQL บน Ubuntu 14.04 LTS ตอนที่ 6 การติดตั้ง NRPE บน Ubuntu 14.04 ตอนที่ 7 การติดตั้ง NRPE บน Windows 2012 R2 ตอนที่ 8 การติดตั้งโปรแกรมและการตั้งค่าเบื้องต้น Cacti บน Ubuntu 14.04 LTS ตอนที่ 9 การติดตั้ง snmpd และการตั้งค่า shorewall บน Ubuntu 14.04 LTS ตอนที่ 10 การติดตั้ง snmp service และการตั้งค่า firewall บน Windows 2012 R2 ตอนที่ 11 เรียนรู้วิธีการใช้งาน Cacti เพื่อ Monitor Server บทความเพิ่มเติม ยังไม่มี  

Read More »

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

ขอนำเสนอ Powershell Script สำหรับใช้ Monitor Server Performance สำหรับเครื่อง Windows 2008 R2 ขึ้นไปดังนี้ การติดตั้ง PowerShell Editor และ วิธีใช้งานบน Windows 2008 1. ติดตั้งโดยไปที่ Server Manager -> Features -> Add Features 2. เลือกหัวข้อ Windows PowerShell Integrated Script Environment (ISE) 3. ให้เลือกใช้งาน Version X86 ซึ่งเมื่อลงแล้ว Icon จะอยู่ที่ Accessories -> Windows PowerShell -> Windows PowerShell ISE (x86) คำสั่งในการเปิด Execution Policy Set-ExecutionPolicyUnrestricted -Force การสร้าง Loop เพื่อให้สามารถรันได้ตลอดเวลา While($true){ …..[Monitor Script]….. } คำสั่งตรวจหา IP ของ Server Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.Description -eq “Intel(R) Dual Band Wireless-N 7260” } | ForEach-Object { $ipaddr = $_.IPAddress } $ip = $ipaddr[0] คำสั่งตรวจสอบ Max CPU Clock Speed Get-WmiObject-class win32_processor -Property  “maxclockspeed” | ForEach-Object {$clockspeed = $_.maxclockspeed } Get-WmiObject-class win32_computersystem -Property “NumberOfLogicalProcessors” | ForEach-Object { $lprocessor = $_.NumberOfLogicalProcessors } $sumclockspeed = $clockspeed*$lprocessor คำสั่งตรวจสอบ Current CPU (Get-Counter “\Processor(_Total)\% Processor Time”).CounterSamples | ForEach-Object {$cpuuse = $_.CookedValue}     $cpuuse = [math]::round($cpuuse,0)     $clockspeed = ($cpuuse*$sumclockspeed)/100     $clockspeed = [math]::round($clockspeed,0)     $pcpuuse = $clockspeed/$sumclockspeed     $pcpuuse = [math]::round($pcpuuse,0) คำสั่งตรวจสอบ Maximum Memory $sumram = 0 Get-WMIObject-class win32_physicalmemory -Property “Capacity”  | ForEach-Object { $sumram = $sumram + $_.Capacity} $sumram = $sumram/(1024*1024) คำสั่งตรวจสอบ Current Memory (Get-Counter “\Memory\Available MBytes”).CounterSamples | ForEach-Object {$memfree = $_.CookedValue}     $memuse = $sumram-$memfree;     $pmemuse = ($memuse/$sumram)*100     $pmemuse = [math]::round($pmemuse,0) คำสั่งตรวจสอบ Connection  $netstat = “netstat

Read More »

วิธีการเขียน 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

Read More »

การสร้างระบบ 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/

Read More »

การตรวจสอบสถานะการให้บริการ 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

Read More »