3月 30

centos 硬盘只读

       通过ftp上传文件失败。直接登录服务器查看,权限都没有问题,直接创建文件,系统也是报系统只读。
       首先卸载分区,然后扫描,结果如下:
# fsck -f /dev/sdb1
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
fsck.ext3: Attempt to read block from filesystem resulted in short read while trying to open /dev/sdb1
Could this be a zero-length partition?

        很奇怪怎么分区为0呀,是不是主磁道坏了,如果是就比较麻烦了,真实不应该卸载。应该先把需要的数据考出来就好了。只能尝试修复了。之前看多过相关修复的页面如下:
http://www.cyberciti.biz/tips/surviving-a-linux-filesystem-failures.html
修复前先看看,系统日志里是否有些提示,如下:
kernel: sdb: Write Protect is off
kernel: SCSI device sdb: drive cache: write back
       奇怪硬盘被写保护了。直接看看硬盘状态
fdisk -l
      没有查询到相关的硬盘信息。这个就没有办法了,系统认不到,但是全部坏掉的几率不大,因为之前还可以读出文件。重启操作系统。
硬盘又认出来了。做扫描,果然有坏块,修复完毕挂载,一切正常。有惊无险。

       通过资料查询,此种处理为保护数据,应该是比较安全的。
提醒:如果参照面页面进行处理,输入命令“hdparm -W0 /dev/sdb”关闭写缓冲区,这个我没有测试。如果要重启,分区又不是必要的要将/etc/fstab中的扫描部分停掉,以避免因为扫描失败不能进入系统。
http://www.linuxforums.org/forum/red-hat-fedora-linux/136472-possible-sata-related-system-slowdowns.html

12月 11

新服务器的硬件测试

【2011-12-09 整理人:simon-zzm@163.com】
新买的服务器安装CentOS的操作系统,需要测试服务器硬件的稳定性。如果不运行服务
空跑根本没有意义,所以使用压测软件进行测试。
本次测试使用到stress软件,网站地址如下:
http://weather.ou.edu/~apw/projects/stress/
如果是服务器没有安装操作系统也可以到以下的网站下载iso或usb基本操作系统加压力软件的
集合。网站地址如下:
www.stresslinux.org

本次只是记录使用stress的过程。
stress用C编写,可以运行在x86,ppc64和PPC 32 GNU / Linux的,Tru64的,SPARC Solaris的平台。
可以自由配置参数,对cpu、内存、IO进行测试。

软件下载、编译、测试
为了避免编译失败,建议先确认已经安装gcc编译器。
# yum -y gcc*
# wget http://weather.ou.edu/~apw/projects/stress/stress-1.0.4.tar.gz
# tar zxvf stress-1.0.4.tar.gz
# cd stress-1.0.4
# ./configure && make

至此已经编译完毕,本软件不打算安装,所以直接使用。
# cd /root/stress-1.0.4/src
# ./stress –cpu 8 –io 8 –vm 8 –vm-bytes 32000M –timeout 180s

服务器的硬件为4核双cpu,32G内存,测试180秒中。所以使用以上命令,io和vm后的8为8个
进行,vm-bytes为32G的内存。

使用感受,能将所有的硬件资源耗尽,但是如果运行时间过长,就不知道是死机还是再测试了。
更主要的是测试完毕也没有测试报告生成。

9月 05

使用TCMalloc优化内存

安装系统CentOS x86_64mysql 5.1.44,已经在运行。

  网上很多介绍64位安装了libunwind,但是用1.0的总是编译不通过,不找原因了跳过了。直接安装google-perftools-1.8.3,因为是64位所以要加–enable-frame-pointers参数。

# wget http://google-perftools.googlecode.com/files/google-perftools-1.8.3.tar.gz

# tar zxvf google-perftools-1.8.3.tar.gz

# cd google-perftools-1.8.3

# ./configure –enable-frame-pointers

# make

# make install

Google-perftools生效

# echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf

# /sbin/ldconfig

mysql启动部分添加

# vi /usr/local/mysql/bin/mysqld_safe

在“# executing mysqld_safe”在下面增加“export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

重启mysql服务,查看tcmalloc是否生效。如显示如下说明已经生效。

# lsof -n | grep tcmalloc

mysqld 32480 mysql mem REG 8,2 1916397 412627 /usr/local/lib/libtcmalloc.so.0.2.2

TCMalloc (google-perftools) 是用于优化C++写的多线程应用,比glibc 2.3malloc快,原理参见http://shiningray.cn/tcmalloc-thread-caching-malloc.html。这个模块可以用来让MySQL在高并发下内存占用更加稳定。再nginx中使用参数–with-google_perftools_module可以使用。再redis里也可使用再make 时使用“make USE_TCMALLOC=yes”。

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月 05

linux系统中的在线安装升级工具

linux安装软件时经常需要依赖其他软件包,使用源码和安装包安装给工作带来很多不便。幸好linux厂商提供了强大的
更新升级工具:
yum用于centos、RedHat和fedora
apt-get用于Ubuntu
pacman用于Arch
安装工具均为单进程,所以只能逐个升级。我收集的主要使用命令如下。

yum 使用
um update packagename #升级包
yum -y upgrade #升级系统中的所有包
yum check-update #显示可升级的软件包
yum clean #删除下载后的旧的header。和clean all相同
yum clean oldheaders #删除旧的headers
yum clean packagename #删除下载后的软件包
yum info #显示可用软件包信息
yum info packagename #显示指定软件包信息
yum install packagename #安装指定软件包
yum list #显示可用软件包
yum list packagename #列出软件包
yum list installed #显示安装了的软件包
yum list updates #显示可升级的软件包
yum provides packagename #显示软件包所包含的文件
yum remove packagename #删除制定的软件包,确认判定指定软件包的依存关系。
yum search 关键字 #利用关键字搜索软件包。搜索对象是,RPM文件名,Packager(包),Dummary,Description的各型

apt-get 使用
apt-cache search packagename #搜索包
apt-cache show packagename #获取包的相关信息
apt-cache depends packagename #了解使用依赖
apt-cache rdepends packagename #查看该包被哪些包依赖
apt-get install packagename #安装包
apt-get install packagename – – reinstall #重新安装包
apt-get -f install #修复安装”-f = ——fix-missing”
apt-get remove packagename #删除包
apt-get remove packagename – – purge #删除包,包括删除配置文件等
apt-get update #更新源
apt-get upgrade #更新已安装的包
apt-get dist-upgrade #升级系统
apt-get dselect-upgrade #使用 dselect 升级
apt-get build-dep packagename #安装相关的编译环境
apt-get source packagename #下载该包的源代码
apt-get autoclean #清理无用的包
apt-get check #检查是否有损坏的依赖

pacman 使用
pacman -S packagename #安装软件包
pacman -R packagename #删除软件包
pacman -Syu #升级系统中的所有包
pacman -Ss package #查询软件包
pacman -Qs package #查询已安装的包
pacman -Sw package #下载但不安装包

(本文为张子萌整理,如有遗漏请提醒)