stats_httpd.py.in 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #!@PYTHON@
  2. # Copyright (C) 2011 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 isc.cc
  31. import isc.config
  32. import isc.util.process
  33. import isc.log
  34. from isc.log_messages.stats_httpd_messages import *
  35. isc.log.init("b10-stats-httpd")
  36. logger = isc.log.Logger("stats-httpd")
  37. # Some constants for debug levels.
  38. DBG_STATHTTPD_INIT = logger.DBGLVL_START_SHUT
  39. DBG_STATHTTPD_MESSAGING = logger.DBGLVL_COMMAND
  40. # If B10_FROM_SOURCE is set in the environment, we use data files
  41. # from a directory relative to that, otherwise we use the ones
  42. # installed on the system
  43. if "B10_FROM_SOURCE" in os.environ:
  44. BASE_LOCATION = os.environ["B10_FROM_SOURCE"] + os.sep + \
  45. "src" + os.sep + "bin" + os.sep + "stats"
  46. else:
  47. PREFIX = "@prefix@"
  48. DATAROOTDIR = "@datarootdir@"
  49. BASE_LOCATION = "@datadir@" + os.sep + "@PACKAGE@"
  50. BASE_LOCATION = BASE_LOCATION.replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  51. SPECFILE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd.spec"
  52. XML_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xml.tpl"
  53. XSD_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsd.tpl"
  54. XSL_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsl.tpl"
  55. # These variables are paths part of URL.
  56. # eg. "http://${address}" + XXX_URL_PATH
  57. XML_URL_PATH = '/bind10/statistics/xml'
  58. XSD_URL_PATH = '/bind10/statistics/xsd'
  59. XSL_URL_PATH = '/bind10/statistics/xsl'
  60. # TODO: This should be considered later.
  61. XSD_NAMESPACE = 'http://bind10.isc.org/bind10'
  62. # Assign this process name
  63. isc.util.process.rename()
  64. class HttpHandler(http.server.BaseHTTPRequestHandler):
  65. """HTTP handler class for HttpServer class. The class inhrits the super
  66. class http.server.BaseHTTPRequestHandler. It implemets do_GET()
  67. and do_HEAD() and orverrides log_message()"""
  68. def do_GET(self):
  69. body = self.send_head()
  70. if body is not None:
  71. self.wfile.write(body.encode())
  72. def do_HEAD(self):
  73. self.send_head()
  74. def send_head(self):
  75. try:
  76. req_path = self.path
  77. req_path = urllib.parse.urlsplit(req_path).path
  78. req_path = urllib.parse.unquote(req_path)
  79. req_path = os.path.normpath(req_path)
  80. path_dirs = req_path.split('/')
  81. path_dirs = [ d for d in filter(None, path_dirs) ]
  82. module_name = None
  83. item_name = None
  84. # in case of /bind10/statistics/xxx/YYY/zzz/
  85. if len(path_dirs) >= 5:
  86. item_name = path_dirs[4]
  87. # in case of /bind10/statistics/xxx/YYY/
  88. if len(path_dirs) >= 4:
  89. module_name = path_dirs[3]
  90. if req_path.startswith(XML_URL_PATH):
  91. body = self.server.xml_handler(module_name, item_name)
  92. elif req_path.startswith(XSD_URL_PATH):
  93. body = self.server.xsd_handler(module_name, item_name)
  94. elif req_path.startswith(XSL_URL_PATH):
  95. body = self.server.xsl_handler(module_name, item_name)
  96. else:
  97. if req_path == '/' and 'Host' in self.headers.keys():
  98. # redirect to XML URL only when requested with '/'
  99. self.send_response(302)
  100. self.send_header(
  101. "Location",
  102. "http://" + self.headers.get('Host') + XML_URL_PATH)
  103. self.end_headers()
  104. return None
  105. else:
  106. # Couldn't find HOST
  107. self.send_error(404)
  108. return None
  109. except StatsHttpdError as err:
  110. # Couldn't find neither specified module name nor
  111. # specified item name
  112. if str(err).startswith('Stats module: specified arguments are incorrect:'):
  113. self.send_error(404)
  114. else:
  115. self.send_error(500)
  116. logger.error(STATHTTPD_SERVER_ERROR, err)
  117. return None
  118. else:
  119. self.send_response(200)
  120. self.send_header("Content-type", "text/xml")
  121. self.send_header("Content-Length", len(body))
  122. self.end_headers()
  123. return body
  124. class HttpServerError(Exception):
  125. """Exception class for HttpServer class. It is intended to be
  126. passed from the HttpServer object to the StatsHttpd object."""
  127. pass
  128. class HttpServer(http.server.HTTPServer):
  129. """HTTP Server class. The class inherits the super
  130. http.server.HTTPServer. Some parameters are specified as
  131. arguments, which are xml_handler, xsd_handler, xsl_handler, and
  132. log_writer. These all are parameters which the StatsHttpd object
  133. has. The handler parameters are references of functions which
  134. return body of each document. The last parameter log_writer is
  135. reference of writer function to just write to
  136. sys.stderr.write. They are intended to be referred by HttpHandler
  137. object."""
  138. def __init__(self, server_address, handler,
  139. xml_handler, xsd_handler, xsl_handler, log_writer):
  140. self.server_address = server_address
  141. self.xml_handler = xml_handler
  142. self.xsd_handler = xsd_handler
  143. self.xsl_handler = xsl_handler
  144. self.log_writer = log_writer
  145. http.server.HTTPServer.__init__(self, server_address, handler)
  146. class StatsHttpdError(Exception):
  147. """Exception class for StatsHttpd class. It is intended to be
  148. thrown from the the StatsHttpd object to the HttpHandler object or
  149. main routine."""
  150. pass
  151. class StatsHttpd:
  152. """The main class of HTTP server of HTTP/XML interface for
  153. statistics module. It handles HTTP requests, and command channel
  154. and config channel CC session. It uses select.select function
  155. while waiting for clients requests."""
  156. def __init__(self):
  157. self.running = False
  158. self.poll_intval = 0.5
  159. self.write_log = sys.stderr.write
  160. self.mccs = None
  161. self.httpd = []
  162. self.open_mccs()
  163. self.config = {}
  164. self.load_config()
  165. self.http_addrs = []
  166. self.mccs.start()
  167. self.open_httpd()
  168. def open_mccs(self):
  169. """Opens a ModuleCCSession object"""
  170. # create ModuleCCSession
  171. logger.debug(DBG_STATHTTPD_INIT, STATHTTPD_STARTING_CC_SESSION)
  172. self.mccs = isc.config.ModuleCCSession(
  173. SPECFILE_LOCATION, self.config_handler, self.command_handler)
  174. self.cc_session = self.mccs._session
  175. def close_mccs(self):
  176. """Closes a ModuleCCSession object"""
  177. if self.mccs is None:
  178. return
  179. logger.debug(DBG_STATHTTPD_INIT, STATHTTPD_CLOSING_CC_SESSION)
  180. self.mccs.close()
  181. self.mccs = None
  182. def load_config(self, new_config={}):
  183. """Loads configuration from spec file or new configuration
  184. from the config manager"""
  185. # load config
  186. if len(self.config) == 0:
  187. self.config = dict([
  188. (itm['item_name'], self.mccs.get_value(itm['item_name'])[0])
  189. for itm in self.mccs.get_module_spec().get_config_spec()
  190. ])
  191. self.config.update(new_config)
  192. # set addresses and ports for HTTP
  193. addrs = []
  194. if 'listen_on' in self.config:
  195. for cf in self.config['listen_on']:
  196. if 'address' in cf and 'port' in cf:
  197. addrs.append((cf['address'], cf['port']))
  198. self.http_addrs = addrs
  199. def open_httpd(self):
  200. """Opens sockets for HTTP. Iterating each HTTP address to be
  201. configured in spec file"""
  202. for addr in self.http_addrs:
  203. self.httpd.append(self._open_httpd(addr))
  204. def _open_httpd(self, server_address):
  205. httpd = None
  206. try:
  207. # get address family for the server_address before
  208. # creating HttpServer object. If a specified address is
  209. # not numerical, gaierror may be thrown.
  210. address_family = socket.getaddrinfo(
  211. server_address[0], server_address[1], 0,
  212. socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST
  213. )[0][0]
  214. HttpServer.address_family = address_family
  215. httpd = HttpServer(
  216. server_address, HttpHandler,
  217. self.xml_handler, self.xsd_handler, self.xsl_handler,
  218. self.write_log)
  219. logger.info(STATHTTPD_STARTED, server_address[0],
  220. server_address[1])
  221. return httpd
  222. except (socket.gaierror, socket.error,
  223. OverflowError, TypeError) as err:
  224. if httpd:
  225. httpd.server_close()
  226. raise HttpServerError(
  227. "Invalid address %s, port %s: %s: %s" %
  228. (server_address[0], server_address[1],
  229. err.__class__.__name__, err))
  230. def close_httpd(self):
  231. """Closes sockets for HTTP"""
  232. while len(self.httpd)>0:
  233. ht = self.httpd.pop()
  234. logger.info(STATHTTPD_CLOSING, ht.server_address[0],
  235. ht.server_address[1])
  236. ht.server_close()
  237. def start(self):
  238. """Starts StatsHttpd objects to run. Waiting for client
  239. requests by using select.select functions"""
  240. self.running = True
  241. while self.running:
  242. try:
  243. (rfd, wfd, xfd) = select.select(
  244. self.get_sockets(), [], [], self.poll_intval)
  245. except select.error as err:
  246. # select.error exception is caught only in the case of
  247. # EINTR, or in other cases it is just thrown.
  248. if err.args[0] == errno.EINTR:
  249. (rfd, wfd, xfd) = ([], [], [])
  250. else:
  251. raise
  252. # FIXME: This module can handle only one request at a
  253. # time. If someone sends only part of the request, we block
  254. # waiting for it until we time out.
  255. # But it isn't so big issue for administration purposes.
  256. for fd in rfd + xfd:
  257. if fd == self.mccs.get_socket():
  258. self.mccs.check_command(nonblock=False)
  259. continue
  260. for ht in self.httpd:
  261. if fd == ht.socket:
  262. ht.handle_request()
  263. break
  264. self.stop()
  265. def stop(self):
  266. """Stops the running StatsHttpd objects. Closes CC session and
  267. HTTP handling sockets"""
  268. logger.info(STATHTTPD_SHUTDOWN)
  269. self.close_httpd()
  270. self.close_mccs()
  271. self.running = False
  272. def get_sockets(self):
  273. """Returns sockets to select.select"""
  274. sockets = []
  275. if self.mccs is not None:
  276. sockets.append(self.mccs.get_socket())
  277. if len(self.httpd) > 0:
  278. for ht in self.httpd:
  279. sockets.append(ht.socket)
  280. return sockets
  281. def config_handler(self, new_config):
  282. """Config handler for the ModuleCCSession object. It resets
  283. addresses and ports to listen HTTP requests on."""
  284. logger.debug(DBG_STATHTTPD_MESSAGING, STATHTTPD_HANDLE_CONFIG,
  285. new_config)
  286. errors = []
  287. if not self.mccs.get_module_spec().\
  288. validate_config(False, new_config, errors):
  289. return isc.config.ccsession.create_answer(
  290. 1, ", ".join(errors))
  291. # backup old config
  292. old_config = self.config.copy()
  293. self.load_config(new_config)
  294. # If the http sockets aren't opened or
  295. # if new_config doesn't have'listen_on', it returns
  296. if len(self.httpd) == 0 or 'listen_on' not in new_config:
  297. return isc.config.ccsession.create_answer(0)
  298. self.close_httpd()
  299. try:
  300. self.open_httpd()
  301. except HttpServerError as err:
  302. logger.error(STATHTTPD_SERVER_ERROR, err)
  303. # restore old config
  304. self.load_config(old_config)
  305. self.open_httpd()
  306. return isc.config.ccsession.create_answer(1, str(err))
  307. else:
  308. return isc.config.ccsession.create_answer(0)
  309. def command_handler(self, command, args):
  310. """Command handler for the ModuleCCSesson object. It handles
  311. "status" and "shutdown" commands."""
  312. if command == "status":
  313. logger.debug(DBG_STATHTTPD_MESSAGING,
  314. STATHTTPD_RECEIVED_STATUS_COMMAND)
  315. return isc.config.ccsession.create_answer(
  316. 0, "Stats Httpd is up. (PID " + str(os.getpid()) + ")")
  317. elif command == "shutdown":
  318. logger.debug(DBG_STATHTTPD_MESSAGING,
  319. STATHTTPD_RECEIVED_SHUTDOWN_COMMAND)
  320. self.running = False
  321. return isc.config.ccsession.create_answer(0)
  322. else:
  323. logger.debug(DBG_STATHTTPD_MESSAGING,
  324. STATHTTPD_RECEIVED_UNKNOWN_COMMAND, command)
  325. return isc.config.ccsession.create_answer(
  326. 1, "Unknown command: " + str(command))
  327. def get_stats_data(self, owner=None, name=None):
  328. """Requests statistics data to the Stats daemon and returns
  329. the data which obtains from it. args are owner and name."""
  330. param = {}
  331. if owner is None and name is None:
  332. param = None
  333. if owner is not None:
  334. param['owner'] = owner
  335. if name is not None:
  336. param['name'] = name
  337. try:
  338. seq = self.cc_session.group_sendmsg(
  339. isc.config.ccsession.create_command('show', param), 'Stats')
  340. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  341. if answer:
  342. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  343. except (isc.cc.session.SessionTimeout,
  344. isc.cc.session.SessionError) as err:
  345. raise StatsHttpdError("%s: %s" %
  346. (err.__class__.__name__, err))
  347. else:
  348. if rcode == 0:
  349. return value
  350. else:
  351. raise StatsHttpdError("Stats module: %s" % str(value))
  352. def get_stats_spec(self, owner=None, name=None):
  353. """Requests statistics data to the Stats daemon and returns
  354. the data which obtains from it. args are owner and name."""
  355. param = {}
  356. if owner is None and name is None:
  357. param = None
  358. if owner is not None:
  359. param['owner'] = owner
  360. if name is not None:
  361. param['name'] = name
  362. try:
  363. seq = self.cc_session.group_sendmsg(
  364. isc.config.ccsession.create_command('showschema', param), 'Stats')
  365. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  366. if answer:
  367. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  368. if rcode == 0:
  369. return value
  370. else:
  371. raise StatsHttpdError("Stats module: %s" % str(value))
  372. except (isc.cc.session.SessionTimeout,
  373. isc.cc.session.SessionError) as err:
  374. raise StatsHttpdError("%s: %s" %
  375. (err.__class__.__name__, err))
  376. def xml_handler(self, module_name=None, item_name=None):
  377. """Handler which requests to Stats daemon to obtain statistics
  378. data and returns the body of XML document"""
  379. def stats_data2xml(stats_spec, stats_data, xml_elem):
  380. """Internal use for xml_handler. Reads stats_data and
  381. stats_spec specified as first and second arguments, and
  382. modify the xml object specified as third
  383. argument. xml_elem must be modified and always returns
  384. None."""
  385. # assumed started with module_spec or started with
  386. # item_spec in statistics
  387. if type(stats_spec) is dict:
  388. # assumed started with module_spec
  389. if 'item_name' not in stats_spec \
  390. and 'item_type' not in stats_spec:
  391. for module_name in stats_spec.keys():
  392. elem = xml.etree.ElementTree.Element(module_name)
  393. stats_data2xml(stats_spec[module_name],
  394. stats_data[module_name], elem)
  395. xml_elem.append(elem)
  396. # started with item_spec in statistics
  397. else:
  398. elem = xml.etree.ElementTree.Element(stats_spec['item_name'])
  399. if stats_spec['item_type'] == 'map':
  400. stats_data2xml(stats_spec['map_item_spec'],
  401. stats_data,
  402. elem)
  403. elif stats_spec['item_type'] == 'list':
  404. for item in stats_data:
  405. stats_data2xml(stats_spec['list_item_spec'],
  406. item, elem)
  407. else:
  408. elem.text = str(stats_data)
  409. xml_elem.append(elem)
  410. # assumed started with stats_spec
  411. elif type(stats_spec) is list:
  412. for item_spec in stats_spec:
  413. stats_data2xml(item_spec,
  414. stats_data[item_spec['item_name']],
  415. xml_elem)
  416. return None
  417. stats_spec = self.get_stats_spec(module_name, item_name)
  418. stats_data = self.get_stats_data(module_name, item_name)
  419. xml_elem = xml.etree.ElementTree.Element(
  420. 'bind10:statistics',
  421. attrib={ 'xsi:schemaLocation' : XSD_NAMESPACE + ' ' + XSD_URL_PATH,
  422. 'xmlns:bind10' : XSD_NAMESPACE,
  423. 'xmlns:xsi' : "http://www.w3.org/2001/XMLSchema-instance" })
  424. stats_data2xml(stats_spec, stats_data, xml_elem)
  425. # The coding conversion is tricky. xml..tostring() of Python 3.2
  426. # returns bytes (not string) regardless of the coding, while
  427. # tostring() of Python 3.1 returns a string. To support both
  428. # cases transparently, we first make sure tostring() returns
  429. # bytes by specifying utf-8 and then convert the result to a
  430. # plain string (code below assume it).
  431. xml_string = str(xml.etree.ElementTree.tostring(xml_elem, encoding='utf-8'),
  432. encoding='us-ascii')
  433. self.xml_body = self.open_template(XML_TEMPLATE_LOCATION).substitute(
  434. xml_string=xml_string,
  435. xsl_url_path=XSL_URL_PATH)
  436. assert self.xml_body is not None
  437. return self.xml_body
  438. def xsd_handler(self, module_name=None, item_name=None):
  439. """Handler which just returns the body of XSD document"""
  440. def stats_spec2xsd(stats_spec, xsd_elem):
  441. """Internal use for xsd_handler. Reads stats_spec
  442. specified as first arguments, and modify the xml object
  443. specified as second argument. xsd_elem must be
  444. modified. Always returns None with no exceptions."""
  445. # assumed module_spec or one stats_spec
  446. if type(stats_spec) is dict:
  447. # assumed module_spec
  448. if 'item_name' not in stats_spec:
  449. for mod in stats_spec.keys():
  450. elem = xml.etree.ElementTree.Element(
  451. "element", { "name" : mod })
  452. complextype = xml.etree.ElementTree.Element("complexType")
  453. alltag = xml.etree.ElementTree.Element("all")
  454. stats_spec2xsd(stats_spec[mod], alltag)
  455. complextype.append(alltag)
  456. elem.append(complextype)
  457. xsd_elem.append(elem)
  458. # assumed stats_spec
  459. else:
  460. if stats_spec['item_type'] == 'map':
  461. alltag = xml.etree.ElementTree.Element("all")
  462. stats_spec2xsd(stats_spec['map_item_spec'], alltag)
  463. complextype = xml.etree.ElementTree.Element("complexType")
  464. complextype.append(alltag)
  465. elem = xml.etree.ElementTree.Element(
  466. "element", { "name" : stats_spec["item_name"] })
  467. elem.append(complextype)
  468. xsd_elem.append(elem)
  469. elif stats_spec['item_type'] == 'list':
  470. alltag = xml.etree.ElementTree.Element("all")
  471. stats_spec2xsd(stats_spec['list_item_spec'], alltag)
  472. complextype = xml.etree.ElementTree.Element("complexType")
  473. complextype.append(alltag)
  474. elem = xml.etree.ElementTree.Element(
  475. "element", { "name" : stats_spec["item_name"] })
  476. elem.append(complextype)
  477. xsd_elem.append(elem)
  478. else:
  479. elem = xml.etree.ElementTree.Element(
  480. "element",
  481. attrib={
  482. 'name' : stats_spec["item_name"],
  483. 'type' : stats_spec["item_type"] \
  484. if stats_spec["item_type"].lower() != 'real' \
  485. else 'float',
  486. 'minOccurs' : "1",
  487. 'maxOccurs' : "1"
  488. }
  489. )
  490. annotation = xml.etree.ElementTree.Element("annotation")
  491. appinfo = xml.etree.ElementTree.Element("appinfo")
  492. documentation = xml.etree.ElementTree.Element("documentation")
  493. if "item_title" in stats_spec:
  494. appinfo.text = stats_spec["item_title"]
  495. if "item_description" in stats_spec:
  496. documentation.text = stats_spec["item_description"]
  497. annotation.append(appinfo)
  498. annotation.append(documentation)
  499. elem.append(annotation)
  500. xsd_elem.append(elem)
  501. # multiple stats_specs
  502. elif type(stats_spec) is list:
  503. for item_spec in stats_spec:
  504. stats_spec2xsd(item_spec, xsd_elem)
  505. return None
  506. # for XSD
  507. stats_spec = self.get_stats_spec(module_name, item_name)
  508. alltag = xml.etree.ElementTree.Element("all")
  509. stats_spec2xsd(stats_spec, alltag)
  510. complextype = xml.etree.ElementTree.Element("complexType")
  511. complextype.append(alltag)
  512. documentation = xml.etree.ElementTree.Element("documentation")
  513. documentation.text = "A set of statistics data"
  514. annotation = xml.etree.ElementTree.Element("annotation")
  515. annotation.append(documentation)
  516. elem = xml.etree.ElementTree.Element(
  517. "element", attrib={ 'name' : 'statistics' })
  518. elem.append(annotation)
  519. elem.append(complextype)
  520. documentation = xml.etree.ElementTree.Element("documentation")
  521. documentation.text = "XML schema of the statistics data in BIND 10"
  522. annotation = xml.etree.ElementTree.Element("annotation")
  523. annotation.append(documentation)
  524. xsd_root = xml.etree.ElementTree.Element(
  525. "schema",
  526. attrib={ 'xmlns' : "http://www.w3.org/2001/XMLSchema",
  527. 'targetNamespace' : XSD_NAMESPACE,
  528. 'xmlns:bind10' : XSD_NAMESPACE })
  529. xsd_root.append(annotation)
  530. xsd_root.append(elem)
  531. # The coding conversion is tricky. xml..tostring() of Python 3.2
  532. # returns bytes (not string) regardless of the coding, while
  533. # tostring() of Python 3.1 returns a string. To support both
  534. # cases transparently, we first make sure tostring() returns
  535. # bytes by specifying utf-8 and then convert the result to a
  536. # plain string (code below assume it).
  537. xsd_string = str(xml.etree.ElementTree.tostring(xsd_root, encoding='utf-8'),
  538. encoding='us-ascii')
  539. self.xsd_body = self.open_template(XSD_TEMPLATE_LOCATION).substitute(
  540. xsd_string=xsd_string)
  541. assert self.xsd_body is not None
  542. return self.xsd_body
  543. def xsl_handler(self, module_name=None, item_name=None):
  544. """Handler which just returns the body of XSL document"""
  545. def stats_spec2xsl(stats_spec, xsl_elem, path='.'):
  546. """Internal use for xsl_handler. Reads stats_spec
  547. specified as first arguments, and modify the xml object
  548. specified as second argument. xsl_elem must be
  549. modified. The third argument is a base path used for
  550. making anchor tag in XSL. Always returns None with no
  551. exceptions."""
  552. # assumed module_spec or one stats_spec
  553. if type(stats_spec) is dict:
  554. # assumed module_spec
  555. if 'item_name' not in stats_spec:
  556. table = xml.etree.ElementTree.Element("table")
  557. tr = xml.etree.ElementTree.Element("tr")
  558. th = xml.etree.ElementTree.Element("th")
  559. th.text = "Module Names"
  560. tr.append(th)
  561. th = xml.etree.ElementTree.Element("th")
  562. th.text = "Module Items"
  563. tr.append(th)
  564. table.append(tr)
  565. for mod in stats_spec.keys():
  566. foreach = xml.etree.ElementTree.Element(
  567. "xsl:for-each", attrib={ "select" : mod })
  568. tr = xml.etree.ElementTree.Element("tr")
  569. td = xml.etree.ElementTree.Element("td")
  570. a = xml.etree.ElementTree.Element(
  571. "a", attrib={ "href": urllib.parse.quote(path + "/" + mod) })
  572. a.text = mod
  573. td.append(a)
  574. tr.append(td)
  575. td = xml.etree.ElementTree.Element("td")
  576. stats_spec2xsl(stats_spec[mod], td,
  577. path + "/" + mod)
  578. tr.append(td)
  579. foreach.append(tr)
  580. table.append(foreach)
  581. xsl_elem.append(table)
  582. # assumed stats_spec
  583. else:
  584. tr = xml.etree.ElementTree.Element("tr")
  585. td = xml.etree.ElementTree.Element(
  586. "td",
  587. attrib={ "class" : "title",
  588. "title" : stats_spec["item_description"] \
  589. if "item_description" in stats_spec \
  590. else "" })
  591. a = xml.etree.ElementTree.Element(
  592. "a", attrib={ "href": urllib.parse.quote(path + "/" + stats_spec["item_name"]) })
  593. a.text = stats_spec["item_name"]
  594. td.append(a)
  595. tr.append(td)
  596. if stats_spec['item_type'] == 'map':
  597. stats_spec2xsl(stats_spec['map_item_spec'], tr,
  598. path + "/" + stats_spec["item_name"])
  599. elif stats_spec['item_type'] == 'list':
  600. stats_spec2xsl(stats_spec['list_item_spec'], tr,
  601. path + "/" + stats_spec["item_name"])
  602. else:
  603. td = xml.etree.ElementTree.Element("td")
  604. xsl_valueof = xml.etree.ElementTree.Element(
  605. "xsl:value-of",
  606. attrib={'select': stats_spec["item_name"]})
  607. td.append(xsl_valueof)
  608. tr.append(td)
  609. xsl_elem.append(tr)
  610. # multiple stats_specs
  611. elif type(stats_spec) is list:
  612. table = xml.etree.ElementTree.Element("table")
  613. tr = xml.etree.ElementTree.Element("tr")
  614. th = xml.etree.ElementTree.Element("th")
  615. th.text = "Item Names"
  616. tr.append(th)
  617. th = xml.etree.ElementTree.Element("th")
  618. th.text = "Item Values"
  619. tr.append(th)
  620. table.append(tr)
  621. for item_spec in stats_spec:
  622. stats_spec2xsl(item_spec, table, path)
  623. xsl_elem.append(table)
  624. return None
  625. # for XSL
  626. stats_spec = self.get_stats_spec(module_name, item_name)
  627. xsd_root = xml.etree.ElementTree.Element( # started with xml:template tag
  628. "xsl:template",
  629. attrib={'match': "bind10:statistics"})
  630. if module_name is not None and item_name is not None:
  631. stats_spec2xsl([ stats_spec ], xsd_root, './' + module_name)
  632. elif module_name is not None:
  633. stats_spec2xsl(stats_spec, xsd_root, './' + module_name)
  634. else:
  635. stats_spec2xsl(stats_spec, xsd_root)
  636. # The coding conversion is tricky. xml..tostring() of Python 3.2
  637. # returns bytes (not string) regardless of the coding, while
  638. # tostring() of Python 3.1 returns a string. To support both
  639. # cases transparently, we first make sure tostring() returns
  640. # bytes by specifying utf-8 and then convert the result to a
  641. # plain string (code below assume it).
  642. xsl_string = str(xml.etree.ElementTree.tostring(xsd_root, encoding='utf-8'),
  643. encoding='us-ascii')
  644. self.xsl_body = self.open_template(XSL_TEMPLATE_LOCATION).substitute(
  645. xsl_string=xsl_string,
  646. xsd_namespace=XSD_NAMESPACE)
  647. assert self.xsl_body is not None
  648. return self.xsl_body
  649. def open_template(self, file_name):
  650. """It opens a template file, and it loads all lines to a
  651. string variable and returns string. Template object includes
  652. the variable. Limitation of a file size isn't needed there."""
  653. f = open(file_name, 'r')
  654. lines = "".join(f.readlines())
  655. f.close()
  656. assert lines is not None
  657. return string.Template(lines)
  658. if __name__ == "__main__":
  659. try:
  660. parser = OptionParser()
  661. parser.add_option(
  662. "-v", "--verbose", dest="verbose", action="store_true",
  663. help="display more about what is going on")
  664. (options, args) = parser.parse_args()
  665. if options.verbose:
  666. isc.log.init("b10-stats-httpd", "DEBUG", 99)
  667. stats_httpd = StatsHttpd()
  668. stats_httpd.start()
  669. except OptionValueError as ove:
  670. logger.fatal(STATHTTPD_BAD_OPTION_VALUE, ove)
  671. sys.exit(1)
  672. except isc.cc.session.SessionError as se:
  673. logger.fatal(STATHTTPD_CC_SESSION_ERROR, se)
  674. sys.exit(1)
  675. except HttpServerError as hse:
  676. logger.fatal(STATHTTPD_START_SERVER_INIT_ERROR, hse)
  677. sys.exit(1)
  678. except KeyboardInterrupt as kie:
  679. logger.info(STATHTTPD_STOPPED_BY_KEYBOARD)