正在浏览 系统监控 里的文章

前段时间写的一个脚本,准备大规模的应用在nagios中,当nagios需要检查的服务超过3000,硬件差一点的机器负责这些服务的检查就有些力不从心了,因此被动检测以及nagios的分布式就显得更加重要了。下面是脚本详情

#!/bin/bash

# Name  : 被动检查统一脚本
# CTime : 2012-04-13

# =====================公共变量=============================

NagiosPath="/usr/local/nagios/"
NSCABin=${NagiosPath}"bin/send_nsca"

NSCAHost="192.168.8.200"
NSCAPort="5667"
NSCAConfig=${NagiosPath}"etc/send_nsca.cfg"

Date=`date +%Y%m%d%H`
InfoFile=/logs/dmesg/${Date}.$$
HostName="bzweb"

# check_sys_files variable
MD5FILE="/home/okooo/apps/nagios/libexec/sys_files_md5.conf"
MD5SUMBIN="/usr/bin/md5sum"

# check_load variable
LoadWarning="3,3,3"
LoadCriticl="4,4,4"
LoadBin=${NagiosPath}"libexec/check_load -w $LoadWarning -c $LoadCriticl"

# check_disk variable
DiskWarning="20%"
DiskCriticl="10%"
DiskBin=${NagiosPath}"libexec/check_disk -w $DiskWarning -c $DiskCriticl -A -i /dev/shm"

# check_openmanage variable
OpmBin=${NagiosPath}"libexec/check_openmanage -b ctrl_fw=ALL\/ctrl_driver=ALL\/bat=ALL -p"

# check_jmx variable
JMXBin=${NagiosPath}"libexec/check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:"
JMXPar="/jmxrmi -O java.lang:type=Memory -A HeapMemoryUsage -K used -I HeapMemoryUsage -J used -v"
# =======================================================
# 函数名  : getResult
# 功能    : 得到检查结果的函数
# 参数列表
# 参数1   : 待执行的检查命令
# 参数2   : 检查结果的状态字符再哪列
# 参数3   : 检查类型,目前包含: jmx
# 参数4   :对应参数3中对应的附加信息
function getResult()
{
if [ "$3" == "jmx" ];then
        Info=`${JMXBin}$4${JMXPar}`
        Result=$Info
        State=`echo $Info | awk '{print $'''$2'''}'`
else
        Info=`$1`
        Result=`echo $Info|cut -d '|' -f 1`
        Profdata=`echo $Info|cut -d '|' -f 2`
        State=`echo $Info | awk '{print $'''$2'''}'`
fi
}

# 函数名  : SendResult
# 功能    : 发送被动检查结果
# 参数列表
# 参数1   : 被检查的服务名
function SendResult()
{

case $State in
"OK")
    echo -e "$HostName\t$1\t0\t$Result|$Profdata" | ${NSCABin} -H $NSCAHost -c $NSCAConfig;;
"WARNING")
    echo -e "$HostName\t$1\t1\t$Result|$Profdata" | ${NSCABin} -H $NSCAHost -c $NSCAConfig;;
"CRITICAL")
    echo -e "$HostName\t$1\t2\t$Result|$Profdata" | ${NSCABin} -H $NSCAHost -c $NSCAConfig;;
*)
    echo -e "$HostName\t$1\t3\t$Result" | ${NSCABin} -H $NSCAHost -c $NSCAConfig;;
esac

}
# check_jmx function
function check_jmx()
{
        ServiceName=$1
        ServicePort=$2
        getResult null 1 jmx $ServicePort
        SendResult $ServiceName
}

# check_dmesg function
function check_dmesg()
{

/bin/dmesg |grep -v 'Treason uncloaked\|NFS: Buggy server\|blocks\|heads\|^$' >$InfoFile

Messages=`cat $InfoFile|head -1 |awk '{print substr($0,1,20)}'`

if [ "`cat $InfoFile`" == "" ];then
   echo -e "$HostName\tcheck_dmesg\t0\tOK" | ${NSCABin} -H $NSCAHost -c $NSCAConfig
   rm -rf $InfoFile
elif
   cat $InfoFile | egrep -v "not|warning|drop|Down|error|dis|high|hot|disconnect|promiscuous" >/dev/null 2>&1
   [ $? == 0 ] ;then
   echo -e "$HostName\tcheck_dmesg\t1\tWARNING,内容包含'$Messages' "| ${NSCABin} -H $NSCAHost -c $NSCAConfig
   /bin/dmesg -c

else
   echo -e "$HostName\tcheck_dmesg\t2\tCRITICAL,内容包含'$Messages'"| ${NSCABin} -H $NSCAHost -c $NSCAConfig
   /bin/dmesg -c
fi

}

# check_sys_files function
function check_sys_files()
{

Result=`$MD5SUMBIN -c $MD5FILE 2>/dev/null | grep FAILED`

if [ "$Result" != "" ];then
        echo -e "$HostName\tcheck_sys_files\t2\t$Result" | ${NSCABin} -H $NSCAHost -c $NSCAConfig
else
        echo -e "$HostName\tcheck_sys_files\t0\tSystem File is OK." | ${NSCABin} -H $NSCAHost -c $NSCAConfig
fi
}

# CPU Load
function check_load()
{

getResult "$LoadBin" "1"

SendResult "CPU Load"
}

function check_disk()
{
getResult "$DiskBin" "2"

SendResult "check_disk"
}


# check_openmanage Dell硬件检查
function check_openmanage()
{
getResult "OpmBin" "1"

SendResult "check_openmanage"
}

#============= Main ============

while getopts Amplsdj: OPTION
do
case $OPTION  in
A)
        check_dmesg
        check_sys_files
        check_load
        check_disk
        check_openmanage;;
m)
        check_dmesg;;
p)
        check_disk;;
l)
        check_load;;
s)
        check_sys_files;;
d)
        check_openmanage;;
j)
        for jmxinfo  in $OPTARG
        do
                eval $(echo $jmxinfo | awk -F : '{ printf("type=%s\nport=%s\n",$1,$2); }')
                check_jmx $type $port
        done;;
*)
        echo -e "please input correct parameter:
                  Usage: $0 -A | -[m p l s d]
                  -A check all services
                  -m check_dmesg
                  -p check_disk
                  -l check_load
                  -s check_sys_file
                  -d check_openmanage"

esac
done

今天 帮群里一兄弟配了下nagios上的飞信,这个东西 我个人感觉还是很实用的,不过好久没配了,今天配置了一遍,顺便 就把过程记录下来了,供大家学习!!!

一、安装飞信

cd /root/tools
wget http://ebook.elain.org/tools/fetion20101205.tar.gz

安装步骤略,软件包中有
添加要接收报警短信的手机号为飞信好友

/usr/local/fetion/fetion --mobile=137xxxxxxxx --pwd=1111111 --to=138xxxxxxxx --msg-type=1 --msg-utf8="The Fetion test is ok"

注:第一次要输入验证码,生成验证码后,另开一终端把图片下载下来,打开查看,注意区分大小写!!!

二、vi commands.cfg  添加

define command{
command_name notify-host-by-fetion
command_line /usr/local/fetion/fetion --mobile=137xxxxxxxx --pwd=1111111 --to=138xxxxxxxx --msg-type=1 --msg-utf8="** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
}

define command{
command_name notify-service-by-fetion
command_line /usr/local/fetion/fetion --mobile=137xxxxxxxx --pwd=1111111 --to=138xxxxxxxx --msg-type=1 --msg-utf8="**$NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$ **"
}

继续阅读

一、简介

通过将近2周的了解,个人认为munin属于轻量级的性能展示工具,相对于cacti其部署、展示、添加编辑插件都很简洁,如果拿来主义使用 Munin Exchange 提供的插件建立自己的监控项目那就更简单了。按照Munin官方的介绍,其最初是用来对网络进行监控,目前Munin Exchange 的插件库已经扩展到各个服务领域,通过官方的文档也很方便的写自己的插件。先来看一下安装后默认几个监控图:


网卡流量


中断与上下文切换
继续阅读

1. 安装中文字体

yum install fonts-chinese -y

2.设置cacti使用中文字体调用rrdtool

在cacti的WEB界面选择:Console -> Cacti Settings -> Paths 标签中的:

RRDTool Default Font Path: /usr/share/fonts/chinese/TrueType/ukai.ttf

这样Devices中的主机名,图形显示的标签可以使用中文了。

ZABBIX 简介

抢沙发

ZABBIX是由Alexei Vladishev开发的网络管理系统,其目的是监测和跟踪各种网络服务、服务器的状态和其他网络硬件。
它使用MySQL、PostgreSQL、SQLite或Oracle存储数据。其后端用C语言编写,网页前端使用PHP编写的。ZABBIX的监测提供了多种方式。简单的检查无需安装任何监控主机软件,可以监测如SMTP或HTTP等标准服务的可用性和响应能力。ZABBIX代理可以安装在UNIX和 Windows主机上,可以监控如CPU负载、网络利用率、磁盘空间等信息。在未在主机上安装ZIBBIX代理程序的情况下,ZIBBIX可以通过 SNMP协议、TCP和ICMP检查、IPMI和自定义参数来实现对主机状态的检查。ZABBIX支持多种实时通报机制,包括XMPP协议。

ZABBIX根据GNU通用公共许可证的第二版发布,是一个自由软件。

ZABBIX是什么?

ZABBIX项目由Alexei Vladishev创建,目前由ZABBIX SIA负责开发和技术支持。

ZABBIX是一个企业级开源分布式网络监控解决方案。

ZABBIX可以监控网络的众多参数以及服务器的健康和完整性。ZABBIX采用灵活的报警机制,允许用户配置以电子邮件为基础的几乎所有事件报警,可以使维护人员快速响应服务器故障。ZABBIX提供基于所存储数据的报表和数据可视化特性,这使得ZABBIX成为复合规划的理想选择。

ZABBIX支持轮询和陷阱两种方式。ZABBIX所有的报告、统计、参数配置都可以通过一个基于WEB的前端程序访问。基于WEB的前端程序可以保证您可以任何地方了解您网络的状态和服务器的健康情况。通过正确配置,ZABBIX可以在监控IT基础设施方面发挥重要作用。这同样适用于只有几台服务器的小型企业,和有大量服务器的大公司。
ZABBIX是免费的,它基于GPL通用公共许可证第二版开发和发行,这意味着ZABBIX的源代码免费发布并对一般公众发布。免费及收费支持均由ZABBIX公司提供。

ZABBIX都能做什么?

ZABBIX可以实现以下功能:

*      自动发现服务器及网络设备
*      分布式监控及集中WEB管理
*      支持轮询和陷阱两种机制
*      服务端软件支持Linux, Solaris, HP-UX, AIX, Free BSD, Open BSD, OS X等操作系统
*      本地高性能代理程序 (代理程序支持Linux, Solaris, HP-UX, AIX, Free BSD, Open BSD, OS X, Tru64/OSF1, Windows NT4.0, Windows 2000, Windows 2003, Windows XP, Windows Vista等操作系统)
*      无代理监控
*      加密的用户认证机制
*      灵活的用户权限管理
*      基于WEB的用户界面
*      灵活的预定义时间电子邮件通知
*      对监控资源的高层次(业务)视图
*      审计日志

为什么使用ZABBIX?

*      开源的解决方案
*      基于UNIX和WIN32平台的高性能代理程序
*      较低的学习曲线low learning curve
*      高投资回报(停机时间是非常昂贵的)
*      较低的拥有成本
*      很简单的配置
*      集中监控系统,所有信息(配置,性能数据)存储在关系数据库
*      高级服务树
*      非常容易安装
*      支持SNMP (v1,v2)的轮询和陷阱.
*      可视化能力
*      内置的内务处理程序

谁在用ZABBIX?

世界各地不同大小的很多企业都信赖ZABBIX,并使用它作为主要的网络监控平台。

新买的dell 服务器默认情况下都带有 omsa  服务器管理程序安装盘,linux,windows各个系统的版本都有,安装起来也比较方便。在之前本站已经介绍过了利用dell yum源安装omsa的方法。安装好omsa后程序会提供https的1311端口供用户通过浏览器登陆管理页面,查看哪些硬件出了问题。但如果服务器成百上千台,这样管理的话非常费时费力。下面介绍利用nagiso的omsa插件来监控dell服务器的硬件情况。

下载插件
登陆http://folk.uio.no/trondham/software/check_openmanage.html 下载最新版本

