Allow user to pass zabbix parameter.
[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 installZabbixAgentd(self, ip, port, hostname, server_ip,
83 user_parameter_string=''):
84 log_file = os.path.join(self.log_directory, 'zabbix_agentd.log')
85 self.registerLogRotation('zabbix_agentd', [log_file])
86
87 zabbix_agentd_conf = dict(
88 pid_file=os.path.join(self.run_directory, "zabbix_agentd.pid"),
89 log_file=log_file,
90 ip=ip,
91 server=server_ip,
92 hostname=hostname,
93 port=port,
94 user_parameter_string=user_parameter_string)
95
96 zabbix_agentd_path = self.createConfigurationFile(
97 "zabbix_agentd.conf",
98 pkg_resources.resource_string(
99 __name__, 'template/zabbix_agentd.conf.in') % zabbix_agentd_conf)
100
101 self.path_list.append(zabbix_agentd_path)
102
103 wrapper = zc.buildout.easy_install.scripts([('zabbixagentd',
104 'slapos.recipe.librecipe.execute', 'execute')], self.ws, sys.executable,
105 self.bin_directory, arguments=[
106 self.options['zabbix_agentd_binary'].strip(), '-c',
107 zabbix_agentd_path])[0]
108
109 self.path_list.extend(zc.buildout.easy_install.scripts([
110 ('zabbixagentd', __name__ + '.svcdaemon', 'svcdaemon')],
111 self.ws, sys.executable, self.wrapper_directory, arguments=[dict(
112 real_binary=wrapper, pid_file=zabbix_agentd_conf['pid_file'])]))
113
114 return zabbix_agentd_conf
115
116 def _install(self):
117 self.path_list = []
118 self.requirements, self.ws = self.egg.working_set()
119 # self.cron_d is a directory, where cron jobs can be registered
120 self.cron_d = self.installCrond()
121 self.logrotate_d, self.logrotate_backup = self.installLogrotate()
122 zabbix_agentd_conf = self.installZabbixAgentd(
123 self.getGlobalIPv6Address(),
124 10050,
125 self.parameter_dict['hostname'],
126 self.parameter_dict['server'],
127 self.parameter_dict.get('custom-user-parameter', '')
128 )
129 self.setConnectionDict(dict(ip=zabbix_agentd_conf['ip'],
130 name=zabbix_agentd_conf['hostname'],
131 port=str(zabbix_agentd_conf['port'])))
132 return self.path_list