Skip to content

Commit

Permalink
Add Python module for the icinga2.debug file
Browse files Browse the repository at this point in the history
refs #6702
  • Loading branch information
gunnarbeutner committed Aug 16, 2014
1 parent e359c55 commit be3fda7
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ add_subdirectory(doc)
add_subdirectory(test)
add_subdirectory(pki)
add_subdirectory(contrib)
add_subdirectory(python)

set(CPACK_PACKAGE_NAME "Icinga2")
set(CPACK_PACKAGE_VENDOR "Icinga Development Team")
Expand Down
2 changes: 1 addition & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ parentheses):
on Debian)
* GNU bison (bison)
* GNU flex (flex) >= 2.5.35
* Python (python-devel on RHEL, python-dev on Debian)
* recommended: libexecinfo on FreeBSD
* optional: MySQL (mysql-devel on RHEL, libmysqlclient-dev on Debian)
* optional: Python (python-devel on RHEL, python-dev on Debian)

Note: RHEL5 ships an ancient flex version. Updated packages are available for
example from the repoforge buildtools repository.
Expand Down
22 changes: 20 additions & 2 deletions icinga2.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
%endif
%endif

%{!?__python2: %global __python2 /usr/bin/python2}
%{!?python2_sitelib: %global python2_sitelib %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
%{!?python2_sitearch: %global python2_sitearch %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}

%define icinga_user icinga
%define icinga_group icinga
%define icingacmd_group icingacmd
Expand All @@ -63,7 +67,6 @@ Group: Applications/System
Source: https://github.com/Icinga/%{name}/archive/v%{version}.tar.gz
URL: http://www.icinga.org/
BuildRoot: %{_tmppath}/%{name}-%{version}-build

Requires: %{name}-bin = %{version}
#Requires: %{name}-ido-mysql = %{version}
#Requires: %{icingaweb2name} >= %{icingaweb2version}
Expand All @@ -75,6 +78,7 @@ Meta package for Icinga 2 Core, DB IDO and Web.
Summary: Icinga 2 binaries and libraries
Group: Applications/System

Requires: python-%{name} = %{version}
%if "%{_vendor}" == "suse"
PreReq: permissions
%endif
Expand Down Expand Up @@ -168,6 +172,17 @@ Icinga 1.x Classic UI Standalone configuration with locations
for Icinga 2.


%package -n python-icinga2
Summary: Python module for Icinga 2
Group: Application/System
BuildRequires: python
BuildRequires: python-devel
BuildRequires: python-setuptools
Requires: python-setuptools

%description -n python-icinga2
Python module for Icinga 2.

%prep
%setup -q -n %{name}-%{version}

Expand Down Expand Up @@ -207,7 +222,6 @@ cmake $CMAKE_OPTS .

make %{?_smp_mflags}

rm -f components/db_ido_*sql/schema/upgrade/.gitignore

%install
[ "%{buildroot}" != "/" ] && [ -d "%{buildroot}" ] && rm -rf %{buildroot}
Expand Down Expand Up @@ -448,6 +462,7 @@ exit 0
%{_bindir}/%{name}-build-ca
%{_bindir}/%{name}-build-key
%{_bindir}/%{name}-sign-key
%{_bindir}/%{name}-list-objects
%{_sbindir}/%{name}-enable-feature
%{_sbindir}/%{name}-disable-feature
%{_sbindir}/%{name}-prepare-dirs
Expand Down Expand Up @@ -506,5 +521,8 @@ exit 0
%config(noreplace) %{apacheconfdir}/icinga.conf
%config(noreplace) %attr(0640,root,%{apachegroup}) %{icingaclassicconfdir}/passwd

%files -n python-icinga2
%{python2_sitelib}/icinga2*


%changelog
31 changes: 31 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

find_package(PythonInterp REQUIRED)

add_subdirectory(icinga2)

configure_file(setup.py.cmake ${CMAKE_CURRENT_BINARY_DIR}/setup.py @ONLY)

install(CODE "execute_process(
COMMAND ${CMAKE_COMMAND}
-DSETUP_PY=${CMAKE_CURRENT_BINARY_DIR}/setup.py
-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
-DPREFIX=${CMAKE_INSTALL_PREFIX}
-DWDIR=${CMAKE_CURRENT_BINARY_DIR}
-P ${PROJECT_SOURCE_DIR}/third-party/cmake/PythonSetup.cmake)"
)
23 changes: 23 additions & 0 deletions python/icinga2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

add_subdirectory(commands)
add_subdirectory(utils)

configure_file(config.py.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.py @ONLY)
configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)

Empty file added python/icinga2/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions python/icinga2/commands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)
configure_file(list_objects.py ${CMAKE_CURRENT_BINARY_DIR}/list_objects.py COPYONLY)

Empty file.
25 changes: 25 additions & 0 deletions python/icinga2/commands/list_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

from icinga2.utils.debug import ObjectsFile
from icinga2.config import LocalStateDir

def main():
fp = open(LocalStateDir + "/cache/icinga2/icinga2.debug")
of = ObjectsFile(fp)
for obj in of:
print obj
24 changes: 24 additions & 0 deletions python/icinga2/config.py.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

Prefix = '@CMAKE_INSTALL_PREFIX@'
SysconfDir = '@CMAKE_INSTALL_FULL_SYSCONFDIR@'
RunDir = '@ICINGA2_RUNDIR@'
LocalStateDir = '@CMAKE_INSTALL_FULL_LOCALSTATEDIR@'
PkgDataDir = '@CMAKE_INSTALL_FULL_DATADIR@/icinga2'
IncludeConfDir = '@CMAKE_INSTALL_FULL_DATADIR@/icinga2/include'

21 changes: 21 additions & 0 deletions python/icinga2/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)
configure_file(debug.py ${CMAKE_CURRENT_BINARY_DIR}/debug.py COPYONLY)
configure_file(netstring.py ${CMAKE_CURRENT_BINARY_DIR}/netstring.py COPYONLY)

Empty file.
113 changes: 113 additions & 0 deletions python/icinga2/utils/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Icinga 2
# Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

from icinga2.utils import netstring
import subprocess

try:
import json
except ImportError:
import simplejson as json

class ConsoleColors(object):
@staticmethod
def _exec(args):
return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]

def __init__(self):
self.RESET = ConsoleColors._exec(['tput', 'sgr0'])
self.GREEN = ConsoleColors._exec(['tput', 'setaf', '2'])
self.CYAN = ConsoleColors._exec(['tput', 'setaf', '6'])

_colors = ConsoleColors()

class DebugObject(object):
def __init__(self, obj):
self._obj = obj

def __str__(self):
return self.format_object()

def format_object(self):
if self._obj["abstract"]:
result = "Template '"
else:
result = "Object '"
result += self._obj["properties"]["__name"] + "' of type '" + self._obj["type"] + "':\n"
result += self.format_properties(2)
return result

@staticmethod
def format_value(value):
if isinstance(value, list):
result = ""
for avalue in value:
if result != "":
result += ", "
result += DebugObject.format_value(avalue)
return "[%s]" % (result)
else:
return "'%s'" % (str(value))

def format_properties(self, indent=0, path=[]):
props = self._obj["properties"]
for component in path:
props = props[component]

result = ""
for key, value in props.items():
path.append(key)
result += ' ' * indent + "* %s%s%s" % (_colors.GREEN, key, _colors.RESET)
hints = self.format_hints(self, indent + 2, path)
if isinstance(value, dict):
result += "\n" + hints
result += self.format_properties(indent + 2, path)
else:
result += " = %s\n" % (DebugObject.format_value(value))
result += hints
path.pop()
return result

def format_hints(self, dhints, indent=0, path=[]):
dhints = self._obj["debug_hints"]
try:
for component in path:
dhints = dhints["properties"][component]
except KeyError:
return ""

result = ""
for message in dhints["messages"]:
result += ' ' * indent + "%% %smodified in %s, lines %s:%s-%s:%s%s\n" % (_colors.CYAN,
message[1], message[2], message[3], message[4], message[5], _colors.RESET)
return result

class ObjectsFile(object):
def __init__(self, file):
self._file = file

def __iter__(self):
fr = netstring.FileReader(self._file)

while True:
try:
json_data = fr.readskip()
except EOFError:
break
if json_data == "":
break
yield DebugObject(json.loads(json_data))
Loading

0 comments on commit be3fda7

Please sign in to comment.