对于系统员来说,监控服务器各种运行状态是任务之一,对于数量较少服务器来说,采取人工方式观察未尝不可,但是对于服务器集群来说,太麻烦,于是很多人采用了一些自动化工具,比如脚本之类,但是今天介绍的是一个非常著名的开源项目nagios。
Nagios准确来是只是一个监控平台,所有的监控任务由插件来完成,自由定制化程度非常高,可配置性也很好,NRPE就是一个流行的插件,负责监控远程服务器运行状态,包括磁盘空间,CPU负载率等一些可定制内容。
2,Nagios配置
这是一台安装nagios平台的机器,接受其他被监控服务器的相关信息。
环境:Centos系统
按本指南安装,安装目录如下:
Nagios和插件安装到/usr/local/nagios
将被配置为监控本地系统的几个主要服务(CPU负荷、磁盘利用率等)
Nagios的Web接口:http://localhost/nagios/
2.1 准备工作
应确认你的服务器上安装有:
Apache
GCC编译器
GD库以及开发库
可以通过以下命令安装
yum install httpd
yum install gcc
yum install glibc glibc-common
yum install gd gd-devel
2.2 切换root用户
su -l
创建一个nagios帐号并给定登录口令
/usr/sbin/useradd nagios
passwd nagios
创建一个用户组名为nagcmd用于WEB接口(主要是WEB权限的问题)。将nagios用户和apache用户都加到这个组中。
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -G nagcmd nagios
/usr/sbin/usermod -G nagcmd apache
2.3 下载Nagios安装包
mkdir ~/downloads
cd ~/downloads
从http://www.nagios.org/download/下载最新版本,nagios以及nagios-plugins
2.4 编译与安装
cd ~/downloads
tar xzf nagios-**.tar.gz
cd nagios-*
./configure --with-command-group =nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
2.5 客户化配置
编辑/usr/local/nagios/etc/objects/contacts.cfg配置文件,修改其中的邮箱地址,这个地址是nagios系统发邮件报警的地址。
2.6 配置WEB接口
安装nagios的WEB配置文件到Apache的conf.d目录下:
make install-webconf
创建一个nagiosadmin用户用于Nagios的WEB接口登录。记下你输入的密码:
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
重启Apache服务使设置生效
service httpd restart
2.7 编译并安装Nagios插件
cd ~/downloads
tar xzf nagios-plugins-*.tar.gz
cd nagios-plugins-*
编译安装插件
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
2.8 启动Nagios
chkconfig --add nagios
chkconfig nagios on
验证Nagios的样例配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
没有报错的话,可以启动Nagios服务
service nagios start
2.9 更改SELiux设置
RED HAT系列系统有SELINUX增强安全性,但在有些时候,这个东西很让人费事,这里需要开设一下权限,如果嫌麻烦,可以直接关闭SElinux。
如果SELinux处于强制安全模式时候,需要先
get enforce
令SELinux处于容许模式
set enforce 0
不过建议还是修改SELINUX的文件强制目标模式:
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
2.10 登录WEB接口
现在就可以登录http://localhost/nagios/来查看本机的服务信息了,要输入前面刚录入的用户名和口令。
到此,基本上完成了nagios的配置
3.0 NRPE插件的配置
现在监控机已经配置好了,但是远程服务器需要插件才能与这个平台进行联系,好在开源的东西比较全面,现成的有NRPE插件,可以实时监控服务器磁盘空间问题,CPU负载问题,而且可以自己定制监控项目
3.1 创建账户
su -l
/usr/sbin/useradd nagios
passwd nagios
3.2 下载插件
下载nagios-plugins和nrpe插件,注意下载新的版本。
tar xzf nagios-plugins-1.4.6.tar.gz
cd nagios-plugins-1.4.6
./configure
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
3.3 安装xinetd
由于nrpe是通过xinetd进程加载的,所以需要安装xinetd,在RED HAT系统中默认没有安装xinetd.
yum install xinetd
3.4 安装NRPE
tar xzf nrpe-2.8.tar.gz
cd nrpe-2.8
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
3.5 编辑nrpe文件
编辑/etc/xinetd.d/nrpe文件,添加你的监控平台机器的IP地址在下面这行后面,用空格隔开。
only_from = 127.0.0.1 <你的监控机器IP>
安装环境:
CentOS 5.8
boost 1.5.3
thrift 0.9.0
编译时的错误提示:
/usr/local/include/thrift/protocol/TBinaryProtocol.tcc: In member function 'uint32_t apache::thrift::protocol::TBinaryProtocolT<Transport_>::writeI16(int16_t)': /usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147: error: there are no arguments to 'htons' that depend on a template parameter, so a declaration of 'htons' must be available /usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147: error: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) /usr/local/include/thrift/protocol/TBinaryProtocol.tcc: In member function 'uint32_t apache::thrift::protocol::TBinaryProtocolT<Transport_>::writeI32(int32_t)': /usr/local/include/thrift/protocol/TBinaryProtocol.tcc:154: error: there are no arguments to 'htonl' that depend on a template parameter, so a declaration of 'htonl' must be available /usr/local/include/thrift/transport/TSocket.h: At global scope: /usr/local/include/thrift/transport/TSocket.h:219: error: expected ';' before '*' token /usr/local/include/thrift/transport/TSocket.h:240: error: expected ',' or '...' before '*' token /usr/local/include/thrift/transport/TSocket.h:293: error: 'sockaddr_in' does not name a type /usr/local/include/thrift/transport/TSocket.h:294: error: 'sockaddr_in6' does not name a type /usr/local/include/thrift/server/TNonblockingServer.h:780: error: expected ',' or '...' before '*' token store.cpp: In member function 'void BufferStore::setNewRetryInterval(bool)': store.cpp:1681: warning: comparison between signed and unsigned integer expressions store.cpp:1693: warning: comparison between signed and unsigned integer expressions store.h: In constructor 'NetworkStore::NetworkStore(StoreQueue*, const std::string&, bool)': store.h:407: warning: 'NetworkStore::opened' will be initialized after store.h:401: warning: 'time_t NetworkStore::lastServiceCheck' store.cpp:1743: warning: when initialized here make[3]: *** [store.o] Error 1 make[3]: Leaving directory `/root/download/scribe/src' make[2]: *** [all] Error 2 make[2]: Leaving directory `/root/download/scribe/src' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/download/scribe' make: *** [all] Error 2
解决办法:
export CPPFLAGS="-DHAVE_NETDB_H=1 -fpermissive"
./bootstrap.sh
./configure
SecureCRT脚本
js版本
XX.js内容如下:
# $language = "JScript"
# $interface = "1.0"
crt.screen.send("\n")
crt.screen.send("su - opr\n")
crt.screen.WaitForString("Password:",10)
crt.screen.send("#log\n")
crt.screen.send("stg\n")
crt.screen.send("slogssh -s test XX.XX.XX.XX\n")
crt.screen.send("cd /XX/XX.log\n")
crt.screen.send("tail -f XX.log\n")
VBScript版本
XX.vbs内容如下:
#$language = "VBScript"
#$interface = "1.0"
Sub main
' turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True
' Wait for a string that looks like "login: " or "Login: "
crt.Screen.WaitForString "login: "
' Send your username followed by a carriage return
crt.Screen.Send "su - opr" & VbCr
' Wait for a tring that looks like "password: " or "Password: "
crt.Screen.WaitForString "assword:"
' Send your password followed by a carriage return
crt.Screen.Send "#log" & VbCr
' Send your password followed by a carriage return
crt.Screen.Send "stg" & VbCr
crt.Screen.Send "slogssh -s test XX.XX.XX.XX & VbCr
crt.Screen.Send "cd /XX/XX.log" & VbCr
' turn off synchronous mode to restore normal input processing
crt.Screen.Synchronous = False
End Sub