3月 29

linux shell 加密

经常使用脚本,但是在脚本中写入密码明文很不安全,所以考虑加密问题。当前比较普遍、简单的方法为使用shc程序
把脚本使用RC4加密算法再编译一次。

shc的主页
http://www.datsi.fi.upm.es/%7Efrosal/
当前最新为http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz

具体操作如下
# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz
# tar zxvf shc-3.8.7.tgz
# cd shc-3.8.7
# make test
# make strings
到此,目录下已经有了shc的程序,可以直接使用,如果希望使用方便也可以在安装一下。
# make install

安装完成,非常简单。使用也非常简单。
# ./shc -v -f myshell.sh
编译完成后会产生两个myshell.sh.x和myshell.sh.x.c。myshell.sh.x为二进制文件,
myshell.sh.x.c为C源码文件。

3月 28

nagios 安装调试

[整理人:张子萌 2010-04]

需要准备软件如下:
centos

nagios-3.2.0
nagios-plugin-1.4.14

除了nagios还需要安装以下软件
apache2 安装路径为/usr/local/apache
perl
gcc 编译工作系统默认即可,如果没有安装推荐使用yum安装。
php nagios3.2页面需要php环境
yum install gcc glibc glibc-common
绘制图表需要安装以下包
freetype-2.3.5.tar.gz
libiconv-1.11.tar.gz
libmcrypt-2.5.7.tar.gz
libpng-1.2.10.tar.bz2
jpegsrc.v6b.tar.gz
gd-2.0.35.tar.gz
可以使用yum或者源码安装。
以上绘图包和apache安装可以参照:http://simon-zzm.blog.163.com/blog/static/88809522201028104721790/

1. 建组建帐号nagios

# groupadd nagios
# groupadd apache
# useradd -g nagios -G apache nagios
# useradd -g apache -G nagios apache

2. 编译安装nagios

# tar zxvf nagios-3.2.0.tar.gz
# cd nagios-3.2.0
# ./configure –prefix=/usr/local/nagios
–with-command-group=nagios
–with-httpd-conf=/usr/local/apache/conf/
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
# make install-webconf

3. 装nagios-plugin-1.4.13

# tar zxvf nagios-plugin-1.4.14.tar.gz
# cd nagios-plugin-1.4.14
# ./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios
# make
# make install

注:centos4 上安装时
在checking for redhat spopen problem…停住。
需要添加–enable-redhat-pthread-workaround参数

4. 创建web登录nagios账号

# cd /usr/local/apache/bin
# ./htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin ;创建一个登录WEB的帐号

5. 配置nagios和apache整合
本文使用虚拟机配置。
# cd /usr/local/apache/conf/extra/
# vi httpd-vhosts.conf

配置apache的cgi ,在httpd-vhosts.conf后增加配置如下:

ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin

Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user

Alias /nagios /usr/local/nagios/share

Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user

首先测试配置文件是否正确
# /usr/local/apache/bin/apachectl configtest

重启apache
# /usr/local/apache/bin/apachectl stop
# /usr/local/apache/bin/apachectl start

6. 启动nagios

测试nagios配置是否可用
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果可用启动服务
# /etc/init.d/nagios start

现在你可以通过浏览器访问监控的界面了,http://x.x.x.x/nagios

7. 定义监控组

7.1 建立test监控组,监控192.268.1.103服务器。

在/usr/local/nagios/etc/目录下建立test目录,并定义主机.
监控192.168.1.103服务器的配置文件名为test_1_103.cfg.
# mkdir -p /usr/local/nagios/etc/test
# vi /usr/local/nagios/etc/testgroup.cfg
# 定义主机
define host{
use test-host
alias test Linux Server
address test_1_103; 如多个文件可以用逗号分开
}

