Skip to content

Instantly share code, notes, and snippets.

@YUChoe
Last active August 29, 2015 14:09
Show Gist options
  • Save YUChoe/cfaa4f6f4653f36cfe66 to your computer and use it in GitHub Desktop.
Save YUChoe/cfaa4f6f4653f36cfe66 to your computer and use it in GitHub Desktop.
simple nagios3 node maker for www.plus-i.co.kr
#!/usr/bin/env python
import sys
import os
fpath = '/home/plus-i/nagios3-conf.d/'
def print_usage() :
print "Usage: %s add {hostname} {ip address|domain name}" % (sys.argv[0])
print " %s del {hostname}" % (sys.argv[0])
print " %s list " % (sys.argv[0])
def fpwriteln(fp, _str) :
fp.write( _str + os.linesep )
def add_host(host, ip) :
fname = "%s%s.mng.cfg" % (fpath, host)
fp = open(fname, 'w')
fpwriteln(fp, 'define host{')
fpwriteln(fp, ' use generic-host')
fpwriteln(fp, ' host_name %s' % host)
fpwriteln(fp, ' alias %s' % host)
fpwriteln(fp, ' address %s' % ip)
fpwriteln(fp, '}')
fpwriteln(fp, '')
fpwriteln(fp, 'define service{')
fpwriteln(fp, ' use generic-service')
fpwriteln(fp, ' host_name %s' % host)
fpwriteln(fp, ' service_description Alive')
fpwriteln(fp, ' check_command check-host-alive')
fpwriteln(fp, '}')
fp.close()
nogios3_reload()
def nogios3_reload() :
os.system('sudo /etc/init.d/nagios3 reload')
def del_host(host) :
pass
def get_msgcfg_list() :
from os import listdir
from os.path import isfile, join
flist = [ f for f in listdir(fpath) if isfile(join(fpath, f)) and (join(fpath,f).split('.')[-2] == 'mng') ]
return flist
def print_list() :
print 'list --'
for cfg in get_msgcfg_list() :
print cfg.replace(".mng.cfg", "")
def print_header() :
print "%s 1.0 by plus-i 2014" % sys.argv[0]
# -- main --
if len(sys.argv) < 2 :
print_header()
print_usage()
sys.exit()
if sys.argv[1] == 'add' :
print_header()
add_host( sys.argv[2], sys.argv[3] )
elif sys.argv[1] == 'del' :
print_header()
del_host( sys.argv[2])
elif sys.argv[1] == 'list' :
print_header()
print_list()
else :
print_header()
print_usage()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment