Config MYSQL อย่างเซียน

Author: pookpui  |  Category: Personal

ภาคที่1
MySQL นับว่าเป็นหัวใจของ Web Server อีกตัวหนึ่งเลยก็ว่าได้เพราะว่า MySQL
นั้นเป็นแหล่งข้อมูลที่สามารถเรียกใช้งานได้อย่างรวดเร็ว วันนี้ผมจะเอาประสบการณ์เกี่ยวกับการ Config
MySQL มาให้อ่านกัน

ผมอิงตามรุ่น 4.1 โดยใช้กับ Server ที่ใช้ tis620 เป็น Default นะครับ

เริ่มต้นที่การ Compile PHP ให้สนับสนุน MySQL
ปกติแล้วสามารถ Compile PHP ให้สนับสนุน MySQL ด้วยการใช้ –with-mysql วิธีการนี้จะเป็นการใช้ MySQL
Lib Client ที่ Bundle มากับ PHP ครับ ซึ่งเป็น Version เก่า นอกจากนี้ยังมี Extension ใหม่ชื่อ MySQLi
ซึ่งถ้าจะใช้ MySQLi จะไม่สามารถใช้ MySQL Lib Client ที่ Bungle มาด้วยได้ มันจะตีกัน
ดังนั้นเริ่มต้นผมแนะนำให้คุณ Compile PHP ด้วย –with-mysql=/usr/local/mysql (หรือถ้า mysql
อยู่ที่อื่นก็ใช้ path อื่นแล้วกันครับ)

เพื่อความสะดวกในการใช้งานภาษาไทย มักจะ setup ใน my.cnf ว่า default-character-set = tis620
วิธีการนี้จะทำให้ MySQL ทำงานช้าลงไปประมาณ 20 – 30% แต่ไม่เป็นไรครับ
เพราะว่ายังไงผมก็ต้องใช้ภาษาไทยอยู่แล้ว

หลังจากใส่คำสั่งว่า default-character-set = tis620 ลงไปใน my.cnf แล้ว ผมที่ได้คือ MySQL Client
มันต๊องครับ เพราะว่า Charset ของ Server เป็น tis620 แต่ของ Client เป็น Latin ครับ ดังนั้นต้อง setup
เพิ่มอีกตัวหนึ่งคือ skip-character-set-client-handshake ใส่ส่วน my.cnf ครับ วิธีการนี้จะทำให้ Client
ทำงานที่ Charset เดียวกับ Server เลยครับ

skip-locking – อันนี้ถ้าจำไม่ผิดเขาเปลี่ยนชื่อเป็น skip-external-locking เกี่ยวกับการทำ Repicate
MySQL Server ผมไม่แน่ใจว่าถ้ามี Server เดียวจะช่วยเพิ่มประสิทธิภาพอะไรได้หรือเปล่า
แต่ใส่ไว้ก็ไม่เสียหายครับ

skip-thread-priority – เป็นการกำหนดครับว่าไม่ต้องให้ thread แซงคิวกันได้ MySQL จะให้ QUERY
แต่ละแบบมีความสำคัญไม่เท่ากัน ผมจำไม่ได้ว่าอะไรมากกว่าอะไร แต่การเอาหัวข้อนี้ออกทำให้ระบบ queue ของ
MySQL ไม่ต้องมายุ่งยากกับการจัดคิวและทำงานเป็น FIFO แทนครับ

skip-bdb – ไม่ได้ใช้ก็ข้ามไปครับ ถ้าใช้ bdb ก็ Comment บรรทัดนี้ซะ สำหรับผมแล้วผมใช้แค่ MYISAM กับ
INNODB ครับ

skip-networking – อันนี้เป็นการบอก MySQL Server ว่าไม่ต้อง Listen ที่ INET SOCKET ครับ ให้ Listen
ที่ UNIX SOCKET อย่างเดียวพอ อันนี้ไม่ได้เพิ่มความเร็วมากนัก แต่ลดโอกาสการโดนโจมตีได้ครับ

log-slow-queries – อันนี้ใช้เฉพาะเวลาที่ต้องการดูว่า Query อันไหนทำงานช้า จะได้มาปรับแต่งได้ครับ

ภาคที่ 2
การบริหาร Thread – ตัวแปรเกี่ยวกับ Thread ที่สำคัญของ MySQL คือ thread_cache
ตัวแปรนี้จะเป็นการไม่ทำลาย thread ของ MySQL ให้ต่ำกว่าเลขนี้ครับ ปกติก็เดาไปเรื่อยๆ ครับ โดยดูจาก
Status ของ MySQL ผมแนะนำให้ดูจาก phpMyAdmin ครับ สะดวกดี จะมีค่าเกี่ยวกับ thread คือ