7.2 建立监控文件
# vi /usr/local/nagios/etc/test_1_103.cfg
# 定义Ping远程Linux主机
define service{
use test-service ; 这个名字要和监控模板中一样,见7.3
host_name test;
service_description PING;
check_command check_ping!100.0,20%!500.0,60% ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分割。
}
# 检查远程Linux主机根分区使用情况,必须安装nrpe并在/usr/local/nagios/etc/objects/commands.cfg中定义,见第8部分。
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Root Partition
check_command check_nrpe!check_disk_root
}
# 检查远程Linux主机的登录人数
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Current Users
check_command check_nrpe!check_users
}
# 检查远程Linux的主机的负载
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Current Load
check_command check_nrpe!check_load
}
# 检查远程Linux主机swap分区使用情况
define service{
&nb
sp; use generic-service ; Name of service template to use
host_name sectop
service_description Swap Usage
check_command check_nrpe!check_swap
}
# 检查远程Linux主机的SSH服务
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description SSH
check_command check_ssh
notifications_enabled 1 ;0为不发送告警信息 1为发送

}
# 检查远程Linux主机的HTTP服务
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description HTTP
check_command check_http
notifications_enabled 1 ;0为不发送告警信息 1为发送
}

7.3 定义监控组的模板
# vi /usr/local/nagios/etc/objects/templates.cfg

define contact{
name test-contact ; The name of this contact template
service_notification_period 24×7 ; service notifications can be sent anytime
host_notification_period 24×7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via email
host_notification_commands notify-host-by-email ; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}

define host{
name test-host ; The name of this host template
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
notification_period 24×7 ; Send host notifications at any time
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL HOST, JUST A TEMPLATE!
}

define host{
name te
st-server ; The name of this host template
use test-host ; This template inherits other values from the generic-host template
check_period 24×7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL HOST, JUST A TEMPLATE!
}

define service{
name test-service ; The ‘name’ of this service template
active_checks_enabled 1 ; Active service checks are enabled
passive_checks_enabled 1 ; Passive service checks are enabled/accepted
parallelize_check 1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)
obsess_over_service 1 ; We should obsess over this service (if necessary)
check_freshness 0 ; Default is to NOT check service ‘freshness’
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
is_volatile 0 ; The service is not volatile
check_period 24×7 ; The service can be checked at any time of the day
max_check_attempts 3 ; Re-check the serv
ice up to 3 times in order to determine its final (hard) state
normal_check_interval 10 ; Check the service every 10 minutes under normal conditions
retry_check_interval 2 ; Re-check the service every two minutes until a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the ‘admins’ group
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and recovery events
notification_interval 60 ; Re-notify about service problems every hour
notification_period 24×7 ; Notifications can be sent out at any time
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}

7.4 修改告警模板
# vi vi /usr/local/nagios/etc/objects/contact.cfg
define contact{
contact_name nagiosadmin ; Short name of user
use test-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email admin@mail.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}

7.5 添加加载项

在nagios.cfg配置文件中开启对/usr/local/nagios/etc/test/加载
cfg_dir=/usr/local/nagios/etc/test/testgroups.cfg
cfg_dir=/usr/local/nagios/etc/test/test_1_103.cfg

测试nagios配置是否可用
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果重启服务
# /etc/init.d/nagios restart

8. 安装nrpe
nrpe用于和远程服务器交互使用。
nrpe分为两部分:一部分是运行在client端。一部分运行在客户端

8.1 nagios服务器上安装
#tar xzvf nrpe-2.12.tar.gz
#cd nrpe-2.12
#./configure
#make all
#make install-plugin #服务器端只要安装nrpe监控插件就行

在/usr/local/nagios/etc/objects/commands.cfg中定义check_nrpe命令
#vi /usr/local/nagios/etc/objects/commands.cfg
添加如下
# ‘check_nrpe ‘ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

8.2 在要监控的客户端上
参照 1 中在客户服务器上建立nagios用户组和nagios用户
# tar xzvf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
# ./configure –prefix=/usr/local/nagios
# make
# make install
# chown -R nagios:nagios /usr/local/nagios/

# tar xzvf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure –prefix=/usr/local/nagios
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
修改配置文件
# vi /usr/local/nagios/etc/nrpe.cfg #修改nrpe配置文件,允许Nagios监控服务器监控本机

