Module: check_mk
Branch: master
Commit: 09fd2d81968401c66f77ae5c06c4017fecf12d33
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=09fd2d81968401…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Nov 10 10:53:23 2014 +0100
Implement kind of cron job URL in Multisite
---
scripts/setup.sh | 10 +++++++
web/htdocs/cron.py | 64 +++++++++++++++++++++++++++++++++++++++++++++
web/htdocs/index.py | 6 ++---
web/plugins/pages/cron.py | 31 ++++++++++++++++++++++
4 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/scripts/setup.sh b/scripts/setup.sh
index 8101f88..7907667 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -955,8 +955,18 @@ and change the path there. Restart Apache afterwards."
Allow from all
Satisfy any
</Location>
+
+ # Trigger cron jobs. This is done without authentication
+ <Location "${url_prefix}check_mk/run_cron.py">
+ Order allow,deny
+ Allow from all
+ Satisfy any
+ </Location>
+
</IfModule>
+
+
<IfModule !mod_python.c>
Alias ${url_prefix}check_mk $web_dir/htdocs
<Directory $web_dir/htdocs>
diff --git a/web/htdocs/cron.py b/web/htdocs/cron.py
new file mode 100644
index 0000000..9748f74
--- /dev/null
+++ b/web/htdocs/cron.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk 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 in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# ails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+import time
+from lib import *
+import defaults
+
+loaded_with_language = False
+multisite_cronjobs = []
+
+lock_file = defaults.tmp_dir + "/cron.lastrun"
+
+# Load all view plugins
+def load_plugins():
+ global loaded_with_language
+ if loaded_with_language == current_language:
+ return
+
+ global multisite_cronjobs
+ multisite_cronjobs = []
+ load_web_plugins("cron", globals())
+
+ loaded_with_language = current_language
+
+
+# Page called by some external trigger (usually cron job in OMD site)
+# Note: this URL is being called *without* any login. We have no
+# user. Everyone can call this! We must not read any URL variables.
+def page_run_cron():
+ now = time.time()
+ # Prevent cron jobs from being run too often, also we need
+ # locking in order to prevent overlapping runs
+ if os.path.exists(lock_file):
+ last_run = os.stat(lock_file).st_mtime
+ if time.time() - last_run < 59:
+ raise MKGeneralException("Cron called too early. Skipping.")
+ file(lock_file, "w") # touches the file
+ aquire_lock(lock_file)
+
+ for cron_job in multisite_cronjobs:
+ cron_job()
diff --git a/web/htdocs/index.py b/web/htdocs/index.py
index 67ee234..b889c96 100644
--- a/web/htdocs/index.py
+++ b/web/htdocs/index.py
@@ -67,7 +67,7 @@ if defaults.omd_root:
# Call the load_plugins() function in all modules
def load_all_plugins():
for module in [ hooks, userdb, visuals, views, sidebar, dashboard,
- wato, bi, mobile, notify, webapi, reporting ]:
+ wato, bi, mobile, notify, webapi, reporting, cron ]:
try:
module.load_plugins # just check if this function exists
except AttributeError:
@@ -90,8 +90,8 @@ def handler(req, fields = None, profiling = True):
html.enable_debug = config.debug
html.id = {} # create unique ID for this request
__builtin__.html = html
-
response_code = apache.OK
+
try:
# Ajax-Functions want no HTML output in case of an error but
@@ -160,7 +160,7 @@ def handler(req, fields = None, profiling = True):
# Special handling for automation.py. Sorry, this must be hardcoded
# here. Automation calls bybass the normal authentication stuff
- if html.myfile == "automation":
+ if html.myfile in [ "automation", "run_cron" ]:
try:
handler()
except Exception, e:
diff --git a/web/plugins/pages/cron.py b/web/plugins/pages/cron.py
new file mode 100644
index 0000000..9ec92df
--- /dev/null
+++ b/web/plugins/pages/cron.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk 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 in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# ails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+import cron
+
+pagehandlers.update({
+ "run_cron" : cron.page_run_cron,
+})