Wednesday, September 14, 2011

Munin คืออะไร และการติดตั้ง munin

Munin คืออะไร และการติดตั้ง munin

Munin ก็คือ web mornitoring ตัวหนึ่ง ที่เอาไว้ดูสถานะของเครื่องเรา และเครื่อข่าย ผ่าน บน http port 80  ซึ่ง Monitoring นั้นก็มีหลายตัวให้เลือกใช้ไม่ว่าจะเป็น NagiOS   MRTG Zenoss CACTI สามารถดูรายระเอียดได้มากน้อยแต่ต่างกัน สามารถเลือกได้ตรงตามความต้องการ แต่เจ้า munin นั้นจะมีรายละเอียดคือ Apache Disk Mysql Network Processes Sendmail Sensors System
หลังจากติดตั้ง เสร็จรอสักพัก 5-10 นาทีลอง refesh ดู โดย http://localhost/munin

 

 

การติดตั้ง munin 
จากที่ผมได้ทดสอบติดตั้งทดลองสาม OS นี้

CentOS
cd /root

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm

yum install munin munin-node
chkconfig --levels 235 munin-node on
/etc/init.d/munin-node start
chown -R munin:munin /var/www/munin
ln -s /var/www/munin/ /var/www/html/munin
service httpd restart && service crond restart
ln -s /usr/share/munin/plugins/apache_* /etc/munin/plugins/
/etc/init.d/munin-node restart

หลังจากติดตั้ง แล้ว สามารถเข้าไปดูได้โดย http://yourdomain.com/munin

 

Debian
# aptitude install munin

#sudo apt-get install munin

ส่วน package ต่อไปนี้ระบบจะจัดการติดตั้งให้เอง gawk, libio-multiplex-perl, libnet-cidr-perl, libnet-server-perl, libnet-snmp-perl, librrd4, librrds-perl, munin, munin-node,rrdtool

 

สำหรับ fedora ผมลอง fedora 9