allowed_hosts=127.0.0.1,192.168.0.19 #此处IP为监控服务器的地址,如多个地址可以使用逗号分隔

启动客户端
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
查看端口是否已经监听
#netstat -tln #查看端口
tcp 0 0 0.0.0.0:5666 0.0.0.0:*

添加随系统启动,启动nrep客户端
#vi /etc/rc.d/rc.local
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

9. 设置被监控的windows服务器。
找了个NSClient++-windows*.msi的安装包,安装完毕修改配置,在nagios服务器也需要配
置,此处不做详解。

附录:
1. 配置nagios及其plugins

/usr/local/nagios/etc下的文件
#控制cgi访问的配置文件
cgi.cfg

#Nagios主配置文件
nagios.cfg

#resource.cfg定义了一些变量,以便被其它文件引用,如$USER1$
resource.cfg

# objects是一个目录,用于定义Nagios对象
objects

/usr/local/nagios/etc/objects下的文件

#命令

3月 24

rsync安装配置

rsync是linux下快速镜像工具。

下载地址:http://samba.anu.edu.au/rsync/

1. 安装

# tar zxvf rsync-3.0.5.tar.gz
# ./configure –prefix=/usr/local/rsync
# make && make install

2. 添加配置文件

添加/etc/rsyncd.conf文件,文件内容如下:

log file = /var/log/rsyncd.log #日志位置
pid file = /var/run/rsyncd.pid #进程号存放位置
lock file = /var/run/rsync.lock #锁文件存放位置

uid = root #守护进程的用户权限
gid = root

#port=873 #rsync使用的端口,默认873

#hosts allow = 192.168.1.2 #全局允许通过的IP地址

max connections = 5 # 客户端最大连接数目

[back] # 要同步的模块名
path = /back #要同步的目录
comment = source #
ignore errors #忽略IO错误
read only = no # no客户端可上传文件,yes只读
write only = no # no客户端可下载文件,yes不能下载
#list = yes #是否提供资源列表
auth users = back_root #登陆系统使用的用户名,没有默认为匿名。
hosts allow = 192.168.1.3 #本模块允许通过的IP地址
hosts deny = 192.168.1.4 #禁止主机IP
secrets file=/etc/rsync.pass #密码文件存放的位置

3. 配置密码文件

密码文件为配置文件中所写的文件/etc/rsync.pass格式为

账户:密码

4. 修改配置文件和密码文件权限为600

# chmod 600 /etc/rsyncd.conf
# chmod 600 /etc/rsync.pass

5. 启动守护进程

# rsync –daemon

可以通过以下两条命令查看进程是否存在
ps -aux |grep rsync
netstat -an |grep 873

6. 服务端程序启动后,在客户端执行第一步操作。

实例演示
cd /usr/local/rsync/bin
下载文件
./rsync -vzrtopg –progress –delete back_root@x.x.x.x::back /tmp/rsync
上传文件
./rsync -vzrtopg –progress –delete /tmp/rsync back_root@x.x.x.x::back

rsync参数的具体解释如下:

-v, –verbose 详细模式输出
-q, –quiet 精简输出模式
-c, –checksum 打开校验开关,强制对文件传输进行校验
-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, –recursive 对子目录以递归模式处理
-R, –relative 使用相对路径信息
-b, –backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用–suffix选项来指定不同的备份文件前缀。
–backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, –update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, –links 保留软链结
-L, –copy-links 想对待常规文件一样处理软链结
–copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
–safe-links 忽略指向SRC路径目录树以外的链结
-H, –hard-links 保留硬链结
-p, –perms 保持文件权限
-o, –owner 保持文件属主信息
-g, –group 保持文件属组信息
-D, –devices 保持设备文件信息
-t, –times 保持文件时间信息
-S, –sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, –dry-run现实哪些文件将被传输
-W, –whole-file 拷贝文件,不进行增量检测
-x, –one-file-system 不要跨越文件系统边界
-B, –block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, –rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
–rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, –cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
–existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
–delete 删除那些DST中SRC没有的文件
–delete-excluded 同样删除接收端那些被该选项指定排除的文件
–delete-after 传输结束以后再删除
–ignore-errors 及时出现IO错误也进行删除
–max-delete=NUM 最多删除NUM个文件
–partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
–force 强制删除目录,即使不为空
–numeric-ids 不将数字的用户和组ID匹配为用户名和组名
–timeout=TIME IP超时时间,单位为秒
-I, –ignore-times 不跳过那些有同样的时间和长度的文件
–size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
–modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T –temp-dir=DIR 在DIR中创建临时文件
–compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 –partial
–progress 显示备份过程
-z, –compress 对备份的文件在传输时进行压缩处理
–exclude=PATTERN 指定排除不需要传输的文件模式
–include=PATTERN 指定不排除而需要传输的文件模式
–exclude-from=FILE 排除FILE中指定模式的文件
–include-from=FILE 不排除FILE指定模式匹配的文件
–version 打印版本信息
–address 绑定到特定的地址
–config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
–port=PORT 指定其他的rsync服务端口
–blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
–progress 在传输时现实传输过程
–log-format=formAT 指定日志文件格式
–password-file=FILE 从FILE中得到密码
–bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, –help 显示帮助信息

rsync有六种不同的工作模式:

1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号”:”分隔符时就启动这种工作模式。如:rsync -a /data /backup

2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号”:”分隔符时启动该模式。如:rsync -avz *.c foo:src

3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号”:”分隔符时启动该模式。如:rsync -avz foo:src/bar /data

4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含”::”分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack

5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含”::”分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www

6)列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://172.16.78.192/www

3月 18

mysql 主从服务器数据同步

[整理:张子萌 2009-10-12]

操作系统:CentOS

数据库版本:mysql-5.1.44-linux-i686-glibc23.tar.gz

A服务器: 192.168.1.100 主服务器(master)

B服务器: 192.168.1.101 从服务器(slave)

主副服务器安装比较简单,此处只列安装顺序的命令。

详细操作可以见http://simon-zzm.blog.163.com/blog/static/88809522201028104721790/中mysql部分。

1. mysql安装

在主从服务器上做以下操作。

# groupadd -g700 mysql

# useradd -g700 -u700 mysql mysql

# tar zxvf mysql-5.1.44-linux-i686-glibc23.tar.gz

# cp mysql-5.1.44-linux-i686-glibc23 /usr/local/mysql

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf

# chown -R mysql:mysql /usr/local/mysql

2. 配置主服务器

2.1 登录主服务器。初始化数据库,并启动

# /usr/local/mysql/scripts/mysql_install_db –user=mysql

–basedir=/usr/local/mysql –datadir=/usr/local/mysql/data

# /etc/init.d/mysql start

2.2 连接数据库,并创建所使用的库。本文为了测试创建名为testas的数据库

# /usr/local/mysql/bin/mysql -u root

mysql> create database testas;

Query OK, 1 row affected (0.03 sec)

2.3 创建从服务器同步主服务器时使用的账户。账户名为slavetest,密码为123;

Mysql>grant replication slave,reload,super on *.* To

slavetest@192.168.1.101 identified by ‘123’ ;

Mysql> flush privileges;

2.4 停止mysql数据库

# /etc/init.d/mysql stop

2.5 使用系统拷贝命令将主服务器的已经创建的数据,拷贝到备用服务器上。

# scp r /usr/local/mysql root@1192.168.1.101:/usr/local/mysql

注: 向备机传送备份数据库有两种方法。
a)备份数据直接将data目录需要备份数据库的文件夹传到备份数据中。
b)使用MySQLdump的方式来备份主数然后导入备库中(备份时加上”–master-data “参数)。
本文使用第一种方法来备,也建议使用第一种。如果是不能停机,则将主数据库进行锁库后在进行备份操作。
数据库锁定:
FLUSH TABLES WITH READ LOCK;
数据库打开:
UNLOCK TABLES;

