1#!/usr/bin/env python 2 3# (c) 2014, Jonathan Lestrelin <jonathan.lestrelin@gmail.com> 4# 5# This file is part of Ansible, 6# 7# Ansible is free software: you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation, either version 3 of the License, or 10# (at your option) any later version. 11# 12# Ansible is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 19 20""" 21Nagios NDO external inventory script. 22======================================== 23 24Returns hosts and hostgroups from Nagios NDO. 25 26Configuration is read from `nagios_ndo.ini`. 27""" 28 29import os 30import argparse 31import sys 32from ansible.module_utils.six.moves import configparser 33import json 34 35try: 36 from sqlalchemy import text 37 from sqlalchemy.engine import create_engine 38except ImportError: 39 sys.exit("Error: SQLAlchemy is needed. Try something like: pip install sqlalchemy") 40 41 42class NagiosNDOInventory(object): 43 44 def read_settings(self): 45 config = configparser.SafeConfigParser() 46 config.read(os.path.dirname(os.path.realpath(__file__)) + '/nagios_ndo.ini') 47 if config.has_option('ndo', 'database_uri'): 48 self.ndo_database_uri = config.get('ndo', 'database_uri') 49 50 def read_cli(self): 51 parser = argparse.ArgumentParser() 52 parser.add_argument('--host', nargs=1) 53 parser.add_argument('--list', action='store_true') 54 self.options = parser.parse_args() 55 56 def get_hosts(self): 57 engine = create_engine(self.ndo_database_uri) 58 connection = engine.connect() 59 select_hosts = text("SELECT display_name \ 60 FROM nagios_hosts") 61 select_hostgroups = text("SELECT alias \ 62 FROM nagios_hostgroups") 63 select_hostgroup_hosts = text("SELECT h.display_name \ 64 FROM nagios_hostgroup_members hgm, nagios_hosts h, nagios_hostgroups hg \ 65 WHERE hgm.hostgroup_id = hg.hostgroup_id \ 66 AND hgm.host_object_id = h.host_object_id \ 67 AND hg.alias =:hostgroup_alias") 68 69 hosts = connection.execute(select_hosts) 70 self.result['all']['hosts'] = [host['display_name'] for host in hosts] 71 72 for hostgroup in connection.execute(select_hostgroups): 73 hostgroup_alias = hostgroup['alias'] 74 self.result[hostgroup_alias] = {} 75 hosts = connection.execute(select_hostgroup_hosts, hostgroup_alias=hostgroup_alias) 76 self.result[hostgroup_alias]['hosts'] = [host['display_name'] for host in hosts] 77 78 def __init__(self): 79 80 self.defaultgroup = 'group_all' 81 self.ndo_database_uri = None 82 self.options = None 83 84 self.read_settings() 85 self.read_cli() 86 87 self.result = {} 88 self.result['all'] = {} 89 self.result['all']['hosts'] = [] 90 self.result['_meta'] = {} 91 self.result['_meta']['hostvars'] = {} 92 93 if self.ndo_database_uri: 94 self.get_hosts() 95 if self.options.host: 96 print(json.dumps({})) 97 elif self.options.list: 98 print(json.dumps(self.result)) 99 else: 100 sys.exit("usage: --list or --host HOSTNAME") 101 else: 102 sys.exit("Error: Database configuration is missing. See nagios_ndo.ini.") 103 104 105NagiosNDOInventory() 106