stats_httpd.py.in 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #!@PYTHON@
  2. # Copyright (C) 2011-2012 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """
  17. A standalone HTTP server for HTTP/XML interface of statistics in BIND 10
  18. """
  19. import sys; sys.path.append ('@@PYTHONPATH@@')
  20. import os
  21. import time
  22. import errno
  23. import select
  24. from optparse import OptionParser, OptionValueError
  25. import http.server
  26. import socket
  27. import string
  28. import xml.etree.ElementTree
  29. import urllib.parse
  30. import re
  31. import isc.cc
  32. import isc.config
  33. import isc.util.process
  34. import isc.log
  35. from isc.log_messages.stats_httpd_messages import *
  36. isc.log.init("b10-stats-httpd", buffer=True)
  37. logger = isc.log.Logger("stats-httpd")
  38. # Some constants for debug levels.
  39. DBG_STATSHTTPD_INIT = logger.DBGLVL_START_SHUT
  40. DBG_STATSHTTPD_MESSAGING = logger.DBGLVL_COMMAND
  41. # If B10_FROM_SOURCE is set in the environment, we use data files
  42. # from a directory relative to that, otherwise we use the ones
  43. # installed on the system
  44. if "B10_FROM_SOURCE" in os.environ:
  45. BASE_LOCATION = os.environ["B10_FROM_SOURCE"] + os.sep + \
  46. "src" + os.sep + "bin" + os.sep + "stats"
  47. else:
  48. PREFIX = "@prefix@"
  49. DATAROOTDIR = "@datarootdir@"
  50. BASE_LOCATION = "@datadir@" + os.sep + "@PACKAGE@"
  51. BASE_LOCATION = BASE_LOCATION.replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  52. SPECFILE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd.spec"
  53. XML_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xml.tpl"
  54. XSD_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsd.tpl"
  55. XSL_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsl.tpl"
  56. # These variables are paths part of URL.
  57. # eg. "http://${address}" + XXX_URL_PATH
  58. XML_URL_PATH = '/bind10/statistics/xml'
  59. XSD_URL_PATH = '/bind10/statistics.xsd'
  60. XSL_URL_PATH = '/bind10/statistics.xsl'
  61. # TODO: This should be considered later.
  62. XSD_NAMESPACE = 'http://bind10.isc.org/bind10'
  63. XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
  64. # constant parameter of XML
  65. XML_ROOT_ELEMENT = 'bind10:statistics'
  66. XML_ROOT_ATTRIB = { 'xsi:schemaLocation' : '%s %s' % (XSD_NAMESPACE, XSD_URL_PATH),
  67. 'xmlns:bind10' : XSD_NAMESPACE,
  68. 'xmlns:xsi' : XMLNS_XSI }
  69. # Assign this process name
  70. isc.util.process.rename()
  71. def item_name_list(element, identifier):
  72. """Return a list of strings. The strings are string expression of
  73. the first argument element which is dict type. The second argument
  74. identifier is a string for specifying the strings which are
  75. returned from this method as a list. For example, if we specify as
  76. item_name_list({'a': {'aa': [0, 1]}, 'b': [0, 1]}, 'a/aa'),
  77. then it returns
  78. ['a/aa', 'a/aa[0]', 'a/aa[1]'].
  79. If an empty string is specified in the second argument, all
  80. possible strings are returned as a list. In that example if we
  81. specify an empty string in the second argument, then it returns
  82. ['a', 'a/aa', 'a/aa[0]', 'a/aa[1]', 'b', 'b[0]', 'b[1]'].
  83. The key name of element which is in the first argument is sorted.
  84. Even if we specify as
  85. item_name_list({'xx': 0, 'a': 1, 'x': 2}, ''),
  86. then it returns
  87. ['a', 'x', 'xx'].
  88. This method internally invokes isc.cc.data.find(). The arguments
  89. of this method are passed to isc.cc.data.find(). So an exception
  90. DataNotFoundError or DataTypeError might be raised via
  91. isc.cc.data.find() depending on the arguments. See details of
  92. isc.cc.data.find() for more information about exceptions"""
  93. elem = isc.cc.data.find(element, identifier)
  94. ret = []
  95. ident = identifier
  96. if ident:
  97. ret.append(ident)
  98. if type(elem) is dict:
  99. if ident:
  100. ident = ident + '/'
  101. for key in sorted(elem.keys()):
  102. idstr = '%s%s' % (ident, key)
  103. ret += item_name_list(element, idstr)
  104. elif type(elem) is list:
  105. for i in range(0, len(elem)):
  106. idstr = '%s[%d]' % (ident, i)
  107. ret += item_name_list(element, idstr)
  108. return ret
  109. class HttpHandler(http.server.BaseHTTPRequestHandler):
  110. """HTTP handler class for HttpServer class. The class inhrits the super
  111. class http.server.BaseHTTPRequestHandler. It implemets do_GET()
  112. and do_HEAD() and orverrides log_message()"""
  113. def do_GET(self):
  114. body = self.send_head()
  115. if body is not None:
  116. self.wfile.write(body.encode())
  117. def do_HEAD(self):
  118. self.send_head()
  119. def send_head(self):
  120. try:
  121. req_path = self.path
  122. req_path = urllib.parse.urlsplit(req_path).path
  123. req_path = urllib.parse.unquote(req_path)
  124. body = None
  125. # In case that the requested path (req_path),
  126. # e.g. /bind10/statistics/Auth/, is started with
  127. # XML_URL_PATH + '/'
  128. if req_path.find('%s/' % XML_URL_PATH) == 0:
  129. # remove './' from the path if there is
  130. req_path = os.path.normpath(req_path)
  131. # get the strings tailing after XML_URL_PATH
  132. req_path = req_path.lstrip('%s/' % XML_URL_PATH)
  133. # remove empty dir names from the path if there are
  134. path_dirs = req_path.split('/')
  135. path_dirs = [ d for d in filter(None, path_dirs) ]
  136. req_path = '/'.join(path_dirs)
  137. # pass the complete requested path,
  138. # e.g. Auth/queries.upd, to xml_handler()
  139. body = self.server.xml_handler(req_path)
  140. # In case that the requested path (req_path) is exactly
  141. # matched with XSD_URL_PATH
  142. elif req_path == XSD_URL_PATH:
  143. body = self.server.xsd_handler()
  144. # In case that the requested path (req_path) is exactly
  145. # matched with XSL_URL_PATH
  146. elif req_path == XSL_URL_PATH:
  147. body = self.server.xsl_handler()
  148. else:
  149. if 'Host' in self.headers.keys() and \
  150. ( req_path == '/' or req_path == XML_URL_PATH ):
  151. # redirect to XML URL only when requested with '/' or XML_URL_PATH
  152. toloc = "http://%s%s/" % (self.headers.get('Host'), XML_URL_PATH)
  153. self.send_response(302)
  154. self.send_header("Location", toloc)
  155. self.end_headers()
  156. return None
  157. else:
  158. # Couldn't find HOST
  159. self.send_error(404)
  160. return None
  161. except StatsHttpdDataError as err:
  162. # Couldn't find neither specified module name nor
  163. # specified item name
  164. self.send_error(404)
  165. logger.error(STATSHTTPD_SERVER_DATAERROR, err)
  166. return None
  167. except Exception as err:
  168. self.send_error(500)
  169. logger.error(STATSHTTPD_SERVER_ERROR, err)
  170. return None
  171. else:
  172. self.send_response(200)
  173. self.send_header("Content-type", "text/xml")
  174. self.send_header("Content-Length", len(body))
  175. self.send_header("Last-Modified", self.date_time_string(time.time()))
  176. self.end_headers()
  177. return body
  178. def log_message(self, format, *args):
  179. """overrides the parent method log_message()
  180. to use the bind10 logging framework.
  181. """
  182. logger.debug(DBG_STATSHTTPD_MESSAGING, STATSHTTPD_HTTPLOG,
  183. self.address_string(),
  184. format%args)
  185. class HttpServerError(Exception):
  186. """Exception class for HttpServer class. It is intended to be
  187. passed from the HttpServer object to the StatsHttpd object."""
  188. pass
  189. class HttpServer(http.server.HTTPServer):
  190. """HTTP Server class. The class inherits the super
  191. http.server.HTTPServer. Some parameters are specified as
  192. arguments, which are xml_handler, xsd_handler, xsl_handler, and
  193. log_writer. These all are parameters which the StatsHttpd object
  194. has. The handler parameters are references of functions which
  195. return body of each document. The last parameter log_writer is
  196. reference of writer function to just write to
  197. sys.stderr.write. They are intended to be referred by HttpHandler
  198. object."""
  199. def __init__(self, server_address, handler,
  200. xml_handler, xsd_handler, xsl_handler, log_writer):
  201. self.server_address = server_address
  202. self.xml_handler = xml_handler
  203. self.xsd_handler = xsd_handler
  204. self.xsl_handler = xsl_handler
  205. self.log_writer = log_writer
  206. http.server.HTTPServer.__init__(self, server_address, handler)
  207. class StatsHttpdError(Exception):
  208. """Exception class for StatsHttpd class. It is intended to be
  209. thrown from the the StatsHttpd object to the HttpHandler object or
  210. main routine."""
  211. pass
  212. class StatsHttpdDataError(Exception):
  213. """Exception class for StatsHttpd class. The reason seems to be
  214. due to the data. It is intended to be thrown from the the
  215. StatsHttpd object to the HttpHandler object or main routine."""
  216. pass
  217. class StatsHttpd:
  218. """The main class of HTTP server of HTTP/XML interface for
  219. statistics module. It handles HTTP requests, and command channel
  220. and config channel CC session. It uses select.select function
  221. while waiting for clients requests."""
  222. def __init__(self):
  223. self.running = False
  224. self.poll_intval = 0.5
  225. self.write_log = sys.stderr.write
  226. self.mccs = None
  227. self.httpd = []
  228. self.open_mccs()
  229. self.config = {}
  230. self.load_config()
  231. self.http_addrs = []
  232. self.mccs.start()
  233. try:
  234. self.open_httpd()
  235. except HttpServerError:
  236. # if some exception, e.g. address in use, is raised, then it closes mccs and httpd
  237. self.close_mccs()
  238. raise
  239. def open_mccs(self):
  240. """Opens a ModuleCCSession object"""
  241. # create ModuleCCSession
  242. logger.debug(DBG_STATSHTTPD_INIT, STATSHTTPD_STARTING_CC_SESSION)
  243. self.mccs = isc.config.ModuleCCSession(
  244. SPECFILE_LOCATION, self.config_handler, self.command_handler)
  245. self.cc_session = self.mccs._session
  246. def close_mccs(self):
  247. """Closes a ModuleCCSession object"""
  248. if self.mccs is None:
  249. return
  250. self.mccs.send_stopping()
  251. logger.debug(DBG_STATSHTTPD_INIT, STATSHTTPD_CLOSING_CC_SESSION)
  252. self.mccs.close()
  253. self.mccs = None
  254. def load_config(self, new_config={}):
  255. """Loads configuration from spec file or new configuration
  256. from the config manager"""
  257. # load config
  258. if len(self.config) == 0:
  259. self.config = dict([
  260. (itm['item_name'], self.mccs.get_value(itm['item_name'])[0])
  261. for itm in self.mccs.get_module_spec().get_config_spec()
  262. ])
  263. self.config.update(new_config)
  264. # set addresses and ports for HTTP
  265. addrs = []
  266. if 'listen_on' in self.config:
  267. for cf in self.config['listen_on']:
  268. if 'address' in cf and 'port' in cf:
  269. addrs.append((cf['address'], cf['port']))
  270. self.http_addrs = addrs
  271. def open_httpd(self):
  272. """Opens sockets for HTTP. Iterating each HTTP address to be
  273. configured in spec file"""
  274. for addr in self.http_addrs:
  275. self.httpd.append(self._open_httpd(addr))
  276. def _open_httpd(self, server_address):
  277. httpd = None
  278. try:
  279. # get address family for the server_address before
  280. # creating HttpServer object. If a specified address is
  281. # not numerical, gaierror may be thrown.
  282. address_family = socket.getaddrinfo(
  283. server_address[0], server_address[1], 0,
  284. socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST
  285. )[0][0]
  286. HttpServer.address_family = address_family
  287. httpd = HttpServer(
  288. server_address, HttpHandler,
  289. self.xml_handler, self.xsd_handler, self.xsl_handler,
  290. self.write_log)
  291. logger.info(STATSHTTPD_STARTED, server_address[0],
  292. server_address[1])
  293. return httpd
  294. except (socket.gaierror, socket.error,
  295. OverflowError, TypeError) as err:
  296. if httpd:
  297. httpd.server_close()
  298. raise HttpServerError(
  299. "Invalid address %s, port %s: %s: %s" %
  300. (server_address[0], server_address[1],
  301. err.__class__.__name__, err))
  302. def close_httpd(self):
  303. """Closes sockets for HTTP"""
  304. while len(self.httpd)>0:
  305. ht = self.httpd.pop()
  306. logger.info(STATSHTTPD_CLOSING, ht.server_address[0],
  307. ht.server_address[1])
  308. ht.server_close()
  309. def start(self):
  310. """Starts StatsHttpd objects to run. Waiting for client
  311. requests by using select.select functions"""
  312. self.running = True
  313. while self.running:
  314. try:
  315. (rfd, wfd, xfd) = select.select(
  316. self.get_sockets(), [], [], self.poll_intval)
  317. except select.error as err:
  318. # select.error exception is caught only in the case of
  319. # EINTR, or in other cases it is just thrown.
  320. if err.args[0] == errno.EINTR:
  321. (rfd, wfd, xfd) = ([], [], [])
  322. else:
  323. raise
  324. # FIXME: This module can handle only one request at a
  325. # time. If someone sends only part of the request, we block
  326. # waiting for it until we time out.
  327. # But it isn't so big issue for administration purposes.
  328. for fd in rfd + xfd:
  329. if fd == self.mccs.get_socket():
  330. self.mccs.check_command(nonblock=False)
  331. continue
  332. for ht in self.httpd:
  333. if fd == ht.socket:
  334. ht.handle_request()
  335. break
  336. self.stop()
  337. def stop(self):
  338. """Stops the running StatsHttpd objects. Closes CC session and
  339. HTTP handling sockets"""
  340. logger.info(STATSHTTPD_SHUTDOWN)
  341. self.close_httpd()
  342. self.close_mccs()
  343. self.running = False
  344. def get_sockets(self):
  345. """Returns sockets to select.select"""
  346. sockets = []
  347. if self.mccs is not None:
  348. sockets.append(self.mccs.get_socket())
  349. if len(self.httpd) > 0:
  350. for ht in self.httpd:
  351. sockets.append(ht.socket)
  352. return sockets
  353. def config_handler(self, new_config):
  354. """Config handler for the ModuleCCSession object. It resets
  355. addresses and ports to listen HTTP requests on."""
  356. logger.debug(DBG_STATSHTTPD_MESSAGING, STATSHTTPD_HANDLE_CONFIG,
  357. new_config)
  358. errors = []
  359. if not self.mccs.get_module_spec().\
  360. validate_config(False, new_config, errors):
  361. return isc.config.ccsession.create_answer(
  362. 1, ", ".join(errors))
  363. # backup old config
  364. old_config = self.config.copy()
  365. self.load_config(new_config)
  366. # If the http sockets aren't opened or
  367. # if new_config doesn't have'listen_on', it returns
  368. if len(self.httpd) == 0 or 'listen_on' not in new_config:
  369. return isc.config.ccsession.create_answer(0)
  370. self.close_httpd()
  371. try:
  372. self.open_httpd()
  373. except HttpServerError as err:
  374. logger.error(STATSHTTPD_SERVER_ERROR, err)
  375. # restore old config
  376. self.load_config(old_config)
  377. self.open_httpd()
  378. return isc.config.ccsession.create_answer(1, str(err))
  379. else:
  380. return isc.config.ccsession.create_answer(0)
  381. def command_handler(self, command, args):
  382. """Command handler for the ModuleCCSesson object. It handles
  383. "status" and "shutdown" commands."""
  384. if command == "status":
  385. logger.debug(DBG_STATSHTTPD_MESSAGING,
  386. STATSHTTPD_RECEIVED_STATUS_COMMAND)
  387. return isc.config.ccsession.create_answer(
  388. 0, "Stats Httpd is up. (PID " + str(os.getpid()) + ")")
  389. elif command == "shutdown":
  390. logger.debug(DBG_STATSHTTPD_MESSAGING,
  391. STATSHTTPD_RECEIVED_SHUTDOWN_COMMAND)
  392. self.running = False
  393. return isc.config.ccsession.create_answer(0)
  394. else:
  395. logger.debug(DBG_STATSHTTPD_MESSAGING,
  396. STATSHTTPD_RECEIVED_UNKNOWN_COMMAND, command)
  397. return isc.config.ccsession.create_answer(
  398. 1, "Unknown command: " + str(command))
  399. def get_stats_data(self, owner=None, name=None):
  400. """Requests statistics data to the Stats daemon and returns
  401. the data which obtains from it. The first argument is the
  402. module name which owns the statistics data, the second
  403. argument is one name of the statistics items which the the
  404. module owns. The second argument cannot be specified when the
  405. first argument is not specified. It returns the statistics
  406. data of the specified module or item. When the session timeout
  407. or the session error is occurred, it raises
  408. StatsHttpdError. When the stats daemon returns none-zero
  409. value, it raises StatsHttpdDataError."""
  410. param = {}
  411. if owner is None and name is None:
  412. param = None
  413. if owner is not None:
  414. param['owner'] = owner
  415. if name is not None:
  416. param['name'] = name
  417. try:
  418. seq = self.cc_session.group_sendmsg(
  419. isc.config.ccsession.create_command('show', param), 'Stats')
  420. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  421. if answer:
  422. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  423. except (isc.cc.session.SessionTimeout,
  424. isc.cc.session.SessionError) as err:
  425. raise StatsHttpdError("%s: %s" %
  426. (err.__class__.__name__, err))
  427. else:
  428. if rcode == 0:
  429. return value
  430. else:
  431. raise StatsHttpdDataError("Stats module: %s" % str(value))
  432. def get_stats_spec(self, owner=None, name=None):
  433. """Requests statistics data to the Stats daemon and returns
  434. the data which obtains from it. The first argument is the
  435. module name which owns the statistics data, the second
  436. argument is one name of the statistics items which the the
  437. module owns. The second argument cannot be specified when the
  438. first argument is not specified. It returns the statistics
  439. specification of the specified module or item. When the
  440. session timeout or the session error is occurred, it raises
  441. StatsHttpdError. When the stats daemon returns none-zero
  442. value, it raises StatsHttpdDataError."""
  443. param = {}
  444. if owner is None and name is None:
  445. param = None
  446. if owner is not None:
  447. param['owner'] = owner
  448. if name is not None:
  449. param['name'] = name
  450. try:
  451. seq = self.cc_session.group_sendmsg(
  452. isc.config.ccsession.create_command('showschema', param), 'Stats')
  453. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  454. if answer:
  455. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  456. if rcode == 0:
  457. return value
  458. else:
  459. raise StatsHttpdDataError("Stats module: %s" % str(value))
  460. except (isc.cc.session.SessionTimeout,
  461. isc.cc.session.SessionError) as err:
  462. raise StatsHttpdError("%s: %s" %
  463. (err.__class__.__name__, err))
  464. def xml_handler(self, path=''):
  465. """Requests the specified statistics data and specification by
  466. using the functions get_stats_data and get_stats_spec
  467. respectively and loads the XML template file and returns the
  468. string of the XML document.The argument is a path in the
  469. requested URI. For example, the path is assumed to be like
  470. ${module_name}/${top_level_item_name}/${second_level_item_name}/..."""
  471. dirs = [ d for d in path.split("/") if len(d) > 0 ]
  472. module_name = None
  473. item_name = None
  474. if len(dirs) > 0:
  475. module_name = dirs[0]
  476. if len(dirs) > 1:
  477. item_name = dirs[1]
  478. # removed an index string when list-type value is
  479. # requested. Because such a item name can be accept by the
  480. # stats module currently.
  481. item_name = re.sub('\[\d+\]$', '', item_name)
  482. stats_spec = self.get_stats_spec(module_name, item_name)
  483. stats_data = self.get_stats_data(module_name, item_name)
  484. path_list = []
  485. try:
  486. path_list = item_name_list(stats_data, path)
  487. except isc.cc.data.DataNotFoundError as err:
  488. raise StatsHttpdDataError(
  489. "%s: %s" % (err.__class__.__name__, err))
  490. item_list = []
  491. for path in path_list:
  492. dirs = path.split("/")
  493. if len(dirs) < 2: continue
  494. item = {}
  495. item['identifier'] = path
  496. value = isc.cc.data.find(stats_data, path)
  497. if type(value) is bool:
  498. value = str(value).lower()
  499. if type(value) is dict or type(value) is list:
  500. value = None
  501. if value is not None:
  502. item['value'] = str(value)
  503. owner = dirs[0]
  504. item['owner'] = owner
  505. item['uri'] = urllib.parse.quote('%s/%s' % (XML_URL_PATH, path))
  506. item_path = '/'.join(dirs[1:])
  507. spec = isc.config.find_spec_part(stats_spec[owner], item_path)
  508. for key in ['name', 'type', 'description', 'title', \
  509. 'optional', 'default', 'format']:
  510. value = spec.get('item_%s' % key)
  511. if type(value) is bool:
  512. value = str(value).lower()
  513. if type(value) is dict or type(value) is list:
  514. value = None
  515. if value is not None:
  516. item[key] = str(value)
  517. item_list.append(item)
  518. xml_elem = xml.etree.ElementTree.Element(
  519. XML_ROOT_ELEMENT, attrib=XML_ROOT_ATTRIB)
  520. for item in item_list:
  521. item_elem = xml.etree.ElementTree.Element('item', attrib=item)
  522. xml_elem.append(item_elem)
  523. # The coding conversion is tricky. xml..tostring() of Python 3.2
  524. # returns bytes (not string) regardless of the coding, while
  525. # tostring() of Python 3.1 returns a string. To support both
  526. # cases transparently, we first make sure tostring() returns
  527. # bytes by specifying utf-8 and then convert the result to a
  528. # plain string (code below assume it).
  529. # FIXME: Non-ASCII characters might be lost here. Consider how
  530. # the whole system should handle non-ASCII characters.
  531. xml_string = str(xml.etree.ElementTree.tostring(xml_elem, encoding='utf-8'),
  532. encoding='us-ascii')
  533. self.xml_body = self.open_template(XML_TEMPLATE_LOCATION).substitute(
  534. xml_string=xml_string, xsl_url_path=XSL_URL_PATH)
  535. return self.xml_body
  536. def xsd_handler(self):
  537. """Loads the XSD template file, replaces the variable strings,
  538. and returns the string of the XSD document."""
  539. self.xsd_body = self.open_template(XSD_TEMPLATE_LOCATION).substitute(
  540. xsd_namespace=XSD_NAMESPACE)
  541. return self.xsd_body
  542. def xsl_handler(self):
  543. """Loads the XSL template file, replaces the variable strings,
  544. and returns the string of the XSL document."""
  545. self.xsl_body = self.open_template(XSL_TEMPLATE_LOCATION).substitute(
  546. xsd_namespace=XSD_NAMESPACE)
  547. return self.xsl_body
  548. def open_template(self, file_name):
  549. """It opens a template file, and it loads all lines to a
  550. string variable and returns string. Template object includes
  551. the variable. Limitation of a file size isn't needed there. XXXX"""
  552. lines = None
  553. try:
  554. with open(file_name, 'r') as f:
  555. lines = "".join(f.readlines())
  556. except IOError as err:
  557. raise StatsHttpdDataError(
  558. "%s: %s" % (err.__class__.__name__, err))
  559. return string.Template(lines)
  560. if __name__ == "__main__":
  561. try:
  562. parser = OptionParser()
  563. parser.add_option(
  564. "-v", "--verbose", dest="verbose", action="store_true",
  565. help="enable maximum debug logging")
  566. (options, args) = parser.parse_args()
  567. if options.verbose:
  568. isc.log.init("b10-stats-httpd", "DEBUG", 99, buffer=True)
  569. stats_httpd = StatsHttpd()
  570. stats_httpd.start()
  571. except OptionValueError as ove:
  572. logger.fatal(STATSHTTPD_BAD_OPTION_VALUE, ove)
  573. sys.exit(1)
  574. except isc.cc.session.SessionError as se:
  575. logger.fatal(STATSHTTPD_CC_SESSION_ERROR, se)
  576. sys.exit(1)
  577. except HttpServerError as hse:
  578. logger.fatal(STATSHTTPD_START_SERVER_INIT_ERROR, hse)
  579. sys.exit(1)
  580. except KeyboardInterrupt as kie:
  581. logger.info(STATSHTTPD_STOPPED_BY_KEYBOARD)
  582. logger.info(STATSHTTPD_EXITING)