stats_httpd.py.in 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 isc.cc
  30. import isc.config
  31. import isc.util.process
  32. # If B10_FROM_SOURCE is set in the environment, we use data files
  33. # from a directory relative to that, otherwise we use the ones
  34. # installed on the system
  35. if "B10_FROM_SOURCE" in os.environ:
  36. BASE_LOCATION = os.environ["B10_FROM_SOURCE"]
  37. else:
  38. PREFIX = "@prefix@"
  39. DATAROOTDIR = "@datarootdir@"
  40. BASE_LOCATION = "@datadir@" + os.sep + "@PACKAGE@"
  41. BASE_LOCATION = BASE_LOCATION.replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  42. SPECFILE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd.spec"
  43. STATS_SPECFILE_LOCATION = BASE_LOCATION + os.sep + "stats.spec"
  44. XML_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xml.tpl"
  45. XSD_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsd.tpl"
  46. XSL_TEMPLATE_LOCATION = BASE_LOCATION + os.sep + "stats-httpd-xsl.tpl"
  47. # These variables are paths part of URL.
  48. # eg. "http://${address}" + XXX_URL_PATH
  49. XML_URL_PATH = '/bind10/statistics/xml'
  50. XSD_URL_PATH = '/bind10/statistics/xsd'
  51. XSL_URL_PATH = '/bind10/statistics/xsl'
  52. # TODO: This should be considered later.
  53. XSD_NAMESPACE = 'http://bind10.isc.org' + XSD_URL_PATH
  54. DEFAULT_CONFIG = dict(listen_on=[('127.0.0.1', 8000)])
  55. # Assign this process name
  56. isc.util.process.rename()
  57. class HttpHandler(http.server.BaseHTTPRequestHandler):
  58. """HTTP handler class for HttpServer class. The class inhrits the super
  59. class http.server.BaseHTTPRequestHandler. It implemets do_GET()
  60. and do_HEAD() and orverrides log_message()"""
  61. def do_GET(self):
  62. body = self.send_head()
  63. if body is not None:
  64. self.wfile.write(body.encode())
  65. def do_HEAD(self):
  66. self.send_head()
  67. def send_head(self):
  68. try:
  69. if self.path == XML_URL_PATH:
  70. body = self.server.xml_handler()
  71. elif self.path == XSD_URL_PATH:
  72. body = self.server.xsd_handler()
  73. elif self.path == XSL_URL_PATH:
  74. body = self.server.xsl_handler()
  75. else:
  76. if 'Host' in self.headers.keys():
  77. # redirect to XML URL
  78. self.send_response(302)
  79. self.send_header(
  80. "Location",
  81. "http://" + self.headers.get('Host') + XML_URL_PATH)
  82. self.end_headers()
  83. return None
  84. else:
  85. # Couldn't find HOST
  86. self.send_error(404)
  87. return None
  88. except StatsHttpdError as err:
  89. self.send_error(500)
  90. if self.server.verbose:
  91. self.server.log_writer(
  92. "[b10-stats-httpd] %s\n" % err)
  93. return None
  94. else:
  95. self.send_response(200)
  96. self.send_header("Content-type", "text/xml")
  97. self.send_header("Content-Length", len(body))
  98. self.end_headers()
  99. return body
  100. def log_message(self, format, *args):
  101. """Change the default log format"""
  102. if self.server.verbose:
  103. self.server.log_writer(
  104. "[b10-stats-httpd] %s - - [%s] %s\n" %
  105. (self.address_string(),
  106. self.log_date_time_string(),
  107. format%args))
  108. class HttpServerError(Exception):
  109. """Exception class for HttpServer class. It is intended to be
  110. passed from the HttpServer object to the StatsHttpd object."""
  111. pass
  112. class HttpServer(http.server.HTTPServer):
  113. """HTTP Server class. The class inherits the super
  114. http.server.HTTPServer. Some parameters are specified as
  115. arguments, which are xml_handler, xsd_handler, xsl_handler, and
  116. log_writer. These all are parameters which the StatsHttpd object
  117. has. The handler parameters are references of functions which
  118. return body of each document. The last parameter log_writer is
  119. reference of writer function to just write to
  120. sys.stderr.write. They are intended to be referred by HttpHandler
  121. object."""
  122. def __init__(self, server_address, handler,
  123. xml_handler, xsd_handler, xsl_handler, log_writer, verbose=False):
  124. self.server_address = server_address
  125. self.xml_handler = xml_handler
  126. self.xsd_handler = xsd_handler
  127. self.xsl_handler = xsl_handler
  128. self.log_writer = log_writer
  129. self.verbose = verbose
  130. http.server.HTTPServer.__init__(self, server_address, handler)
  131. class StatsHttpdError(Exception):
  132. """Exception class for StatsHttpd class. It is intended to be
  133. thrown from the the StatsHttpd object to the HttpHandler object or
  134. main routine."""
  135. pass
  136. class StatsHttpd:
  137. """The main class of HTTP server of HTTP/XML interface for
  138. statistics module. It handles HTTP requests, and command channel
  139. and config channel CC session. It uses select.select function
  140. while waiting for clients requests."""
  141. def __init__(self, verbose=False):
  142. self.verbose = verbose
  143. self.running = False
  144. self.poll_intval = 0.5
  145. self.write_log = sys.stderr.write
  146. self.mccs = None
  147. self.httpd = []
  148. self.open_mccs()
  149. self.load_config()
  150. self.load_templates()
  151. self.open_httpd()
  152. def open_mccs(self):
  153. """Opens a ModuleCCSession object"""
  154. # create ModuleCCSession
  155. if self.verbose:
  156. self.write_log("[b10-stats-httpd] Starting CC Session\n")
  157. self.mccs = isc.config.ModuleCCSession(
  158. SPECFILE_LOCATION, self.config_handler, self.command_handler)
  159. self.cc_session = self.mccs._session
  160. # read spec file of stats module and subscribe 'Stats'
  161. self.stats_module_spec = isc.config.module_spec_from_file(STATS_SPECFILE_LOCATION)
  162. self.stats_config_spec = self.stats_module_spec.get_config_spec()
  163. self.stats_module_name = self.stats_module_spec.get_module_name()
  164. def close_mccs(self):
  165. """Closes a ModuleCCSession object"""
  166. if self.mccs is None:
  167. return
  168. if self.verbose:
  169. self.write_log("[b10-stats-httpd] Closing CC Session\n")
  170. self.mccs.close()
  171. self.mccs = None
  172. def load_config(self, new_config={}):
  173. """Loads configuration from spec file or new configuration
  174. from the config manager"""
  175. # load config
  176. if len(new_config) > 0:
  177. self.config.update(new_config)
  178. else:
  179. self.config = DEFAULT_CONFIG
  180. self.config.update(
  181. dict([
  182. (itm['item_name'], self.mccs.get_value(itm['item_name'])[0])
  183. for itm in self.mccs.get_module_spec().get_config_spec()
  184. ])
  185. )
  186. # set addresses and ports for HTTP
  187. self.http_addrs = [ (cf['address'], cf['port']) for cf in self.config['listen_on'] ]
  188. def open_httpd(self):
  189. """Opens sockets for HTTP. Iterating each HTTP address to be
  190. configured in spec file"""
  191. for addr in self.http_addrs:
  192. self.httpd.append(self._open_httpd(addr))
  193. def _open_httpd(self, server_address, address_family=None):
  194. try:
  195. # try IPv6 at first
  196. if address_family is not None:
  197. HttpServer.address_family = address_family
  198. elif socket.has_ipv6:
  199. HttpServer.address_family = socket.AF_INET6
  200. httpd = HttpServer(
  201. server_address, HttpHandler,
  202. self.xml_handler, self.xsd_handler, self.xsl_handler,
  203. self.write_log, self.verbose)
  204. except (socket.gaierror, socket.error,
  205. OverflowError, TypeError) as err:
  206. # try IPv4 next
  207. if HttpServer.address_family == socket.AF_INET6:
  208. httpd = self._open_httpd(server_address, socket.AF_INET)
  209. else:
  210. raise HttpServerError(
  211. "Invalid address %s, port %s: %s: %s" %
  212. (server_address[0], server_address[1],
  213. err.__class__.__name__, err))
  214. else:
  215. if self.verbose:
  216. self.write_log(
  217. "[b10-stats-httpd] Started on address %s, port %s\n" %
  218. server_address)
  219. return httpd
  220. def close_httpd(self):
  221. """Closes sockets for HTTP"""
  222. if len(self.httpd) == 0:
  223. return
  224. for ht in self.httpd:
  225. if self.verbose:
  226. self.write_log(
  227. "[b10-stats-httpd] Closing address %s, port %s\n" %
  228. (ht.server_address[0], ht.server_address[1])
  229. )
  230. ht.server_close()
  231. self.httpd = []
  232. def start(self):
  233. """Starts StatsHttpd objects to run. Waiting for client
  234. requests by using select.select functions"""
  235. self.mccs.start()
  236. self.running = True
  237. while self.running:
  238. try:
  239. (rfd, wfd, xfd) = select.select(
  240. self.get_sockets(), [], [], self.poll_intval)
  241. except select.error as err:
  242. # select.error exception is caught only in the case of
  243. # EINTR, or in other cases it is just thrown.
  244. if err.args[0] == errno.EINTR:
  245. (rfd, wfd, xfd) = ([], [], [])
  246. else:
  247. raise
  248. # FIXME: This module can handle only one request at a
  249. # time. If someone sends only part of the request, we block
  250. # waiting for it until we time out.
  251. # But it isn't so big issue for administration purposes.
  252. for fd in rfd + xfd:
  253. if fd == self.mccs.get_socket():
  254. self.mccs.check_command(nonblock=False)
  255. continue
  256. for ht in self.httpd:
  257. if fd == ht.socket:
  258. ht.handle_request()
  259. break
  260. self.stop()
  261. def stop(self):
  262. """Stops the running StatsHttpd objects. Closes CC session and
  263. HTTP handling sockets"""
  264. if self.verbose:
  265. self.write_log("[b10-stats-httpd] Shutting down\n")
  266. self.close_httpd()
  267. self.close_mccs()
  268. def get_sockets(self):
  269. """Returns sockets to select.select"""
  270. sockets = []
  271. if self.mccs is not None:
  272. sockets.append(self.mccs.get_socket())
  273. if len(self.httpd) > 0:
  274. for ht in self.httpd:
  275. sockets.append(ht.socket)
  276. return sockets
  277. def config_handler(self, new_config):
  278. """Config handler for the ModuleCCSession object. It resets
  279. addresses and ports to listen HTTP requests on."""
  280. if self.verbose:
  281. self.write_log("[b10-stats-httpd] Loading config : %s\n" % str(new_config))
  282. for key in new_config.keys():
  283. if key not in DEFAULT_CONFIG:
  284. if self.verbose:
  285. self.write_log(
  286. "[b10-stats-httpd] Unknown known config: %s" % key)
  287. return isc.config.ccsession.create_answer(
  288. 1, "Unknown known config: %s" % key)
  289. # backup old config
  290. old_config = self.config.copy()
  291. self.close_httpd()
  292. self.load_config(new_config)
  293. try:
  294. self.open_httpd()
  295. except HttpServerError as err:
  296. if self.verbose:
  297. self.write_log("[b10-stats-httpd] %s\n" % err)
  298. self.write_log("[b10-stats-httpd] Restoring old config\n")
  299. # restore old config
  300. self.config_handler(old_config)
  301. return isc.config.ccsession.create_answer(
  302. 1, "[b10-stats-httpd] %s" % err)
  303. else:
  304. return isc.config.ccsession.create_answer(0)
  305. def command_handler(self, command, args):
  306. """Command handler for the ModuleCCSesson object. It handles
  307. "status" and "shutdown" commands."""
  308. if command == "status":
  309. if self.verbose:
  310. self.write_log("[b10-stats-httpd] Received 'status' command\n")
  311. return isc.config.ccsession.create_answer(
  312. 0, "Stats Httpd is up. (PID " + str(os.getpid()) + ")")
  313. elif command == "shutdown":
  314. if self.verbose:
  315. self.write_log("[b10-stats-httpd] Received 'shutdown' command\n")
  316. self.running = False
  317. return isc.config.ccsession.create_answer(
  318. 0, "Stats Httpd is shutting down.")
  319. else:
  320. if self.verbose:
  321. self.write_log("[b10-stats-httpd] Received unknown command\n")
  322. return isc.config.ccsession.create_answer(
  323. 1, "Unknown command: " + str(command))
  324. def get_stats_data(self):
  325. """Requests statistics data to the Stats daemon and returns
  326. the data which obtains from it"""
  327. try:
  328. seq = self.cc_session.group_sendmsg(
  329. isc.config.ccsession.create_command('show'),
  330. self.stats_module_name)
  331. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  332. if answer:
  333. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  334. except (isc.cc.session.SessionTimeout,
  335. isc.cc.session.SessionError) as err:
  336. raise StatsHttpdError("%s: %s" %
  337. (err.__class__.__name__, err))
  338. else:
  339. if rcode == 0:
  340. return value
  341. else:
  342. raise StatsHttpdError("Stats module: %s" % str(value))
  343. def get_stats_spec(self):
  344. """Just returns spec data"""
  345. return self.stats_config_spec
  346. def load_templates(self):
  347. """Setup the bodies of XSD and XSL documents to be responds to
  348. HTTP clients. Before that it also creates XML tag structures by
  349. using xml.etree.ElementTree.Element class and substitutes
  350. concrete strings with parameters embed in the string.Template
  351. object."""
  352. # for XSD
  353. xsd_root = xml.etree.ElementTree.Element("all") # started with "all" tag
  354. for item in self.get_stats_spec():
  355. element = xml.etree.ElementTree.Element(
  356. "element",
  357. dict( name=item["item_name"],
  358. type=item["item_type"] if item["item_type"].lower() != 'real' else 'float',
  359. minOccurs="1",
  360. maxOccurs="1" ),
  361. )
  362. annotation = xml.etree.ElementTree.Element("annotation")
  363. appinfo = xml.etree.ElementTree.Element("appinfo")
  364. documentation = xml.etree.ElementTree.Element("documentation")
  365. appinfo.text = item["item_title"]
  366. documentation.text = item["item_description"]
  367. annotation.append(appinfo)
  368. annotation.append(documentation)
  369. element.append(annotation)
  370. xsd_root.append(element)
  371. xsd_string = xml.etree.ElementTree.tostring(xsd_root)
  372. self.xsd_body = self.open_template(XSD_TEMPLATE_LOCATION).substitute(
  373. xsd_string=xsd_string,
  374. xsd_namespace=XSD_NAMESPACE
  375. )
  376. assert self.xsd_body is not None
  377. # for XSL
  378. xsd_root = xml.etree.ElementTree.Element(
  379. "xsl:template",
  380. dict(match="*")) # started with xml:template tag
  381. for item in self.get_stats_spec():
  382. tr = xml.etree.ElementTree.Element("tr")
  383. td1 = xml.etree.ElementTree.Element(
  384. "td", { "class" : "title",
  385. "title" : item["item_description"] })
  386. td1.text = item["item_title"]
  387. td2 = xml.etree.ElementTree.Element("td")
  388. xsl_valueof = xml.etree.ElementTree.Element(
  389. "xsl:value-of",
  390. dict(select=item["item_name"]))
  391. td2.append(xsl_valueof)
  392. tr.append(td1)
  393. tr.append(td2)
  394. xsd_root.append(tr)
  395. xsl_string = xml.etree.ElementTree.tostring(xsd_root)
  396. self.xsl_body = self.open_template(XSL_TEMPLATE_LOCATION).substitute(
  397. xsl_string=xsl_string,
  398. xsd_namespace=XSD_NAMESPACE)
  399. assert self.xsl_body is not None
  400. def xml_handler(self):
  401. """Handler which requests to Stats daemon to obtain statistics
  402. data and returns the body of XML document"""
  403. xml_list=[]
  404. for (k, v) in self.get_stats_data().items():
  405. (k, v) = (str(k), str(v))
  406. elem = xml.etree.ElementTree.Element(k)
  407. elem.text = v
  408. xml_list.append(
  409. xml.etree.ElementTree.tostring(elem))
  410. xml_string = "".join(xml_list)
  411. self.xml_body = self.open_template(XML_TEMPLATE_LOCATION).substitute(
  412. xml_string=xml_string,
  413. xsd_namespace=XSD_NAMESPACE,
  414. xsd_url_path=XSD_URL_PATH,
  415. xsl_url_path=XSL_URL_PATH)
  416. assert self.xml_body is not None
  417. return self.xml_body
  418. def xsd_handler(self):
  419. """Handler which just returns the body of XSD document"""
  420. return self.xsd_body
  421. def xsl_handler(self):
  422. """Handler which just returns the body of XSL document"""
  423. return self.xsl_body
  424. def open_template(self, file_name):
  425. """It opens a template file, and it loads all lines to a
  426. string variable and returns string. Template object includes
  427. the variable. Limitation of a file size isn't needed there."""
  428. lines = "".join(
  429. open(file_name, 'r').readlines())
  430. assert lines is not None
  431. return string.Template(lines)
  432. if __name__ == "__main__":
  433. try:
  434. parser = OptionParser()
  435. parser.add_option(
  436. "-v", "--verbose", dest="verbose", action="store_true",
  437. help="display more about what is going on")
  438. (options, args) = parser.parse_args()
  439. stats_httpd = StatsHttpd(verbose=options.verbose)
  440. stats_httpd.start()
  441. except OptionValueError:
  442. sys.exit("[b10-stats-httpd] Error parsing options")
  443. except isc.cc.session.SessionError as se:
  444. sys.exit("[b10-stats-httpd] Error creating module, "
  445. + "is the command channel daemon running?")
  446. except HttpServerError as hse:
  447. sys.exit("[b10-stats-httpd] %s" % hse)
  448. except KeyboardInterrupt as kie:
  449. sys.exit("[b10-stats-httpd] Interrupted, exiting")