From f5b9fda5c4f2313b09add0fdb4754492675f9416 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 30 Apr 2014 23:52:47 +0200 Subject: [PATCH] MT#6817 add pid_watcher.py Use inotify in order to monitor the pid change for some services --- bin/pid_watcher.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++ debian/control | 1 + run_tests.sh | 14 +++++--- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100755 bin/pid_watcher.py diff --git a/bin/pid_watcher.py b/bin/pid_watcher.py new file mode 100755 index 00000000..514efcab --- /dev/null +++ b/bin/pid_watcher.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +""" + Copyright: 2014 Sipwise Development Team + + 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 3 of the License, or + (at your option) any later version. + + This package 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, see . + + On Debian systems, the complete text of the GNU General + Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +""" +import sys +import os.path +import pyinotify + +base_path = "/var/run" + +""" +files we want to watch +""" +services = [ + 'kamailio/kamailio.lb.pid', + 'kamailio/kamailio.proxy.pid', + 'sems/sems.pid', + 'collectdmon.pid' +] + +watched_dirs = [ + '/var/run/kamailio', + '/var/run/sems', + '/var/run' +] + +watched = {} + +class Handler(pyinotify.ProcessEvent): + def my_init(self, watched): + self.watched = watched + + def check_all(self): + all = True + for k,v in self.watched.iteritems(): + all = (all and (v['created'] or v['modified'])) + print "checking: %s[%s] all:%s" % (k,v, all) + return all + + def process_IN_CREATE(self, event): + if watched.has_key(event.pathname): + watched[event.pathname]['created'] = True + print "created %s" % event.pathname + if self.check_all(): + sys.exit(0) + + def process_IN_IGNORED(self, event): + if watched.has_key(event.pathname): + watched[event.pathname]['deleted'] = True + print "deleted %s" % event.pathname + + def process_IN_MODIFY(self, event): + if watched.has_key(event.pathname): + watched[event.pathname]['modified'] = True + print "modified %s" % event.pathname +# for debug +# def process_default(self, event): +# if watched.has_key(event.pathname): +# print event + +wm = pyinotify.WatchManager() +handler = Handler(watched=watched) +notifier = pyinotify.Notifier(wm, default_proc_fun=handler) +for service in services: + service_pid = os.path.join(base_path, service) + print "Watching %s" % service_pid + watched[service_pid] = {'deleted': False, 'created': False, 'modified': False } + wm.add_watch(service_pid, pyinotify.IN_IGNORED) +for d in watched_dirs: + wm.add_watch(d, pyinotify.ALL_EVENTS) + +notifier.loop() diff --git a/debian/control b/debian/control index ece5a204..c0268737 100644 --- a/debian/control +++ b/debian/control @@ -15,6 +15,7 @@ Depends: curl, ngcp-ossbss-clients-perl, parallel, python, + python-pyinotify, python-yaml, sip-tester ( >= 1:3.2-1~ ), tcpdump, diff --git a/run_tests.sh b/run_tests.sh index 0a9e6e69..2cfdddae 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -49,13 +49,19 @@ fi if [ -z $SKIP ]; then echo "$(date) - Setting config debug on" ${BIN_DIR}/config_debug.pl on ${DOMAIN} + if [ "${PROFILE}" == "PRO" ]; then + ( timeout 60 ${BIN_DIR}/pid_watcher.py )& + fi ngcpcfg apply - echo "$(date) - Setting config debug on. Done." if [ "${PROFILE}" == "PRO" ]; then - echo "$(date) wait for monit to restart services" - sleep 5 - echo "$(date) wait for monit to restart services. Done" + wait $! + if [ "$?" != "0" ]; then + echo "error on apply config" + echo "$(date) - Done[1]" + exit 1 + fi fi + echo "$(date) - Setting config debug on. Done." fi echo "$(date) - Clean log dir"