標籤:
1、Nagios安裝
yum install -y nagios.i686
yum install -y nagios-plugins-all.i686
安裝完後會在apache的設定檔目錄下/etc/httpd/conf.d/產生一個外部的設定檔nagios.conf
service httpd start
service nagios start
default user nagiosadmin password nagiosadmin
2.設定檔產生器
Django前期的收集主機資訊代碼
在nagios上分組
vim gen.py
1 #!/usr/bin/env python 2 3 import os 4 import urllib,urllib2 5 import json 6 7 CUR_DIR = os.path.dirname(__file__) 8 CONF_DIR = os.path.join(os.path.abspath(CUR_DIR),‘hosts‘) 9 10 HOST_TEMP="""define host{11 use linux-server12 host_name %(hostname)s13 alias %(hostname)s14 address %(ip)s15 }16 """17 HOSTGROUP_TEMP="""define hostgroup{18 hostgroup_name %(groupname)s19 alias %(groupname)s20 members %(members)s21 }22 """23 def initDir():24 if not os.path.exists(CONF_DIR):25 os.mkdir(CONF_DIR)26 27 def getData():28 url = ‘http://192.168.1.120:8000/hostinfo/getjson/‘29 req = urllib2.urlopen(url)30 data = json.loads(req.read())31 return data32 33 def writeConf(f,data):34 with open(f,‘w‘) as fd:35 fd.write(data)36 37 def parseData(data):38 host_conf = ‘‘39 hostgroup_conf = ‘‘40 for hg in data:41 groupname = hg[‘groupname‘]42 members = []43 for h in hg[‘members‘]:44 hostname = h[‘hostname‘]45 members.append(hostname)46 host_conf += HOST_TEMP % h47 hostgroup_conf += HOSTGROUP_TEMP % {‘groupname‘:groupname, ‘members‘:‘,‘.join(members)}48 fp_hostconf = os.path.join(CONF_DIR,‘hosts.cfg‘)49 fp_hostgroupconf = os.path.join(CONF_DIR,‘hostgroup.cfg‘)50 writeConf(fp_hostconf,host_conf)51 writeConf(fp_hostgroupconf,hostgroup_conf)52 53 54 if __name__ == ‘__main__‘:55 initDir()56 data = getData()57 parseData(data)gen.py
同步設定檔sync_gen.sh
1 #!/bin/bash 2 3 cur=`dirname $0` 4 cd $cur 5 python gen.py 6 md5=`find hosts/ -type f -exec md5sum {} \;|md5sum` 7 cd /etc/nagios/conf.d 8 conf_md5=`find hosts/ -type f -exec md5sum {} \;|md5sum` 9 if [ "$md5" != "$conf_md5" ];then10 cd -11 cp -rp hosts/ /etc/nagios/conf.d12 /etc/init.d/nagios restart13 fi
python-Django監控系統二次開發Nagios