码迷,mamicode.com
首页 > 移动开发 > 详细

python-Django监控系统二次开发Nagios

时间:2016-08-09 09:28:48      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

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-server
12                 host_name       %(hostname)s
13                 alias           %(hostname)s
14                 address         %(ip)s
15                 }
16         """
17 HOSTGROUP_TEMP="""define hostgroup{
18         hostgroup_name  %(groupname)s
19         alias           %(groupname)s
20         members         %(members)s
21         }
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 data
32 
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 % h
47         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" ];then
10     cd -
11     cp -rp hosts/ /etc/nagios/conf.d
12     /etc/init.d/nagios restart
13 fi

 

python-Django监控系统二次开发Nagios

标签:

原文地址:http://www.cnblogs.com/Nyan-Workflow-FC/p/5749065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!