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

delphi简单代码收集

【收集人:张子萌 】

1. Form1中调用Form2窗口

Form2.ShowModal; //显示form2窗口

Form2.close; //关闭窗口

2. 获取title,可查看UTF-8编码

procedure TForm2.Button1Click(Sender: TObject); //获取title
var
webboby:string;
r:TRegExpr;
s1:string;
charseti:integer;
gethttp:Tidhttp;
begin
gethttp:=Tidhttp.Create(self);
webboby:=gethttp.Get(Edit1.Text);
charseti:=pos(‘charset=’,webboby);
if (copy(webboby,charseti+8,3)=’utf’) or (copy(webboby,charseti+8,3)=’UTF’) then
webboby:=UTF8Decode(webboby);
s1:=”;
r:= TRegExpr.Create;
try // ensure memory clean-up
r.Expression := ‘[^a]*‘;
if r.Exec(webboby) then
REPEAT
s1 :=r.Match [0];
Edit2.Text:=s1;
UNTIL not
r.ExecNext;
finally
r.Free;
end;
end;

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

脚本放入crontab不能运行

【整理:张子萌】

linux下编写shell脚本,手动运行正常,但是放到crontab中,就不能正常运行了,这是环境变量的问题。即使使用同一个账户,手动登陆和crontab自动运行所加载的环境变量也是不同的。

有两种方法可以解决:

1. 比较简单,应该在shell编写时就注意。

在shell脚本的头部添加”. $HOME/.bash_profile”(要注意前面有个点,点后面是空格,之后是环境变量位置)。

也可以直接写环境变量的绝对位置。如test用户的根目录在/home/test下可以在shell头部添加”. /home/test/.bash_profile”

2.比较紧急情况,再确认可以手动执行脚本情况下,可以使用另一种方法。使用root权限调用shell脚本。

如果有/home/test/test.sh需要每分钟运行一次,可以使用root登陆,在root的crontab中添加如下:

# crontab -e

*/1 * * * * /home/test/test.sh

3月 23

快捷命令收集

1.快速切换到上次访问目录。

linux中操作经常在两个目录中切换。简单操作方法,在命令行输入“cd -”即可跳转到上次访问目录。

2.转换文本文件编码

iconv -f gbk -t utf8 test.txt > test.txt.utf8

参数:-f 输入编码 -t ENCODING 输出编码 -l 列出支持的编码 -s 不显示有关转换出错的信息