Threads cached 143
Threads connected 7
Threads created 532
Threads running 1

Threads cached – คือจำนวน threads ที่อยู่ในโปรแกรม MySQL ตอนนี้ จะเห็นได้ว่ามี 143 threads
Threads connected – คือจำนวน threads ที่ใช้งานจริงๆ ครับ
Threads running – คือ threads ที่กำลังหาผลการ Query อยู่ครับ
Threads created – คือจำนวน threads ที่สร้างใหม่ตั้งแต่เริ่ม Server มาครับ ถ้าค่านี้เพิ่มเร็วเกินไป
ให้เพิ่มจำนวน Thread_cache ครับ ผลที่ได้คือ MySQL จะทำงานเร็วขึ้นนิดหน่อยเพราะว่าจะไม่ต้องเสียเวลา
สร้างและทำลาย Threads บ่อยๆ ครับ

ภาคที่ 3

MYISAM กับหน่วยความจำ ตัวแปรที่เราสนใจคือ

key_buffer=32M
sort_buffer_size=1M
read_buffer_size=1M
read_rnd_buffer_size=4M

- Key Buffer คือพื้นที่สำหรับ Cache ค่า Key ของแต่ละ Table ครับ โดยที่ Key ของ MySQL มี 3 ตัวคือ
PRIMARY, INDEX และ UNIQUE ครับ ปกติถ้ามีการใช้ Table MyISAM มากๆ ค่านี้ควรจะมากๆ ครับ
ของผมมีใช้ไม่มากเลยไม่ต้องใช้ค่า Key_Buffer มาก แนะนำ 16MB สำหรับแรม 256
และเพิ่มมากขึ้นเมื่อแรมมากขึ้น

- Sort Buffer คือหน่วยความจำที่ MySQSL แต่ละ Connection จะจองเพิ่ม เพื่อทำ Table Scan ครับ
ปกติถ้าคุณจะ Sort Field ที่ไม่ใช่ Key จะต้องใช้หน่วยความจำส่วนนี้เสมอ ให้ Setup เริ่มต้นตั้งแต่ 512K
ขึ้นไป เนื่องจากเป็นหน่วยความจำที่จะมีการจองเพิ่มต่อ Connection ดังนั้นจะไม่ควรจะ Setup
ให้สูงเกินเพราะว่าจะทำให้ MySQL ทำงานจนหน่วยความจำหมด

- Read Buffer คือหน่วยความจำที่ MySQL จะใช้ในการเก็บค่าที่อ่านจากตารางแบบต่อเนื่อง (คือไม่ได้ Sort)
ไม่จำเป็นต้องมากนักก็ได้ เพราะว่าปกติเราจะมีการทำ LIMIT ในการอ่านค่ามาแสดงบนเว็บอยู่แล้ว

- Read-Random Buffer Size คือหร่วมความจำที่ MySQL จะใช้เก็บค่าจากตารางแบบไม่ต่อเนื่อง (เช่นผลการ
Sort) ควรจะใหญ่กว่า Read Buffer

ของผมไม่ค่อยได้ใช้ MyISAM ค่าต่างๆ นี้เลยไม่ได้ Set เอาไว้มากนัก ครับ เดี๋ยวไปต่อภาค 4 เรื่องของ
tmp_table ครับ

ภาคที่ 4
ในภาดนี้เราจะพูดถึง tmp_table ปกติแล้วในการ Complex Query นั้น MySQL
จะทำการสร้างตารางผลลัทธ์ขึ้นมาในหน่วยความจำเป็น เป็น TABLE แบบ HEAP
แต่ถ้าตารางมีขนาดใหญ่กว่าค่าค่าหนึ่ง MySQL จะคัดลอกตารางนั้นลง Disk เป็น MyISAM TABLE ครับ
เราจะมาดูค่าค่านั้นกันครับ

ก่อนที่จะไปไกลกว่านั้น เรามาพูดถึง Complex Query ก่อนครับ โดยมากเราจะพูดถึง Query ที่มีการใช้ GROUP
BY, UNIQUE, LIKE และที่ไม่แน่ใจคือ SUB SELECT ครับ

วิธีการดูว่ามีการ Swap ลงหน่วยความจำมากน้อยแค่ไหน สามารถดูได้จาก
Created tmp disk tables 14652
Created tmp tables 222220

โดยเมื่อมีการสร้าง tmp_table MySQL จะเพิ่มค่า Created tmp tables ครับ และถ้ามีการ Swap ลง Disk
จะเพิ่มค่า Created tmp disk tables ปกติ ถ้านำสองค่านี้มาหารกัน คูณ ร้อย ไม่ควรจะเกิน 5-10% ครับ
ขึ้นอยู่กับว่าตารางที่คุณใช้ใหญ่เล็กอย่างไร มีความซับซ้อนมากแค่ไหน

