Month: July 2014

  • วิธีการตั้งค่าและการใช้งาน NagiosQL บน Ubuntu 14.04 LTS

    สามารถอ่านวิธีติดตั้ง NagiosQL บน Ubuntu 14.04 LTS ได้ที่
    http://sysadmin.psu.ac.th/2014/07/23/nagiosql-ubuntu/

    หลังจากการติดตั้งจะต้องมีการตั้งค่าให้สามารถควบคุม Nagios ได้ดังนี้

    วิธีการตั้งค่า Nagiosql configuration

    1) หลังจาก Login ให้ทำการเข้าไปแก้ไข config path ดังนี้

    Administrator -> Config targets -> Configuration directories
    Nagios base directory -> /etc/nagios3
    Import Directory -> /etc/nagios3/conf.d
    
    Nagios command file -> /var/lib/nagios3/rw/nagios.cmd
    Nagios binary file -> /usr/sbin/nagios3
    Nagios process file -> /var/run/nagios3/nagios3.pid
    Nagios config file -> /etc/nagios3/nagios.cfg
    

    2) จากนั้นทำการ save config โดยการกดปุ่ม Save ตามรูป

    3) หลังจากทำการ save จะปรากฎข้อความดังรูป

    2014-07-23_200526

    4) ทำการแก้ไขไฟล์ของ nagios ให้มาเรียก configuration ของ nagiosql (ยกเว้น nagios config, cgi config ที่ยังใช้ของ nagios อยู่ เนื่องจากเป็น config ของโปรแกรม nagios)

    Tools -> Nagios config -> Nagios main configuration file

    5) ทำการเพิ่มข้อความดังภาพ

    ...
    
    log_file=/var/log/nagios3/nagios.log
    
    cfg_file=/etc/nagiosql/contacttemplates.cfg
    cfg_file=/etc/nagiosql/contactgroups.cfg
    cfg_file=/etc/nagiosql/contacts.cfg
    cfg_file=/etc/nagiosql/timeperiods.cfg
    cfg_file=/etc/nagiosql/commands.cfg
    cfg_file=/etc/nagiosql/hostgroups.cfg
    cfg_file=/etc/nagiosql/servicegroups.cfg
    
    cfg_dir=/etc/nagiosql/hosts
    cfg_dir=/etc/nagiosql/services
    
    cfg_file=/etc/nagiosql/hosttemplates.cfg
    cfg_file=/etc/nagiosql/servicetemplates.cfg
    cfg_file=/etc/nagiosql/servicedependencies.cfg
    cfg_file=/etc/nagiosql/serviceescalations.cfg
    cfg_file=/etc/nagiosql/hostdependencies.cfg
    cfg_file=/etc/nagiosql/hostescalations.cfg
    cfg_file=/etc/nagiosql/hostextinfo.cfg
    cfg_file=/etc/nagiosql/serviceextinfo.cfg
    
    # Commands definitions
    #cfg_file=/etc/nagios3/commands.cfg
    
    # Debian also defaults to using the check commands defined by the debian
    # nagios-plugins package
    #cfg_dir=/etc/nagios-plugins/config
    
    # Debian uses by default a configuration directory where nagios3-common,
    # other packages and the local admin can dump or link configuration
    # files into.
    #cfg_dir=/etc/nagios3/conf.d
    
    ...

    6) ทำการแก้ค่า check_external_command จาก 0 เป็น 1 ดังนี้

    ...
    
    # EXTERNAL COMMAND OPTION
    # This option allows you to specify whether or not Nagios should check
    # for external commands (in the command file defined below). By default
    # Nagios will *not* check for external commands, just to be on the
    # cautious side. If you want to be able to use the CGI command interface
    # you will have to enable this.
    # Values: 0 = disable commands, 1 = enable commands
    
    check_external_commands=1
    
    ...

    7) จากนั้นทำการ save config โดยการกดปุ่ม Save ตามรูป

    2014-07-23_203407

    8) เนื่องจาก Nagios มี config ของตัวเองอยู่แล้ว ให้ทำการ Import Data ของ Nagios มาไว้ด้วยดังนี้

    8.1) ทำการ Import Data จาก /etc/nagios3/conf.d ซึ่งได้ตั้งค่า Import directory ไว้แล้วตามข้อ 1)

    Tools -> Data import -> Configuration Import

    – จากนั้นทำการ กดปุ่ม Import ดังรูป

    8.2) ทำการ Import Data จาก /etc/nagios-plugins/config ซึ่งต้องทำการตั้งค่า Import directory ใหม่ดังรูป

    – จากนั้นทำการ กดปุ่ม Import ดังรูป

    Administrator -> Config targets -> Configuration directories

    2014-07-23_204140– จากนั้นกลับไป Import Data อีกครั้งดังนี้

    Tools -> Data import -> Configuration Import

    – จากนั้นทำการ กดปุ่ม Import ดังรูป

    2014-07-23_204610

    9) จากนั้นทำการ Save ข้อมูลจากฐานข้อมูลทั้งหมดลง config file และทำการทดสอบ config และสั่ง Restart nagios ดังรูป

    Tools -> Nagios control -> Check Written configuration files

    2014-07-23_214921

    10) ทดสอบเข้า web nagios เพื่อตรวจสอบผลการ Import configuration

    การตั้งค่า Alert mail

    1) ทำการแก้ไข Contact name : nagiosadmin

    Alerting -> Contact data

    2014-07-23_222435

    2) ทำการเพิ่มข้อมูลดังรูป โดยรายละเอียกที่ต้องการให้ส่งสามารถเลือกได้ที่ Host command และ Service command สำหรับ profile default ของ nagiosadmin จะส่งทั้งหมด

    2014-07-23_222616

    การเพิ่ม Host

    1) ทำการเพิ่มข้อมูล Host

    Supervision -> Host

    2014-07-23_223609

    2) ทำการตั้งค่าทั่วไปและทำการเลือก Host Template ทำให้เราไม่ต้องตั้งค่าเยอะ เพราะจะ Inherite มาจาก Template

    2014-07-23_224822

    3) ทำการตั้งค่าในกรณีที่ต้องการแสดงข้อมูลเพิ่มเติมในกรณีเครื่องมีปัญหาให้ติดต่อใคร

    2014-07-23_224927
    4) จากนั้นทำ Save และทดสอบ เข้าไป Restart Nagios

    Tools -> Nagios control -> Check Written configuration files

    จะพบว่าในขั้นตอน Check configuration files มีข้อความฟ้องเป็น Warning ว่า Linux-Server ยังไม่มี Service ให้ทำการเพิ่ม Service ให้กับ Host ในขั้นตอนต่อไป

    การเพิ่ม Service

    1) ทำการเพิ่มข้อมูล Service ให้กับ Host ชื่อ Linux-Server

    Supervision -> Services

    2014-07-23_230036

    2) ทำการตั้งค่าทั่วไปและทำการเลือก Service Template เพื่อดึงค่าต่าง (Monitor Period, Retry, Interval ฯลฯ) โดยเฉพาะเมื่อติดตั้ง NagiosGraph จะทำที่ Template ที่เดียวจะสามารถขึ้น Graph ให้กับทุก Service โดยไม่ต้องไปไล่ใส่ทุก Service

    2014-07-23_230809

    3) จากนั้นทำ Save และทดสอบ เข้าไป Restart Nagios

    Tools -> Nagios control -> Check Written configuration files

    จะพบว่าจะไม่มี Warning ฟ้องแล้ว ให้ลองใช้งานดูครับ ตัว NagiosQL จริง ๆ ก็คือเป็นแค่ Web ที่เขียน config แทนที่เราจะต้องแก้ไขด้วยมือ Function การใช้งานก็เหมือน Nagios ปกติแต่จะสบายกว่าในกรณีที่มีเครื่องเยอะ ๆ แล้วจะติดใจครับ ^ ^

  • การติดตั้ง NagiosQL บน Ubuntu 14.04 LTS

    สำหรับการติดตั้ง NagiosQL ต้องการทำการติดตั้ง PHP5 และ Mysql-Server ก่อนครับ

    วิธีการติดตั้ง PHP5, MySQL 5.5

    1) ทำการติดตั้ง Apache ผ่าน apt-get ดังนี้ครับ

    sudo apt-get install -y php5 mysql-server php5-mysql 
    

    2) จะปรากฎข้อความถามว่าจะให้ตั้งรหัสผ่าน root ให้ใส่รหัสผ่านที่ต้องการ

    3) จากนั้นให้สั่ง Restart Apache ตามปกติ

    sudo service apache2 restart

    4) ทำการแก้ไขไฟล์ /etc/php5/apache2/php.ini เพื่อแก้ไข timezone ดังนี้

    sudo nano /etc/php5/apache2/php.ini

    5) ทำการเพิ่มข้อความในส่วนของ [Date] ดังนี้ (ประมาณบรรทัดที่  879)

    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    ;date.timezone =
    date.timezone = Asia/Bangkok

    6) จากนั้นให้สั่ง Restart Apache อีกครั้ง

    sudo service apache2 restart

    ขั้นตอนการเตรียมการติดตั้ง NagiosQL

    1) ทำการ Download โปรแกรม 3 File ดังนี้

    cd /usr/src

    Download Nagios 3.2.0 Main File

    sudo wget http://downloads.sourceforge.net/project/nagiosql/nagiosql/NagiosQL%203.2.0/nagiosql_320.tar.gz

    Nagios 3.2.0 Service Pack 1

    sudo wget http://downloads.sourceforge.net/project/nagiosql/nagiosql/NagiosQL%203.2.0/nagiosql_320_service_pack_1_additional_fixes_only.zip

    Nagios 3.2.0 Service Pack 2

    sudo wget http://downloads.sourceforge.net/project/nagiosql/nagiosql/NagiosQL%203.2.0/nagiosql_320_service_pack_2_additional_fixes_only.zip

    2) ทำการแตกไฟล์ทั้ง 3 ไฟล์ ดังนี้

    sudo tar -xvzf nagiosql_320.tar.gz
    sudo unzip nagiosql_320_service_pack_1_additional_fixes_only.zip
    sudo unzip nagiosql_320_service_pack_2_additional_fixes_only.zip

    3) ทำการสร้าง folder nagiosql และ copy ไฟล์ทั้งหมดไปใส่ดังนี้

    sudo mkdir /var/www/html/nagiosql
    sudo cp -R /usr/src/nagiosql32/* /var/www/html/nagiosql/
    sudo cp -R /usr/src/NagiosQL_3.2.0_SP1/* /var/www/html/nagiosql/
    sudo cp -R /usr/src/NagiosQL_3.2.0_SP2/* /var/www/html/nagiosql/

    4) ทำการสร้าง directory สำหรับ เก็บ config ไฟล์ดังนี้

    sudo mkdir /etc/nagiosql
    sudo mkdir /etc/nagiosql/hosts
    sudo mkdir /etc/nagiosql/services
    sudo mkdir /etc/nagiosql/backup
    sudo mkdir /etc/nagiosql/backup/hosts
    sudo mkdir /etc/nagiosql/backup/services

    5) ทำการแก้ Permission และ Owner ในแต่ละ Folder ดังนี

    Nagios configuration

    sudo chgrp www-data /etc/nagios3
    sudo chgrp -R www-data /var/lib/nagios3
    sudo chgrp -R www-data /var/run/nagios3
    sudo chgrp www-data /etc/nagios3/nagios.cfg 
    sudo chgrp www-data /etc/nagios3/cgi.cfg
    sudo chgrp www-data /etc/nagios3/resource.cfg
    sudo chmod 775 /etc/nagios3
    sudo chmod -R 775 /var/lib/nagios3
    sudo chmod g+s /var/lib/nagios3/rw
    sudo chmod 664 /etc/nagios3/nagios.cfg
    sudo chmod 664 /etc/nagios3/cgi.cfg
    sudo chmod 664 /etc/nagios3/resource.cfg
    sudo usermod -G nagios www-data

    NagiosQL configuration

    sudo chmod 6755 /etc/nagiosql
    sudo chown www-data.nagios /etc/nagiosql
    sudo chmod 6755 /etc/nagiosql/hosts
    sudo chown www-data.nagios /etc/nagiosql/hosts
    sudo chmod 6755 /etc/nagiosql/services
    sudo chown www-data.nagios /etc/nagiosql/services
    

    NagiosQL backup configuration

    sudo chmod 6755 /etc/nagiosql/backup
    sudo chown www-data.nagios /etc/nagiosql/backup
    sudo chmod 6755 /etc/nagiosql/backup/hosts
    sudo chown www-data.nagios /etc/nagiosql/backup/hosts
    sudo chmod 6755 /etc/nagiosql/backup/services
    sudo chown www-data.nagios /etc/nagiosql/backup/services
    

    Nagios Permission

    sudo chgrp www-data /etc/nagios3
    sudo chgrp www-data /etc/nagios3/nagios.cfg
    sudo chgrp www-data /etc/nagios3/cgi.cfg
    sudo chmod 775 /etc/nagios3
    sudo chmod 664 /etc/nagios3/nagios.cfg
    sudo chmod 664 /etc/nagios3/cgi.cfg
    
    

    NagiosQL for setup config

    sudo chown -R www-data:www-data /var/www/html/nagiosql

    6) ทำการติดตั้ง php extension เพิ่มเติมดังนี้

    sudo apt-get install libssh2-1-dev libssh2-php

    การติดตั้ง NagiosQL ผ่าน Web Browser

    1) เริ่มขั้นตอนการติดตั้งผ่าน Web ทำการเปิด URL ดังนี้

    https://localhost/nagiosql

    – กดปุ่ม Start Installation

    2014-07-23_111928

    2) หลังจากนั้นระบบจะตรวจสอบความพร้อม ถ้าระบบพร้อมแล้วจะปรากฎดังภาพ

    2014-07-23_135438

    3) จากนั้นกด Next จะปรากฎหน้าต่างตั้งค่าเกี่ยวกับฐานข้อมูล ให้ใส่ข้อมูลดังภาพ

    2014-07-23_142113

    4) จากนั้นจะปรากฎหน้าจอแสดงผลการติดตั้ง ดังนี้

    2014-07-23_142256

    5) ทำการลบ installation folder ดังนี้

    sudo rm -rf /var/www/html/nagiosql/installation

    6) ทำการกด Finish หรือเข้า URL ใหม่ดังนี้

    https://localhost/nagiosql

    7) ทำการ Login โดยใช้ Username Password ที่ตั้งไว้ดังรูป

    2014-07-23_142756

    สามารถอ่านต่อไปในส่วนของการตั้งค่าให้สามารถควบคุม Nagios ได้ที่นี่
    http://sysadmin.psu.ac.th/2014/07/23/setup-nagiosql-ubuntu

  • การติดตั้งและการตั้งค่าเบื้องต้น Nagios บน Ubuntu 14.04 LTS

    มาเริ่มขั้นตอนการติดตั้งกันเลยครับ โดยก่อนจะติดตั้ง Nagios
    ต้องทำการติดตั้ง Apache2 ก่อนนะครับ

    วิธีการติดตั้ง Apache2 Web Server

    1) ทำการติดตั้ง Apache ผ่าน apt-get ดังนี้ครับ

    sudo apt-get install -y apache2 
    

    2) ติดตั้ง SSL ให้พิมพ์คำสั่งเพื่อเปิด site ดังนี้

    sudo a2enmod ssl
    sudo a2ensite default-ssl

    3) จากนั้นให้สั่ง Restart Apache ตามปกติ

    sudo service apache2 restart

    วิธีการติดตั้ง Nagios

    1) ติดตั้ง Nagios ดังนี้

    sudo apt-get -y install nagios3

    2) จะปรากฎหน้าต่างถามเกี่ยวกับการตั้งค่าส่งเมล์ให้เลือกใช้เป็น Internet with smarthost เพื่อส่งเมล์ผ่าน mail relay ดังรูป
    2014-07-23_054238

    3) จะปรากฎหน้าต่างถามว่าจะใส่ email ผู้ส่งว่าอะไรก็ให้ใส่ไปครับ ไม่ต้องมีอยู่จริงก็ได้ครับ

    2014-07-23_054257

    4) ในหน้าต่างต่อไปจะให้ใส่ว่าจะให้ Relay ไปยัง Mail Server ใดให้ใส่ mail server ที่ต้องการ relay
    2014-07-23_054357

    5) จากนั้นจะให้ตั้งรหัสผ่าน ทำการตั้งรหัสผ่านตามที่ต้องการ (User ตั้งต้นชื่อ nagiosadmin)

    2014-07-23_054430

    6) ทำการตั้งรหัสผ่านเพิ่มเติมได้ที่ไฟล์ /etc/nagios3/htpasswd.users ยกตัวอย่างการเพิ่ม  User ชื่อ workshop (ไม่เกี่ยวข้องกับ user ของระบบใช้เข้า web nagios เท่านั้น) ดังนี้

    sudo htpasswd /etc/nagios3/htpasswd.users workshop

    จะปรากฎข้อความให้ใส่รหัสผ่านดังนี้

    New password: 
    Re-type new password: 
    Adding password for user workshop

    7) สามารถตรวจสอบไฟล์ได้โดยคำสั่ง

    sudo cat /etc/nagios3/htpasswd.users

    8) ทดสอบเปิด Website ดูดังนี้

    http://localhost/nagios3

    9) จะปรากฎหน้าต่างให้กรอก username และ password ให้กรอกไปตามที่ตั้งไว้
    วิธีการ Logout คือปิด browser เท่านั้น

    2014-07-23_060534

    10) จะปรากฎหน้าแรกของ Nagios

    2014-07-23_060555

    วิธีการให้ Redirect ไปยัง SSL อัตโนมัติเพื่อความปลอดภัยของรหัสผ่าน

    1) ทำเปลี่ยนชื่อไฟล์ หน้า default เก่าของ apache จาก index.html เป็น index.html.ori ดังนี้

    sudo mv /var/www/html/index.html /var/www/html/index.html.ori

    2) สร้างไฟล์ /var/www/html/index.html ใหม่ ดังนี้

    sudo nano /var/www/html/index.html
    

    3) เพิ่มข้อความในไฟล์ดังนี้ (วิธีการ save ให้กด Ctrl-X แล้วกด y แล้ว enter) โดยการใช้งานจริงให้แทนที่ localhost ด้วยชื่อเครื่องจริง

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>Auto Redirect to nagios</title>
    <meta http-equiv="REFRESH" content="0;url=https://localhost/nagios3"></HEAD>
    <BODY>
    Please Wait.....
    </BODY>
    </HTML>

    4) ทดสอบเปิด Website อีกรอบดังนี้ จะเห็นได้ว่าระบบจะ redirect ไปยังหน้า nagios3 ที่เป็น ssl อย่างที่ต้องการ

    http://localhost

    วิธีการเพิ่มสิทธิ์ให้กับ User ที่เพิ่มมาใหม่

    1) ทดสอบเข้าเครื่องโดยใช้ User ที่สร้างขึ้นมาใหม่ ลองเข้าดูหน้าต่าง ๆ จะเห็นว่าจะขึ้นบอกว่าไม่สิทธิ์ในการใช้งาน

    2) ทำการแก้ไขไฟล์ /etc/nagios3/cgi.cfg ดังนี้

    sudo nano /etc/nagios3/cgi.cfg

    3) ทำการแก้ไขไฟล์ /etc/nagios3/cgi.cfg ในหัวข้อที่ต้องการจะเปิดสิทธิ์ให้ user นั้น ๆ ยกตัวอย่างกำหนดสิทธิ์ให้ username ชื่อ workshop การเข้าถึงได้ทุกหัวข้อ

    authorized_for_system_information=nagiosadmin,workshop
    authorized_for_configuration_information=nagiosadmin,workshop
    authorized_for_all_services=nagiosadmin,workshop
    authorized_for_all_hosts=nagiosadmin,workshop
    authorized_for_all_service_commands=nagiosadmin,workshop
    authorized_for_all_host_commands=nagiosadmin,workshop
    

    4) จากนั้นให้สั่ง Restart Nagios ดังนี้

    sudo service nagios3 restart

    วิธีการตั้งค่าเสียงเตือน

    1) ทำการแก้ไขไฟล์ /etc/nagios3/cgi.cfg ดังนี้

    sudo nano /etc/nagios3/cgi.cfg

    2) ทำการเปิด comment ดังนี้ (ลบ # ออก) และแก้จาก .wav เป็น .mp3 ดังนี้

    host_unreachable_sound=hostdown.mp3
    host_down_sound=hostdown.mp3
    service_critical_sound=critical.mp3
    service_warning_sound=warning.mp3
    #service_unknown_sound=warning.mp3
    #normal_sound=noproblem.mp3

    3) ทำการโหลดไฟล์เสียงมาเก็บไว้ที่ /usr/share/nagios/htdocs/media ดังนี้

    cd /usr/share/nagios3/htdocs/media
    sudo wget http://ftp.psu.ac.th/pub/psu-monitor/sound.tar.gz
    sudo tar -xvzf sound.tar.gz

    *สามารถโหลดเสียงเพิ่มเติมได้ที่
    http://www.4shared.com/archive/Cr5UBS_W/Nagios-Sound-Pack1.html
    http://www.4shared.com/archive/pJsbVw0v/Nagios-Sound-Pack2.html

    4) จากนั้นให้สั่ง Restart Nagios ดังนี้

    sudo service nagios3 restart
  • 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
    บทความเพิ่มเติม
    ยังไม่มี

     

  • c# string concatenations

    การต่อ String ใน C# ทำได้  4 แบบดังนี้

    1. ใช้เครื่องหมาย plus (+) แบบยอดนิยมใช้งานกันบ่อยๆ.
      string txt = “aaa”+”bbb”+”ccc”;
    2. ใช้ string.Concat() สะดวกับการต่อ list หรือ array มาก.
      string [] s = { “ManU”, “Liverpool”, “Asenal” };
      Console.WriteLine(string.Concat(s));
    3. ใช้ string.Format() เหมาะสำหรับต่อ string และจัดการรูปแบบการแสดงผลด้วยไปในคราวเดียวกันเลย.
      string value1 = “Hello World!”;
      int value2 = 2557;
      DateTime value3 = DateTime.Now();
      string result = string.Format(“{0}: {1:0.0} – {2:dd-mm-yyyy}”, value1, value2, value3);
    4. ใช้ stringBuilder เหมาะสำหรับการต่อ sting ที่มีปริมาณมากๆจะทำงานได้เร็วประมาณ 1000 ขึ้นไป.
      StringBuilder returnNumber = new StringBuilder(1500);
      for(int i = 0; i<1500; i++)
      {
      returnNumber.Append(i.ToString());
      }

    ถ้ามีเวลาจะมาขยายเรื่องนี้ในหัวข้อการทดสอบ Performance จะมาลองดูว่าแบบไหนเร็วกว่ากันไปซักเท่าไร ตอนนี้เอาเท่านี้ไปก่อนแล้วกันครับ

  • วิธีการเขียน 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 -na"
    
     $netstatvalue = invoke-expression -command "$netstat" | where 
    
     {$_ -match "ESTABLISHED"}
    
     $port80 = [string]::join(":",($ip,"80"))
    
     $port443 = [string]::join(":",($ip,"443"))
    
    #Connection
    
    $conn = (invoke-expression -command "$netstat" | where {$_ -match "ESTABLISHED"} | Measure-Object -Line).Lines 
    
    #Unique IP
    
     if($netstatvalue){
    
         $uniqip = ($netstatvalue | where {$_ -match $port80 -or 
    
    $_ -match $port443 } | %{ $_.Split(' ',[StringSplitOptions]'RemoveEmptyEntries')[2] } | %{ $_.Split(':')[0] } | Sort-Object -Unique | Measure-Object -Line).Lines
    
     }else{
    
          $uniqip = 0
    
     }

    ตัวอย่างวิธีการ Display Stat บน Powershell Console

    cls
    
     Write-Host “              Token  : "$token
    
     Write-Host "              Server IP  : "$ip
    
     Write-Host "              TimeStamp  : "$timestamp    

    อาจจะยังไม่ได้อธิบายวิธีการทำ Service Process หรือในส่วนของการตั้ง Task Manager เพื่อทำระบบ Check อัตโนมัติ ไว้ว่าง ๆ เดี๋ยวค่อยมาเขียนต่อให้ครับ

  • แนะนำ ffDiaporama โปรแกรมนำภาพถ่ายมาทำภาพยนตร์ (Movie creator)

    แนะนำ ffDiaporama โปรแกรมนำภาพถ่ายมาทำภาพยนตร์ (Movie creator)

    ffdiaporama-logo

    Movie creator from photos and video clips with ffDiaporama

    ตัวอย่าง http://ftp.psu.ac.th/pub/cc/2557cop1/ccadmintour2014.mp4 (392M)

    โปรแกรม ffDiaporama เป็นโปรแกรมทำภาพยนตร์จากภาพถ่ายและคลิปวิดีโอ มีทั้งเวอร์ชั่นที่ใช้งานบน linux และ Windows มีเว็บไซต์อยู่ที่นี่ URL: http://ffdiaporama.tuxfamily.org/

    โดยทั่วไปวิธีติดตั้งโปรแกรมเพิ่มใน linux mint คือ เลือก Menu > Software Manager
    แต่โปรแกรม ffDiaporama นั้นมันเป็นเวอร์ชั่น 1.5 ซึ่งยังขาดความสามารถในการแปลง (render) จัดได้ว่ามันไม่ทันสมัย (outdated!) จึงจำเป็นต้องติดตั้งด้วยวิธี command line จากเว็บไซต์ของผู้พัฒนาโปรแกรมโดยตรง (Launchpad on the ffDiaporama-stable PPA)

    ทำคำสั่งนี้เพื่อเพิ่มชื่อ server ต้นทางที่เก็บโปรแกรม
    sudo add-apt-repository ppa:ffdiaporamateam/stable
    จะได้ผลลัพธ์แบบนี้
    mint@mint-PSU ~ $ sudo add-apt-repository ppa:ffdiaporamateam/stable
    You are about to add the following PPA to your system:
    This PPA supplies stable packages of ffDiaporama 2.1

    For more information on ffDiaporama see : http://ffdiaporama.tuxfamily.org
    More info: https://launchpad.net/~ffdiaporamateam/+archive/stable
    Press [ENTER] to continue or ctrl-c to cancel adding it
    เมื่อกด Enter แล้วจะได้แบบนี้
    Executing: gpg –ignore-time-conflict –no-options –no-default-keyring –homedir /tmp/tmp.JiNHX5a3T2 –no-auto-check-trustdb –trust-model always –keyring /etc/apt/trusted.gpg –primary-keyring /etc/apt/trusted.gpg –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 77296259
    gpg: requesting key 77296259 from hkp server keyserver.ubuntu.com
    gpg: key 77296259: public key “Launchpad PPA for ffDiaporama Team” imported
    gpg: Total number processed: 1
    gpg:               imported: 1  (RSA: 1)

    สั่งอัปเดตรายการซอฟต์แวร์
    sudo apt-get update
    ติดตั้งโปรแกรม
    sudo apt-get install ffdiaporama
    จะได้ผลลัพธ์แบบนี้
    mint@mint-PSU ~ $ sudo apt-get install ffdiaporama
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    The following packages were automatically installed and are no longer required:
    ffdiaporama-data libqimageblitz4
    Use ‘apt-get autoremove’ to remove them.
    The following extra packages will be installed:
    libavcodec-extra libqt5clucene5 libqt5help5 libqt5svg5
    qt5-image-formats-plugins
    The following NEW packages will be installed:
    ffdiaporama libavcodec-extra libqt5clucene5 libqt5help5 libqt5svg5
    qt5-image-formats-plugins
    0 upgraded, 6 newly installed, 0 to remove and 33 not upgraded.
    Need to get 20.4 MB of archives.
    After this operation, 29.5 MB of additional disk space will be used.
    Do you want to continue? [Y/n]
    ให้กด Enter เพื่อติดตั้ง (ขนาด19.7MB)

    เพิ่มลูกเล่น
    sudo apt-get install ffdiaporama-texturemate
    sudo apt-get install ffdiaporama-openclipart

    References:
    http://ffdiaporama.tuxfamily.org/?page_id=10472

  • วิธีตรวจสอบเว็บไซต์ที่โดน Hack #15

    เทคนิคนี้ ใช้ผ่าน Internet Information Services (IIS) Manager โดยการแก้ไข Request Filtering ในระดับ Web Server เลย โดยดำเนินการตามวิธีการต่อไปนี้

    1. เรียก Command ด้วย การกดปุ่ม Windows + R แล้ว พิมพ์ inetmgr แล้วกดปุ่ม Enter
    2. คลิกเว็บเซิร์ฟเวอร์ของเครื่องที่ต้องการใน Connection Tab (ตัวอย่างในภาพ คลิกที่ WUNCAWEBSEC)
    3. ต่อไป ภายใต้หัวข้อ IIS ให้ Double-Click ที่ Request Filtering
    4. คลิกที่ Rules tab
    5. เพิ่มกฏสำหรับ JCE Bot
      ซึ่ง ไม่ต้องการให้ PHP ทำงานภายใต้ URL ซึ่งมีข้อความว่า “images/stories”
      โดย ไปที่ Action ด้านขวามือ แล้ว คลิกที่ Add Filtering Rules …
      แล้วใส่ข้อมูลตามภาพ แล้วคลิกปุ่ม OK

    1. เพิ่มกฏสำหรับ Upload โฟลเดอร์
      ซึ่ง ไม่ต้องการให้ PHP ทำงานภายใต้ URL ซึ่งมีข้อความว่า “upload”
      โดย ไปที่ Action ด้านขวามือ แล้ว คลิกที่ Add Filtering Rules …
      แล้วใส่ข้อมูลตามภาพ แล้วคลิกปุ่ม OK
    2. ผลที่ได้ใน Rules tab

    ทดสอบผลการทำงาน

    สมมุติเดิมโดนวางไฟล์ Backdoor ไว้ที่

    http://localhost/corin/images/stories/backdoor.php

    แต่เมื่อตั้ง Rules ดังกล่าวแล้ว จะทำให้ Hacker ไม่สามารถเรียกใช้งาน PHP ที่วางไว้ใน images/stories ได้ โดยจะได้ Error เช่นนี้

    วิธีนี้มีข้อดีคือ สามารถป้องกันการใช้งาน PHP ใน images/stories (และใน upload โฟลเดอร์) แต่ยังสามารถเรียกไฟล์ภาพและไฟล์อื่นๆได้ตามปรกติ เช่น

    http://localhost/corin/images/stories/clownspin.gif

    ลองใช้งานดูครับ 😉