2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
31 import cmk
.utils
.debug
32 from cmk
.utils
.exceptions
import MKTimeout
33 from cmk
.utils
.plugin_loader
import load_plugins
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
53 class Automations(object):
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
)
67 automation
= self
._automations
[cmd
]
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():
85 except Exception as e
:
86 if cmk
.utils
.debug
.enabled():
88 console
.error("%s\n" % cmk_base
.utils
.make_utf8("%s" % e
))
92 profiling
.output_profile()
94 if cmk
.utils
.debug
.enabled():
95 console
.output(pprint
.pformat(result
) + "\n")
97 console
.output("%r\n" % (result
,))
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":
105 timeout
= int(args
.pop(0))
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):
124 # Initialize the modes object and load all available modes
127 automations
= Automations()
129 load_plugins(__file__
, __package__
)