Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
d56165b7
Commit
d56165b7
authored
Apr 09, 2014
by
Jonathan Lestrelin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Nagios NDO inventory plugin
parent
5adcd705
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
0 deletions
+119
-0
contrib/inventory/nagios_ndo.ini
+10
-0
contrib/inventory/nagios_ndo.py
+109
-0
No files found.
contrib/inventory/nagios_ndo.ini
0 → 100644
View file @
d56165b7
# Ansible Nagios external inventory script settings
#
[ndo]
# NDO database URI
# Make sure that data is returned as strings and not bytes if using python 3.
# See http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html
# for supported databases and URI format.
# Example for mysqlclient module :
database_uri
=
mysql+mysqldb://user:passwd@hostname/ndo?charset=utf8&use_unicode=1
contrib/inventory/nagios_ndo.py
0 → 100755
View file @
d56165b7
#!/usr/bin/env python
# (c) 2014, Jonathan Lestrelin <jonathan.lestrelin@gmail.com>
#
# This file is part of Ansible,
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
"""
Nagios NDO external inventory script.
========================================
Returns hosts and hostgroups from Nagios NDO.
Configuration is read from `nagios_ndo.ini`.
"""
import
os
import
argparse
try
:
import
configparser
except
ImportError
:
import
ConfigParser
configparser
=
ConfigParser
import
json
try
:
from
sqlalchemy
import
text
from
sqlalchemy.engine
import
create_engine
except
ImportError
:
print
(
"Error: SQLAlchemy is needed. Try something like: pip install sqlalchemy"
)
exit
(
1
)
class
NagiosNDOInventory
(
object
):
def
read_settings
(
self
):
config
=
configparser
.
SafeConfigParser
()
config
.
read
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
+
'/nagios_ndo.ini'
)
if
config
.
has_option
(
'ndo'
,
'database_uri'
):
self
.
ndo_database_uri
=
config
.
get
(
'ndo'
,
'database_uri'
)
def
read_cli
(
self
):
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'--host'
,
nargs
=
1
)
parser
.
add_argument
(
'--list'
,
action
=
'store_true'
)
self
.
options
=
parser
.
parse_args
()
def
get_hosts
(
self
):
engine
=
create_engine
(
self
.
ndo_database_uri
)
connection
=
engine
.
connect
()
select_hosts
=
text
(
"SELECT display_name
\
FROM nagios_hosts"
)
select_hostgroups
=
text
(
"SELECT alias
\
FROM nagios_hostgroups"
)
select_hostgroup_hosts
=
text
(
"SELECT h.display_name
\
FROM nagios_hostgroup_members hgm, nagios_hosts h, nagios_hostgroups hg
\
WHERE hgm.hostgroup_id = hg.hostgroup_id
\
AND hgm.host_object_id = h.host_object_id
\
AND hg.alias =:hostgroup_alias"
)
hosts
=
connection
.
execute
(
select_hosts
)
self
.
result
[
'all'
][
'hosts'
]
=
[
host
[
'display_name'
]
for
host
in
hosts
]
for
hostgroup
in
connection
.
execute
(
select_hostgroups
):
hostgroup_alias
=
hostgroup
[
'alias'
]
self
.
result
[
hostgroup_alias
]
=
{}
hosts
=
connection
.
execute
(
select_hostgroup_hosts
,
hostgroup_alias
=
hostgroup_alias
)
self
.
result
[
hostgroup_alias
][
'hosts'
]
=
[
host
[
'display_name'
]
for
host
in
hosts
]
def
__init__
(
self
):
self
.
defaultgroup
=
'group_all'
self
.
ndo_database_uri
=
None
self
.
options
=
None
self
.
read_settings
()
self
.
read_cli
()
self
.
result
=
{}
self
.
result
[
'all'
]
=
{}
self
.
result
[
'all'
][
'hosts'
]
=
[]
self
.
result
[
'_meta'
]
=
{}
self
.
result
[
'_meta'
][
'hostvars'
]
=
{}
if
self
.
ndo_database_uri
:
self
.
get_hosts
()
if
self
.
options
.
host
:
print
(
json
.
dumps
({}))
elif
self
.
options
.
list
:
print
(
json
.
dumps
(
self
.
result
))
else
:
print
(
"usage: --list or --host HOSTNAME"
)
exit
(
1
)
else
:
print
(
"Error: Database configuration is missing. See nagios_ndo.ini."
)
exit
(
1
)
NagiosNDOInventory
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment