3
# -*- coding: utf-8 -*-
5
# Copyright (C) 2009-2012:
6
# Gabes Jean, naparuba@gmail.com
7
# Gerhard Lausser, Gerhard.Lausser@consol.de
8
# Gregory Starck, g.starck@gmail.com
9
# Hartmut Goebel, h.goebel@goebel-consult.de
11
# This file is part of Shinken.
13
# Shinken is free software: you can redistribute it and/or modify
14
# it under the terms of the GNU Affero General Public License as published by
15
# the Free Software Foundation, either version 3 of the License, or
16
# (at your option) any later version.
18
# Shinken is distributed in the hope that it will be useful,
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
# GNU Affero General Public License for more details.
23
# You should have received a copy of the GNU Affero General Public License
24
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
25
# This module imports hosts and services configuration from a MySQL Database
26
# Queries for getting hosts and services are pulled from shinken-specific.cfg configuration file.
30
# Try to import landscape API
32
from landscape_api.base import API, HTTPError
34
API = HTTPError = None
36
from shinken.basemodule import BaseModule
37
from shinken.log import logger
40
'daemons': ['arbiter'],
41
'type': 'landscape_import',
43
'phases': ['configuration'],
47
# called by the plugin manager to get a broker
48
def get_instance(plugin):
49
logger.debug("[Landscape Importer Module]: Get Landscape importer instance for plugin %s" % plugin.get_name())
51
raise Exception('Missing module landscape-api. Please install it from https://launchpad.net/~landscape/+archive/landscape-api.')
53
# Beware : we must have RAW string here, not unicode!
54
key = str(plugin.key.strip())
55
secret = str(plugin.secret.strip())
56
ca = getattr(plugin, 'ca', None)
57
default_template = getattr(plugin, 'default_template', '')
58
https_proxy = getattr(plugin, 'https_proxy', '')
60
instance = Landscape_importer_arbiter(plugin, key, secret, ca, default_template, https_proxy)
64
# Retrieve hosts from landscape API
65
class Landscape_importer_arbiter(BaseModule):
66
def __init__(self, mod_conf, key, secret, ca, default_template, https_proxy):
67
BaseModule.__init__(self, mod_conf)
71
self.default_template = default_template
72
self.https_proxy = https_proxy
75
# Called by Arbiter to say 'let's prepare yourself guy'
78
os.environ["https_proxy"] = self.https_proxy
80
logger.debug("[Landscape Importer Module]: Try to open a Landscape connection")
81
uri = "https://landscape.canonical.com/api/"
84
self.api = API(uri, self.key, self.secret, self.ca)
86
self.api = API(uri, self.key, self.secret)
88
logger.debug("Landscape Module: Error %s" % e)
90
logger.info("[Landscape Importer Module]: Connection opened")
93
# Main function that is called in the CONFIGURATION phase
94
def get_objects(self):
96
computers = self.api.get_computers(with_network=True, limit=30000)
98
logger.debug("Landscape Module: Error %s" % e)
101
# Create variables for result
105
hname = c['hostname']
108
if tags == [] and self.default_template:
109
tags = [self.default_template]
110
ips = [net["ip_address"] for net in c["network_devices"]]
112
# If there is more than one IP address, use the first
116
# By default take the IP as the address
118
# But if not available, use the hostname
121
h = {'host_name' : hname,
124
'use' : ','.join(tags)
128
logger.debug("[Landscape Importer Module]: Returning to Arbiter %d hosts" % len(r['hosts']))