初級: nagios用 監視ホスト追加スクリプト

## 使い方

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

以下が生成される。

* /etc/nagios3/conf.d/host_group1.cfg
* /etc/nagios3/conf.d/host_group1_check.cfg

## 参考

* [YUChoe/nadmin: nagios cfg administrator](https://github.com/YUChoe/nadmin)

## ソース

* [download](https://github.com/tukiyo/nadmin)

```py: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")
```

----

## 例2.1

中身だけ出力するほうが使いやすかも。

```py: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)
```

## 例2.2 三連クォートを使う場合

* formatを使う場合は`{`は`{{`、`}`は`}}`と書かなければならない。

```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):
  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)
```