Nagios 被动模式

Wednesday, January 27, 2010

背景:公司有台内网的机器需要通过外网的Nagios来监控,并发送报警信息。

前提:内网部署的Nagios主机可以连外网。

方案:在内网部署Nagios服务器,并通过NSCA Client端发送消息到外网Nagios主机。

步骤:
1、在外网的Nagios服务器上安装NSCA服务端,并开启NSCA服务端,监听端口为5667
2、配置nagios.cfg文件修改
accept_passive_service_checks=1
accept_passive_host_checks=1
3、配置commond.cfg添加
# 'check_dummy' command definition
define command{
    command_name check_dummy
    command_line $USER1$/check_dummy $ARG1$
}
4、定义被动模式的服务器模板
define host {
    name    passive_host
    check_period    24x7
    check_command   check_dummy!2
    contact_groups  nagiosadmin
    notification_period 24x7
    check_interval  5
    retry_interval  1
    max_check_attempts  10
    active_checks_enabled   0
    passive_checks_enabled  1
    obsess_over_host    1
    event_handler_enabled   1
    low_flap_threshold  0.000000
    high_flap_threshold 0.000000
    flap_detection_enabled  1
    flap_detection_options  o,d,u
    freshness_threshold 0
    check_freshness 0
    notification_options    d,u,r
    notifications_enabled   1
    notification_interval   30
    stalking_options    n
    process_perf_data   1
    failure_prediction_enabled  1
    retain_status_information   1
    retain_nonstatus_information    1
check_freshness 1
freshness_threshold 600
    register    0
    }
5、定义被动模式的服务模板
define service {
    name   passive_service
    check_period    24x7
    check_command   check_dummy!2
    contact_groups  nagiosadmin
    notification_period 24x7
    check_interval  10
    retry_interval  2
    max_check_attempts  3
    parallelize_check   1
    active_checks_enabled   0
    passive_checks_enabled  1
    obsess_over_service 1
    event_handler_enabled   1
    low_flap_threshold  0.000000
    high_flap_threshold 0.000000
    flap_detection_enabled  1
    flap_detection_options  o,w,u,c
    freshness_threshold 0
    check_freshness 0
    notification_options    u,w,c,r
    notifications_enabled   1
    notification_interval   10
    stalking_options    n
    process_perf_data   1
    failure_prediction_enabled  1
    retain_status_information   1
    retain_nonstatus_information    1
check_freshness 1
freshness_threshold 600
    register    0
    }
注:
# 服务对象定义里的check_freshness选项设为1,这将打开针对该服务的"刷新检测"特性;
# 服务对象定义里的freshness_threshold选项须设定为一个以秒为单位的数值,该值反应出由分布式服务器所提供的检测数据将应该以什么样频度来提供出来,一般是分布式服务器normal——check_interval的2倍;

6、配置内网的nagios服务器只是发消息到外网nagios服务器上,所以并不需要apache而且只需要编译一下nsca的安装包,并把src目录下的send_nsca复制到nagios plugins的目录,sample-config目录下的send_nsca.cfg复制到nagios的etc目录下
7、配置内网nagios的nagios.cfg修改
obsess_over_services=1
ocsp_command=submit_check_result
obsess_over_hosts=1
ochp_command=submit_host_alive
enable_notifications=0
8、配置commond.cfg添加
# 'submit_check_result' command definition
define command{
        command_name submit_check_result
        command_line $USER1$/submit_check_result.sh $HOSTNAME$ '$SERVICEDESC$' $SERVICESTATE$ '$SERVICEOUTPUT$' '$SERVICEPERFDATA$'
}
# 'submit_host_alive' command definition
define command{
        command_name submit_host_alive
        command_line $USER1$/submit_host_alive.sh $HOSTNAME$ $HOSTSTATE$ '$HOSTOUTPUT$' '$HOSTPERFDATA$'
}
9、建立$USER1$/submit_check_result.sh并修改权限为755,修改执行用户和组为nagios
#!/bin/sh

# Arguments:
# $1 = host_name (Short name of host that the service is
# associated with)
# $2 = svc_description (Description of the service)
# $3 = state_string (A string representing the status of
# the given service - "OK", "WARNING", "CRITICAL"
# or "UNKNOWN")
# $4 = plugin_output (A text string that should be used
# as the plugin output for the service checks)
# $5 = perdata

# Convert the state string to the corresponding return code
return_code=-1