# wget http://folk.uio.no/trondham/software/files/check_openmanage-3.5.1.tar.gz
# tar -xvzf  check_openmanage-3.5.1.tar.gz
# ll check_openmanage-3.5.1
total 3524
-rw-r--r-- 1 45150 55150   15379 Oct 22 17:39 CHANGES
-rwxr-xr-x 1 45150 55150  133625 Oct 22 17:09 check_openmanage
-rw-r--r-- 1 45150 55150   24065 Oct 22 17:09 check_openmanage.8
-rwxr-xr-x 1 45150 55150 3290403 Oct 22 17:12 check_openmanage.exe
-rw-r--r-- 1 45150 55150    5304 Oct 22 17:09 check_openmanage.php
-rw-r--r-- 1 45150 55150   16269 Oct 22 17:09 check_openmanage.pod
-rw-r--r-- 1 45150 55150    3988 Oct 22 17:09 check_openmanage.spec
-rw-r--r-- 1 45150 55150   35147 Oct 22 17:09 COPYING
-rw-r--r-- 1 45150 55150     533 Oct 22 17:09 INSTALL
-rwxr-xr-x 1 45150 55150     406 Oct 22 17:09 install.bat
-rwxr-xr-x 1 45150 55150    1082 Oct 22 17:09 install.sh
-rw-r--r-- 1 45150 55150    2727 Oct 22 17:09 README

压缩包中有2个版本的程序分别是for linux和for windows
安装
check_openmanage组件不用编译,解压后就可以使用,但有3个前提:
1、要监控的服务器一定是dell服务器
2、被监控的服务器一定先安装好dell 的omsa程序
3、nagiso已经安装完毕并正常运行

将check_openmanage 复制到nagios的插件目录
# cp check_openmanage-3.5.1/check_openmanage

check_openmanage 有两种方式获得dell服务器硬件信息,分别为本地运行获得和通过snmp方式获得。由于我在linux通过yum安装omsa时snmp的设置有些问题,因此linux系统下我打算使用nrpe插件调用的方式监控。

配置nagios(监控linux)
利用nrpe插件监控linux服务器
nagios监控端

check_omsa服务定义文件
# vi /usr/local/nagios/etc/objects/Dell_OMSA/dell_service_linux.cfg
define service {
host_name                       sns001
service_description             check_omsa
use                             linux-web-service
hostgroup_name                  DellLinuxHosts
check_command                   check_nrpe!check_omsa
_ser_info                       dell omsa
check_interval                  10
notification_options            c,r
}

主机组定义文件
define hostgroup{
hostgroup_name          DellLinuxHosts
alias                   dell Linux 服务器组
members                 sns001,snsdb001,rms001,rmsdb001,opt-001
}

nagios 主配置文件修改
# vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/Dell_OMSA/dell_service_linux.cfg

被控端nrpe配置

# vi /usr/local/nagios/etc/nrpe.cfg
添加
command[check_omsa]=/usr/local/nagios/libexec/check_openmanage -b ctrl_fw=ALL\/ctrl_driver=ALL -p
# service nrped restart

配置nagios(监控windows)
通过snmp方式监控windows服务器

# vi /usr/local/nagios/etc/objects/Dell_OMSA/dell_service_win.cfg
define service {
host_name                       heidrick
service_description             check_omsa
use                             win-rrd-service
hostgroup_name                  DellWinHosts
check_command                   check_omsa4win
_ser_info                       dell omsa
check_interval                  10
notification_options            c,r
}

nagios 主配置文件修改
# vi /usr/local/nagios/etc/nagios.cfg
添加
cfg_file=/usr/local/nagios/etc/objects/Dell_OMSA/dell_service_win.cfg

如果nagios安装了pnp4nagios插件的话,还可以显示出check_openmanage监测出的服务器风扇转速和机箱温度,如下图
check_omsa

详细资料可以参加:http://folk.uio.no/trondham/software/check_openmanage.html

Nagios配置文件分布图

Restart Windows Failed Service batch script with log.


