stats_httpd.py.in 19 KB

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