ตัวแปรที่จะควบคุมการ Swap จะมี 2 ตัวคือ

tmp_table_size=32M
max_tmp_tables=32

โดยถ้า tmp_table ใหญ่กว่า tmp_table_size จะ Swap ลง Disk ครับ หรือถ้ามีจำนวน tmp_table มากกว่า
max_tmp_tables ก็จะ Swap ลง Disk เช่นกันครับ
ค่า tmp_table_size ปกติเป็น 32M และ max_tmp_tables จะเป็น 32 ครับ คุณไม่ควร Setup ให้สูงกว่า 2
เท่าของค่าปกติ แต่แนะนำให้ลองไปตรวจสอบครับว่าโปรแกรมของคุณมีทางที่จะ Optimize Query ได้มากแค่ไหน
หรือ จะใช้วิธีการ Cache ผมลัพธ์ของหน้าเว็บเข้ามาช่วยก็ได้ครับ

ภาคที่ 5 – Key Buffer แบบเชิงลึก

Key Buffer คือหน่วยความจำที่ MySQL จองไว้หนเดียว แล้วใช้งานแชร์กันทุกๆ Process
(ดังที่ได้พูดไว้ก่อนหน้านี้ครับ)

แต่เราจะมาพูดถึงประสิทธิภาพของ Key กันครับ ค่าที่น่าสนใจคือ

Key blocks unused 27683
Key blocks used 1312
Key read requests 1318393
Key reads 1344

คู่แรกจะบอกว่า Key Buffer ของคุณใช้งานไปมากน้อยแค่ใด ปกติแล้ว Key Blocks Unused จะไม่มากครับหรือเป็น
0 เลยก็ได้ อย่างตัวอย่างแสดงว่าเรากำหนดค่า Key_Buffer มากเกินไปครับ

คู่ที่สองถ้าเอา (Key read requests – Key reads) * 100 / Key read requests เราจะเรียกว่า Key Hits
Rate ครับ อย่างตัวอย่างคือ 99.9 ครับ แสดงว่า Key Hits Rate ดีมากครับ ปกติแล้วจะอยู่ที่ประมาณ 95 -
99% ครับ ถ้าน้อยกว่านี้แนะนำให้เพิ่ม Key_Buffer ครับ สำหรับ Key Hits Rate นั้นจะต้องคิดเมื่อทำงาน
MySQL ไปแล้วสักพักนะครับ อาจจะ 2-3 วัน ครับ

ภาคที่ 6 – Table Cache

สำหรับ Table Cache นั้นเป็นการเปิด Handle ของ Table ทิ้งเอาไว้ครับ เพื่อการเข้าถึงข้อมูลใน Table
ได้อย่างรวดเร็วครับ แต่ถ้าคุณเพิ่มค่านี้มากๆ คุณอาจจะเกิดปัญหาว่า File Descriptor ไม่พอครับ
ถ้าผมจำไม่ผิดแนะนำให้เพิ่ม File Descriptor ได้จากการแก้ไขตัวแปร Kernel ที่ /proc/sys/fs/file-max
ครับโดยการใช้คำสั่ง

echo 392604 > /proc/sys/fs/file-max

ผมไม่แน่ใจว่าการใช้คำสั่ง ulimit จะได้ผลเหมือนกันหรือไม่

กลับมาต่อที่ table_cache ปกติแล้วถ้าในระบบที่มีตารางมากๆ table_cache
ควรจะครอบคลุมตารางพื้นฐานทั้งหมด และอีกประมาณ 50% ของตารางที่เหลือ
แต่ถ้าเป็นไปได้จะครอบคลุมทั้งหมดเลยก็ไม่ผิดแต่อย่างใด อย่างของผมเอง set ไว้ที่ 1024 เลยครับ

วิธีการจะดูว่า set ไว้น้อยเกินไปหรือเปล่า ให้ดูที่

Open tables 1024
Opened tables 1120

โดย Open tables คือจำนวน Table ที่เปิดอยู่ขณะนี้ และ Opened tables คือจำนวน Table ที่เปิดมาทั้งหมด
นับตั้งแต่เริ่ม MySQL Server มา โดยถ้าค่าของ Opened tables เพิ่มเร็วเกินไป แนะนำให้เพิ่มค่า
table_cache ครับ

วิธีการปรับค่า table cache ทำได้โดย เพิ่มบรรทัดนี้ใน my.cnf

table_cache=1024

ภาคที่ 7 Query Cache

Query Cache นั้นเป็นคุณสมบัติใหม่ที่มีใน MySQL รุ่นที่ 4.x ขึ้นมาครับ

