[PYTHON] Débutant: ajouter un script hôte de surveillance pour les nagios

Comment utiliser

python


git clone https://github.com/tukiyo/nadmin
cd nadmin
sudo ./nadm.py group1 host1 192.168.1.1

Ce qui suit est généré.

référence

La source

nadm.py


#!/usr/bin/env python 
import os
import sys 
dir = '/etc/nagios3/conf.d'
dir = '.'
#------------------------------------------------------
def help(emsg):
    print(emsg)
    print("usage: {} group host ip").format(sys.argv[0])
    list()

def list():
    for f in os.listdir(dir) :
        if '.cfg' in f and '_nagios2' not in f:
            print "\t{dir}/{file}".format(dir=dir,file=f)

def add(group, host, ip):
    write(define_host(group, host, ip)
        ,"{dir}/host_{group}.cfg".format(dir=dir, group=group))
    write(define_chek(group, host, ip)
        ,"{dir}/host_{group}_check.cfg".format(dir=dir, group=group))
    list()

def define_host(group, host, ip):
    form = """# {host} ({ip})"
define host{{
    use        generic-host
    host_name  {group}-{host}
    alias      {host}
    address    {ip}
#   check_command return-ok
    }}
"""
    text = form.format(group=group, host=host, ip=ip)
    return text

# see: /usr/lib/nagios/plugins/
def define_chek(group, host, ip):
    form = """## {host} ({ip})
#define service{{
#    service_description Alive
#    host_name           {group}-{host}
#    use                 generic-service
#    check_command       check-host-alive
#    }}
"""
    text = form.format(group=group, host=host, ip=ip)
    return text

def write(txt, fname):
    fp = open(fname, 'a+')
    fp.write(txt)
    fp.close()
#------------------------------------------------------
if __name__ == '__main__' :
    if len(sys.argv) != 4:
        help('[ERROR] specify argument is wrong.')
        sys.exit(1)

    add(sys.argv[1], sys.argv[2], sys.argv[3])
    print("check: nagios3 -v /etc/nagios3/nagios.cfg && service nagios3 restart")

Exemple 2.1

Il peut être plus facile à utiliser si seul le contenu est sorti.

nadm.py


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import subprocess

servers = {
    '0': {'group': 'home', 'host': 'ubuntu1', 'ip': '192.168.0.2'},
    '1': {'group': 'home', 'host': 'win7-1', 'ip': '192.168.0.3'},
    '2': {'group': 'home', 'host': 'win7-2', 'ip': '192.168.0.4'},
}

def define_host(server):
  txt = "## {host} ({ip})\n".format(**servers[server])
  txt+= "define host{\n"
  txt+= "    use        generic-host\n"
  txt+= "    host_name  {group}-{host}\n".format(**servers[server])
  txt+= "    alias      {host}\n".format(**servers[server])
  txt+= "    address    {ip}\n".format(**servers[server])
  txt+= "#   check_command return-ok\n"
  txt+= "    }\n"
  return txt

if __name__ == '__main__':
    for server in sorted(servers):
        print define_host(server)

Exemple 2.2 Lors de l'utilisation de guillemets triples

python


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import subprocess

servers = {
    '0': {'group': 'home', 'host': 'ubuntu1', 'ip': '192.168.0.2'},
    '1': {'group': 'home', 'host': 'win7-1', 'ip': '192.168.0.3'},
    '2': {'group': 'home', 'host': 'win7-2', 'ip': '192.168.0.4'},
}

def define_host(server):
  form = """## {host} ({ip})
define host{{
    use        generic-host
    host_name  {group}-{host}
    alias      {host}
    address    {ip}
#   check_command return-ok
    }}
"""
  text = form.format(**servers[server])
  return text

if __name__ == '__main__':
    for server in sorted(servers):
        print define_host(server)

Recommended Posts

Débutant: ajouter un script hôte de surveillance pour les nagios
Script de surveillance du journal