顯示具有 Linux-教學 標籤的文章。 顯示所有文章
顯示具有 Linux-教學 標籤的文章。 顯示所有文章

2010年12月27日 星期一

Linux 上 .a 跟 .so

Library可分成三種,static、shared與dynamically loaded。


1. Static libraries


Static 程式庫用於靜態連結,簡單講是把一堆object檔用ar(archiver)

包裝集合起來,檔名以`.a’ 結尾。優點是執行效能通常會比後兩者快,

而且因為是靜態連結,所以不易發生執行時找不到library或版本錯置而

無法執行的問題。缺點則是檔案較大,維護度較低;例如library如果發

現bug需要更新,那麼就必須重新連結執行檔。


1.1 編譯


編譯方式很簡單,先例用`-c’ 編出object 檔,再用ar 包起來即可。


____ hello.c ____

#include

void hello(){ printf(”Hello “); }


____ world.c ____

#include

void world(){ printf(”world.”); }


____ mylib.h ____

void hello();

void world();


$ gcc -c hello.c world.c /* 編出hello.o 與world.o */

$ ar rcs libmylib.a hello.o world.o /* 包成limylib.a */


這樣就可以建出一個檔名為libmylib.a 的檔。輸出的檔名其實沒有硬性規定,

但如果想要配合gcc 的’-l’ 參數來連結,一定要以`lib’ 開頭,中間是你要

的library名稱,然後緊接著`.a’ 結尾。


1.2 使用


____ main.c ____

#include “mylib.h”

int main() {

hello();

world();

}


使用上就像與一般的object 檔連結沒有差別。


$ gcc main.c libmylib.a


也可以配合gcc 的`-l’ 參數使用


$ gcc main.c -L. -lmylib


-L dir 參數用來指定要搜尋程式庫的目錄,`.' 表示搜尋現在所在的目
錄。

通常預設會搜/usr/lib 或/lib 等目錄。

-l library 參數用來指定要連結的程式庫,'mylib' 表示要與mylib進
行連結

,他會搜尋library名稱前加`lib'後接`.a'的檔案來連結。


$ ./a.out

Hello world.

2. Shared libraries


Shared library 會在程式執行起始時才被自動載入。因為程式庫與執行檔

是分離的,所以維護彈性較好。有兩點要注意,shared library是在程式起始

時就要被載入,而不是執行中用到才載入,而且在連結階段需要有該程式庫

才能進行連結。


首先有一些名詞要弄懂,soname、real name與linker name。


soname 用來表示是一個特定library 的名稱,像是libmylib.so.1 。

前面以`lib' 開頭,接著是該library 的名稱,然後是`.so' ,接著

是版號,用來表名他的介面;如果介面改變時,就會增加版號來維護相容度。


real name 是實際放有library程式的檔案名稱,後面會再加上minor 版號與

release 版號,像是libmylib.so.1.0.0 。


一般來說,版號的改變規則是(印象中在APress-Difinitive Guide to GCC中有

提到,但目前手邊沒這本書),最尾碼的release版號用於程式內容的修正,

介面完全沒有改變。中間的minor用於有新增加介面,但相舊介面沒改變,所以

與舊版本相容。最前面的version版號用於原介面有移除或改變,與舊版不相容

時。


linker name是用於連結時的名稱,是不含版號的soname ,如: libmylib.so。

通常linker name與real name是用ln 指到對應的real name ,用來提供

彈性與維護性。


2.1 編譯

shared library的製作過程較複雜。


$ gcc -c -fPIC hello.c world.c


編譯時要加上-fPIC 用來產生position-independent code。也可以用-fpic

參數。(不太清楚差異,只知道-fPIC 較通用於不同平台,但產生的code較大

,而且編譯速度較慢)。


$ gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0.0 \

hello.o world.o


-shared 表示要編譯成shared library

-Wl 用於參遞參數給linker,因此-soname與libmylib.so.1會被傳給linker處理。

-soname用來指名soname 為limylib.so.1

library會被輸出成libmylib.so.1.0.0 (也就是real name)


若不指定soname 的話,在編譯結連後的執行檔會以連時的library檔名為

soname,並載入他。否則是載入soname指定的library檔案。


可以利用objdump 來看library 的soname。


$ objdump -p libmylib.so | grep SONAME

SONAME libmylib.so.1


若不指名-soname參數的話,則library不會有這個欄位資料。


在編譯後再用ln 來建立soname 與linker name 兩個檔案。

$ ln -s libmylib.so.1.0.0 libmylib.so

$ ln -s libmylib.so.1.0.0 libmylib.so.1

2.2 使用


與使用static library 同。


$ gcc main.c libmylib.so


以上直接指定與libmylib.so 連結。


或用


$ gcc main.c -L. -lmylib


linker會搜尋libmylib.so 來進行連結。


如果目錄下同時有static與shared library的話,會以shared為主。

使用-static 參數可以避免使用shared連結。


$ gcc main.c -static -L. -lmylib


此時可以用ldd 看編譯出的執行檔與shared程式庫的相依性

$ldd a.out

linux-gate.so.1 => (0xffffe000)

[1;33mlibmylib.so.1 => not found[m

libc.so.6 => /lib/libc.so.6 (0xb7dd6000)

/lib/ld-linux.so.2 (0xb7f07000)

輸出結果顯示出該執行檔需要libmylib.so.1 這個shared library。

會顯示not found 因為沒指定該library所在的目錄,所找不到該library。


因為編譯時有指定-soname參數為libmylib.so.1 的關係,所以該執行檔會

載入libmylib.so.1。否則以libmylib.so連結,執行檔則會變成要求載入

libmylib.so


$ ./a.out

/a.out: error while loading shared libraries: libmylib.so.1

cannot open shared object file: No such file or directory


因為找不到libmylib.so.1 所以無法執行程式。

有幾個方式可以處理。


a. 把libmylib.so.1 安裝到系統的library目錄,如/usr/lib下

b. 設定/etc/ld.so.conf ,加入一個新的library搜尋目錄,並執行ldconfig

更新快取

c. 設定LD_LIBRARY_PATH 環境變數來搜尋library

這個例子是加入目前的目錄來搜尋要載作的library

$ LD_LIBRARY_PATH=. ./a.out

Hello world.

3. Dynamically loaded libraries


Dynamicaaly loaded libraries 才是像windows 所用的DLL ,在使用到

時才載入,編譯連結時不需要相關的library。動態載入庫常被用於像plug-ins

的應用。


3.1 使用方式

動態載入是透過一套dl function來處理。

#include

void *dlopen(const char *filename, int flag);

開啟載入filename 指定的library。

void *dlsym(void *handle, const char *symbol);

取得symbol 指定的symbol name在library被載入的記憶體位址。

int dlclose(void *handle);

關閉dlopen開啟的handle。

char *dlerror(void);

傳回最近所發生的錯誤訊息。


____ dltest.c ____

#include

#include

#include

int main() {

void *handle;

void (*f)();

char *error;


/* 開啟之前所撰寫的libmylib.so 程式庫*/

handle = dlopen("./libmylib.so", RTLD_LAZY);

if( !handle ) {

fputs( dlerror(), stderr);

exit(1);

}


/* 取得hello function 的address */

f = dlsym(handle, "hello");

if(( error=dlerror())!=NULL) {

fputs(error, stderr);

exit(1);

}

/* 呼叫該function */

f();

dlclose(handle);

}


編譯時要加上-ldl 參數來與dl library 連結

$ gcc dltest.c -ldl

結果會印出Hello 字串

$ ./a.out

Hello


關於dl的詳細內容請參閱man dlopen


--

參考資料:


Creating a shared and static library with the gnu compiler [gcc]

http://www.adp-gmbh.ch/cpp/gcc/create_lib.html


Program Library HOWTO

http://tldp.org/HOWTO/Program-Library-HOWTO/index.html


APress - Definitive Guide to GCC

2010年12月12日 星期日

How to extract/unzip RPM or DEB packages

Extracting the contents of the RPM package is a one step process:
$ rpm2cpio mypackage.rpm | cpio -vid

If you just need to list the contents of the package without extracting them, use the following:
$ rpm2cpio mypackage.rpm | cpio -vt

RHEL manual install php5.3.3+mysql5.1+apache2 and phpize pdo_dblib.so

# yum install relative package
$ yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel

#(1) GD2
$ cd /usr/local/src
$ wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
$ tar xzvf gd-2.0.33.tar.gz
$ cd gd-2.0.33
$ ./configure –prefix=/usr/local/gd2 –mandir=/usr/share/man //./configure 配置。
$ make //make 是用來編譯的,它從Makefile 中讀取指令,然後編譯。
$ make install //make install 是用來安裝的,它也從Makefile 中讀取指令,安裝到指定的位置。

#(2) Apache 日誌截斷程序
$ cd /usr/local/src
$ wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
$ tar xzvf cronolog-1.6.2.tar.gz
$ cd cronolog-1.6.2
$ ./configure –prefix=/usr/local/cronolog
$ make
$ make install

#(3) libxml 庫程序
$ cd /usr/local/src
$ wget http://ftp.gnome.org/pub/gnome/sources/libxml2/2.6/libxml2-2.6.26.tar.gz
$ tar zxvf libxml2-2.6.26.tar.gz
$ cd libxml2-2.6.26
$ ./configure –prefix=/usr/local/libxml2
$ make
$ make install



$ groupadd mysql
$ useradd -g mysql mysql
$ gunzip < mysql-VERSION.tar.gz | tar -xvf -
$ cd mysql-VERSION
$ ./configure --prefix=/usr/local/mysql
$ make
$ make install
$
$ cp support-files/my-medium.cnf /etc/my.cnf
$ cd /usr/local/mysql
$ chown -R mysql .
$ chgrp -R mysql .
$ bin/mysql_install_db --user=mysql
$ chown -R root .
$ chown -R mysql var
$ bin/mysqld_safe --user=mysql &

4、編譯安裝Apache2.24
這個最簡單了,基本不會有錯誤發生。
$ yum -y install openssl openssl-devel
# tar zxvf httpd-2.2.17.tar.gz
# cd httpd-2.2.17
$ ./configure --prefix=/opt/httpd-2.2.17 --enable-modules=all --enable-mods-shared=all --enable-proxy --enable-ssl --enable-so --with-mpm=prefork --with-pcre
$ make -j8
$ make install
$ ln -s /opt/httpd-2.2.17 /opt/httpd
$ echo '
# Start Apache
/usr/local/apache2/bin/apachectl start' >> /etc/rc.local


編譯安裝curl
下載curl 安裝到/usr/local/curl


5、編譯安裝php5.3.3
./configure --prefix=/usr/local/php --with-apxs2=/opt/httpd/bin/apxs --with-config-file-path=/usr/local/php --with-openssl --enable-bcmath --enable-calendar --with-curl=/usr/local/curl --with-curlwrappers --enable-ftp --with-gd --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr --enable-gd-native-ttf --with-gettext --enable-mbstring --enable-exif --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-xmlrpc --enable-soap --enable-sockets --enable-zip

$ make
$ make install
$ cp php.ini-recommended /etc/php.ini

$ yum -y install php-devel
1、安裝配置freetds
下載地址: http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
用以Linux和Unix連接MS SQLServer和Sybase數據庫。

$ wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
$ tar zxvf freetds-stable.tgz
$ cd freetds-stable
$ ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib
$ make && make install

$ cd /usr/local/src/php-5.3.3/ext/pdo_dblib
$ phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds --enable-pdo --with-pdo-dblib=/usr /local/freetds

如configure出現下列錯誤請按如下方法解決
configure: error: Directory /usr/local/freetds is not a FreeTDS installation directory
就是php找不到freetds的安裝路徑
其實是PHP檢測其安裝目錄的時候有些問題,檢查依據是兩個已經不用的文件,創建兩個空文件就OK
touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a

修改php.ini文件,添加行
extension=pdo_dblib.so

$ cp /usr/local/php-5.3.3/src/ext/pdo_dblib/modules/pdo_dblib.so /usr/lib/php/modules/.
$ /opt/httpd/bin/httpd -k restart
6、整合apache 與php
# vi /opt/httpd/conf/httpd.conf
在最後一行加上:
AddType application/x-httpd-php .php

查找:(設置WEB 默認文件)
DirectoryIndex index.html
替換為:
DirectoryIndex index.php index.html index.htm //在WEB 目錄不到默認文件,httpd 就會執行/var/www/error/noindex.html




Q & A:
Q:
I have the following very simple script that uses PDO/FreeTDS to connect
to a mssql server. I have PHP Version 5.3.3 running on Linux under
Apache. When I view this script via apache/firefox I get proper output.

If I try and run this via the command line, I get
an error connecting to the DB:
SQLSTATE[HY000] Unable to connect: Adaptive
Server is unavailable or does not exist (severity 9).
A:
This is because php-cli is parsing different php.ini.
Please solved this problem as below:
1)Checking the output of phpinfo() of php.ini by execute
$ php -i | grep php.ini
you will get the information of /path_difference/to/php_ini_root/php.ini.
$ cd /path_difference/to/php_ini_root && ln -s /path/to/real/php.ini php.ini

2010年11月17日 星期三

Zenoss Open Source Server and Network Monitoring - Core and Enterprise

Zenoss Enterprise 3.0 is a purpose-built Dynamic Service Assurance
product that improves the delivery of IT service to applications, business
services and supporting infrastructure in the dynamic datacenter. It's one
product that unifies the delivery of IT service across physical, virtual and
hybrid cloud infrastructures.

官網:http://www.zenoss.com/

Ganglia Monitoring System

What is Ganglia?
Ganglia is a scalable distributed monitoring system for high-performance computing systems such as clusters and Grids. It is based on a hierarchical design targeted at federations of clusters. It leverages widely used technologies such as XML for data representation, XDR for compact, portable data transport, and RRDtool for data storage and visualization. It uses carefully engineered data structures and algorithms to achieve very low per-node overheads and high concurrency. The implementation is robust, has been ported to an extensive set of operating systems and processor architectures, and is currently in use on thousands of clusters around the world. It has been used to link clusters across university campuses and around the world and can scale to handle clusters with 2000 nodes.

Ganglia is a BSD-licensed open-source project that grew out of the University of California, Berkeley Millennium Project which was initially funded in large part by the National Partnership for Advanced Computational Infrastructure (NPACI) and National Science Foundation RI Award EIA-9802069. NPACI is funded by the National Science Foundation and strives to advance science by creating a ubiquitous, continuous, and pervasive national computational infrastructure: the Grid. Current support comes from Planet Lab: an open platform for developing, deploying, and accessing planetary-scale services.

官網:http://ganglia.sourceforge.net/

The virtualization API

libvirt is:
A toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes).
Free software available under the GNU Lesser General Public License.
A long term stable C API
A set of bindings for common languages
A CIM provider for the DMTF virtualization schema
A QMF agent for the AMQP/QPid messaging system
libvirt supports:
The Xen hypervisor on Linux and Solaris hosts.
The QEMU emulator
The KVM Linux hypervisor
The LXC Linux container system
The OpenVZ Linux container system
The User Mode Linux paravirtualized kernel
The VirtualBox hypervisor
The VMware ESX and GSX hypervisors
Storage on IDE/SCSI/USB disks, FibreChannel, LVM, iSCSI, NFS and filesystems
libvirt provides:
Remote management using TLS encryption and x509 certificates
Remote management authenticating with Kerberos and SASL
Local access control using PolicyKit
Zero-conf discovery using Avahi multicast-DNS
Management of virtual machines, virtual networks and storage
Portable client API for Linux, Solaris and Windows

管網:http://www.libvirt.org/

2010年7月14日 星期三

netstat 的狀態說明

CLOSED —- Closed. The socket is not being used.
LISTEN —- Listening for incoming connections.
SYN_SENT —- Actively trying to establish connection.
SYN_RECEIVED —- Initial synchronization of the connection under way.
ESTABLISHED —- Connection has been established.
CLOSE_WAIT —- Remote shut down; waiting for the socket to close.
FIN_WAIT_1 —- Socket closed; shutting down connection.
CLOSING —- Closed, then remote shutdown; awaiting acknowledgement.
LAST_ACK —- Remote shut down, then closed ;awaiting acknowledgement.
FIN_WAIT_2 —- Socket closed; waiting for shutdown from remote.
TIME_WAIT —- Wait after close for remote shutdown retransmission.

Mount Samba in /etc/fstab

»»mount設置(Linux)
$ mkdir -p /mnt/samba/ServerBackup
$ echo '#mount point of BuffaloNasServer for Backup
//192.168.0.6/ServerBackup /mnt/samba/ServerBackup cifs username=[username],password=[password] 0 0' >> /etc/fstab
$ mount -a
$ df

»»mount設置(Freebsd)
$ mkdir -p /mnt/samba/ServerBackup
$ vi /etc/fstab
#mount point of BuffaloNasServer for Backup
//itrue@192.168.0.6/ServerBackup /mnt/samba/ServerBackup smbfs rw,-I192.168.0.6,-N 0 0
$ vi /etc/nsmb.conf
[192.168.0.6:ITRUE] #must uppercase
password=[password]
$ mount -a
$ df

unknown filesystem type 'smbfs' when mounting

Solution :
mount -t cifs //hppavilion/wayne /mnt/nfs

2010年7月13日 星期二

搜尋Server中大容量的檔案

find /bin -type d | xargs -i du -sh {} | sort -n > /home/log_bin.log&
find /home -type d | xargs -i du -sh {} | sort -n > /home/log_home.log&
find /etc -type d | xargs -i du -sh {} | sort -n > /home/log_etc.log&
find /lib -type d | xargs -i du -sh {} | sort -n > /home/log_lib.log&
find /mnt -type d | xargs -i du -sh {} | sort -n > /home/log_mnt.log&
find /net -type d | xargs -i du -sh {} | sort -n > /home/log_net.log&
find /opt -type d | xargs -i du -sh {} | sort -n > /home/log_opt.log&
find /root -type d | xargs -i du -sh {} | sort -n > /home/log_root.log&
find /usr -type d | xargs -i du -sh {} | sort -n > /home/log_usr.log&
find /var -type d | xargs -i du -sh {} | sort -n > /home/log_var.log&

2010年6月30日 星期三

RHEL java 1.6安裝

#安裝JDK,目前最新版本是JDK 6 Update 17,請到此下載
http://java.sun.com/javase/downloads/index.jsp

#安裝JDK,回答完Yes的版權問題後, 開始自動解壓縮並安裝RPM

