2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui Core.
6 # Copyright (C) 2009-2010 HDE, Inc.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
19 from karesansui.lib.dict_op import DictOp
20 from karesansui.lib.parser.base.xml_like_conf_parser import xmlLikeConfParser as Parser
21 from karesansui.lib.utils import preprint_r, r_chgrp, r_chmod
22 from karesansui.lib.const import VENDOR_SYSCONF_DIR, \
23 COLLECTD_DATA_DIR, KARESANSUI_GROUP
27 Define Variables for This Parser
29 PARSER_COLLECTD_PLUGIN_DIR = "%s/collectd.d" % VENDOR_SYSCONF_DIR
31 class collectdpluginParser:
33 _module = "collectdplugin"
37 self.dop.addconf(self._module,{})
39 self.parser = Parser()
40 self.parser.set_delim("[ \t]+")
41 self.parser.set_new_delim("\t")
42 self.parser.set_comment("#")
43 self.base_parser_name = self.parser.__class__.__name__
45 from karesansui.lib.parser.collectd import collectdParser
46 collectdp = collectdParser()
48 self.parser.set_opt_uni(collectdp.opt_uni)
49 self.parser.set_opt_multi(collectdp.opt_multi)
50 self.parser.set_opt_sect(collectdp.opt_sect)
54 def set_footer(self, footer=""):
55 self.parser.set_footer(footer)
57 def source_file(self):
60 glob_str = "%s/*.conf" % (PARSER_COLLECTD_PLUGIN_DIR,)
61 for _afile in glob.glob(glob_str):
66 def read_conf(self,extra_args=None):
69 for _afile in self.source_file():
71 plugin_name = re.sub("\.conf$","",os.path.basename(_afile))
75 if not re.search(extra_args['include'],plugin_name):
80 self.parser.set_source_file([_afile])
81 conf_arr = self.parser.read_conf()
83 self.dop.set(self._module,[plugin_name],conf_arr[_afile]['value'])
87 self.dop.set(self._module,['@BASE_PARSER'],self.base_parser_name)
88 #self.dop.preprint_r(self._module)
89 return self.dop.getconf(self._module)
91 def _pre_write_conf(self,conf_arr={}):
93 # Change permission to be able to read/write data kss group.
94 if os.path.exists(COLLECTD_DATA_DIR):
96 r_chgrp(COLLECTD_DATA_DIR,KARESANSUI_GROUP)
97 r_chmod(COLLECTD_DATA_DIR,"g+rwx")
98 r_chmod(COLLECTD_DATA_DIR,"o-rwx")
101 dop.addconf("__",conf_arr)
103 if dop.isset("__",["python"]) is True:
104 dop.cdp_unset("__",["python","Plugin","python","@ORDERS"],multiple_file=True)
106 orders.append(['Encoding'])
107 orders.append(['LogTraces'])
108 orders.append(['Interactive'])
109 orders.append(['ModulePath'])
110 orders.append(['Import'])
111 orders.append(['Module'])
112 dop.cdp_set("__",["python","Plugin","python","@ORDERS"],orders,is_opt_multi=True,multiple_file=True)
114 return dop.getconf("__")
116 def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
119 conf_arr = self._pre_write_conf(conf_arr)
121 for plugin_name,_v in conf_arr.iteritems():
123 _afile = "%s/%s.conf" % (PARSER_COLLECTD_PLUGIN_DIR,plugin_name,)
126 if _v['action'] == "delete":
127 if os.path.exists(_afile):
137 self.dop.addconf("parser",{})
138 self.dop.set("parser",[_afile],_v['value'])
139 #self.dop.preprint_r("parser")
140 arr = self.dop.getconf("parser")
141 self.parser.write_conf(arr,dryrun=dryrun)
149 if __name__ == '__main__':
153 parser = collectdpluginParser()
157 dop.addconf("dum",parser.read_conf())
159 new_plugin_name = "takuma"
161 ##########################################################
162 # Uniオプション (一箇所しか設定できないオプション) の追加
163 ##########################################################
164 # 'Foo foo' を追加(設定値リスト形式モードよる addメソッド)
165 dop.add("dum",[new_plugin_name,"Foo"],["foo",[["comment foo1","comment foo2"],"comment foo3"]])
167 # 'Bar bar' を追加(設定値文字列形式モードによる cdp_setメソッド)
168 dop.cdp_set("dum",[new_plugin_name,"Bar"],"bar",multiple_file=True)
169 dop.cdp_set_pre_comment("dum",[new_plugin_name,"Bar"],["","comment bar1","comment bar2"],multiple_file=True)
170 dop.cdp_set_post_comment("dum",[new_plugin_name,"Bar"],"comment bar3",multiple_file=True)
172 ##########################################################
173 # Multiオプション (複数設定できるオプション) の追加
174 ##########################################################
175 # 'LoadPlugin target_hoge' を追加
176 dop.cdp_set("dum",[new_plugin_name,"LoadPlugin","target_hoge"],"target_hoge",multiple_file=True,is_opt_multi=True)
177 dop.cdp_set_pre_comment("dum",[new_plugin_name,"LoadPlugin","target_hoge"],["","Dis is target_hoge"],multiple_file=True)
179 ##########################################################
180 # Sectオプション (<ブラケット>ディレクティブオプション) の追加
181 ##########################################################
185 # SubOpt1 gege # post
190 dop.cdp_set("dum",[new_plugin_name,"Plugin","foobar","Option1"],"true",multiple_file=True)
191 dop.cdp_set("dum",[new_plugin_name,"Plugin","foobar","Option2"],"false",multiple_file=True)
192 dop.cdp_set_pre_comment("dum",[new_plugin_name,"Plugin","foobar","Option2"],"pre comment",multiple_file=True)
193 dop.cdp_set_post_comment("dum",[new_plugin_name,"Plugin","foobar","Option2"],"post comment",multiple_file=True)
194 dop.cdp_set("dum",[new_plugin_name,"Plugin","foobar","View","hoge","SubOpt1"],"gege",multiple_file=True)
195 dop.cdp_set_post_comment("dum",[new_plugin_name,"Plugin","foobar","View","hoge","SubOpt1"],"post",multiple_file=True)
197 print dop.get("dum",["filter","@ORDERS"],multiple_file=True)
199 # 複数ファイルを読み込むパーサーの場合は、is_parent_parser=Trueにすること
200 # '<Plugin foobar>' を 先頭にする
201 key = [new_plugin_name,"Plugin","foobar"]
202 dop.insert_order("dum",key,0,is_parent_parser=True)
204 # 'LoadPlugin target_hoge' を 先頭にする => '<Plugin foobar>' は2番目になる
205 key = [new_plugin_name,"LoadPlugin","target_hoge"]
206 dop.insert_order("dum",key,0,is_parent_parser=True)
208 # 'Foo foo' を 先頭にする => 'LoadPlugin target_hoge' は2番目になる
209 key = [new_plugin_name,"Foo"]
210 dop.insert_order("dum",key,0,is_parent_parser=True)
213 #dop.cdp_comment("dum",["python","Plugin","python","Import"],multiple_file=True)
214 #dop.cdp_comment("dum",["python","Plugin","python","Module","notification"],multiple_file=True)
215 #dop.cdp_comment("dum",["python","Plugin","python","Module","notification","CountupDBPath"],multiple_file=True)
216 #dop.cdp_set("dum",["python","Plugin","python","Module","notification","@ORDERS"],[['Environ'],['CountupDBPath']],multiple_file=True,is_opt_multi=True)
217 # work completely, too.
218 #dop.cdp_comment("dum",["python","Plugin","python","ModulePath"],multiple_file=True)
219 # work completely, too. (but this is overwritten by _pre_write_conf() method)
220 #dop.cdp_set("dum",["python","Plugin","python","@ORDERS"],[['ModulePath'],['Encoding']],multiple_file=True,is_opt_multi=True)
224 conf = dop.getconf("dum")
227 parser.write_conf(conf,dryrun=True)