สำหรับเครื่อง fedora  นั้นผมเทสแบบ VirtualHost แบบ user dir (/home/*/html) เช่น /home/user1  /home/user2 แต่หลังจากติดตั้งแล้ว munin ก็จะยังคงอยู่ที่
/var/www/html/munin ส่วน munin.conf จะอยู่ที่  etc/munun จึงจำเป็นต้องเซ็ต Alias เอา

yum -y install munin*
vi /etc/httpd/conf/httpd.conf

เพิ่ม Alias

Alias /munin "/var/www/html/munin"
<Directory "/var/www/html/munin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

#service httpd restart
# /etc/rc.d/init.d/munin-node start
#chkconfig munin-node on

Test
http://localhost/munin



* localhost-df-day.png (19.18 KB, 501x331 - ดู 903 ครั้ง.)

วิธีติดตั้ง Munin ใน Centos 5

เริ่มแรก ก็อัพเดท Server ก่อนเลย
1. yum update
2. wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
3. rpm -Uvh rpmforge-release-0.5.1-1.el5.rf.i386.rpm
4. yum install munin munin-node
5. chkconfig ?levels 235 munin-node on
6. /etc/init.d/munin-node start
7. chown -R munin:munin /var/www/munin
8. ln -s /var/www/munin /var/www/html/munin
9. service httpd restart && service crond restart
เสร็จแล้วครับ ทดสอบเข้าดูผ่าน
http://IP/munin หากเข้าได้ ก็ถือว่าผ่านแล้วครับ

ติดตั้ง munin บน Centos VPS

wget http://packages.sw.be/rpmforge...ease-0.3.6-1.el5.rf.x86_64.rpm 
rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
rm -f rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
yum install munin munin-node
chkconfig --levels 235 munin-node on
/etc/init.d/munin-node start
/etc/munin/munin.conf
mkdir -p /var/www/html/munin
chown munin:munin /var/www/html/munin


เอาสูตรผมไปบ้าง 

โค๊ด:
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
rpm -Uvh rpmforge-release-0.5.1-1.el5.rf.i386.rpm
yum install munin munin-node
chkconfig –levels 235 munin-node on
/etc/init.d/munin-node start
chown -R munin:munin /var/www/munin
ln -s /var/www/munin /var/www/html/munin
service httpd restart && service crond restart
ln -s /usr/share/munin/plugins/apache_* /etc/munin/plugins/
/etc/init.d/munin-node restart

ถ้าเปิด server-status เรียบร้อยแล้ว ก็รอครับ อับเดทครั้งหน้าจะมีกราฟขึ้นเองครับ

: )


มาแล้วจริงๆ ด้วย

ขอบคุณคุณ iPokz มากๆ ค่ะ และท่านอื่นๆ ด้วยนะคะ
 
 

นติดตั้ง munin บน Centos VPS

เคยลองติดตั้งบน kloxo อ่ะค่ะ แต่ก็งงๆ เพราะดูได้ไม่ค่อยครบ

ตอนนี้มี VPS อีกตัวหนึ่ง เป็น Centos DirectAdmin

แต่ไม่รู้วิธีติดตั้งชัวร์ๆ เลยอยากถามว่า พอจะมีใครแนะนำได้ไหมคะ

ขอบคุณค่ะ


 Copy ไปได้เลยครับ ถ้าเป็น OS แบบ 64Bit

rpm -Uvh http://download.fedora.redhat....64/epel-release-5-4.noarch.rpm  
yum -y install munin munin-node 
chkconfig munin-node on
mkdir -p /var/www/html/munin 
chown munin. /var/www/html/munin 
mv /var/www/html/munin/.htaccess /var/www/html/munin/htaccess.default 
service munin-node start


จากนั้นเวลาเข้าดูให้เข้าดูผ่าน http://Server-IP/munin  


Operating System เป็นแบบนี้อ่ะค่ะ CentOS 5.5 32 bit

 rpm -Uvh http://download.fedora.redhat....86/epel-release-5-4.noarch.rpm
yum -y install munin munin-node 
chkconfig munin-node on
mkdir -p /var/www/html/munin 
chown munin. /var/www/html/munin 
mv /var/www/html/munin/.htaccess /var/www/html/munin/htaccess.default 
service munin-node start

Tuesday, March 22, 2011

Linux Tips : การติดตั้งแพ็กเกจด้วย Yum

Yum ย่อมาจาก
ใช้ติดตั้งแพ็กเกจบนลีนุกซ์
การติดตั้งแพ็กเกจ
# yum install mc [enter]
ตอบ y [enter]
การติดตั้งแพ็กเกจครั้งละหลายๆ แพ็กเกจ
# yum install httpd php mysql mysql-server php-mysql phpmyadmin [enter]
ตอบ y [enter]
การถอนแพ็กเกจออกจากระบบ
# yum remove mc [enter]

ดูข้อมูลเกี่ยวกับ hard disk บน linux

  • ทดสอบกับ ubuntu 8.0.4


  1. ใช้คำสั่ง  lshw  ตามคำแนะนำใน ดูข้อมูลเกี่ยวกับ hardware ของ PC บน linux

  2. ดูข้อมูลเกี่ยวกับ partition ของ hard disk  ใช้คำสั่ง sudo fdisk -l  ได้ผลตัวอย่างประมาณว่า
    Disk /dev/sda: 41.1 GB, 41110142976 bytes
    255 heads, 63 sectors/track, 4998 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0xc241c241

    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1        4787    38451546   83  Linux
    /dev/sda2            4788        4998     1694857+   5  Extended
    /dev/sda5            4788        4998     1694826   82  Linux swap / Solaris

    บรรทัดแรก ข้อความว่า Disk /dev/sda: 41.1 GB  หมายถึง hard disk ชื่อ /dev/sda มีความจุ 41.1 GB
    บรรทัดล่างๆที่ขึ้นต้นว่า /dev/sda? หมายถึงการจัด partition ต่างๆ ของ hard disk นี้


  3. ดูข้อมูลการใช้พื้นที่ของ hard disk  ใช้คำสั่ง  df -h ได้ผลตัวอย่างประมาณว่า
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1              19G  2.6G   16G  14% /
    varrun                250M   60K  250M   1% /var/run
    varlock               250M     0  250M   0% /var/lock
    udev                  250M   44K  250M   1% /dev
    devshm                250M     0  250M   0% /dev/shm

    บรรทัดที่สอง ข้อความว่า  /dev/sda1  19G  2.6G   16G  14% /
    หมามถึง partition  /dev/sda1 มีขนาด 19 G ใช้ไปแล้ว 2.6 G เหลืออีก 16 G หรือประมาณ  14 %  ถูก mount เป็นพื้นที่ /

  4. ดูข้อมูล swap ของ hard disk ใช้คำสั่ง  cat /proc/swaps  ได้ผลตัวอย่างประมาณว่า
    Filename                Type        Size    Used    Priority
    /dev/sda5                partition   377488    132    -1

    บรรทัดที่สอง ข้อความว่า  /dev/sda5  partition 377488  132 -1
    หมายถึง partition /dev/sda5 ถูกทำเป็น swap มีขนาด 377,488  Kbytes ใช้ไปแล้วจำนวน 132  Kbytes

  5. หากต้องการดูอุณหภูมิการทำงานของ hard disk ต้องติดตั้ง smartmontools ตามคำแนะนำใน ติดตั้ง_smartmontools_บน_ubuntu
    แล้ว ใช้คำสั่งตัวอย่างประมาณว่า sudo smartctl -A /dev/sda|grep "Temperature_Celsius"

  6. ดูข้อมูล uuid ของ hard disk ด้วยคำสั่ง  ls -l /dev/disk/by-uuid

Monday, March 21, 2011

Linux Command Reference รวมคำสั่งลีนุกซ์บางส่วนที่เป็นประโยชน์

คำสั่งหน้าที่รายละเอียด
File/Directory Basics
lsList filesแสดงรายชื่อไฟล์และไดเร็คทอรี่
cpCopy filesสำเนาไฟล์
mvRename filesเปลี่ยนชื่อไฟล์
rmDelete filesลบไฟล์
lnLink filesสร้างไฟล์เชื่อมโยง
cdChange directoryย้ายไปยังไดเร็คทอรี่ที่ต้องการ
pwdPrint current directory nameแสดงชื่อไดเร็คทอรี่ปัจจุบัน
mkdirCreate directoryสร้างไดเร็คทอรี่ใหม่
rmdirDelete directoryลบไดเร็คทอรี่ (ที่ว่างเปล่าเท่านั้น)
File Viewing
catView filesดูเนื้อหาของ text file
lessPage trough filesเลื่อนดูเนื้อหาของไฟล์
headView file beginningแสดงส่วนต้นของไฟล์
tailView files endingแสดงส่วนท้ายของไฟล์
nlNumber linesแสดงหมายเลขบรรทัด
odView binary filesแสดงเนื้อหาในไฟล์ไบนารี่
xxdView binary filesแสดงเนื้อหาในไฟล์ไบนารี่
gvView Postscript/PDF filesแสดงไฟล์แบบโพสต์สคริปต์หรือ PDF
xdviView TeX DVI filesแสดงไฟล์รูปแบบ TeX
File Creation and Editing
emacsText editorโปรแกรมแก้ไขข้อความของ GNU
vimText editorโปรแกรมแก้ไขข้อความที่ปรับปรุงจาก vi
umaskSet default file protectionsแสดง/กำหนดค่าสำหรับคำนวณค่า permission mode
sofficeEdit Word/Excel/PowerPoint docsแก้ไขไฟล์เอกสาร Word/Excel/PowerPoint
abiwordEdit Word documentsแก้ไขเอกสาร Word
gnumericEdit Excel documentsแก้ไขเอกสาร Excel
File Properties
statDisplay file attributesแสดงสถานะ/สถิติ/คุณลักษณะของไฟล์
wcCount bytes/words/linesนับจำนวนอักขระ คำ บรรทัด
duMeasure disk usageแสดงปริมาณการใช้เนื้อที่ไดเร็คทอรี่
fileIdentify file typesแสดงชนิดของไฟล์
touchChange file timestampsเปลี่ยนค่าเวลาของไฟล์
chownChange file ownerเปลี่ยนชื่อเจ้าของไฟล์
chgrpChange file groupเปลี่ยนชื่อกรุ๊ปเจ้าของไฟล์
chmodChange file protectionsเปลี่ยนระดับการป้องกันไฟล์
chattrChange advanced file attributesเปลี่ยนคุณลักษณะของไฟล์ในขั้นสูง
IsattrList advanced file attributesแสดงคุณลักษณะของไฟล์ในขั้นสูง
File Location
findLocate filesค้นหาตำแหน่งของไฟล์
slocateLocate files via indexค้นหาตำแหน่งของไฟล์ด้วยฐานข้อมูลดัชนี
whichLocate commandsค้นหาคำสั่ง
whereisLocate standard filesค้นหาไฟล์มาตรฐาน
File Text Manipulation
grepSearch text for matching linesค้นหาข้อความในระดับบรรทัด
cutExtract columnsคัดแยกคำโดยระบุตำแหน่ง
pasteAppend columnsเชื่อมต่อไฟล์ในแนวระนาบ
trTranslate charactersแปลงข้อความ
sortSort linesจัดเรียงข้อความระดับบรรทัด
uniqLocate indentical linesรวมบรรทัดที่เหมือนกัน
teeCopy stdin to file and to stdout simultaneouslyสำเนาข้อความออกทางไฟล์และ stdout พร้อมๆ กัน
File Compression
gzipCompress files (GNU Zip)บีบอัดไฟล์ให้เป็น .gz
CompressCompress files (Unix)บีบอัดไฟล์แบบมาตรฐาน Unix
bzip2Compress files (BZip2)บีบอัดไฟล์ให้เป็น .bz2
zipCompress files (Windows Zip)บีบอัดไฟล์สำหรับ WinZip
File Comparison
diffCompare files line by lineเปรียบเทียบไฟล์ในระดับบรรทัด
commCompare sorted filesเปรียบเทียบไฟล์ที่ผ่านการเรียงข้อมูลมาแล้ว
cmpCompare files byte by byteเปรียบเทียบไฟล์ระดับไบต์
md5sumCompute Checksumsคำนวณหาค่า md5 ของไฟล์
Disks and Filesystems
dfShow free disk spaceรายงานขนาดดิสก์ที่เหลืออยู่
mountMake a disk accessibleเชื่อมต่ออุปกรณ์จัดเก็บข้อมูลเข้าสู่ระบบ
fsckCheck a disk for errorsตรวจสอบแก้ไขความผิดปรกติของเนื้อที่ดิสก์
syncFlush disk cachesเขียนข้อมูลในแคชกลับคืนสู่ดิสก์
Backups and Remote Storage
mtControl a type driveควบคุมเทป
dumpBack up a diskสำรองข้อมูลจากดิสก์
restoreRestore a dumpนำข้อมูลที่สำรองไว้กลับคืนที่เดิม
tarRead/write type archivesจัดเก็บไฟล์ให้รวมกันไว้ที่เดียว
cdrecordBurn a CDเขียนไฟล์ลงสู่แผ่นซีดี
rsyncMirror a set of filesสำรองข้อมูลระหว่างโฮสต์
Printing
lprPrint filesส่งไฟล์ไปพิมพ์ที่เครื่องพิมพ์
lpqView print queueเปิดดูลำดับงานพิมพ์ที่ค้างอยู่
lprmRemove print jobsยกเลิกงานพิมพ์ที่ค้างอยู่
Spelling Operations
lookLook up spellingเปิดสารบัญคำศัพท์
aspellCheck spelling interactivelyตรวจคำสะกดว่าถูกต้องหรือไม่
spellCheck spelling in batchตรวจคำถูกผิดในไฟล์จำนวนมาก
Processes
psList all processesแสดงโปรเซสทั้งหมด
wList users' processesแสดงรายชื่อยูสเซอร์ที่กำลังใช้งานโปรเซส
uptimeView the system loadแสดงปริมาณภาระของระบบ
topMonitor processesแสดงข้อมูลเกี่ยวกับโปรเซสแบบต่อเนื่อง
xloadMonitor system loadแสดงภาระของระบบในแบบกราฟฟิก
freeDisplay free memoryแสดงปริมาณหน่วยความจำประเภทต่างๆในปัจจุบัน
killTerminate processesส่งรหัสควบคุมไปยังโปรเซส
niceSet process prioritiesตั้งค่าระดับความสำคัญให้โปรเซส
reniceChange process prioritiesปรับระดับความสำคัญของโปรเซส
Scheduling Jobs
sleepWait for some time หน่วงเวลา
watchRun programs at set intervalsรันโปรแกรมซ้ำในระยะเวลาที่กำหนด
atSchedule a jobตั้งเวลารันกลุ่มคำสั่ง
crontabSchedule repeated jobsตั้งเวลารันคำสั่งเป็นรอบเวลาที่กำหนด
Hosts
unamePrint system informationแสดงรายละเอียดของระบบปฏิบัติการ
hostnamePrint the system's hostnameแสดง/กำหนดชื่อโฮสต์
ifconfigSet/display network informationแสดง/กำหนดค่าเกี่ยวกับเครือข่าย
hostLook up DNSสืบค้นชื่อและไอพีของโฮสต์ในระบบ DNS
whoisLookup domain registrantsสืบค้นข้อมูลการจดทะเบียนโดเมน
pingCheck if host is reachableทดสอบการตอบสนองของโฮสต์ปลายทาง
tracerouteView network path to a hostตรวจสอบเส้นทางไปสู่โฮสต์ปลายทาง
Networking
sshSecurely log into remote hostsเข้าสู่โฮสต์จากระยะไกล (มีการเข้ารหัสข้อมูล)
telnetLog into remote hostsเข้าสู่โฮสต์จากระยะไกล(ไม่มีการเข้ารหัส)
scpSecurely copy files between hostsสำเนาไฟล์ระหว่างโฮสต์(มีการเข้ารหัสข้อมูล)
stfpSecurely copy files between hostsบริการโอนถ่ายไฟล์ระหว่างโฮสต์(มีการเข้ารหัสข้อมูล)
ftpCopy files between hostsบริการโอนถ่ายไฟล์ระหว่างโฮสต์(ไม่มีการเข้ารหัสข้อมูล)
evolutionGUI email clientโปรแกรมใช้งานอีเมล์แบบกราฟฟิก
muttText-based email clientโปรแกรมใช้งานอีเมล์แบบ text
mailMinimal email clientคำสั่งรับส่งอีเมล์ขนาดเล็กมาก
mozillaWeb browserโปรแกรมเว็บบราวเซอร์แบบกราฟฟิก
lynxText-only web browserโปรแกรมเว็บบราวเซอร์แบบ text
wgetRetrieve web pages to diskดาวน์โหลดข้อมูลเว็บมาสู่ดิสก์
slrnRead Usenet newsอ่านข่าวใน usenet
gaimInstant messaging/IRCโปรแกรมรับส่งข้อความ
talkLinux/Unix chatคำสั่งรับส่งข้อความโต้ตอบ
writeSend messages to a termainalคำสั่งส่งข้อความไปยังจอภาพอื่น
mesgProhibit talk/writeเปิด/ปิดการรับข้อความจากคำสั่ง write
Audio and Video
gripPlay CDs and rip MP3sเล่นแผ่นซีดีเพลงและแปลงเป็นไฟล์ MP3
xmmsPlay audio filesเล่นไฟล์เสียงชนิดต่างๆ
cdparanoiaRip audioแปลงแทร็กเพลงให้เป็นไฟล์
audacityEdit audioปรับแต่ง/แก้ไขไฟล์เสียง
xcdroastBurn CDsบันทึกข้อมูล/แทร็กเสียงลงแผ่นซีดี

Monday, January 24, 2011

Automatically optimize all tables in a MySQL database

Use this script preferably with a cronjob to automatically optimize all the tables in your database. Under root privilleges the script will search for all the databases in your server and will optimize them all.

Just change the connection variables ($h, $u, $p) and execute it from your browser.

Code:
<?phpecho '<pre>' "\n\n";set_time_limit100 );

  
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$start $time;
  
//Connection variables :$h 'localhost';$u 'root';$p 'password';
$dummy_db 'mysql';
/*The php->mysql API needs to connect to a database even when executing scripts like this.
 If you got an error from this(permissions),
 just replace this with the name of your database*/
$db_link mysql_connect($h,$u,$p);
$res mysql_db_query($dummy_db'SHOW DATABASES'$db_link) or die('Could not connect: ' mysql_error());
echo 
'Found 'mysql_num_rows$res ) . ' databases' "\n";$dbs = array();
while ( 
$rec mysql_fetch_array($res) )
{
$dbs [] = $rec [0];
}

foreach ( 
$dbs as $db_name )
{
echo 
"Database : $db_name \n\n";$res mysql_db_query($dummy_db"SHOW TABLE STATUS FROM `" $db_name "`"$db_link) or die('Query : ' mysql_error());$to_optimize = array();
while ( 
$rec mysql_fetch_array($res) )
{
if ( 
$rec['Data_free'] > )
{
$to_optimize [] = $rec['Name'];
echo 
$rec['Name'] . ' needs optimization' "\n";
}
}
if ( 
count $to_optimize ) > )
{
foreach ( 
$to_optimize as $tbl )
{
mysql_db_query($db_name"OPTIMIZE TABLE `" $tbl ."`"$db_link );
}
}
}
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$finish $time;
  
$total_time round(($finish $start), 6);
  echo 
'Parsed in ' $total_time ' secs' "\n\n";?>

Automatically optimize all tables in a MySQL database

Use this script preferably with a cronjob to automatically optimize all the tables in your database. Under root privilleges the script will search for all the databases in your server and will optimize them all.

Just change the connection variables ($h, $u, $p) and execute it from your browser.

Code:
<?phpecho '<pre>' "\n\n";set_time_limit100 );

  
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$start $time;
  
//Connection variables :$h 'localhost';$u 'root';$p 'password';
$dummy_db 'mysql';
/*The php->mysql API needs to connect to a database even when executing scripts like this.
 If you got an error from this(permissions),
 just replace this with the name of your database*/
$db_link mysql_connect($h,$u,$p);
$res mysql_db_query($dummy_db'SHOW DATABASES'$db_link) or die('Could not connect: ' mysql_error());
echo 
'Found 'mysql_num_rows$res ) . ' databases' "\n";$dbs = array();
while ( 
$rec mysql_fetch_array($res) )
{
$dbs [] = $rec [0];
}

foreach ( 
$dbs as $db_name )
{
echo 
"Database : $db_name \n\n";$res mysql_db_query($dummy_db"SHOW TABLE STATUS FROM `" $db_name "`"$db_link) or die('Query : ' mysql_error());$to_optimize = array();
while ( 
$rec mysql_fetch_array($res) )
{
if ( 
$rec['Data_free'] > )
{
$to_optimize [] = $rec['Name'];
echo 
$rec['Name'] . ' needs optimization' "\n";
}
}
if ( 
count $to_optimize ) > )
{
foreach ( 
$to_optimize as $tbl )
{
mysql_db_query($db_name"OPTIMIZE TABLE `" $tbl ."`"$db_link );
}
}
}
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$finish $time;
  
$total_time round(($finish $start), 6);
  echo 
'Parsed in ' $total_time ' secs' "\n\n";?>

# service kloxo restart

---------------------------------------


# service kloxo restart

# service httpd restart

# service mysqld restart

# service named restart

# service lighttpd restart

# service mysqld restart







-----------------------------------
sub ของ WPMU เหรอครับ

เข้าไปที่ domain ==> Extra ==> Server Aliases ใส่ *

--------------------------------
หน้า เว็บ ขึ้น แต่ Apache 2 Test Page

ให้แก้ไฟล์ /etc/hosts เป็นประมาณนี้ครับ

127.0.0.1 mydomain.com centos localhost
192.168.0.98 mydomain centos

หมายเลข 192.168.0.98 คือ หมายเลขไอพีที่เราได้รับมานะครับ

Monday, January 17, 2011

Troubleshooting Memory Usage

Processing dying unexpectedly?  Want to know if you need more memory?
Check your /var/log/messages.  If you see (on a 2.4.23 kernel):
Dec 11 10:21:43 www kernel: __alloc_pages: 0-order allocation failed (gfp=0x1d2/0)
Dec 11 10:21:44 www kernel: __alloc_pages: 0-order allocation failed (gfp=0x1f0/0)
Or (on a pre-2.4.23 kernel):

Dec 7 23:49:03 www kernel: Out of Memory: Killed process 31088 (java).
Dec 7 23:49:03 www kernel: Out of Memory: Killed process 31103 (java).
Or on a Xen-based VPS console:

swapper: page allocation failure. order:0, mode:0x20
 [<c01303a4>] __alloc_pages+0x327/0x3e3
Then your programs need more memory than they can get.

Interpreting Free

To see how much memory you are currently using, run free -m.  It will provide output like:
            total    used   free    shared buffers cached
Mem:        90      85       4      0       3       34
-/+ buffers/cache:  46      43
Swap:       9        0       9
The top row 'used' (85) value will almost always nearly match the top row mem value (90).  Since Linux likes to use any spare memory to cache disk blocks (34).
The key used figure to look at is the buffers/cache row used value (46).  This is how much space your applications are currently using.  For best performance, this number should be less than your total (90) memory.  To prevent out of memory errors, it needs to be less than the total memory (90) and swap space (9).
If you wish to quickly see how much memory is free look at the buffers/cache row free value (43). This is the total memory (90)- the actual used (46). (90 - 46 = 44, not 43, this will just be a rounding issue)

Interpreting ps

If you want to see where all your memory is going, run ps aux.  That will show the percentage of memory each process is using.  You can use it to identify the top memory users (usually Apache, MySQL and Java processes).
For example in this output snippet:
USER PID %CPU %MEM VSZ     RSS   TTY   STAT   START TIME COMMAND
root 854 0.5  39.2 239372  36208 pts/0 S     22:50 0:05 /usr/local/jdk/bi
n/java -Xms16m -Xmx64m -Djava.awt.headless=true -Djetty.home=/opt/jetty -cp /opt
/jetty/ext/ant.jar:/opt/jetty/ext/jasper-compiler.jar:/opt/jetty/ext/jasper-runt
ime.jar:/opt/jetty/ext/jcert.jar:/opt/jetty/ext/jmxri.jar:/opt/jetty/ext/jmxtool
We can see that java is using up 39.2% of the available memory.

Interpreting vmstat

vmstat helps you to see, among other things, if your server is swapping.  Take a look at the following run of vmstat doing a one second refresh for two iterations.

# vmstat 1 2
   procs                      memory    swap          io     system         cpu
 r  b  w   swpd   free   buff  cache  si  so    bi    bo   in    cs  us  sy  id
 0  0  0  39132   2416    804  15668   4   3     9     6  104    13   0   0 100
 0  0  0  39132   2416    804  15668   0   0     0     0   53     8   0   0 100
 0  0  0  39132   2416    804  15668   0   0     0     0   54     6   0   0 100
The first row shows your server averages.  The si (swap in) and so (swap out) columns show if you have been swapping (i.e. needing to dip into 'virtual' memory) in order to run your server's applications.  The si/so numbers should be 0 (or close to it).  Numbers in the hundreds or thousands indicate your server is swapping heavily.  This consumes a lot of CPU and other server resources and you would get a very (!) significant benefit from adding more memory to your server.
Some other columns of interest: The r (runnable) b (blocked) and w (waiting) columns help see your server load.  Waiting processes are swapped out.  Blocked processes are typically waiting on I/O.  The runnable column is the number of processes trying to something.  These numbers combine to form the 'load' value on your server.  Typically you want the load value to be one or less per CPU in your server.
The bi (bytes in) and bo (bytes out) column show disk I/O (including swapping memory to/from disk) on your server.
The us (user), sy (system) and id (idle) show the amount of CPU your server is using.  The higher the idle value, the better.

Resolving: High Java Memory Usage