File: win_service_restart.cmd
Author: Vadims Zenins http://vadimszenins.blogspot.com
Version: 1.04
Date: 20/04/2009 17:41
Windows Failed Service restart batch file for Nagios Event Handler
Copy win_service_restart.cmd to \NSClient++\scripts\ folder.
Nagios commands.cfg:
define command{
command_name win_service_restart
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -c win_service_restart -a "$SERVICEDESC$" $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}
Nagios template-services_common-win.cfg
define service{
name generic-service-win-wuauserv
service_description wuauserv
display_name Automatic Updates
event_handler win_service_restart
event_handler_enabled 1
check_command check_nt!SERVICESTATE!-d SHOWALL -l $SERVICEDESC$
}
NSCLIENT++ NSC.ini:
[NRPE]
allowed_hosts=192.168.1.1/32 ; your Nagios server IP
allow_arguments=1
[External Script]
allow_arguments=1
allow_nasty_meta_chars=1
[NRPE Handlers]
command[win_service_restart]=scripts\win_service_restart.cmd "$ARG1$" $ARG2$ $ARG3$ $ARG4$
Version 1.04 revision:
Double restart of the servise is fixed
Version 1.03 revision:
Description is changed
Version 1.02 revision:
@NET changed to @SC
Version 1.01 revision:
Service name's with spase problem is fixed
http://vadimszenins.blogspot.com/2008/12/nagios-restart-windows-failed-services.html
md5: fd00753533e5fb655d824c3bf1d36d4f *win_service_restart.zip

Windows Failed Service restart batch file
Vadims Zenins
Tue, 20 Oct 2009 13:35:38 GMT

nginx的性能远远优于apache,但由于nagios的web界面中包含php和c-cgi程序,因此需要两套fcgi管理工具(并非必须)和两套解释器(必须)。php用php-cgi跑就可以,c-cgi我选用fcgiwrap。下面介绍安装/配置步骤。

环境介绍:

先前环境:apache+php+nagios 除了nagios 其余的都使用yum自动安装
目标环境:nginx+php5.2.10(php-fpm patch)+nagios 均为源码编译安装

php-fpm:是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi
Spawn-fcgi:是lighttpd的一个分支项目,是一个cgi进程的管理器

● php 打php-fpm补丁,编译时启用--enable-fastcgi --enable-fpm 参数,使用php-fpm管理php-cgi。php安装详细步骤参见 张宴文章:http://blog.s135.com/nginx_php_v5/
● c-cgi 使用 Spawn-fcgi 管理 ,利用fcgiwrap驱动。fcgiwrap 介绍参见 http://nginx.localdomain.pl/wiki/FcgiWrap

php-cgi 监听 127.0.0.1:9000
fcgiwrap 监听 127.0.0.1:10000

nagios 安装配置不是本文重点,略过。web 目录如下:
/usr/local/nagios/share

安装配置

yum install fcgi fcgi-devel -y

php-fpm 的配置与运行这里略过,下面主要讲解Spawn-fcgi+fcgiwrap 的安装与配置

spawn-fcgi

# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
# tar -xvzf spawn-fcgi-1.6.2.tar.gz
# cd spawn-fcgi-1.6.2/
# ./configure
# make
# cp src/spawn-fcgi /usr/local/bin/

fcgiwrap

# wget http://github.com/gnosek/fcgiwrap/tarball/master
# tar -xvzf gnosek-fcgiwrap-28ac6f9aa5e7dadf9aaf2062ab003a0fb4c82ad8.tar.gz
# cd gnosek-fcgiwrap-28ac6f9aa5e7dadf9aaf2062ab003a0fb4c82ad8/
# make
# mv fcgiwrap /usr/local/bin/

创建一个shell脚本来用spawn-fcgi 启动fcgiwrap实例
# vi /usr/local/bin/c-fcgi.sh

#!/bin/sh
/usr/local/bin/spawn-fcgi -f /usr/local/bin/fcgiwrap -a 127.0.0.1 -p 10000 -F 3 -P /var/run/fastcgi-c.pid -u www -g www

# chmod +x /usr/local/bin/c-fcgi.sh

创建一个系统启动进程,方便使用service 和chkconfig 命令管理
# vi /etc/init.d/c-fcgi

#!/bin/bash
# c-fcgi - this script starts and stops the fcgiwrap instance
#
# chkconfig:   - 96 28
# description:  c-fcgi
# processname: c-fcgi

C_SCRIPT=/usr/local/bin/c-fcgi.sh

RETVAL=0

case "$1" in
start)
echo "Starting fastcgi"
$C_SCRIPT
RETVAL=$?
;;
stop)
echo "Stopping fastcgi"
killall -9 fcgiwrap
RETVAL=$?
;;
restart)
echo "Restarting fastcgi"
killall -9 fcgiwrap
$C_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: c-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
# <span style="color: #0000ff;">chkconfig --add c-fcgi</span>
# <span style="color: #0000ff;">chkconfig c-fcgi on</span>
# <span style="color: #0000ff;">service c-fcgi start</span>

