cd ~/tmp
mkdir nagios
cd nagios
wget http://jaist.dl.sourceforge.net/project/nagios/nagios-4.x/nagios-4.0.2/nagios-4.0.2.tar.gz
wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
tar xvf nagios-3.4.4.tar.gz
tar xvf nagios-plugins-1.4.16.tar.gz
配置 nagios 源码并编译安装
12345678910111213
cd ~/tmp/nagios/nagios-4.0.2
./configure --prefix /usr/local/nagios \
--sysconfdir=/etc/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-user=nagios \
--with-command-group=nagcmd
make all
make install
make install-config
make install-commandmode
安装参考文章,需要 make install-init 的, 但我发现4.0.2版的 init 脚本用不了, 弄了好久最后在All the geeky things 这个博客里找到了一个稍微修改就可以使用的脚本。将它放到 /etc/init.d/nagios 即可
#!/usr/bin/env pythonimportsysimportgetoptimportsocketimportxml.parsers.expatclassGParser:def__init__(self,host,metric):self.inhost=0self.inmetric=0self.value=Noneself.host=hostself.metric=metricdefparse(self,file):p=xml.parsers.expat.ParserCreate()p.StartElementHandler=parser.start_elementp.EndElementHandler=parser.end_elementp.ParseFile(file)ifself.value==None:raiseException('Host/value not found')returnfloat(self.value)defstart_element(self,name,attrs):ifname=="HOST":ifattrs["NAME"]==self.host:self.inhost=1elifself.inhost==1andname=="METRIC"andattrs["NAME"]==self.metric:self.value=attrs["VAL"]defend_element(self,name):ifname=="HOST"andself.inhost==1:self.inhost=0defusage():print"""Usage: check_ganglia \-h|--host= -m|--metric= -w|--warning= \-c|--critical= [-s|--server=] [-p|--port=] """sys.exit(3)if__name__=="__main__":##############################################################ganglia_host='127.0.0.1'ganglia_port=8649host=Nonemetric=Nonewarning=Nonecritical=Nonetry:options,args=getopt.getopt(sys.argv[1:],"h:m:w:c:s:p:",["host=","metric=","warning=","critical=","server=","port="],)exceptgetopt.GetoptError,err:print"check_gmond:",str(err)usage()sys.exit(3)foro,ainoptions:ifoin("-h","--host"):host=aelifoin("-m","--metric"):metric=aelifoin("-w","--warning"):warning=float(a)elifoin("-c","--critical"):critical=float(a)elifoin("-p","--port"):ganglia_port=int(a)elifoin("-s","--server"):ganglia_host=aifcritical==Noneorwarning==Noneormetric==Noneorhost==None:usage()sys.exit(3)try:s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((ganglia_host,ganglia_port))parser=GParser(host,metric)value=parser.parse(s.makefile("r"))s.close()exceptException,err:print"CHECKGANGLIA UNKNOWN: Error while getting value \"%s\""%(err)sys.exit(3)ifvalue>=critical:print"CHECKGANGLIA CRITICAL: %s is %.2f"%(metric,value)sys.exit(2)elifvalue>=warning:print"CHECKGANGLIA WARNING: %s is %.2f"%(metric,value)sys.exit(1)else:print"CHECKGANGLIA OK: %s is %.2f"%(metric,value)sys.exit(0)