Query Cache ทำงานง่ายๆ คือ ถ้ามี Query เหมือนเดิม MySQL จะเรียกจาก Cache แทนที่จะไป Query ใหม่ครับ

แต่ Query Cache ไม่ได้มีประโยชน์กับทุก Database Structure นะครับ Query Cache เหมาะกับ Table
ที่ไม่ค่อยได้ Update แต่มีจำนวน Records เป็นจำนวนมาก เช่น 50,000 records ขึ้นไป Query Cache จะใช้กับ
Select เท่านั้นครับ ถ้าระบบของคุณแตกต่างจากนี้การใช้ Query Cache อาจจะทำให้ได้ผลตรงกันข้ามก็ได้ครับ

วิธีการเปิดใช้งาน Query Cache ให้ใส่บรรทัดนี้ลงใน my.cnf ครับ

query_cache_type=1
query_cache_size=32M

query_cache_type จะมีได้ 3 ค่าคือ
0 – ปิด Query Cache
1 – เปิด Query Cache คุณสามารถสั่งให้ไม่ต้อง Cache ได้โดยการใช้ “SELECT SQL_NO_CACHE”
2 – แบบ On Demand คุณสามารถสั่งให้ MySQL Cache โดยการใช้ “SELECT SQL_CACHE”

ปกติแล้วถ้า Table มีการ Update แล้ว MySQL จะลบ Cache ของ Table นั้นๆ ทั้งหมดทันที และ Query Cache
นั้นเป็น Case Sensitive ครับ ดังนั้น

SELECT * FROM a WHERE b=1

กับ

select * from a where b=1

จะไม่เหมือนกันนะครับ ถ้าเราเรียกตัวแรกแล้วเรียกตัวที่ 2 ตัวที่ 2 จะไม่ได้เรียกจาก Cache

ดังนั้นถ้า
1. ในระบบของคุณมีการเขียน SQL แบบไม่ได้วางแผนเรืองตัวใหญ่ตัวเล็ก คุณจะได้รับประโยชน์จาก Query Cache
น้อยลง
2. ถ้า Table หลักๆ ของคุณมีการ Update ตลอดเวลา คุณจะได้รับประโยชน์จาก Query Cache น้อยลง
3. ถ้า Table หลักๆ ของคุณไม่ได้มีจำนวน Records มากคุณก็แทบจะไม่ได้รับประโยชน์จาก Query Cache
เลยครับ

แหล่งที่มา
http://www.thaiadmin.org/board/index.php?topic=18693.new

Tunning and Optimizing MYSQL

Author: pookpui  |  Category: Linux

[mysqld]
socket=/path/to/mysql.sock
datadir=/var/lib/mysql
skip-locking
skip-innodb
# MySQL 4.x has query caching available.
# Enable it for vast improvement and it may be all you need to tweak.
query_cache_type=1
query_cache_limit=1M
query_cache_size=32M
# max_connections=500
# Reduced to 200 as memory will not be enough for 500 connections.
# memory=key_buffer+(sort_buffer_size+read_buffer_size)*max_connections
# which is now: 64 + (1 + 1) * 200 = 464 MB
# max_connections = approx. MaxClients setting in httpd.conf file
# Default set to 100.
#max_connections=200
#interactive_timeout=180
interactive_timeout=100
#wait_timeout=180
#wait_timeout=100
# Reduced wait_timeout to prevent idle clients holding connections.
#wait_timeout=30
wait_timeout=15
connect_timeout=10
# max_connect_errors is set to 10 by default
#max_connect_errors=10
#table_cache=256
#table_cache=1024
# Checked opened tables and adjusted accordingly after running for a while.
table_cache=512
#tmp_table_size=32M by default
#thread_cache=128
# Reduced it to 32 to prevent memory hogging. Also, see notes below.
thread_cache=32
# key_buffer=258M
# Reduced it by checking current size of *.MYI files, see notes below.
key_buffer=128M
# Commented out the buffer sizes and keeping the default.
# sort_buffer_size=2M by default.
#sort_buffer_size=1M
# read_buffer_size=128K by default.
#read_buffer_size=1M
# 1Mb of read_rnd_buffer_size for 1GB RAM — see notes below.
# read_rnd_buffer_size=256K by default.
#read_rnd_buffer_size=1M
# myisam_sort_buffer_size used for ALTER, OPTIMIZE, REPAIR TABLE commands.
# myisam_sort_buffer_size=8M by default.
#myisam_sort_buffer_size=64M
# thread_concurrency = 2 * (no. of CPU)
thread_concurrency=2
# log slow queries is a must. Many queries that take more than 2 seconds.
# If so, then your tables need enhancement.
log_slow_queries=/var/log/mysqld.slow.log
long_query_time=2

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
open_files_limit=8192

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[myisamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[mysqlhotcopy]
interactive-timeout

[client]
socket=/path/to/mysql.sock
Below are notes on some of the important variables, I took down while tuning the config file.

query_cache_size:
MySQL 4 provides one feature that can prove very handy – a query cache. In a situation where the database has to repeatedly run the same queries on the same data set, returning the same results each time, MySQL can cache the result set, avoiding the overhead of running through the data over and over and is extremely helpful on busy servers.
key_buffer_size:
The value of key_buffer_size is the size of the buffer used with indexes. The larger the buffer, the faster the SQL command will finish and a result will be returned. The rule-of-thumb is to set the key_buffer_size to at least a quarter, but no more than half, of the total amount of memory on the server. Ideally, it will be large enough to contain all the indexes (the total size of all .MYI files on the server).

A simple way to check the actual performance of the buffer is to examine four additional variables: key_read_requests, key_reads, key_write_requests, and key_writes.

If you divide the value of key_read by the value of key_reads_requests, the result should be less than 0.01. Also, if you divide the value of key_write by the value of key_writes_requests, the result should be less than 1.
table_cache:
The default is 64. Each time MySQL accesses a table, it places it in the cache. If the system accesses many tables, it is faster to have these in the cache. MySQL, being multi-threaded, may be running many queries on the table at one time, and each of these will open a table. Examine the value of open_tables at peak times. If you find it stays at the same value as your table_cache value, and then the number of opened_tables starts rapidly increasing, you should increase the table_cache if you have enough memory.
sort_buffer:
The sort_buffer is very useful for speeding up myisamchk operations (which is why it is set much higher for that purpose in the default configuration files), but it can also be useful everyday when performing large numbers of sorts.
read_rnd_buffer_size:
The read_rnd_buffer_size is used after a sort, when reading rows in sorted order. If you use many queries with ORDER BY, upping this can improve performance. Remember that, unlike key_buffer_size and table_cache, this buffer is allocated for each thread. This variable was renamed from record_rnd_buffer in MySQL 4.0.3. It defaults to the same size as the read_buffer_size. A rule-of-thumb is to allocate 1KB for each 1MB of memory on the server, for example 1MB on a machine with 1GB memory.
thread_cache:
If you have a busy server that’s getting a lot of quick connections, set your thread cache high enough that the Threads_created value in SHOW STATUS stops increasing. This should take some of the load off of the CPU.
tmp_table_size:
“Created_tmp_disk_tables” are the number of implicit temporary tables on disk created while executing statements and “created_tmp_tables” are memory-based. Obviously it is bad if you have to go to disk instead of memory all the time

Credit: http://www.linuxweblog.com

Asterisk on Debain

Author: pookpui  |  Category: Linux

Asterisk on Debian
องค์กรผมมีใช้งาน VoIP Server ติดต่อระหว่างสาขา และใช้งาน SIP Phone , Softphone ( X-Lite ) ติดต่อในองค์กร และมีการเชื่อมต่อกับคู่สาย PSTN ( คู่สาย TOT & TT&T นั่นแหละครับ ) ซึ่งมีความจำเป็นที่จะต้องติดตั้งให้ ใช้งานร่วมกับการ์ด Asterisk ได้ โดยใช้ zaptel

driver โดยซอฟแวร์ทั้งหมดเป็น Opensource เดิมทีนั้น Gateway ของบริษัท ใช้เป็น FreeBSD ใช้มาก็ เกือบๆ 2 ปี ช่วงหลังเริ่มย้าย App Server มาเป็น Linux บ้าง พวก Server ที่ Test App น้องๆ ก็เปลี่ยนมาใช้บน Linux กันหมด
ล่าสุดก็เปลี่ยน Proxy, WebServer , Load Balancer และก็ AAA Gateway มาเป็น Debian ด้วยเหตุผลที่ว่า น้องในองค์กรไม่ถนัด FreeBSD โอเค ตรงนี้ตอบโจทย์ได้หมด เพราะว่า ผมก็พอได้ debian based linux มาบ้าง

โอเค เอาเป็นว่าเข้าเรื่องเลย ก่อนอื่นผมติดตั้ง asterisk ก่อนครับ มี sox พ่วงมาด้วย ซึ่งเอาไว้แปลง .wav เป็น .gsm ใช้เป็นระบบตอบรับอัตโนมัติ และเสียงรอสายครับ

# apt-get install asterisk sox

ติดตั้งเสร็จก็ แก้ไขให้รัน Asterisk Daemon ครับ

# nano -w /etc/default/asterisk

แก้ค่า RUNASTERISK=no เป็น RUNASTERISK=yes

จากนั้นติดตั้ง zaptel เพื่อรองรับ Asterisk Card

# apt-get install zaptel

จากนั้นสร้าง zaptel.conf ไปวางไว้ที่ etc โดยการใช้ zcat -c ไปครับ

# zcat -c /usr/share/doc/zaptel/examples/zaptel.conf.sample.gz >> /etc/zaptel.conf

# nano -w /etc/zaptel.conf

หา fxsks= แล้วแก้ไขค่าเป็น 1

จากนั้นก็กำหนด channel ให้ * card ครับ สังเกตว่าผมจะใช้ txgain / rxgain อันนี้คือ ขยาย gain ความดังของเสียงที่เราได้ยินนั่นเอง ผมใช้ เป็น 6.0 และ 3.0 ตามลำดับ ครับ เป็นค่าที่ใช้งานได้โอเคในองค์กรผม

# nano -w /etc/asterisk/zapata.conf

[channels]
context=default
language=en
echocancel=yes
echocancelwhenbridged=yes
signalling=fxs_ks
group=1
busydetect=yes
busycount=4
channel => 1
txgain=6.0
rxgain=3.0

จากนั้นก็ เริ่ม start asterisk

# /etc/init.d/asterisk start

แล้วลอง เข้า CLI

# asterisk -r

แล้วพิมพ์ quit ออกมา

แล้วลองตรวจสอบดูว่า zaptel ทำงานถูกต้องแล้วหรือยัง

# ztcfg -vv

root@alpha:/etc/asterisk# ztcfg -vv
Notice: Configuration file is /etc/zaptel.conf
line 0: Unable to open master device ‘/dev/zap/ctl’

1 error(s) detected

* Opp! กรณีติดตั้ง Debian/Ubuntu ไม่เจอ ztdummy ตาม error ข้างบนนั้น ตรงนี้ไม่ต้องตกใจครับ แก้ตามนี้เลย ก่อนอื่นก็..เอา zaptel-source มาก่อน

# apt-get install zaptel-source

ตรวจดูว่า มี source อยู่หรือยังถ้ามีแล้วจะเจอ zaptel.tar.bz2 ที่ /usr/src

# ls /usr/src

จากนั้นเตรียมการเพื่อ Build Drivers

# module-assistant prepare

แล้วก็ Build .deb กันเลย

# m-a -t build zaptel

จากนั้นจะเจอ ไฟล์ .deb ที่เหมาะกับ OS version ที่เราใช้ ซึ่งตอนเขียนบทความนี้ผมเขียนตอนติดตั้งบน Ubuntu 8.04.1 Server ถ้าใช้ Debian หรือ version อื่นก็ลองดูนะครับ ชื่อไฟล์แตกต่างกันไป

# cd /usr/src
# ls *.deb
zaptel-modules-2.6.24-19-server_1.4.10~dfsg-1+2.6.24-19.36_i386.deb

ทำการติดตั้งกันเลย

# dpkg -i zaptel-modules-2.6.24-19-server_1.4.10~dfsg-1+2.6.24-19.36_i386.deb

จากนั้นก็อัพเดท Modules ต่างๆ

# depmod -a
# modprobe zaptel
# modprobe wcfxo
# echo ‘zaptel’ >>/etc/modules
# echo ‘wcfxo’ >>/etc/modules

เสร็จแล้ว จากนั้นลองเช็คอีกที

# ztcfg -vv

ถ้าสำเร็จ ผลจะได้ประมาณนี้ ไม่มี errors พร้อมแจ้งกลับมาว่า 1 channels to configure.

Zaptel Version: 1.4.10
Echo Canceller: MG2
Configuration
======================

Channel map:

Channel 01: FXS Kewlstart (Default) (Slaves: 01)

1 channels to configure.

ผ่านแล้ว จากนั้นทำการเพิ่ม sip เบอร์โทรและ รหัสผ่านสำหรับ register มายัง server ตัวนี้ครับ

# nano -w /etc/asterisk/sip.conf

หรือถ้าจะแก้ไข Music On Hold เสียงรอสาย ก็ลองเข้าไปที่

# mkdir mkdir /usr/share/asterisk/mohmp3

แล้วอัพโหลดไฟล์เพลงเสียงรอสายที่เตรียมไว้เป็น .gsm ไปวางไว้จากนั้น

# chmod 755 -R /usr/share/asterisk/mohmp3

# nano -w /etc/asterisk/musiconhold.conf

[default]
;mode=quietmp3
mode=files
directory=/usr/share/asterisk/mohmp3
random=yes

จากนั้นแก้ extensions โดยการสำรองของเก่าไว้ก่อน

# cd /etc/asterisk/
# cp extensions.conf extensions.conf.save

และทำให้ ไฟล์ว่างๆ ก่อนนะครับ

# echo “” > extensions.conf

แก้ rules กันเลยครับ

[globals]
SetGlobalVar(numRings=18)
SetGlobalVar(defaultChannel=Zap/1)

[default]

exten => s,1,Answer( )
exten => s,2,Background(/usr/share/asterisk/moh/welcome3) ; เสียงตอบรับหวานๆ ของน้องที่บริษัท
exten => s,3,WaitExten( )
exten => s,4,Hangup( )
exten => 9,1,Dial(Zap/1) ; ตัด 9 ก่อนโทรออก ของเพื่อนๆ ตัดอะไรก่อนก็ว่ากันไปนะครับ

exten => i,1,Answer( )
exten => i,2,Playback(/usr/share/asterisk/sounds/invalid3) ; เสียงตอบรับเมื่อกด extension ผิด
exten => i,3,Hangup( )

exten => t,1,Answer( )
exten => t,2,Playback(/usr/share/asterisk/sounds/beep3) ; เสียงบี๊บ
exten => t,3,Hangup( )

; office extensions ก็ใส่ไปครับ มีเบอร์อะไรบ้าง จริงๆ มีเยอะกว่านี้

exten => 201,1,Dial(SIP/201,80,m) ; operator
exten => 202,1,Dial(SIP/202,80,m) ; support call
exten => 203,1,Dial(SIP/203,80,m) ; sales call
exten => 204,1,Dial(SIP/204,80,m) ; personal call
exten => 205,1,Dial(SIP/205,80,m) ; problem reports call
exten => 206,1,Dial(SIP/206,80,m) ; fax
exten => XXXX,1,system(/sbin/shutdown -r now)
exten => XXXX,1,system(/usr/local/bin/wakeonlan -i 192.168.1.11 -p 1000 00:14:2A:84:28:8F)

สังเกตว่า ผมจะมีคำสั่ง shutdown -r now และ wakeonlan มาด้วย อันนี้คือเพิ่มเข้าไปนะครับ กรณีนี้คือต้องการ Reboot ตัวมันเอง จากการโทรมาจากภายนอก และอีกตัวหนึ่งคือสั่ง Boot เครื่อง Application ในองค์กรผม อันนี้ผมจะใส่ไว้เป็น XXXX ไปทั้งสองตัว จริงๆ มันใส่

รหัสผ่านอีกชั้นได้นะครับ เอาเป็นคราวหน้ามีเวลาเมื่อไหร่จะมาเขียนต่อครับ

ก็เสร็จแล้วสำหรับการติดตั้ง Asterisk ใช้งาน บน Debian Based สไตล์ผม เอาไว้คราวหน้ามีเทคนิคใหม่ๆ จะมาเขียนต่อให้อ่านกันครับ

* สำหรับ sox นั้นอัพโหลด .wav ขึ้นไป จากนั้น แปลงง่ายๆ ครับ sox [file.wav] -r 8000 -c1 [file.gsm] resample -ql

วิธีการเพิ่มความเร็ว SpamAssassin rule processing บน Debian

Author: pookpui  |  Category: Linux

SpamAssassin is one of the most-used spam filtering systems in use today. Unfortunately, because there are so many different ways SpamAssassin can be used, SpamAssassin remains subject to many performance problems. Fortunately, there are several speed-ups and optimizations that can be applied to most SpamAssassin installations to speed up its rule processing, especially on Debian and Ubuntu GNU/Linux-based systems. These instructions can be adopted to other operating systems as well.

This article does not discuss configuring your mail filtering system (i.e. procmail, maildrop). This depends completely on your setup, and more than likely there are plenty of other articles that describe the best way to setup what you want.

The spamc/spamd client/server combination
A normal SpamAssassin setup invokes the spamassassin command for each incoming e-mail that needs to be scanned. This process is expensive when you consider what this does: for each incoming e-mail, a perl interpreter starts and loads all of SpamAssassin’s perl code, including its thousands of rules. Starting SpamAssassin like this, with a decent amount of incoming e-mail, will increase I/O load and slows things down, fast.

SpamAssassin 2.x introduced the spamc/spamd combination to alleviate this issue. spamd is a version of SpamAssassin intended to be run as long-running process that accepts connections from spamc. spamc is a lightweight C program made to replace the spamassassin command, taking a message and passing it to spamd for processing. This alleviates all of the I/O caused by loading perl and all of SpamAssassin’s rules repeatedly. Instead, both perl and SpamAssassin’s rule sets are only loaded once.

Setting this up and relatively quick and easy. Install:

sudo aptitude install spamc
Then, edit /etc/default/spamassassin to enable startup of the spamd process. In this file, change:

ENABLED=1
Start the spamd process:

sudo /etc/init.d/spamassassin start

Next, in your mail filtering scripts wherever you invoke the spamassassin command, invoke the spamc command instead. After verifying spam filtering still works after all these changes, you are done.

One important thing to note: the spamd/spamc combination sets up another service running on your system, possibly opening up a security hole waiting to be exploited. Make sure to follow due process (securing user accounts, setting up firewall, etc) before enabling new network services.

Precompiled Rules
Erich Schubert went into SpamAssassin 3.2’s ability to precompile rules a few months ago. While Perl’s regular expressions are fast, C regular expressions can be faster (and consume less memory). SpamAssassin can compile its rules into a C shared library that is used instead of interpreted perl code. Eric mentions a negative to precompilation: it requires your mail server to have a C compiler.

If you’re comfortable with that, setting up your system for precompiling SpamAssassin’s rules is easy. Install the required depencencies:

sudo aptitude install re2c build-essential
and run the sa-compile command:

sudo sa-compile
You then need to configure SpamAssassin to use the precompiled rules. This is done by editing /etc/spamassassin/v320.pre, and commenting out the line:

loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody

Drag me Hell

Author: pookpui  |  Category: Personal

วันศุกร์ที่ผ่านมางานเยอะมากๆ รีบๆ ทำงาน ยังไงวันนี้ตอนเย็น ก็จะได้เจอกรุณาแล้ว เรามีนัดไปดูหนังกัน
นั่งทำงานไปเรื่อยๆ 18.15pm แล้ว เลยโทรไปหา ไม่อยากทำงานแล้ว
ไปรับตูนที่ห้อง ตกลงกันว่าจะไปดูที่หนังที่เอสพานาสกัน ถึงเอสพานาสประมาณ 20:10pm เลยไปซื้อตั๋ว
รอบ 20.50pm
Drag me hill ticket

มีเวลาอีกตั้ง 40 นาที เลยลงไปเดินเล่นไปหาอะไรกินกันข้างล่าง ระหว่างนั้นก็เลยแวะไปซื้อเชอร์รี่ของชอบมากินกันก่อน
เชอร์รี่สดๆ อร่อยจัง

ต่อจากนั้นก็เดินไปหาอะไรกินกัน ตอนแรกกะว่าจะไปกินฟูจิกัน แต่ต้องรอคิวเลยแวะไปร้านยาโออิ (ไม่รู้สะกดแบบนี้เปล่า) กรุณาก็หูดีได้ยินว่าผมพูดเป็น อาโออิไปซะงั้น -.-’
ตกลงกันแล้วว่า อาโออิ เอ้ย ยาโออิ เลยเข้าไปหาที่นั่ง แล้วก็สั่งมากินคนละอย่างกัน
อันนี้ของกรุณา สลัดปลาทูน่า
img_0228

อันนี้ของทัศไนย หมูย่างกะทะร้อน ประมาณนั้น
img_0227

ระหว่างทานอาโออิไป เพื่อยืนยันความสดและความอร่อยของเชอร์รี่ เลยเอามาให้ดูกันชัดๆ อีกทีนึง

เชอรี่สด
เห็นมั้ยว่าเชอร์รี่สดจริงๆ จนสีไปติดเล็บของกรุณาเลย แจ่มที่สุดเล็บสีใหม่ของกรุณา

ทานๆ ไปอ่านแผ่นปลิวโฆษณาบนโต๊ะไป เลยเห็นกระติดน้ำสุดน่ารัก น้องแมวอาโยอิ เลยแลกซื้อมาเลย
น้องแมว ยาโออิ

มีหางด้วยนะ
หาง น้องแมว ยาโออิ

น่ารักจริงๆ แต่แล้วกรุณาก็ลืมน้องแมวไว้บนรถผมอีกตามเคย

หนังเรื่อง Drag me Hell เป็นเรื่องสยอง น่ากลัว ทำได้ใจหายไปหลายครั้งมากๆ เคยดูหนังสยองมาหลายเรื่อง
เรื่องนี้จัดได้ว่าสยอง น่ากลัวได้อีก เดินออกมาจากโรงหนังด้วยตัวเกร็งสุดๆ เลย ต้องลุ้นตลอดเลย

ดูหนังเสร็จเลยไปส่งกรุณากลับห้อง ประมาณเที่ยงคืนพอดี กว่าจะกลับถึงห้องก็เกือบตี 1 กว่าแล้ว
หลับฝันดีนะครับ

ปล. ก็เพราะรักเธอทั้งหัวใจ ยังไงก็ยอมเธอ, เค้าขอโทษนะ จะไม่ทำอีกแล้ว ยังไงก็อย่า Drag me hell แบบหนังเลยนะ