Listen on standard port and expose all required information.
[slapos.git] / slapos / recipe / zabbixagent / __init__.py
1 ##############################################################################
2 #
3 # Copyright (c) 2011 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27 from slapos.recipe.librecipe import BaseSlapRecipe
28 import binascii
29 import os
30 import pkg_resources
31 import pprint
32 import hashlib
33 import sys
34 import zc.buildout
35 import zc.recipe.egg
36 import ConfigParser
37
38 class Recipe(BaseSlapRecipe):
39 def installLogrotate(self):
40 """Installs logortate main configuration file and registers its to cron"""
41 logrotate_d = os.path.abspath(os.path.join(self.etc_directory,
42 'logrotate.d'))
43 self._createDirectory(logrotate_d)
44 logrotate_backup = self.createBackupDirectory('logrotate')
45 logrotate_conf = self.createConfigurationFile("logrotate.conf",
46 "include %s" % logrotate_d)
47 logrotate_cron = os.path.join(self.cron_d, 'logrotate')
48 state_file = os.path.join(self.data_root_directory, 'logrotate.status')
49 open(logrotate_cron, 'w').write('0 0 * * * %s -s %s %s' %
50 (self.options['logrotate_binary'], state_file, logrotate_conf))
51 self.path_list.extend([logrotate_d, logrotate_conf, logrotate_cron])
52 return logrotate_d, logrotate_backup
53
54 def registerLogRotation(self, name, log_file_list):
55 """Register new log rotation requirement"""
56 open(os.path.join(self.logrotate_d, name), 'w').write(
57 pkg_resources.resource_string(__name__, 'template/logrotate_entry.in')%
58 dict(file_list=' '.join(['"'+q+'"' for q in log_file_list]),
59 olddir=self.logrotate_backup))
60
61 def installCrond(self):
62 timestamps = self.createDataDirectory('cronstamps')
63 cron_output = os.path.join(self.log_directory, 'cron-output')
64 self._createDirectory(cron_output)
65 catcher = zc.buildout.easy_install.scripts([('catchcron',
66 __name__ + '.catdatefile', 'catdatefile')], self.ws, sys.executable,
67 self.bin_directory, arguments=[cron_output])[0]
68 self.path_list.append(catcher)
69 cron_d = os.path.join(self.etc_directory, 'cron.d')
70 crontabs = os.path.join(self.etc_directory, 'crontabs')
71 self._createDirectory(cron_d)
72 self._createDirectory(crontabs)
73 wrapper = zc.buildout.easy_install.scripts([('crond',
74 'slapos.recipe.librecipe.execute', 'execute')], self.ws, sys.executable,
75 self.wrapper_directory, arguments=[
76 self.options['dcrond_binary'].strip(), '-s', cron_d, '-c', crontabs,
77 '-t', timestamps, '-f', '-l', '5', '-M', catcher]
78 )[0]
79 self.path_list.append(wrapper)
80 return cron_d
81
82 def _install(self):
83 self.path_list = []
84 self.requirements, self.ws = self.egg.working_set()
85 # self.cron_d is a directory, where cron jobs can be registered
86 self.cron_d = self.installCrond()
87 self.logrotate_d, self.logrotate_backup = self.installLogrotate()
88 zabbix_log_file = os.path.join(self.log_directory, 'zabbix_agentd.log')
89 self.registerLogRotation('zabbix_agentd', [zabbix_log_file])
90 zabbix_agentd = dict(
91 pid_file=os.path.join(self.run_directory, "zabbix_agentd.pid"),
92 log_file=zabbix_log_file,
93 ip=self.getGlobalIPv6Address(),
94 server=self.parameter_dict['server'],
95 hostname=self.parameter_dict['hostname'],
96 port='10050'
97 )
98 zabbix_agentd_conf = self.createConfigurationFile("zabbix_agentd.conf",
99 pkg_resources.resource_string(__name__,
100 'template/zabbix_agentd.conf.in') % zabbix_agentd)
101 self.path_list.append(zabbix_agentd_conf)
102 wrapper = zc.buildout.easy_install.scripts([('zabbixagentd',
103 'slapos.recipe.librecipe.execute', 'execute')], self.ws, sys.executable,
104 self.bin_directory, arguments=[
105 self.options['zabbix_agentd_binary'].strip(), '-c',
106 zabbix_agentd_conf])[0]
107 self.path_list.extend(zc.buildout.easy_install.scripts([
108 ('zabbixagentd', __name__ + '.svcdaemon', 'svcdaemon')],
109 self.ws, sys.executable, self.wrapper_directory, arguments=[dict(
110 real_binary=wrapper, pid_file=zabbix_agentd['pid_file'])]))
111 self.setConnectionDict(dict(ip=zabbix_agentd['ip'],
112 name=zabbix_agentd['hostname'], port=zabbix_agentd['port']))
113 return self.path_list