2.6 修改主服务器的配置文件。在/etc/my.cnf文件[mysqld]后添加两个参数。第一个是

server-id=master_id,master_id的值为1到2^32-1之间的正整数,推荐使用1。第二个是

选择添加skip-name-resolve,如果开启远程访问,可以防止远程连接失败。

# vi /etc/my.cnf

修改后结果[mysqld]部分,部分截图如下:

2010年3月18日 - simon-zzm - simon个人观点

2.7 启动数据库,记录同步数据的点。

# /etc/init.d/mysql start

# /usr/local/mysql/bin/mysql -u root

mysql> show master status;

获得数据如下图:

2010年3月18日 - simon-zzm - simon个人观点

请记录file和Position的值,在配置从服务器中需要用到。

3. 开始配置从服务器

3.1 登录从服务器,修改从主服务器上所传文件的权限

# chown -R mysql:mysql /usr/local/mysql

3.2 修改配置从服务器的文件。在/etc/my.cnf文件[mysqld]后添加slave_id 的值是 1 到 2^32-1 之间的正整数,这个值一定不能与主服务器中master_id一样。推荐值为2。

# vi /etc/my.cnf

修改后结果[mysqld]部分,部分截图如下:

2010年3月18日 - simon-zzm - simon个人观点

3.3 启动从数据库

# /etc/init.d/mysql start

3.4 编辑sql(使用组服务器上记录的数据),登录数据库后运行sql。

Sql 编写如下:

CHANGE MASTER TO

MASTER_HOST=’192.168.1.100′,

MASTER_USER=’slavetest’,

MASTER_PASSWORD=’123′,

MASTER_LOG_FILE=’mysql-bin.000004′,

MASTER_LOG_POS=106 ;

如下图:

2010年3月18日 - simon-zzm - simon个人观点

3.5 启动从服务器同步服务

mysql> slave start;

4. 服务器配置完成,可以连到主服务器上,创建表、添加数据后,再看从数据库是否也有相同数据。如果数据相同,则配置成功。

注意:为了安全日常登录和操作最好不要使用root用户。不可以在从数据库中修改数据,最好将从数据库连接账号的权限修改为只读。

5. 登录主从数据库使用命令查看当前状态

5.1 登录从服务器,查看当前I/O情况

mysql> show slave statusG

Connecting to master

该线程证尝试连接到master上。

Checking master version

确定连接到master后出现的一个短暂的状态。

Registering slave on master

确定连接到master后出现的一个短暂的状态。

Requesting binlog dump

确定连接到master后出现的一个短暂的状态。该线程向master发送一个请求,告诉它要请求的二进制文件以及开始位置。

Waiting to reconnect after a failed binlog dump request

如果二进制日志转储(binary log dump)请求失败了(由于连接断开),该线程在休眠时进入这个状态,并定期重连。重连的时间间隔由

–master-connect-retry 选项来指定。

Reconnecting after a failed binlog dump request

该线程正尝试重连到master。

Waiting for master to send event

已经连接到master,正等待它发送二进制日志。如果master闲置时,这个状态可能会持续较长时间,如果它等待超过 slave_read_timeout 秒,就会发生超时。这时,它就会考虑

断开连接,然后尝试重连。

Queueing master event to the relay log

已经读取到一个事件,正把它拷贝到中继日志中以备SQL线程处理。

Waiting to reconnect after a failed master event read

读日志时发生错误(由于连接断开)。该线程在重连之前休眠 master-connect-retry 秒。

Reconnecting after a failed master event read

正尝试重连到master。当连接确定后,状态就变成 Waiting for master to send event。

Waiting for the slave SQL thread to free enough relay log space

relay_log_space_limit 的值非零,中继日志的大小总和超过这个值了。I/O线程等待SQL线程先处理中继日志然后删除它们以释放足够的空间。

Waiting for slave mutex on exit

当前线程停止了,这个时间很短。

3月 12

nginx 0.8 安装 配置

【整理人:张子萌 最近一次修改2010-12】

nginx的官方网站:http://nginx.org/