Java processes can often consume more memory than any other application running on a server.
Java processes can be passed a -Xmx option.  This controls the maximum Java memory heap size.  It is important to set a limit on the heap size, otherwise the heap will keep increasing until you get out of memory errors on your VPS (resulting in the Java process - or even some other, random, process - dying.
Usually the setting can be found in your /usr/local/jboss/bin/run.conf or /usr/local/tomcat/bin/setenv.sh config files.  And your RimuHosting default install should have a reasonable value in there already.
If you are running a custom Java application, check there is a -XmxNNm (where NN is a number of megabytes) option on the Java command line.
The optimal -Xmx setting value will depend on what you are running.  And how much memory is available on your server.
From experience we have found that Tomcat often runs well with an -Xmx between 48m and 64m.  JBoss will need a -Xmx of at least 96m to 128m.  You can set the value higher.  However, you should ensure that there is memory available on your server.
To determine how much memory you can spare for Java, try this: stop your Java process; run free -m; subtract the 'used' value from the "-/+ cache" row from the total memory allocated to your server and then subtract another 'just in case' margin of about 10% of your total server memory.  The number you come up with is a rough indicator of the largest -Xmx setting you can use on your server.

Resolving: High Spam Assassin Memory Usage

Are you running a Spam Assassin 'daemon'?  It can create multiple (typically 5) threads/processes and each of those threads can use a very large amount of memory.
SpamAssassin works very well with just one thread.  So you can reduce the 'children' setting and reclaim some memory on your server for other apps to run with.

for location in /etc/default/spamassassin /etc/sysconfig/spamassassin; do 
if [ ! -e $location ]; then continue; fi
replace "SPAMDOPTIONS=\"-d -c -m5 -H" "SPAMDOPTIONS=\"-d -c -m1 -H" -- /etc/init.d/spamassassin
replace "\-m 10 " "-m 1 " -- $location
replace "\-m 5 " "-m 1 " -- $location
replace "\-m5 " "-m1 " -- $location
replace "max-children 5 " "max-children 1 " -- $location
done 
Another thing to check with spamassassin is that any /etc/procmailrc entry only does one spamassassin check at a time.  Otherwise if you receive a batch of incoming email they will all be processed in parallel.  This could cause your server CPU usage to spike, slowing down your other apps, and it may cause your server to run out of memory.
To make procmailrc run only one email at a time through Spamassassin use a lockfile on your recipe line.  e.g. change the top line of:

:0fw:
# The following line tells Procmail to send messages to Spamassassin only if they are less thatn 256000 bytes. Most spam falls well below this size and a larger size could seriously affect performance.)
* < 256000
| /usr/bin/spamc
To:

:0fw:/etc/mail/spamc.lock
# The following line tells Procmail to send messages to Spamassassin only if they are less thatn 256000 bytes. Most spam falls well below this size and a larger size could seriously affect performance.)
* < 256000
| /usr/bin/spamc

Resolving: High Apache Memory Usage

Apache can be a big memory user.  Apache runs a number of 'servers' and shares incoming requests among them.  The memory used by each server grows, especially when the web page being returned by that server includes PHP or Perl that needs to load in new libraries.  It is common for each server process to use as much as 10% of a server's memory.
To reduce the number of servers, you can edit your httpd.conf file.  There are three settings to tweak: StartServers, MinSpareServers, and MaxSpareServers.  Each can be reduced to a value of 1 or 2 and your server will still respond promptly, even on quite busy sites.  Some distros have multiple versions of these settings depending on which process model Apache is using.  In this case, the 'prefork' values are the ones that would need to change.
To get a rough idea of how to set the MaxClients directive, it is best to find out how much memory the largest apache thread is using. Then stop apache, check the free memory and divide that amount by the size of the apache thread found earlier. The result will be a rough guideline that can be used to further tune (up/down) the MaxClients directive. The following script can be used to get a general idea of how to set MaxClients for a particular server:

#!/bin/bash
echo "This is intended as a guideline only!"
if [ -e /etc/debian_version ]; then
    APACHE="apache2"
elif [ -e /etc/redhat-release ]; then
    APACHE="httpd"
fi
RSS=`ps -aylC $APACHE |grep "$APACHE" |awk '{print $8'} |sort -n |tail -n 1`
RSS=`expr $RSS / 1024`
echo "Stopping $APACHE to calculate free memory"
/etc/init.d/$APACHE stop &amp;ampamp> /dev/null
MEM=`free -m |head -n 2 |tail -n 1 |awk '{free=($4); print free}'`
echo "Starting $APACHE again"
/etc/init.d/$APACHE start &amp;> /dev/null
echo "MaxClients should be around" `expr $MEM / $RSS`
Note: httpd.conf should be tuned correctly on our newer WBEL3 and FC2 distros.  Apache is not installed by default on our Debian distros (since some people opt for Apache 2 and others prefer Apache 1.3).  So this change should only be necessary if you have a Debian distro.
from http://modperlbook.org/html/11-2-Setting-the-MaxRequestsPerChild-Directive.html: "Setting MaxRequestsPerChild to a non-zero limit solves some memory-leakage problems caused by sloppy programming practices and bugs, whereby a child process consumes a little more memory after each request. In such cases, and where the directive is left unbounded, after a certain number of requests the children will use up all the available memory and the server will die from memory starvation."

Resolving: High MySQL Memory Usage

Our rpm based distros (e.g. RH9 and WBEL3) have MySQL preinstalled but not running.  Our pre-install uses a memory efficient /etc/my.cnf file.  If you install MySQL on a Debian server, edit the key_buffer_size setting in /etc/mysql/my.cnf.  A small value like 2M often works well. For an ultra-tiny setup add or change the follow entries to the mysqld section:

# if your are not using the innodb table manager, then just skip it to save some memory
#skip-innodb
innodb_buffer_pool_size = 16k
key_buffer_size = 16k
myisam_sort_buffer_size = 16k
query_cache_size = 1M

Troubleshooting Irregular Out Of Memory Errors

Sometimes a server's regular memory usage is fine.  But it will intermittently run out of memory.  And when that happens you may lose trace of what caused the server to run out of memory.
In this case you can setup a script (see below) that will regularly log your server's memory usage.  And if there is a problem you can check the logs to see what was running.

wget http://proj.ri.mu/memmon.sh -O /root/memmon.sh
chmod +x /root/memmon.sh

# create a cronjob that runs every few minutes to log the memory usage
echo '0-59/10 * * * * root /root/memmon.sh >> /root/memmon.txt' > /etc/cron.d/memmon
/etc/init.d/cron* restart 

# create a logrotate entry so the log file does not get too large
echo '/root/memmon.txt {}' > /etc/logrotate.d/memmon

Just Add Memory

A simple solution to resolving most out of memory problems is to add more memory.  If you'd like to increase the memory on your VPS, just send us a support ticket and let us know how much memory you need (per the pricing here).

มี VPs memory 1024MB ขึ้น Out of memory บ่อยควรทำไงดีครับ

มี VPs memory 1024MB ขึ้น Out of memory บ่อยควรทำไงดีครับ

ผู้รู้เว็บผมคนออนไลพร้อมกันทีไรเมมเต็มทุกทีอะครับ

===================================

คือ ปัญหา มันเกิดจาก memory ไม่พอ สำหรับใช้งาน เมื่อมี user เยอะๆ

ซึ่งการแก้ไข มันทำได้ แค่ 2 วิธี คือ

1. ทำให้ memory เพิ่มขึ้น เช่น เปลี่ยน package VPs ที่ใช้อยู่ ที่มี physical server เพิ่มขึ้น
2. ทำให้การใช้งาน memory ถูกจำกัด เพราะมันอาจจะมี function บาง function หรือ page บางหน้า มันใช้ memory มากผิดปกติก็ได้

    ดังนั้น ทำการ limit โดยถ้าเป็น apache web server จะสามารถทำได้โดย

    Description:   Limits the memory consumption of processes launched by Apache children
    Syntax:           RLimitMEM bytes|max [bytes|max]

    ใน php ก็กำหนด php.ini ให้
    memory_limit = 8M

    อาจจะเปลี่ยน apache เป็น worker mode ซึ่งการจัดการ memory ได้มากขึ้น

Out of memory problems on a VPS

===================================================
I'm getting out of memory issues on my VPS, and wondering if it is normal with my setup, or if there is something seriously wrong.

I have 384 MB of physical memory, burstable up to 750 MB of physical memory, with 1.5 GB available physical + swap. I have one WordPress blog with about 500-1000 pageviews/day, and four other WordPress blogs with about 20 pageviews/day. The first has a number of plugins installed, but the other four are very basic.

Should the traffic and setup I have be causing out of memory errors on my VPS, or is something not configured correctly? I've asked my host for suggestions, but I thought I'd post here too. Any tips about optimizing a VPS for WordPress blogs? Optimizing memory?

Thanks.

===================================================================

Just because your VPS has, for example, 256MB of memory, doesn't mean that all 256MB have been allocated to PHP.

Here are three ways to increase PHP's memory allocation:

1. If you're using WordPress 2.9.2 or lower, try adding define('WP_MEMORY_LIMIT', '256M'); to your wp-config.php file. If you're using WordPress 3.0 or higher, WordPress automatically does this for a variety of tasks, so there's really no reason to try it in this case.

2. If you can edit or override the system php.ini file, increase the memory limit. For example, memory_limit = 256M

3. If you cannot edit or override the system php.ini file, add php_value memory_limit 256M to your .htaccess file.

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | free samples without surveys