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

How to: PSU OpenVPN Ubuntu 14.04/15.04/15.10/Linux Mint 17/17.1/17.2

ติดตั้ง net-workmanager-openvpn คลิก Ubuntu Button แล้วพิมพ์ netw เลือก Network Connections ได้ดังภาพ คลิก Add ได้ดังภาพ เลือก OpenVPN แล้วคลิก Create… กรอกการตั้งค่าต่างๆ ดังภาพ โดยสามารถโหลด PSUCAcert.cer ได้จาก http://netserv.cc.psu.ac.th/documents/doc_download/269-vpnpackageforallwin7 http://ftp.psu.ac.th/pub/openvpn/openvpncer.zip โหลดมาแล้วให้คลายซิบแล้วแยกเอาไฟล์ PSUCAcert.cer ออกมา กรอก User name และ Password ของตัวเอง คลิก Advanced เลือกดัังภาพ กด OK ออกมาแล้วคลิกที่ IPv4 Settings ตั้งค่าตามภาพ คลิก IPv6 Settings ตั้งค่าตามภาพ กลับมาแท็บ VPN คลิก Save… ได้ดังภาพ คลิก Close เหลือบมองมุมขวาบนจะมีรูปภาพ หรือ ให้คลิกที่รูปนี้แล้วเลือก VPN Connection -> PSU รอจนมีข้อความดังภาพปรากฎที่มุมบนขวา เป็นอันเสร็จ สำหรับ Linux Mint 17 ติดตั้ง network-manager-openvpn-gnome แทน นอกนั้นเหมือนกัน เพิ่มเติม เพื่อให้สามารถคลายแฟ้ม vpn_package_for_all_xp-vista-7.exe ได้ต้องติดตั้งโปแกรมเหล่านี้เพิ่มเติม p7zip-rar, p7zip-full, unace, unrar, zip, unzip, sharutils, rar, uudeview mpack, arj, cabextract, file-roller โดยพิมพ์คำสั่ง sudo apt-get install p7zip-rar p7zip-full unace unrar zip unzip sharutils rar uudeview mpack arj cabextract file-roller จบจริงๆ … ขอให้สนุกครับ

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 »

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