Tag: monitor

  • Oracle Database 12CR1 monitoring with MRTG

    • OS: Oracle Enterprise Linux  7.2  (CentOS 7.2)
    • วิธีติดตั้ง MRTG สามารถติดตั้งได้โดยสามารถดูคู่มือที่ ติดตั้ง mrtg บน ubuntu อาจไม่เหมือนกันแต่สามารถทำได้ทำนองเดียวกัน
    • กราฟสำหรับ Idle CPU and Load average, CPU Time spent waiting for IO, Traffic Analysis for eth0, TCP Current Establish สามารถใช้ script เดียวกับลิงค์ในข้อ ๒ ได้เลย
    • สร้างแฟ้ม /etc/mrtg/get-memory.sh มีข้อความว่า
      #!/bin/bash
      FREE=$(free |grep "Mem:"|awk '{print $7}')
      SWAP=$(free |grep "Swap:"|awk '{print $3}')
      TIME=$(uptime)
      echo "${FREE}"
      echo "${SWAP}"
      echo "$TIME"
      hostname 

      สร้างแฟ้ม /etc/mrtg/myhost-memory.cfg มีข้อความว่า
      WorkDir: /var/www/mrtg/myhost
      Target[myhost-mem]:`/etc/mrtg/get-memory.sh`
      MaxBytes[myhost-mem]: 20000000000
      Title[myhost-mem]: Free Memory and Swap Used
      PageTop[myhost-mem]: <H1>Free Memory and Swap Used</H1>
      ShortLegend[myhost-mem]: bytes
      YLegend[myhost-mem]: bytes
      LegendI[myhost-mem]:  Free Memory:
      LegendO[myhost-mem]: Swap Used:
      Legend1[myhost-mem]: Free memory, in bytes
      Legend2[myhost-mem]: Swap Used, in bytes
      Options[myhost-mem]: gauge, nopercent, growrightทดสอบสร้างภาพต้นแบบด้วยคำสั่ง

      env LANG=C /usr/bin/mrtg/myhost-memory.cfgปรับปรุงแฟ้ม index.html ด้วยคำสั่ง
      indexmaker --column=2 --output=/var/www/mrtg/myhost/index.html /etc/mrtg/myhost-cpu.cfg /etc/mrtg/myhost-cpu-io.cfg /etc/mrtg/myhost-speed-eth0.cfg /etc/mrtg/myhost-tcpestab.cfg /etc/mrtg/myhost-memory.cfg

    • โฟลเดอร์ที่ต้องเฝ้าระวังได้แก่ /u02/app/oracle/adump, /u02/app/oracle/diag/rdbms/regist/regist/alert, /u02/app/oracle/rdbms_trace ซึ่งเป็นโฟลเดอร์สำหรับเก็บ Log ไฟล์ต่างๆ ซึ่งอาจมีขนาดเพิ่มขึ้นจนระบบไม่สามารถให้บริการได้ และโฟลเดอร์ /u03 เป็นโฟลเดอร์ที่ใช้เก็บ archive log (ในกรณีที่ฐานข้อมูลเปิด archive log mode)
      • สร้างแฟ้ม /etc/mrtg/get-diskfree-misc1.sh มีข้อความว่า
        #!/bin/bash
        adump=$(du -sm /u02/app/oracle/adump|awk '{ print $1 }')
        free=$(df -m /u02|grep u02|awk '{ print $4 }')
        TEMP=$(uptime|grep -o "load average.*"|awk '{print $3}'|cut -d',' -f 1)
        LOAD=$(echo "${TEMP:-0} * 100"|bc|cut -d'.' -f 1)
        TIME=$(uptime)
        echo "${adump}"
        echo "${free}"
        echo "$TIME"
        hostname
      • สร้างแฟ้ม /etc/mrtg/myhost-diskfree-misc1.cfg มีข้อความว่า
        WorkDir: /var/www/mrtg/myhost
        Target[myhost-misc1]:`/etc/mrtg/get-diskfree-misc1.sh`
        MaxBytes[myhost-misc1]: 20000000000
        Title[myhost-misc1]: Free disk space and disk Used of /u02/app/oracle/adump
        PageTop[myhost-misc1]: Free disk space and disk Used of /u02/app/oracle/adump
        ShortLegend[myhost-misc1]: bytes
        kMG[myhost-misc1]: M,G,T
        kilo[myhost-misc1]: 1024
        YLegend[myhost-misc1]: bytes
        LegendI[myhost-misc1]: Disk Used:
        LegendO[myhost-misc1]: Free Disk:
        Legend1[myhost-misc1]: Disk usage, in Bytes
        Legend2[myhost-misc1]: Free Disk Space, in Bytes
        Options[myhost-misc1]: gauge, nopercent, growright
        Timezone[myhost-misc1]: Bangkok

        ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง
        env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-diskfree-misc1.cfg
        ปรับปรุงแฟ้ม index.html ด้วยคำสั่ง
        indexmaker --column=2 --output=/var/www/mrtg/myhost/index.html /etc/mrtg/myhost-cpu.cfg /etc/mrtg/myhost-cpu-io.cfg /etc/mrtg/myhost-speed-eth0.cfg /etc/mrtg/myhost-tcpestab.cfg /etc/mrtg/myhost-memory.cfg /etc/mrtg/myhost-diskfree-misc1.cfg
      • สร้างแฟ้มเลียนแบบข้อ 1 และ 2 สำหรับโฟลเดอร์ที่เหลือ
      • แก้ไขแฟ้ม /etc/mrtg/mymrtg.sh เพิ่มข้อความ  env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-diskfree-misc1.cfg ต่อท้ายไฟล์และเพิ่มทุกไฟล์ของทุกโฟลเดอร์
      • สำหรับโฟลเดอร์ /u03 ให้สร้างแฟ้ม /etc/mrtg/get-diskfree-u03.sh มีข้อความว่า
        #!/bin/bash
        used=$(df -m /u03|grep u03|awk '{ print $3 }')
        free=$(df -m /u03|grep u03|awk '{ print $4 }')
        TEMP=$(uptime|grep -o "load average.*"|awk '{print $3}'|cut -d',' -f 1)
        LOAD=$(echo "${TEMP:-0} * 100"|bc|cut -d'.' -f 1)
        TIME=$(uptime)
        echo "${used}"
        echo "${free}"
        echo "$TIME"
        hostname
        nof=$(ls -d1 /u03/app/oracle/fast_recovery_area/REGIST/archivelog/* |wc -l)
        max=3if [ "${nof}" == "${max}" ]
        then
                nod=$(expr ${max} - 1)
                f2d=$(ls -d1 /u03/app/oracle/fast_recovery_area/REGIST/archivelog/*|head -${nod})
                rm -rf ${f2d}
                su - oracle -c "/bin/sh /home/oracle/reclaim.sh"
        fi
        สร้างแฟ้ม /home/oracle/reclaim.sh มีข้อความว่า
        rman target / <<EOF
        crosscheck archivelog all;
        delete noprompt expired archivelog all;
        quit
        EOF
        สร้างแฟ้ม /etc/mrtg/myhost-diskfree-u03.cfg มีข้อความว่า
        WorkDir: /var/www/mrtg/myhost
        Target[myhost-u03]:`/etc/mrtg/get-diskfree-u03.sh`
        MaxBytes[myhost-u03]: 20000000000
        Title[myhost-u03]: Free disk space and disk Used of /u03
        PageTop[myhost-u03]: Free disk space and disk Used of /u03
        ShortLegend[myhost-u03]: bytes
        kMG[myhost-u03]: M,G
        kilo[myhost-u03]: 1024
        YLegend[myhost-u03]: bytes
        LegendI[myhost-u03]: Disk Used:
        LegendO[myhost-u03]: Free Disk:
        Legend1[myhost-u03]: Disk usage, in Bytes
        Legend2[myhost-u03]: Free Disk Space, in Bytes
        Options[myhost-u03]: gauge, nopercent, growrightทดสอบสร้างภาพต้นแบบด้วยคำสั่ง
        env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-diskfree-u03.cfg
        อย่าลืมปรับปรุงแฟ้ม index.html ด้วย
    • เฝ้าระวังขนาดของ Tablespace SYSTEM และ USERS
      • สร้างแฟ้ม /etc/mrtg/get-tablespace-system.sh มีข้อความว่า
        #!/bin/bash
        used=$(su - oracle -c "sh /home/oracle/monitor/tablespacesize.sh"|grep SYSTEM|awk '{ print $2 }'|sed -e 's/,//g')
        used=$(expr ${used} \* 1024)
        free=$(su - oracle -c "sh /home/oracle/monitor/tablespacesize.sh"|grep SYSTEM|awk '{ print $3 }'|sed -e 's/,//g')
        free=$(expr ${free} \* 1024)
        TIME=$(uptime)
        echo "${used}"
        echo "${free}"
        echo "$TIME"
        hostname

        สร้างแฟ้ม /home/oracle/monitor/tablespacesize.sh มีข้อความว่า
        #!/bin/bash
        sqlplus / as sysdba << EOF
        col "Tablespace" for a22
        col "Used MB" for 99,999,999
        col "Free MB" for 99,999,999
        col "Total MB" for 99,999,999
        select df.tablespace_name "Tablespace",
        totalusedspace "Used MB",
        (df.totalspace - tu.totalusedspace) "Free MB",
        df.totalspace "Total MB",
        round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace))
        "Pct. Free"
        from
        (select tablespace_name,
        round(sum(bytes) / 1048576) TotalSpace
        from dba_data_files
        group by tablespace_name) df,
        (select round(sum(bytes)/(1024*1024)) totalusedspace, tablespace_name
        from dba_segments
        group by tablespace_name) tu
        where df.tablespace_name = tu.tablespace_name ;
        quit
        EOF

        สร้างแฟ้ม /etc/mrtg/myhost-tablespace-system.cfg
        WorkDir: /var/www/mrtg/myhost
        Target[myhost-system]:`/etc/mrtg/get-tablespace-system.sh`
        MaxBytes[myhost-system]: 20000000000
        Title[myhost-system]: Tablespace SYSTEM disk usage
        PageTop[myhost-system]: Tablespace SYSTEM Disk Usage
        ShortLegend[myhost-system]: bytes
        kMG[myhost-system]: k,M,G
        kilo[myhost-system]: 1024
        YLegend[myhost-system]: bytes
        LegendI[myhost-system]: Disk Used:
        LegendO[myhost-system]: Free Disk:
        Legend1[myhost-system]: Disk usage, in Bytes
        Legend2[myhost-system]: Free Disk Space, in Bytes
        Options[myhost-system]: gauge, nopercent, growrightทดสอบสร้างภาพต้นแบบด้วยคำสั่ง
        env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-tablespace-system.cfgอย่าลืมปรับปรุง index.html ด้วย
      • ทำแบบเดียวกันกับ tablespace users
    • จบขอให้สนุก
  • วิธีการเขียน 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 ได้หลากหลายมุมมอง ได้ตามต้องการเลยครับ

  • การติดตั้งโปรแกรมตรวจสอบประสิทธิภาพ Server : Apache Jmeter บนเครื่อง Windows

    โปรแกรม Jmeter เป็นโปรแกรมสารพัดประโยชน์ใช้สำหรับทดสอบประสิทธิภาพการรองรับโหลดจำนวนมาก โดยในเบื้องต้นผมยังจะไม่อธิบายรายละเอียดโปรแกรมมากนะครับ จะนำเสนอแค่วิธีติดตั้ง และการ Test ในส่วนที่เป็น Web Server แบบไม่มี Script อะไรพิเศษเป็นหลัก แล้ววันหลังค่อยมาเปล่าในส่วนอื่น ๆ เช่นการ Test Load Database, LDAP, FTP, WebService ฯลฯ ถ้ามีโอกาสครับ

    ความต้องการพื้นฐานสำหรับโปรแกรม Jmeter
    1. Java 6 ขึ้นไป
    2. OS ซึ่งรองรับทั้ง Unix, Windows

    วิธีการติดตั้งบน Windows 
    Test OS : Windows 8.1
    1. ติดตั้ง JAVA สามารถทำการ Download และติดตั้งได้จาก

    http://www.java.com/en/download/index.jsp
    

    2. ดาวน์โหลดโปรแกรม Jmeter ได้จาก

    http://jmeter.apache.org/download_jmeter.cgi
    

    3. Save file .zip ดังรูป
    2014-05-22_202646

    4. ทำการแตกไฟล์ออกมา และทำการรันไฟล์ ApacheMeter.jar ดังรูป (จะปรากฎเป็น icon java สามารถ double click รันได้เลย)
    2014-05-22_203814

    ตัวอย่างการสร้าง Test Plan เพื่อทดสอบ Web Server

    1. ทำการสร้าง Thread Group ภายใต้ Test Plan ดังรูป
    2014-05-22_204434

    2. หลังจากนั้นจะปรากฎหน้าต่างให้ตั้งค่าได้ดังนี้
    2014-05-22_205548

    Number of Threads (users) : จำนวนผู้ใช้งานที่ต้องการ ณ เวลานั้น ๆ (concurrent users)
    Ramp-up Period (in seconds) : เวลาหน่วงในการเพิ่มผู้ใช้งานจนถึงจำนวน Number of Threads ที่ตั้งไว้ เช่น Number of Threads = 300, Ramp-up Period = 60 แสดงว่า ภายใน 60 วินาทีจะมี จำนวนผู้ใช้เข้าสู่ระบบรวม 300 คน (ค่อย ๆ เพิ่มเข้าไปจนครบ 300 ใน 60 วินาที)
    Loop Count : คือจำนวนรอบที่ทำ ถ้าติด Forever ก็แสดงว่าทำตลอดไป

    *หมายเหตุ ระวังอย่างตั้งค่าเว่อร์จนเกินไป เครื่องผู้ที่สั่ง Test อาจจะทำงานไม่ไหว Hang ได้

    3. จากนั้นทำการสร้าง Sampler ที่เป็น HTTP Request เพื่อกำหนดค่า config เบื้องต้นที่จำเป็นในการทดสอบ ดังรูป
    2014-05-22_212211

    4. จะปรากฎให้ตั้งค่า หลัก ๆ ถ้าต้องการยิงทดสอบธรรมดา ๆ ให้ใส่เฉพาะ Server Name หรือ IP และ Path เช่น /regist.php ถ้าต้องการยิงหน้าแรกก็ใส่ / เฉย ๆ ดังรูป

    2014-05-22_212413

    5. หลังจากนั้นทำการเพิ่มในส่วนของรายงานผล โดยแนะนำ 2 ตัวคือ View Results Tree และ Graph Results
    2014-05-22_210512

    6. ตัวอย่างผลลัพธ์จาก Results Tree (วิธีการรัน Test ให้กดที่ปุ่ม Play สีเขียว และปุ่ม Stop สีแดงเพื่อหยุด)2014-05-22_212755

    7. ตัวอย่างผลลัพธ์ในรูปแบบ Graph Results2014-05-22_213206

    Reference :
    [1] http://www.narisa.com/forums/index.php?app=blog&blogid=9&showentry=3019
    [2] http://www.narisa.com/forums/index.php?app=blog&blogid=9&showentry=2873