case "$3" in
OK)
return_code=0
;;
WARNING)
return_code=1
;;
CRITICAL)
return_code=2
;;
UNKNOWN)
return_code=-1
;;
esac

# pipe the service check info into the send_nsca program, which
# in turn transmits the data to the nsca daemon on the central
# monitoring server

/usr/bin/printf "%b" "$1\t$2\t$return_code\t$4|$5 \n" | /usr/local/nagios/libexec/send_nsca 外网nagios地址 -c /usr/local/nagios/etc/send_nsca.cfg
10、建立submit_host_alive.sh并修改权限为755,修改执行用户和组为nagios
#!/bin/sh

# Arguments:
# $1 = host_name (Short name of host)
# $2 = state_string (A string representing the status of
# the given service - "UP", "DOWN", "UNREACHABLE")
# $3 = plugin_output (A text string that should be used
# as the plugin output for the service checks)
# $4 = perdata

# Convert the state string to the corresponding return code
return_code=-1

case "$2" in
UP)
return_code=0
;;
DOWN)
return_code=1
;;
UNREACHABLE)
return_code=2
;;
esac

# pipe the service check info into the send_nsca program, which
# in turn transmits the data to the nsca daemon on the central
# monitoring server

/usr/bin/printf "%b" "$1\t$return_code\t$3|$4 \n" | /usr/local/nagios/libexec/send_nsca 外网nagios地址 -c /usr/local/nagios/etc/send_nsca.cfg


后续:外网nagios服务器上和内网nagios服务器定义相同的服务器和服务即可,内网的使用主动模式检测服务器或服务,所有服务消息会通过ocsp_command=submit_check_result,所有的服务器消息会通过ochp_command=submit_host_alive提交到外网nagios服务器,在外网nagios服务器上可以通过查看message日志得到提交的消息日志,前提是nsca.cfg的debug日志开启。

通过这种部署,可以把多台nagios主机的消息汇总到一台上来展现。

Posted by Michael.Ding at 4:04 PM 0 comments

linux下查看内存使用情况(转)

Friday, January 22, 2010

老是忘,贴一篇长长记性

在Linux下查看内存我们一般用free命令:
[root@scs-2 tmp]# free
             total       used       free     shared    buffers     cached
Mem:       3266180    3250004      16176          0     110652    2668236
-/+ buffers/cache:     471116    2795064
Swap:      2048276      80160    1968116

下面是对这些数值的解释:
total:总计物理内存的大小。
used:已使用多大。
free:可用有多少。
Shared:多个进程共享的内存总额。
Buffers/cached:磁盘缓存的大小。
第三行(-/+ buffers/cached):
used:已使用多大。
free:可用有多少。
第四行就不多解释了。
区别:第二行(mem)的used/free与第三行(-/+ buffers/cache) used/free的区别。 这两个的区别在于使用的角度来看,第一行是从OS的角度来看,因为对于OS,buffers/cached 都是属于被使用,所以他的可用内存是16176KB,已用内存是3250004KB,其中包括,内核(OS)使用+Application(X, oracle,etc)使用的+buffers+cached.
第三行所指的是从应用程序角度来看,对于应用程序来说,buffers/cached 是等于可用的,因为buffer/cached是为了提高文件读取的性能,当应用程序需在用到内存的时候,buffer/cached会很快地被回收。
所以从应用程序的角度来说,可用内存=系统free memory+buffers+cached。
如上例:
2795064=16176+110652+2668236

接下来解释什么时候内存会被交换,以及按什么方交换。 当可用内存少于额定值的时候,就会开会进行交换。
如何看额定值:
cat /proc/meminfo

[root@scs-2 tmp]# cat /proc/meminfo
MemTotal:      3266180 kB
MemFree:         17456 kB
Buffers:        111328 kB
Cached:        2664024 kB
SwapCached:          0 kB
Active:         467236 kB
Inactive:      2644928 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:      3266180 kB
LowFree:         17456 kB
SwapTotal:     2048276 kB
SwapFree:      1968116 kB
Dirty:               8 kB
Writeback:           0 kB
Mapped:         345360 kB
Slab:           112344 kB
Committed_AS:   535292 kB
PageTables:       2340 kB
VmallocTotal: 536870911 kB
VmallocUsed:    272696 kB
VmallocChunk: 536598175 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

用free -m查看的结果:
[root@scs-2 tmp]# free -m
             total       used       free     shared    buffers     cached
Mem:          3189       3173         16          0        107       2605
-/+ buffers/cache:        460       2729
Swap:         2000         78       1921


