diff --git a/slapos/recipe/zabbixagent/__init__.py b/slapos/recipe/zabbixagent/__init__.py index 697791007ffd66fc7dbcd4447278c8dba2422815..95f603bc63c2e0bbab205152757232c84f832080 100644 --- a/slapos/recipe/zabbixagent/__init__.py +++ b/slapos/recipe/zabbixagent/__init__.py @@ -87,5 +87,25 @@ class Recipe(BaseSlapRecipe): self.logrotate_d, self.logrotate_backup = self.installLogrotate() zabbix_log_file = os.path.join(self.log_directory, 'zabbix_agentd.log') self.registerLogRotation('zabbix_agentd', [zabbix_log_file]) - raise NotImplementedError + zabbix_agentd = dict( + pid_file=os.path.join(self.run_directory, "zabbix_agentd.pid"), + log_file=zabbix_log_file, + ip=self.getGlobalIPv6Address(), + server=self.parameter_dict['server'], + hostname=self.parameter_dict['hostname'], + port='4567' + ) + zabbix_agentd_conf = self.createConfigurationFile("zabbix_agentd.conf", + pkg_resources.resource_string(__name__, + 'template/zabbix_agentd.conf.in') % zabbix_agentd) + self.path_list.append(zabbix_agentd_conf) + wrapper = zc.buildout.easy_install.scripts([('zabbixagentd', + 'slapos.recipe.librecipe.execute', 'execute')], self.ws, sys.executable, + self.bin_directory, arguments=[ + self.options['zabbix_agentd_binary'].strip(), '-c', + zabbix_agentd_conf])[0] + self.path_list.extend(zc.buildout.easy_install.scripts([ + ('zabbixagentd', __name__ + '.svcdaemon', 'svcdaemon')], + self.ws, sys.executable, self.wrapper_directory, arguments=[dict( + real_binary=wrapper, pid_file=zabbix_agentd['pid_file'])])) return self.path_list diff --git a/slapos/recipe/zabbixagent/svcdaemon.py b/slapos/recipe/zabbixagent/svcdaemon.py new file mode 100644 index 0000000000000000000000000000000000000000..92c0c8f1d997f02ae7434304d52c11193ac916bc --- /dev/null +++ b/slapos/recipe/zabbixagent/svcdaemon.py @@ -0,0 +1,39 @@ +import os +import subprocess +import time +import signal +import sys + +pid_file = None +def sig_handler(s, frame): + print "Killing on signal %s:" % s, + global pid_file + if pid_file is not None: + if os.path.exists(pid_file): + pid = int(open(pid_file).read()) + print 'pid %s with SIGTERM...' % pid, + os.kill(pid, signal.SIGTERM) + if os.kill(pid, 0): + time.sleep(5) + if os.kill(pid, 0): + print 'with SIGKILL...', + os.kill(pid, signal.SIGKILL) + else: + print 'no pid file %r, nothing to do...' % pid_file, + print 'done.' + sys.exit(0) + +signal.signal(signal.SIGINT, sig_handler) +signal.signal(signal.SIGQUIT, sig_handler) +signal.signal(signal.SIGTERM, sig_handler) + + +def svcdaemon(args): + """Utility script to run daemons in supervisord""" + real_binary = args[0]['real_binary'] + global pid_file + pid_file = args[0]['pid_file'] + subprocess.check_call(real_binary) + print 'Started %r' % real_binary + while True: + time.sleep(2)