1.下载nginx源码
nginx-0.8.34.tar.gz

如果希望在Nginx中使用正则表达式使配置更灵活,需要安装PCRE。
查看是否安装pcre
# rpm -qa pcre*

如果希望在是用ssl加密部分请安装openssl。

如果希望压缩传送需要安装gzip

如果使用nginx使用的缓存,需要清理指定url需要下载并同时编译。
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz

推荐使用升级工具安装,升级工具使用可以参照:
http://simon-zzm.blog.163.com/blog/static/88809522201025102237958/

2. 编译安装nginx
首先创建账户,指定用户与组的id均为700
# groupadd -g 700 nginx
# useradd -g700 -u700 nginx
解压、安装nginx,对于不需要的模块可以不编译。
最后一行为清理指定url模块,可以选择性编译
# tar zxvf ngx_cache_purge-1.0.tar.gz
# tar zxvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure –prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-pcre
–with-select_module
–with-poll_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_gzip_static_module
–add-module=/usr/local/src/ngx_cache_purge-1.0
# make && make install

3. nginx配置文件

主要配置文件:/usr/local/nginx/conf/nginx.conf
# 程序运行用户
user nginx nginx;
# 启动进程数
worker_processes 8;

#给每个进程分配cpu,例:将8个进程分配到8个cpu,也可以写多个,或将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# 全局错误日志和程序PID文件
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

# 工作模式及连接数上限
# 工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
# poll(高效模式。适用Linux 2.6+,SuSE 8.2,Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
events {
use poll;
worker_connections 1024;
}

# 设定http服务器,反向代理功能提供负载均衡支持
http {
#设定mime类型
include mime.types;
default_type application/octet-stream;

# 隐藏nginx版本,防止攻击
server_tokens off;
# 设定日志格式
#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#设定access log
#access_log logs/access.log main;
#linux下强大的静态文件发送功能,一定要开启
sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;

# 定义一个叫“mysvr”的记录区,总容量为 10M
# 和下面location中的limit_conn一起限制单个IP的并发连接数为10
# limit_zone只能是用在http中,limit_conn可以使用在http、sever、local中
limit_zone mysvr $binary_remote_addr 10m;

#设定负载均衡的服务器列表
upstream mysvr {
ip_hash #如果不需要可以删除直接采用轮训方法负载
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.0.1:80 weight=5;
server 192.168.0.2:80 weight=1;
server 192.168.0.3:80 weight=6;
server 192.168.0.5:80;
server 192.168.0.6:80;

#down 表示单前的server暂时不参与负载
#weight 默认为1.weight越大,负载的权重就越大。
#max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:max_fails次失败后,暂停的时间。
#backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
}
# 设定缓存临时目录,临时目录要与proxy_cache_path中目录一样
proxy_temp_path /usr/local/nginx_test/proxy_temp;
# proxy_cache_path中参数如下
# levels指定该缓存空间有两层hash目录,第一层目录是1个字母,第二层为2个字母
# keys_zone为这个空间起个名字,比如 cache_one,在server模块中proxy_cache使用
# 200m 内存缓存空间大小为200MB
# inactive 1天没有被访问的内容自动清除;
# max_size使用硬盘缓存大小30G
# clean_time指定一分钟清理一次缓存。
proxy_cache_path /usr/local/nginx_test/proxy_temp/ levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name 192.168.0.7 mysvr.c
om www.mysvr.com;
#charset gb2312;
#charset utf8;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #与后端连接超时时间
proxy_send_timeout 90; #后端服务器回传超时时间
proxy_read_timeout 90; #连成功后,后端服务器响应超时时间。
proxy_buffer_size 4k; #代理服务器保存用户头信息的缓存大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; #高负荷缓村大小。大小计算方法proxy_buffers*2
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值经直接从upstream获得。
limit_conn mysvr 10; #每个IP只能有10个并发
limit_rate 100k; #每个并发只能有100K的速度
index index.html index.htm; #选则文件后缀解析都优先顺序
expires 30d; #客户端缓存时间 30天

}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus