Archive for the '技术-网络管理' Category


mysql 数据库cpu 占用99.9%问题调优札记

  新公司的系统一直很不稳定,店面销售人员经常报登不上系统或速度奇慢的情况,怀疑可能是代码存在数据库连接泄露及内存泄露现象,离春节只有几天时间,也来不急进行代码调优,只有从配置层面看有那些手段来采用,以便暂且缓解一下服务器压力,降低系统的故障率。为了第一时间能够知道服务器故障,基于nagios搭建了服务器监控程序,这样系统有故障时候,能够用短信方式通知系统故障,及时解决。

1、系统情况:

  操作系统:Redhat AS4

  数据库:mysql 4.1.18

  应用服务器:JBoss 3.2.7

  服务器: 4 x3.00GHz的Intel Xeon CPU

  数据库和应用服务器都部署在同一台服务器上。

  简单跟踪了一下,发现平常内存、io负载都不大,数据库连接数也不多。只是很奇怪的是mysql的cpu负载始终是99.9%,但整个系统的速度还行,开始怀疑是JVM、数据库参数、索引没有优化导致的,因此先着手对java虚拟机参数及数据库参数进行了调整。

2、java虚拟机调优

  • 调整虚拟机的参数

  JAVA_OPTS=”$JAVA_OPTS -Xms512m -Xmx1024m -server -XX:MaxPermSize=300m -XX:MaxNewSize=300m”

  • 调整jboss的数据库连接池,修改最大连接数及连接回收时间

<min-pool-size>20</min-pool-size>

<max-pool-size>300</max-pool-size>

<idle-timeout-minutes>1</idle-timeout-minutes>

<min-pool-size>20</min-pool-size>

3、数据库调优

  • 对所有的表,优化及增加索引。

发现一个好用的mysql工具navicat,感觉比ems 好用,用这东西增加索引方便多了。

  • 调整mysql参数

原来是基于my-medium.cnf 修改的参数,由于担心是大数据量查询sort区等不够及程序存在内存泄露问题,因此基于my-huge.cnf进行调整。

[client]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
[mysqld]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 256
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 32M
thread_concurrency = 8
max_connections=300

#skip-networking

#log-bin

server-id       = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
#safe-updates

[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

调整后,支撑了3天左右,除了mysql的cpu占用始终是99%外,系统整体运行基本正常,忙于其他事情,没有继续跟踪。没想到大年初一接了一堆报警短信,执行查看了系统参数,发现系统竟然没有swap区,欣喜一阵,可能是这原因吧,于是临时建立swap区。

4、增加swap区

  • 在/swap下生成1G的文件

     # mkdir /swap

  # dd if=/dev/zero of=/swap/swapfile bs=500M count=2

  • 创建为swap文件

  #mkswap /swap

  • 让swap生效

  #swapon /swap

  • 查看一下swap

  #swapon -s

  • 把新增的swap文件加到fstab文件中让系统引导时自动启动

  #vi /etc/fstab

  /swap/swapfile swap swap defaults 0 0

    增加后,重启应用及服务,mysql的cpu占用还是持续性为99.9%,而且运行上一段时间还是出现无法登录的情况。远程登录到系统,发现内存、io、swap区占用都很正常,数据库连接数也很正常,而且在停止mysql和jboss后,直接重启jboss,不能正常启动成功,需要等上一会儿,怀疑是文件句柄及tcp连接尚未正常释放。联系以前遇到的情况,怀疑与操作系统允许的最大句柄数有关。

用ulimit -a|grep open 命令查看了结果为:

open files                      (-n) 1024

用cat /proc/sys/fs/file-max查看结果为:

379816

由于数据库和jboss同时部署在同一台服务器上,在负荷较小的情况下用lsof -u root |wc -l查看root用户的句柄数仍然为700多,因此在负荷较高的情况下,用户的最大句柄数1024是有点小。

5、修改操作系统句柄数

5.1、修改操作系统的最大限制数

  • 修改 /etc/sysctl.conf

    增加fs.file-max = 8061540

  • 在/etc/pam.d/login 中添加  

    session     required      /lib/security/pam_limits.so

  • 在/etc/security/limits.conf 中添加
    root              -        nofile           1006154

  修改root用户的句柄数(包括hard和soft)限制为1006154

  • 修改 /etc/rc.local   添加
    echo 8061540 > /proc/sys/fs/file-max

 

5.2、修改用户最大限制数

考虑到重启服务器的风险,先暂时修改一下启动jboss的root用户的/root/.bash_profile,增加如下内容:

ulimit -n 65535

重启jboss和mysql。

连续观察了几天,发现cpu始终占用99.9%的情况解决掉了,继续观察中。

 

6、参考文档

http://www.bea.com.cn/support_pattern/Too_Many_Open_Files_Pattern.html

http://kbase.redhat.com/faq/FAQ_80_1540.shtm

基于nagios的运营监控系统安装配置指南

1 目标

基于nagios及cacti搭建网络及系统监控系统,在系统出现故障时候,第一时间以邮件及手机短信方式通知管理员,及时排查并解决系统故障。

2 相关软件版本说明

操作系统:RedHat AS 4.0

Nagios: nagios 2.10

Nagios Plugins:nagios-plugins-1.4.11

Nagios Core Addons:

NRPE:2.11

NSCA:2.7.2

Apache HTTPD: 2.0.52

安装操作系统时候,建议采用完整的安装方式,需要安装httpd、gd-devel、libpng-devel、libjpeg-devel几个包。

如果需要监控mysql,对mysql用的也是Redhat AS缺省安装

3 nagios安装配置指南

3.1 Nagios系统的结构图

clip_image002[4]

3.2 安装Nagios

3.3 安装nagios的插件

3.3.1 建立需要的目录,并赋予权限

useradd nagios

mkdir /usr/local/nagios

mkdir /usr/local/nagios/libexec

chown -R nagios:nagios /usr/local/nagios

3.3.2 解压nagios的安装包:

tar xzvf nagios-2.10.tar.gz

3.3.3 编译安装nagios

cd nagios-2.10;

./configure –prefix=/usr/local/nagios –with-cgiurl=/nagios/cgi-bin \

–with-htmurl=/nagios –with-nagios-user=nagios –with-nagios-group=nagios;

make all;

make install;

make install-init;

make install-commandmode;

make install-config;

3.3.4 安装Nagios plugins

tar xzvf nagios-plugins-1.4.7.tar.gz

cd /nagios-plugins-1.4.7

./configure –prefix=/usr/local/nagios –with-cgiurl=nagios/cgi-bin \

–with-mysql=/usr –enable-ssl –enable-command-args

make;

make install;

注意:此处要对mysql进行监控,采用的mysql是安装时候AS自带的mysql,安装路径为/usr(包含lib/mysql)

Nagios的安装路径为/usr/local/nagios/libexec,安装成功后确认/usr/local/nagios/libexec下有相关的脚本(例如check_http),如果没有安装到此处,修改resource.cfg中的$USER1$

3.3.5 apache 配置

l 修改/etc/httpd/conf/httpd.conf,在文件末尾添加如下内容:

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

<Directory “/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

</Directory>

Alias /nagios “/usr/local/nagios/share/”

<Directory “/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

</Directory>

l 创建nagios管理员nagiosadmin的用户认证密码文件

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

注意:这里的用户既是apache的nagios管理界面的登录认证用户也和nagios监控中的权限有关联。

l 重启apache使apache的配置生效.

apachectl restart

3.3.6 Nagios配置

在/usr/local/nagios的etc目录下的配置文件有:

Config file Description

cgi.cfg CGI脚本的相关设定,如用户认证等

commands.cfg commands定义文件

localhost.cfg localhost本机的一个配置范例

nagios.cfg nagios的主配置文件

resource.cfg 监控使用到的脚本文件

根据具体使用情况,将配置文件的结构做以下规划,为了方便将来的维护和管理:

配置文件结构如下:

etc/ |– cgi.cfg

|– commands.cfg

|– nagios.cfg

|– resource.cfg

(以上为nagios系统主配置文件)

etc/servers |– contacts.cfg 管理人员和管理人员组的的默认初始化设定文件

|– hosts.cfg 服务器的默认初始化设定文件

|– services.cfg 监控服务的默认初始化设定文件

|– timeperiod.cfg 时间周期默认初始化设定文件

以上为监控服务相关的配置文件,都是由原localhost.cfg文件中拆分出来的,这样方面理解和管理。

l 建立servers目录

mkdir /usr/local/nagios/etc/servers

chown –R nagios.nagios /usr/local/nagios/etc/servers

chown –R 755 /usr/local/nagios/etc/servers

l 设置 cgi.cfg :

authorized_for_system_information=nagiosadmin

authorized_for_configuration_information=nagiosadmin

authorized_for_system_commands=nagiosadmin

authorized_for_all_services=nagiosadmin

authorized_for_all_hosts=nagiosadmin

authorized_for_all_service_commands=nagiosadmin

authorized_for_all_host_commands=nagiosadmin

以上设定nagiosadmin为nagios最高权限,有权查看所有hosts和services的状态.

l 设置nagios.cfg :

#cfg_file=/usr/local/nagios/etc/localhost.cfg

cfg_file=/usr/local/nagios/etc/servers/contacts.cfg

cfg_file=/usr/local/nagios/etc/servers/hosts.cfg

cfg_file=/usr/local/nagios/etc/servers/hostgroups.cfg

cfg_file=/usr/local/nagios/etc/servers/services.cfg

cfg_file=/usr/local/nagios/etc/servers/timeperiods.cfg

cfg_file=/usr/local/nagios/etc/commands.cfg

#cfg_dir=/usr/local/nagios/etc/servers

其他相关的参数参看nagios的手册。

需要特别指出的的地方是参数interval_length=60定义了nagios所有配置文件中与时间相关的参数所采用的缺省单位,缺省为60秒。例如interval_length=60,如果在hosts.cfg中定义notification_interval=1,则表示主机的提醒周期为1分钟。

l 设置resource.cfg

$USER1$=/usr/local/nagios/libexec

l /usr/local/nagios/etc/servers目录下各文件的内容简介(红色标注的是需要修改或要注意的地方):

timeperiod.cfg

###############################################################################

###############################################################################

#

# TIME PERIODS

#

###############################################################################

###############################################################################

# This defines a timeperiod where all times are valid for checks,

# notifications, etc. The classic “24×7″ support nightmare. :-)

define timeperiod{

timeperiod_name 24×7

alias 24 Hours A Day, 7 Days A Week

sunday 00:00-24:00

monday 00:00-24:00

tuesday 00:00-24:00

wednesday 00:00-24:00

thursday 00:00-24:00

friday 00:00-24:00

saturday 00:00-24:00

}

# ‘workhours’ timeperiod definition

define timeperiod{

timeperiod_name workhours

alias “Normal” Working Hours

monday 09:00-17:00

tuesday 09:00-17:00

wednesday 09:00-17:00

thursday 09:00-17:00

friday 09:00-17:00

}

# ‘nonworkhours’ timeperiod definition

define timeperiod{

timeperiod_name nonworkhours

alias Non-Work Hours

sunday 00:00-24:00

monday 00:00-09:00,17:00-24:00

tuesday 00:00-09:00,17:00-24:00

wednesday 00:00-09:00,17:00-24:00

thursday 09:00-17:00

friday 09:00-17:00

}

# ‘none’ timeperiod definition

define timeperiod{

timeperiod_name none

alias No Time Is A Good Time

}

定义各种监控的时间段,如24X7全天候的监控, none不工作时间以及workhours工作和nonworkhours非工作时间段等,在hosts和services的定义中可以引用.

contacts.cfg

###############################################################################

###############################################################################

#

# CONTACTS

#

###############################################################################

###############################################################################

# In this simple config file, a single contact will receive all alerts.

# This assumes that you have an account (or email alias) called

# “nagios-admin” on the local host.

define contact{

contact_name nagiosadmin

alias nagiosadmin

service_notification_period 24×7

host_notification_period 24×7

service_notification_options w,u,c,r

host_notification_options d,r

service_notification_commands notify-by-sms, notify-by-email

host_notification_commands notify-by-sms, host-notify-by-email

email liangchuan@mobile-soft.cn

pager 13910823366

}

define contact{

contact_name nagiosadmin2

alias nagiosadmin2

service_notification_period 24×7

host_notification_period 24×7

service_notification_options w,u,c,r

host_notification_options d,r

service_notification_commands notify-by-sms, notify-by-email

host_notification_commands notify-by-sms, host-notify-by-email

email jincheng.xiao@mobile-soft.cn

pager 13141202288

}

###############################################################################

###############################################################################

#

# CONTACT GROUPS

#

###############################################################################

###############################################################################

# We only have one contact in this simple configuration file, so there is

# no need to create more than one contact group.

define contactgroup{

contactgroup_name nagios

alias nagios

members nagiosadmin,nagiosadmin2

}

定义nagios的管理员成员(contact)和管理员组(contactgroup),以及管理员(contact)的联系方式mail或sms(pager传呼机标识手机号)。

hosts.cfg

###############################################################################

###############################################################################

#

# HOSTS

#

###############################################################################

###############################################################################

# Generic host definition template - This is NOT a real host, just a template!

define host{

name generic-host

notifications_enabled 1

event_handler_enabled 1

flap_detection_enabled 1

failure_prediction_enabled 1

process_perf_data 1

retain_status_information 1

retain_nonstatus_information 1

notification_period 24×7

register 0

}

define host {

use generic-host

host_name 192.168.1.5

address 192.168.1.5

check_command check-host-alive

max_check_attempts 1

notification_interval 1

notification_period 24×7

notification_options d,u,r

contact_groups nagios

}

generic-host定义默认的hosts公共属性。192.168.1.5定义需要监控的服务器信息。

hostgroups.cfg

###############################################################################

###############################################################################

#

# HOST GROUPS

#

###############################################################################

###############################################################################

# We only have one host in our simple config file, so there is no need to

# create more than one hostgroup.

define hostgroup{

hostgroup_name mobilesoft

alias mobilesoft

members 192.168.1.5

}

定义hosts所属的分组,方便监控时的观察.hostgroup_name定义分组名称,alias为别名,members定义成员名称,内容为每台hosts配置文件中定义的host_name内容.

services.cfg

###############################################################################

###############################################################################

#

# SERVICES

#

###############################################################################

###############################################################################

# Generic service definition template - This is NOT a real service, just a template!

define service{

name generic-service

active_checks_enabled 1

passive_checks_enabled 1

parallelize_check 1

obsess_over_service 1

check_freshness 0

notifications_enabled 1

event_handler_enabled 1

flap_detection_enabled 1

failure_prediction_enabled 1

process_perf_data 1

retain_status_information 1

retain_nonstatus_information 1

is_volatile 0

register 0

}

define service{

use generic-service

host_name 192.168.1.5

service_description mobilessoft esales platform

is_volatile 0

check_period 24×7

max_check_attempts 1

normal_check_interval 1

retry_check_interval 1

contact_groups nagios

notification_options w,u,c,r

notification_interval 1

notification_period 24×7

check_command check-mobilesoft!www.mobile-soft.cn

}

generic-service定义默认的services公共属性,在每个service定义中引用.

192.168.1.5定义对最终监控主机192.168.1.5的配置文件,包含每台被监控主机的定义和需要监控的服务.

l 修改/usr/local/nagios/etc/commands.cfg,增加对notify-by-sms及check-mobilesoft的定义

#notify-by-sms

define command {

command_name notify-by-sms

command_line $USER1$/notify_by_sms $HOSTADDRESS$ $CONTACTPAGER$

}

#check-mobilesoft

define command {

command_name check-mobilesoft

command_line $USER1$/check_mobilesoft $ARG1$

}

l 创建check_mobilesoft 的plugin脚本

在/usr/local/nagios/libexec下创建check_mobilesoft

touch check_mobilesoft

chown nagios.nagios check_mobilesoft

chmod 755 check_mobilesoft

其中check_mobilesoft的内容如下:

#!/bin/sh

#use the nagios check_http shell to check the monitored machine has some problem

PROGNAME=`basename $0`

PROGPATH=`echo $0 | sed -e ’s,[\\/][^\\/][^\\/]*$,,’`

. $PROGPATH/utils.sh

hostip=$1

result=`/usr/local/nagios/libexec/check_http -H ${hostip} –u /test –s “success” -w 0.0009 -c 0.0040 -t 10 |grep “200 OK”`

if [ -z “${result}” ] ; then

exit $STATE_CRITICAL

else

exit $STATE_OK

fi

注意此处调用了libexec/utils.sh,此脚本文件定义了诸如返回码等变量及一些工具。

l 创建notify_by_sms 的plugin脚本

在/usr/local/nagios/libexec下创建notify_by_sms

touch notif_by_sms

chown nagios.nagios notify_by_sms

chmod 755 notify_by_sms

其中notify_by_sms的内容如下:

#!/bin/sh

#use the nagios check_http shell to check the monitored machine has some problem

#if having problem ,then use the sms gateway to notify the admin

hostip=$1

phonenumber=$2

result=`/usr/local/nagios/libexec/check_http -H ${hostip} -w 0.0009 -c 0.0040 -t 10 |grep “200 OK”`

if [ -z “${result}” ] ; then

/usr/bin/wget “http://my-sms-ip:8800/Send%20Text%20Message.htm?PhoneNumber=${phonenumber}&Text=${hostip}%20machine%20Have%20Problem”

fi

l 检查配置文件正确性

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

执行该命令检查所有配置文件是否正确,如有问题,继续修改配置文件。

l 配置开机时候自动启动nagios及apache服务

chkconfig –add nagios

chkconfig –add httpd

或者直接修改/etc/rc.local,增加如下内容

/sbin/service nagios start

/usr/sbin/ apachectl start

l 启动nagios服务及apache 服务

service nagios restart

apachectl restart

l 登录nagios管理界面,监控服务器情况

访问nagios的服务器web界面http://ip/nagios,输入nagiosadmin 的用户名及密码,开始进行监控。

3.3.7 客户端监控代理NRPE的安装配置:

<TBC>

4 cacti安装配置指南

<TBC>

5 参考文档

http://nagios.sourceforge.net/docs/2_0/toc.html

http://gentoo-wiki.com/HOWTO_Install_Nagios

Technorati 标签: ,,,,