$ sh jdk-6u17-linux-i586-rpm.bin

#新增path在/etc/profile
$ vi /etc/profile

##JAVA Environment
export JAVA_HOME=/usr/java/jdk1.6.0_17
export JAVA_BIN=/usr/java/jdk1.6.0_17/bin
export JAVA_OPTS="$JAVA_OPTS -Xmx512M -Dcom.sun.management.jmxremote"
export PATH=$JAVA_HOME/bin:$PATH
#export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
export CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib/ext

$ source /etc/profile
$ java -version

2010年6月4日 星期五

Linux更新Server時間

echo "#Update Server Time
10 5 * * * root /usr/sbin/ntpdate tock.stdtime.gov.tw && /sbin/hwclock -w" >> /etc/crontab

2010年6月3日 星期四

透過 shell script 發送 email

如果想用 shell script 發送郵件,可以用 mail 實現:

# echo "email content" | mail -s "email subject" you@emaildomain
以上語句會發送一封標題為 "email subject" 的電郵到 you@emaildomain,其中 "email content" 為電郵內容。

以下簡單的兩行 shell script 便會將檔案系統使用量,透過電郵發送給指定的電郵信箱:

#!/bin/sh
/bin/df -h | /usr/bin/mail -s "server capacity" you@emaildomain

摘自:http://www.hkcode.com/linux-bsd-notes/444

2010年5月23日 星期日

tar --exclude語法

Example
/path/to/tarFolder
/path/to/tarFolder/exclude1
/path/to/tarFolder/exclude2
/path/to/tarFolder/a/exclude3

$ cd /path/to
$ tar -cvpf tarFolder.tar tarFolder --exclude="tarFolder/exclude1/*" --exclude="tarFolder/exclude2/*" --exclude="tarFolder/a/exclude3/*"

PS : --exclude="tarFolder/exclude1/*" <--一定要是相對路徑 & 使用雙引號

[Solved]phpThumb far=C 無法成功

如果linux有灌ImageMagick,phpThumb會預設使用ImageMagick來產生圖檔
如果沒有ImagMagick,才會使用php gd librabry來產生圖檔

far=C在ImageMagick無法正常運作
所以解決方法是將linux下的ImageMagick移除
$ rpm -e imageMagick

2010年5月18日 星期二

RHEL5 Tuning

There are a lot of differences between Linux version 2.4 and 2.6, so first we'll cover the tuning issues that are the same in both 2.4 and 2.6. To change TCP settings in, you add the entries below to the file /etc/sysctl.conf, and then run "sysctl -p".

Like all operating systems, the default maximum Linux TCP buffer sizes are way too small. I suggest changing them to the following settings:

# increase TCP max buffer size setable using setsockopt()
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# increase Linux autotuning TCP buffer limits
# min, default, and max number of bytes to use
# set max to at least 4MB, or higher if you use very high BDP paths
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
You should also verify that the following are all set to the default value of 1

sysctl net.ipv4.tcp_window_scaling
sysctl net.ipv4.tcp_timestamps
sysctl net.ipv4.tcp_sack
Note: you should leave tcp_mem alone. The defaults are fine.

You can achieve increases in bandwidth of up to 10x by doing this on some long, fast paths. This is only a good idea for Gigabit Ethernet connected hosts, and may have other side effects such as uneven sharing between multiple streams.

Also, I've been told that for some network paths, using the Linux 'tc' (traffic control) system to pace traffic out of the host can help improve total throughput.

參考網站:http://fasterdata.es.net/TCP-tuning/linux.html

2010年5月14日 星期五

檢查Mail Server是否被列入blacklist

http://www.spamhaus.org/sbl/sbl.lasso
http://cbl.abuseat.org/lookup.cgi

用telnet測試發送信件

nslookup
set type=mx
yahoo.com.tw
telnet mx1.tw.yahoo.com.tw 25

2010年3月30日 星期二

sar應用詳解

sar一個開放源代碼工具;它由 Sebastien Godard 維護。這個工具也包含於大部分 Linux 發行版本中,可用於當前的 2.4 和 2.6 內核,在red hat Linux 9.0 中是Sysstat 。也可以在其主頁下載,下載鏈接:http://download.stmc.edu.hk/redhat/linux/9/en/os/RedHat/RPMS/sysstat-4.0.7-3.i386.rpm 。Sysstat 包括: I/O 與 CPU 統計資料的工具:iostat、mpstat(用於多處理器性能監測)。和sar 。前面我們介紹了iostat、mpstat。下面重點介紹sar。
sar是System Activity Reporter(系統活動情況報告)的縮寫。顧名思義,sar工具將對系統當前的狀態進行取樣,然後通過計算數據和比例來表達系統的當前運行狀態。它的特點是可以連續對系統取樣,獲得大量的取樣數據;取樣數據和分析的結果都可以存入文件,使用它時消耗的系統資源很小。sar是讀 /proc這個內存文件系統進行採樣來得到數據。sar 從/var/log/sa/sadd 讀取記錄文件的資料。/usr/lib/sa/sadc 負責維護系統活動資料和建立這些記錄文件。sar實際包括兩個shell 程序/usr/lib/sa/sa1 和/usr/lib/sa/sa2。/etc/cron.d/systat 是crontab 的系統文件, 每十分鐘執行sa1程序一次,然後將它的輸出加到記錄文件後面。Sa1 以二進制格式儲存sar 所讀取的資料。/usr/lib/sa/sa2將每天數據寫入文件/var/log/sa/sadd。dd標示每月的日期。
sar的語法如下:
sar [-option] [-o file] t [n]
  它的含義是每隔t秒取樣一次,共取樣n次。其中-o file表示取樣結果將以二進制形式存入文件file中。Option主要選項:
-A 匯總所有的報告
  -a 報告文件讀寫使用情況
  -B 報告附加的緩存的使用情況
  -b 報告緩存的使用情況
  -c 報告系統調用的使用情況
  
  
 
應用實例:
察看內存和交換空間的使用率,使用sar -r。
# sar -r
Linux 2.4.20-8 (www.cao.com) 20050503
12:00:01 AM kbmemfree kbmemused %memused kbmemshrd kbbuffers kbcached
12:10:00 AM 240468 1048252 81.34 0 133724 485772
12:20:00 AM 240508 1048212 81.34 0 134172 485600

08:40:00 PM 934132 354588 27.51 0 26080 185364
Average: 324346 964374 74.83 0 96072 467559
kbmemfree 與 kbmemused 字段分別顯示內存的未使用與已使用空間,後面跟著的是已使用空間的百分比(%memused 字段)。kbbuffers 與 kbcached 字段分別顯示緩衝區與系統全域的資料存取量,單位為 KB。使用 2.4 Linux 核心的系統(例如 Red Hat Linux 9),kbmemshrd 字段一律為零。
sar命令它幾乎可以完成上面介紹的所有命令的功能。sar是目前Linux上最為全面的系統性能分析工具之一,可以從14個大方面對系統的活動進行報告,包括文件的讀寫情況、系統調用的使用情況、串口、CPU效率、內存使用狀況、進程活動及IPC有關的活動等,使用也是較為複雜。sar命令非常複雜,只有通過熟練使用才能掌握。

摘自:http://www.lslnet.com/linux/f/docs1/i16/big5178736.htm

linux sar tip in rhel4 (centos4) 安裝教學

有時 sar 的指令無法正常顯示 cpu idle, 可以透過更新 sysstat 的套件來解決此狀況.
使用 rhel4 相容版本 sysstat-8.1.5.tar.gz, 也可於官方網站下載最新版本.
wget http://pagesperso-orange.fr/sebastien.godard/sysstat-8.1.5.tar.gz
移除無法正常顯示 cpuidle 的 sysstat 套件.
rpm -e sysstat
編譯 sysstat 原始碼, 並且安裝於 /usr.
tar zxvf sysstat-8.1.5.tar.gz
cd sysstat-8.1.5
./configure --prefix=/usr && make && make install
當 sar 的資料檔案與版本不符, 將會出現以下訊息.
sysstat: Invalid system activity file: /var/log/sa/saXX
需執行以下指令來重建.
rm -rf /var/log/sa/*
/usr/lib/sa/sa1
將 sar 加入 crontab, 定期產生資料.
echo '# run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib/sa/sa1 1 1
# generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib/sa/sa2 -A' > /etc/cron.d/sysstat

摘自:http://marlboromoo.blogspot.com/2009/06/linux-sar-tip-in-rhel4centos4.html

wibiya widget