验证启动,是否提供了相应的端口
# netstat -tulnp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      32629/php-cgi
tcp        0      0 127.0.0.1:10000             0.0.0.0:*                   LISTEN      20039/fcgiwrap

# ps -ef | grep fcgiwrap | grep -v grep

www      20039     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
www      20040     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
www      20041     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap

# ps -ef | grep php-cgi | grep -v grep

root     32629     1  0 13:37 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
.
.
.
www      32642 32629  0 13:37 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf

我php-fpm配置启动32个php-cgi 实例

配置nginx

user                www www;
worker_cpu_affinity 0001 0010 0100 1000;
worker_processes        4;

# 可以在下方直接使用 [ debug | info | notice | warn | error | crit ]  参数
error_log   /usr/local/nginx/logs/nginx_error.log   crit;

pid     /var/run/nginx.pid;

#进程所能打开的最大文件描述符个数
worker_rlimit_nofile    51200;

events
{
# linux 2.6+ 版本以上的内核推荐使用 epoll 事件模式
use             epoll;

# 最大连接数=worker_processes*worker_connections/4
worker_connections  4096;
}

http
{
include     ./conf.d/mime.types;
default_type    application/octet-stream;

charset utf-8;

server_names_hash_bucket_size   128;
client_header_buffer_size       32k;
large_client_header_buffers     4   32k;
client_max_body_size            8m;

sendfile        on;
tcp_nopush  on;

keepalive_timeout   60;

tcp_nodelay     on;

# global fastcgi configure
include         ./conf.d/fastcgi_custom.conf;

# include fastcgi_params.conf
include               ./conf.d/fastcgi_params.conf;

# gzip configure
include         ./conf.d/gzip.conf;

#limit_zone  crawler  $binary_remote_addr  10m;

server
{
listen          80;
server_name     cacti.opt.jobkoo.com;
index           index.html index.htm index.php;
root            /data/www/cacti;

#limit_conn   crawler  20;

location ~ .*\.(php|php5)?$
{
fastcgi_pass        127.0.0.1:9000;
fastcgi_index       index.php;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires     30d;
}

location ~ .*\.(js|css)?$
{
expires     1h;
}

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log  /usr/local/nginx/logs/access.log  access;
}

server
{
listen          80;
server_name     nagios.opt.jobkoo.com;
index           index.html index.htm index.php;
root            /usr/local/nagios/share/;

auth_basic      "Welcome to Jobkoo Nagios Monitor System!";
auth_basic_user_file    ./conf.d/htpasswd;

# 这个rewrite针对的是pnp插件的鼠标移动到图标后显示的浮动窗
rewrite     ^/nagios/pnp/(.*)\.php /pnp/$1.php break;

# 以 cgi 结尾的文件
location ~ \.cgi$ {
root    /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;

fastcgi_pass   127.0.0.1:10000;
fastcgi_index  index.cgi;

}

# 以 php/php5 结尾的文件
location ~ \.(php|php5)+$
{
fastcgi_pass        127.0.0.1:9000;
fastcgi_index       index.php;
}

log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log      /usr/local/nginx/logs/nagios.access.log  wwwlogs;
}

server
{
listen          80;
server_name     status.apt.jobkoo.com;

location / {
stub_status     on;
access_log      off;
}
}
}

/etc/nginx/conf.d 目录下的cgi 参数配置文件
#vi /etc/nginx/conf.d/fastcgi_custom.conf

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

# vi /etc/nginx/conf.d/fastcgi_params.conf

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

设置nagios页面身份验证
# htpasswd -c /etc/nginx/conf.d/htpasswd admin
# cat /etc/nginx/conf.d/htpasswd
admin:QqbxsY3jdkOpQ

配置nagios

#vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1

# service nagios start
# service nginx start

访问nagios页面
nagios_nginx.png

原创文章,转载请注明: 转自 http://salogs.com

LVS的监控

已有 2 条评论

一、利用ipvsadm命令

通常情况下lvs的运行情况以及资源使用情况是无法使用top或vmstat命令时来查看的。对于LVS-DR结构的LVS来说,其只会转发网络包。几乎不会耗费CPU资源,这时我们查看系统负载应该为0.00。

通过使用ipvsadm命令可以查看当前LVS的运行情况,如下:

[root@LVS-Master]# ipvsadm
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost:https wlc persistent 50
-> localhost:https Route 1 0 0
-> localhost:https Route 1 0 0
TCP localhost:http wlc persistent 50
-> localhost:http Route 1 0 0
-> localhost:http Route 3 0 0

当有新连接过来时,其相关的计数就会增加。

查看连接数/进入包(字节)/输出包(字节):

命令:ipvsadm -Ln --stats -t |u|f service-address
-t, --tcp-service
-u, --udp-service
-f, --fwmark-service
-L, -l, --list List the virtual server table if no argument is specified.
-n, --numeric Numeric output.
--stats Output of statistics information.

示例:

[root@experiment ~]# ipvsadm -Ln --stats -t 192.168.108.180:80
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.108.180:80 5771754 101697K 0 5338M 0
-> 192.168.108.162:80 454297 13736280 0 761572K 0
-> 192.168.108.161:80 5317457 87961304 0 4576M 0

另外通过查看cat /proc/net/ip_vs_stats 也可以查看lvs信息,只不过这里的输出时16进制的。

[root@LVS-Master] # cat /proc/net/ip_vs_stats
Total Incoming Outgoing         Incoming         Outgoing
Conns  Packets  Packets            Bytes            Bytes
594BFC  6335751        0        145F90EC1                0

Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s
0              0             0                     0                     0

二、利用第三方软件以图形的形式查看LVS信息

网上查找了很多资料,只找到了2种比较简单的方法来实现LVS的图形化监控;
1、利用net-snmp-lvs-module 给net-snmp加个模块,然后配合cacti的相关模板来实现监控。由于针对64位系统的net-snmp-lvs-module只支持到rhel4因此放弃使用。

相关网址:net-snmp-lvs-module-0.0.4.tar.gz

2、利用lvs_rrd工具来生成图形
lvs_rrd 工具实现了网页的形式来查看lvs状态图功能。其主要有两个脚本组成:信息收集脚本和图像绘制脚本。信息收集脚本是将lvs的信息生成rrd格式的数据文件,然后利用图像绘制脚本生成图像,并生成一个php页面,这个页面中引用其所生成的图像,这样我们可以通过web页面的形式查看生成的php页面,就可以时时的查看lvs的状态信息了。

lvs_rrd部署
lvs_rrd需要部署在LVS-Master和LVS-Backup上,更准确的说lvs_rrd中的信息收集脚本一定要在LVS director 上运行。通过配置图像生成脚本和图像的生成目录,我们可以将他们时时的复制到其他的服务器中。这里为了方便起见我将它们部署到了LVS director上。

下面简单的介绍部署的步骤

(1).开启LVS director 的apache,配置相关参数,注意端口一定不能为80,我这里修改为9160。指定DocumentRoot 为 "/data/web"
(2).下载lvs_rrd软件。下载地址:lvs-rrd-v0.7.tar.gz
(3).将lvs-rrd-v0.7.tar.gz解压后将文件夹复制到/data/web/目录下并更名为lvs
(4).配置lvs.rrd.update文件
RRDTOOL="/usr/bin/rrdtool"
IPVSADM="/sbin/ipvsadm"
WORKDIR="/data/web/lvs"

配置graph-lvs.sh
WORKDIR="/data/web/lvs"
RRDTOOL="/usr/bin/rrdtool"
GRAPHS="$WORKDIR/graphs"
WEBPATH="/lvs/graphs"

这两个文件根据自己的情况设置一下就可以了。

(5). 将/data/web/lvs/graphs目录修改为apache可读可写。
# chown apache.apache /data/web/lvs/graphs -R
(6).将收集信息的脚本添加到计划任务中
# crontab -e
* * * * * /data/web/lvs/lvs.rrd.update 2> /dev/null > /dev/null

(7).等1分钟后,看lvs目录中是否生成了以rrd为扩展名的文件。如果有的话就可以启动apache通过http://ipaddress:9160/lvs/查看lvs的状态了。如下图:

上面的三张图是lvs_rrd官方给的,由于我搭建的是lvs的测试环境,没有实际使用那么多连接,生成的图像也不是很好看,在这里就贴一张吧:

原创文章,转载请注明: 转自 http://salogs.com