查看/proc/kcore文件的大小(内存镜像):
[root@scs-2 tmp]# ll -h /proc/kcore
-r-------- 1 root root 4.1G Jun 12 12:04 /proc/kcore

备注:

占用内存的测量

测量一个进程占用了多少内存,linux为我们提供了一个很方便的方法,/proc目录为我们提供了所有的信息,实际上top等工具也通过这里来获取相应的信息。

/proc/meminfo 机器的内存使用信息

/proc/pid/maps pid为进程号,显示当前进程所占用的虚拟地址。

/proc/pid/statm 进程所占用的内存

[root@localhost ~]# cat /proc/self/statm

654 57 44 0 0 334 0

输出解释

CPU 以及CPU0。。。的每行的每个参数意思(以第一行为例)为:

参数 解释 /proc//status

Size (pages) 任务虚拟地址空间的大小 VmSize/4

Resident(pages) 应用程序正在使用的物理内存的大小 VmRSS/4

Shared(pages) 共享页数 0

Trs(pages) 程序所拥有的可执行虚拟内存的大小 VmExe/4

Lrs(pages) 被映像到任务的虚拟内存空间的库的大小 VmLib/4

Drs(pages) 程序数据段和用户态的栈的大小 (VmData+ VmStk )4

dt(pages) 04

查看机器可用内存

/proc/28248/>free

total used free shared buffers cached

Mem: 1023788 926400 97388 0 134668 503688

-/+ buffers/cache: 288044 735744

Swap: 1959920 89608 1870312

我们通过free命令查看机器空闲内存时,会发现free的值很小。这主要是因为,在linux中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。

所以 空闲内存=free+buffers+cached=total-used

Posted by Michael.Ding at 4:06 PM 0 comments

nagios 配合 pnp 作图xml error问题

Monday, January 18, 2010

nagios配合pnp作图,只要nagios吐出perfdata就可以了。

在nagios为了能在页面上显示中文,配置了主机的hostgroup中的alias为中文,pnp作图问题就来了

打开被设置过中文的服务器pnp页面显示

XML error: Invalid character at line 107 in /var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml

而其他的没有设置过中文的能正常显示图形

查看了/var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml文件的第107行,有中文字符

上服务器,到pnp的web路径下

grep "XML error" * -rn

找到
include/function.inc.php:1510:            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);

打开include/function.inc.php
if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
}

原来在xml_parse_into_struct时有错误抛出,所以就无法作图,分析是$data中有中文字符造成。

那就用iconv转一下字符喽
$xml_parser = xml_parser_create();
        if(($handle = fopen($rrddef, "rb")) === false ){
        return $NAGIOS;
    }
        $contents = '';
        while (!feof($handle)) {
          $data .= fread($handle, 8192);
        }
        fclose($handle);
        $data=iconv("GB2312","UTF-8",$data);
        if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
        }

好了,大功告成,现在中文别名也能作图了。

Posted by Michael.Ding at 4:59 PM 0 comments

用Gmail来写blogger测试

Saturday, January 9, 2010

网上查了一下用Gmail直接可以写自己的blog,那就试试看,希望通过这个,更新blog会勤一些。

Posted by Michael.Ding at 2:32 AM 0 comments

Nagios@Ubuntu 8.04发送外部mail的解决方法

Saturday, July 11, 2009

在Ubuntu 8.04上安装nagios监控软件还是比较容易的,不过版本是nagios2.11的,如果需要最新的nagios版本只能通过源码安装了。

这里我只说通过apt安装的nagios2
apt-get install nagios2就OK了

主要是发email提醒的功能
ubuntu默认安装的mta是exim4,默认的exim4是不支持对外发信的

需要修改
/etc/exim4/update-exim4.conf.conf

dc_eximconfig_configtype='local'
改为
dc_eximconfig_configtype='internet'

重启exim服务
/etc/init.d/exim4 restart

这时exim4还是监听着本地127.0.0.1的25口
但是已经支持往internet上发信了

好了,我的信箱终于可以收到nagios的提醒email了啦。

reference:http://www.haw-haw.org/node/552

Posted by Michael.Ding at 2:41 PM 0 comments

ubuntu修改服务起停

Thursday, April 30, 2009

这里我们安装一个软件:
sudo apt-get install sysv-rc-conf
然后这样起动:
sudo sysv-rc-conf

在这个软件里,可以用键盘的方向键移动,用空格选取,也可以直接用鼠标选取,最后用q保存退出,一下列出服务的名称和基本的解释。

acpi-support 这个是关于电源支持的默认是1,2,3,4,5下启动,我认为你可以把它调整到s级别。
acpid acpi的守护程序,默认是2-5开启,我认为可以不用管。
alsa alsa声音子系统,应该不用开启它。
alsa-utils 这个服务似乎取代了alsa,所以开启这个就可以了,我在S级别开启它。
anacron 这是一个用于执行到时间没有执行的程序的服务,我认为它无所谓,所以关了它,这个可以随便。
apmd 也是一种电源管理,我认为电脑如果不是很老,它就没有开启的必要了。
atd 和anacron类似,我把它关了。
bluez-utiles 传说中的蓝牙服务,然后遗憾我没有,所以关了。
bootlogd 似乎使用来写log的,安全期间开着他也许比较好。
cron 指定时间运行程序的服务,所以开着比较好的。
cupsys 打印机服务,所以如果你有,就开启吧。
dbus 消息总线系统,非常重要,一定要开。
dns-clean 拨号连接用的,如果不用,就关了它。
evms 企业卷管理系统,由于我并不明白什么叫做企业卷,所以我关了它。
fetchmail 用于邮件守护,我关了它。
gdm gnome桌面管理器,我关了它,然后用startx启动gnome。
halt 关机用的,不要更改
hdparm 这个我刚才有讲,如果没有ide硬盘也就不用开启它了。
hotkey-setup 这个是给某些品牌笔记本设计的热键映射,台式机用户请关了它
hotplug 这个是用于热插拔的,我已经测试过了,在某些电脑上关闭它会使声卡无效,请在S级别开启它。
hplip hp打印机专用的,应该可以关了它。
ifrename 网络接口重命名,好像没用,关了。
ifupdown 这个使用来打开网络的,开着它。
ifupdown-clean 同上。
klogd linux守护程序,接受来自内核和发送信息到syslogd的记录,并记录为一个文件,所以请开着它。
linux-restricted-modules-common 这个使用来使用受限制的模块的,你可以从/lib/linux-restricted-modules下查看,如果没有什么,你可以关掉它。
lvm 逻辑卷管理器,如果你没有请关了它。
makedev 用来创建设备到/dev/请不要动他。
mdamd 管理raid用,如果你没有请关闭它。
module-init-tools 从/etc/modules 加在扩展模块的,这个一般开着。
networking 增加网络接口和配置dns用,将它开启。
ntp-server 与ubuntu时间服务器进行同步的,关了。
pcmcia 激活pcmica设备,遗憾我有生以来都没有见过这样的设备,关了它。
powernowd 用于管理cpu的客户端程序,如果有变频功能,比如amd的quite' cool 那么就开启它吧。
ppp 拨号用的,我关了它。
ppp-dns 一样,也关了。
readahead 预加载服务,让我想起了win的预读,当然他们不同,它会使启动变慢3-4妙,所以我关了它。
reboot 重启用的,不要动。
rmnologin 如果发现nologin,就去除它,在笔记本上不用开启。
rsync rsync协议守护,请视情况而定。
screen-cleanup 一个清除开机屏幕的脚本,随便。
sendsigs 重启和关机时向所有进程发送消息。所以不要管它。
single 激活但用户模式,不用管它。
stop-bootlogd 从2,3,4,5级别停止bootlogd,不用管它。
sudo 这个不用说吧,不用管它。
sysklogd 用于记录系统日志信息,不用管它。
udev 用户空间dev文件系统,不用管它。
udev-mab 同上。
umountfs 用来卸载文件卷的,不用管它。
urandom 生成随即数的,不知道怎么用,不用管它。
usplash 那个漂亮的启动画面,但是我关了它,它也存在,所以想关他需要把内核起动参数中的splash一句删掉。
vbesave 显卡bios配置工具,不用管它。
xorg-common 设置x服务ice socket。不用管它。

Posted by Michael.Ding at 3:29 PM 0 comments

镜像数据库的发布和订阅

Monday, April 27, 2009

镜像数据库是支持发布和订阅,而且可以故障转移

publisher, distributor, subscriber都是必不可少的。

此外就是配置distributor代理服务的PublisherFailoverPartner

reference:http://social.microsoft.com/Forums/zh-CN/sqlserverzhchs/thread/ce7fc816-8619-487c-ac36-80e4a252e163

Posted by Michael.Ding at 4:11 PM 0 comments