Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / automations / __init__.py
blobcc6e4861e89a81ca27826bd9a9f1a812a5289706
1 #!/usr/bin/env python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 import signal
28 import sys
29 import pprint
31 import cmk.utils.debug
32 from cmk.utils.exceptions import MKTimeout
33 from cmk.utils.plugin_loader import load_plugins
35 import cmk_base.utils
36 import cmk_base.config as config
37 import cmk_base.console as console
38 import cmk_base.profiling as profiling
39 import cmk_base.check_api as check_api
42 # TODO: Inherit from MKGeneralException
43 class MKAutomationError(Exception):
44 def __init__(self, reason):
45 # TODO: This disable is needed because of a pylint bug. Remove one day.
46 super(MKAutomationError, self).__init__(reason) # pylint: disable=bad-super-call
47 self.reason = reason
49 def __str__(self):
50 return self.reason
53 class Automations(object):
54 def __init__(self):
55 # TODO: This disable is needed because of a pylint bug. Remove one day.
56 super(Automations, self).__init__() # pylint: disable=bad-super-call
57 self._automations = {}
59 def register(self, automation):
60 self._automations[automation.cmd] = automation
62 def execute(self, cmd, args):
63 self._handle_generic_arguments(args)
65 try:
66 try:
67 automation = self._automations[cmd]
68 except KeyError:
69 raise MKAutomationError("Automation command '%s' is not implemented." % cmd)
71 if automation.needs_checks:
72 config.load_all_checks(check_api.get_check_api_context)
74 if automation.needs_config:
75 config.load(validate_hosts=False)
77 result = automation.execute(args)
79 except (MKAutomationError, MKTimeout) as e:
80 console.error("%s\n" % cmk_base.utils.make_utf8("%s" % e))
81 if cmk.utils.debug.enabled():
82 raise
83 return 1
85 except Exception as e:
86 if cmk.utils.debug.enabled():
87 raise
88 console.error("%s\n" % cmk_base.utils.make_utf8("%s" % e))
89 return 2
91 finally:
92 profiling.output_profile()
94 if cmk.utils.debug.enabled():
95 console.output(pprint.pformat(result) + "\n")
96 else:
97 console.output("%r\n" % (result,))
99 return 0
101 # Handle generic arguments (currently only the optional timeout argument)
102 def _handle_generic_arguments(self, args):
103 if len(args) > 1 and args[0] == "--timeout":
104 args.pop(0)
105 timeout = int(args.pop(0))
107 if timeout:
108 MKTimeout.timeout = timeout
109 signal.signal(signal.SIGALRM, self._raise_automation_timeout)
110 signal.alarm(timeout)
112 def _raise_automation_timeout(self, signum, stackframe):
113 raise MKTimeout("Action timed out. The timeout of %d "
114 "seconds was reached." % MKTimeout.timeout)
117 class Automation(object):
118 cmd = None
119 needs_checks = False
120 needs_config = False
124 # Initialize the modes object and load all available modes
127 automations = Automations()
129 load_plugins(__file__, __package__)