stats_httpd.py.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. import isc.log
  33. from isc.log_messages.stats_httpd_messages import *
  34. isc.log.init("b10-stats-httpd")
  35. logger = isc.log.Logger("stats-httpd")
  36. # Some constants for debug levels, these should be removed when we
  37. # have #1074
  38. DBG_STATHTTPD_INIT = 10
  39. DBG_STATHTTPD_MESSAGING = 30
  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' + XSD_URL_PATH
  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. if self.path == XML_URL_PATH:
  77. body = self.server.xml_handler()
  78. elif self.path == XSD_URL_PATH:
  79. body = self.server.xsd_handler()
  80. elif self.path == XSL_URL_PATH:
  81. body = self.server.xsl_handler()
  82. else:
  83. if self.path == '/' and 'Host' in self.headers.keys():
  84. # redirect to XML URL only when requested with '/'
  85. self.send_response(302)
  86. self.send_header(
  87. "Location",
  88. "http://" + self.headers.get('Host') + XML_URL_PATH)
  89. self.end_headers()
  90. return None
  91. else:
  92. # Couldn't find HOST
  93. self.send_error(404)
  94. return None
  95. except StatsHttpdError as err:
  96. self.send_error(500)
  97. logger.error(STATHTTPD_SERVER_ERROR, err)
  98. return None
  99. else:
  100. self.send_response(200)
  101. self.send_header("Content-type", "text/xml")
  102. self.send_header("Content-Length", len(body))
  103. self.end_headers()
  104. return body
  105. class HttpServerError(Exception):
  106. """Exception class for HttpServer class. It is intended to be
  107. passed from the HttpServer object to the StatsHttpd object."""
  108. pass
  109. class HttpServer(http.server.HTTPServer):
  110. """HTTP Server class. The class inherits the super
  111. http.server.HTTPServer. Some parameters are specified as
  112. arguments, which are xml_handler, xsd_handler, xsl_handler, and
  113. log_writer. These all are parameters which the StatsHttpd object
  114. has. The handler parameters are references of functions which
  115. return body of each document. The last parameter log_writer is
  116. reference of writer function to just write to
  117. sys.stderr.write. They are intended to be referred by HttpHandler
  118. object."""
  119. def __init__(self, server_address, handler,
  120. xml_handler, xsd_handler, xsl_handler, log_writer):
  121. self.server_address = server_address
  122. self.xml_handler = xml_handler
  123. self.xsd_handler = xsd_handler
  124. self.xsl_handler = xsl_handler
  125. self.log_writer = log_writer
  126. http.server.HTTPServer.__init__(self, server_address, handler)
  127. class StatsHttpdError(Exception):
  128. """Exception class for StatsHttpd class. It is intended to be
  129. thrown from the the StatsHttpd object to the HttpHandler object or
  130. main routine."""
  131. pass
  132. class StatsHttpd:
  133. """The main class of HTTP server of HTTP/XML interface for
  134. statistics module. It handles HTTP requests, and command channel
  135. and config channel CC session. It uses select.select function
  136. while waiting for clients requests."""
  137. def __init__(self):
  138. self.running = False
  139. self.poll_intval = 0.5
  140. self.write_log = sys.stderr.write
  141. self.mccs = None
  142. self.httpd = []
  143. self.open_mccs()
  144. self.config = {}
  145. self.load_config()
  146. self.http_addrs = []
  147. self.mccs.start()
  148. self.open_httpd()
  149. def open_mccs(self):
  150. """Opens a ModuleCCSession object"""
  151. # create ModuleCCSession
  152. logger.debug(DBG_STATHTTPD_INIT, STATHTTPD_STARTING_CC_SESSION)
  153. self.mccs = isc.config.ModuleCCSession(
  154. SPECFILE_LOCATION, self.config_handler, self.command_handler)
  155. self.cc_session = self.mccs._session
  156. def close_mccs(self):
  157. """Closes a ModuleCCSession object"""
  158. if self.mccs is None:
  159. return
  160. logger.debug(DBG_STATHTTPD_INIT, STATHTTPD_CLOSING_CC_SESSION)
  161. self.mccs.close()
  162. self.mccs = None
  163. def load_config(self, new_config={}):
  164. """Loads configuration from spec file or new configuration
  165. from the config manager"""
  166. # load config
  167. if len(self.config) == 0:
  168. self.config = dict([
  169. (itm['item_name'], self.mccs.get_value(itm['item_name'])[0])
  170. for itm in self.mccs.get_module_spec().get_config_spec()
  171. ])
  172. self.config.update(new_config)
  173. # set addresses and ports for HTTP
  174. addrs = []
  175. if 'listen_on' in self.config:
  176. for cf in self.config['listen_on']:
  177. if 'address' in cf and 'port' in cf:
  178. addrs.append((cf['address'], cf['port']))
  179. self.http_addrs = addrs
  180. def open_httpd(self):
  181. """Opens sockets for HTTP. Iterating each HTTP address to be
  182. configured in spec file"""
  183. for addr in self.http_addrs:
  184. self.httpd.append(self._open_httpd(addr))
  185. def _open_httpd(self, server_address):
  186. httpd = None
  187. try:
  188. # get address family for the server_address before
  189. # creating HttpServer object. If a specified address is
  190. # not numerical, gaierror may be thrown.
  191. address_family = socket.getaddrinfo(
  192. server_address[0], server_address[1], 0,
  193. socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST
  194. )[0][0]
  195. HttpServer.address_family = address_family
  196. httpd = HttpServer(
  197. server_address, HttpHandler,
  198. self.xml_handler, self.xsd_handler, self.xsl_handler,
  199. self.write_log)
  200. logger.info(STATHTTPD_STARTED, server_address[0],
  201. server_address[1])
  202. return httpd
  203. except (socket.gaierror, socket.error,
  204. OverflowError, TypeError) as err:
  205. if httpd:
  206. httpd.server_close()
  207. raise HttpServerError(
  208. "Invalid address %s, port %s: %s: %s" %
  209. (server_address[0], server_address[1],
  210. err.__class__.__name__, err))
  211. def close_httpd(self):
  212. """Closes sockets for HTTP"""
  213. while len(self.httpd)>0:
  214. ht = self.httpd.pop()
  215. logger.info(STATHTTPD_CLOSING, ht.server_address[0],
  216. ht.server_address[1])
  217. ht.server_close()
  218. def start(self):
  219. """Starts StatsHttpd objects to run. Waiting for client
  220. requests by using select.select functions"""
  221. self.running = True
  222. while self.running:
  223. try:
  224. (rfd, wfd, xfd) = select.select(
  225. self.get_sockets(), [], [], self.poll_intval)
  226. except select.error as err:
  227. # select.error exception is caught only in the case of
  228. # EINTR, or in other cases it is just thrown.
  229. if err.args[0] == errno.EINTR:
  230. (rfd, wfd, xfd) = ([], [], [])
  231. else:
  232. raise
  233. # FIXME: This module can handle only one request at a
  234. # time. If someone sends only part of the request, we block
  235. # waiting for it until we time out.
  236. # But it isn't so big issue for administration purposes.
  237. for fd in rfd + xfd:
  238. if fd == self.mccs.get_socket():
  239. self.mccs.check_command(nonblock=False)
  240. continue
  241. for ht in self.httpd:
  242. if fd == ht.socket:
  243. ht.handle_request()
  244. break
  245. self.stop()
  246. def stop(self):
  247. """Stops the running StatsHttpd objects. Closes CC session and
  248. HTTP handling sockets"""
  249. logger.info(STATHTTPD_SHUTDOWN)
  250. self.close_httpd()
  251. self.close_mccs()
  252. self.running = False
  253. def get_sockets(self):
  254. """Returns sockets to select.select"""
  255. sockets = []
  256. if self.mccs is not None:
  257. sockets.append(self.mccs.get_socket())
  258. if len(self.httpd) > 0:
  259. for ht in self.httpd:
  260. sockets.append(ht.socket)
  261. return sockets
  262. def config_handler(self, new_config):
  263. """Config handler for the ModuleCCSession object. It resets
  264. addresses and ports to listen HTTP requests on."""
  265. logger.debug(DBG_STATHTTPD_MESSAGING, STATHTTPD_HANDLE_CONFIG,
  266. new_config)
  267. errors = []
  268. if not self.mccs.get_module_spec().\
  269. validate_config(False, new_config, errors):
  270. return isc.config.ccsession.create_answer(
  271. 1, ", ".join(errors))
  272. # backup old config
  273. old_config = self.config.copy()
  274. self.load_config(new_config)
  275. # If the http sockets aren't opened or
  276. # if new_config doesn't have'listen_on', it returns
  277. if len(self.httpd) == 0 or 'listen_on' not in new_config:
  278. return isc.config.ccsession.create_answer(0)
  279. self.close_httpd()
  280. try:
  281. self.open_httpd()
  282. except HttpServerError as err:
  283. logger.error(STATHTTPD_SERVER_ERROR, err)
  284. # restore old config
  285. self.load_config(old_config)
  286. self.open_httpd()
  287. return isc.config.ccsession.create_answer(1, str(err))
  288. else:
  289. return isc.config.ccsession.create_answer(0)
  290. def command_handler(self, command, args):
  291. """Command handler for the ModuleCCSesson object. It handles
  292. "status" and "shutdown" commands."""
  293. if command == "status":
  294. logger.debug(DBG_STATHTTPD_MESSAGING,
  295. STATHTTPD_RECEIVED_STATUS_COMMAND)
  296. return isc.config.ccsession.create_answer(
  297. 0, "Stats Httpd is up. (PID " + str(os.getpid()) + ")")
  298. elif command == "shutdown":
  299. logger.debug(DBG_STATHTTPD_MESSAGING,
  300. STATHTTPD_RECEIVED_SHUTDOWN_COMMAND)
  301. self.running = False
  302. return isc.config.ccsession.create_answer(
  303. 0, "Stats Httpd is shutting down.")
  304. else:
  305. logger.debug(DBG_STATHTTPD_MESSAGING,
  306. STATHTTPD_RECEIVED_UNKNOWN_COMMAND, command)
  307. return isc.config.ccsession.create_answer(
  308. 1, "Unknown command: " + str(command))
  309. def get_stats_data(self):
  310. """Requests statistics data to the Stats daemon and returns
  311. the data which obtains from it"""
  312. try:
  313. seq = self.cc_session.group_sendmsg(
  314. isc.config.ccsession.create_command('show'), 'Stats')
  315. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  316. if answer:
  317. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  318. except (isc.cc.session.SessionTimeout,
  319. isc.cc.session.SessionError) as err:
  320. raise StatsHttpdError("%s: %s" %
  321. (err.__class__.__name__, err))
  322. else:
  323. if rcode == 0:
  324. return value
  325. else:
  326. raise StatsHttpdError("Stats module: %s" % str(value))
  327. def get_stats_spec(self):
  328. """Requests statistics data to the Stats daemon and returns
  329. the data which obtains from it"""
  330. try:
  331. seq = self.cc_session.group_sendmsg(
  332. isc.config.ccsession.create_command('showschema'), 'Stats')
  333. (answer, env) = self.cc_session.group_recvmsg(False, seq)
  334. if answer:
  335. (rcode, value) = isc.config.ccsession.parse_answer(answer)
  336. if rcode == 0:
  337. return value
  338. else:
  339. raise StatsHttpdError("Stats module: %s" % str(value))
  340. except (isc.cc.session.SessionTimeout,
  341. isc.cc.session.SessionError) as err:
  342. raise StatsHttpdError("%s: %s" %
  343. (err.__class__.__name__, err))
  344. def xml_handler(self):
  345. """Handler which requests to Stats daemon to obtain statistics
  346. data and returns the body of XML document"""
  347. xml_list=[]
  348. for (mod, spec) in self.get_stats_data().items():
  349. if not spec: continue
  350. elem1 = xml.etree.ElementTree.Element(str(mod))
  351. for (k, v) in spec.items():
  352. elem2 = xml.etree.ElementTree.Element(str(k))
  353. elem2.text = str(v)
  354. elem1.append(elem2)
  355. # The coding conversion is tricky. xml..tostring() of Python 3.2
  356. # returns bytes (not string) regardless of the coding, while
  357. # tostring() of Python 3.1 returns a string. To support both
  358. # cases transparently, we first make sure tostring() returns
  359. # bytes by specifying utf-8 and then convert the result to a
  360. # plain string (code below assume it).
  361. xml_list.append(
  362. str(xml.etree.ElementTree.tostring(elem1, encoding='utf-8'),
  363. encoding='us-ascii'))
  364. xml_string = "".join(xml_list)
  365. self.xml_body = self.open_template(XML_TEMPLATE_LOCATION).substitute(
  366. xml_string=xml_string,
  367. xsd_namespace=XSD_NAMESPACE,
  368. xsd_url_path=XSD_URL_PATH,
  369. xsl_url_path=XSL_URL_PATH)
  370. assert self.xml_body is not None
  371. return self.xml_body
  372. def xsd_handler(self):
  373. """Handler which just returns the body of XSD document"""
  374. # for XSD
  375. xsd_root = xml.etree.ElementTree.Element("all") # started with "all" tag
  376. for (mod, spec) in self.get_stats_spec().items():
  377. if not spec: continue
  378. alltag = xml.etree.ElementTree.Element("all")
  379. for item in spec:
  380. element = xml.etree.ElementTree.Element(
  381. "element",
  382. dict( name=item["item_name"],
  383. type=item["item_type"] if item["item_type"].lower() != 'real' else 'float',
  384. minOccurs="1",
  385. maxOccurs="1" ),
  386. )
  387. annotation = xml.etree.ElementTree.Element("annotation")
  388. appinfo = xml.etree.ElementTree.Element("appinfo")
  389. documentation = xml.etree.ElementTree.Element("documentation")
  390. appinfo.text = item["item_title"]
  391. documentation.text = item["item_description"]
  392. annotation.append(appinfo)
  393. annotation.append(documentation)
  394. element.append(annotation)
  395. alltag.append(element)
  396. complextype = xml.etree.ElementTree.Element("complexType")
  397. complextype.append(alltag)
  398. mod_element = xml.etree.ElementTree.Element("element", { "name" : mod })
  399. mod_element.append(complextype)
  400. xsd_root.append(mod_element)
  401. # The coding conversion is tricky. xml..tostring() of Python 3.2
  402. # returns bytes (not string) regardless of the coding, while
  403. # tostring() of Python 3.1 returns a string. To support both
  404. # cases transparently, we first make sure tostring() returns
  405. # bytes by specifying utf-8 and then convert the result to a
  406. # plain string (code below assume it).
  407. xsd_string = str(xml.etree.ElementTree.tostring(xsd_root, encoding='utf-8'),
  408. encoding='us-ascii')
  409. self.xsd_body = self.open_template(XSD_TEMPLATE_LOCATION).substitute(
  410. xsd_string=xsd_string,
  411. xsd_namespace=XSD_NAMESPACE
  412. )
  413. assert self.xsd_body is not None
  414. return self.xsd_body
  415. def xsl_handler(self):
  416. """Handler which just returns the body of XSL document"""
  417. # for XSL
  418. xsd_root = xml.etree.ElementTree.Element(
  419. "xsl:template",
  420. dict(match="*")) # started with xml:template tag
  421. for (mod, spec) in self.get_stats_spec().items():
  422. if not spec: continue
  423. for item in spec:
  424. tr = xml.etree.ElementTree.Element("tr")
  425. td0 = xml.etree.ElementTree.Element("td")
  426. td0.text = str(mod)
  427. td1 = xml.etree.ElementTree.Element(
  428. "td", { "class" : "title",
  429. "title" : item["item_description"] })
  430. td1.text = item["item_title"]
  431. td2 = xml.etree.ElementTree.Element("td")
  432. xsl_valueof = xml.etree.ElementTree.Element(
  433. "xsl:value-of",
  434. dict(select=mod+'/'+item["item_name"]))
  435. td2.append(xsl_valueof)
  436. tr.append(td0)
  437. tr.append(td1)
  438. tr.append(td2)
  439. xsd_root.append(tr)
  440. # The coding conversion is tricky. xml..tostring() of Python 3.2
  441. # returns bytes (not string) regardless of the coding, while
  442. # tostring() of Python 3.1 returns a string. To support both
  443. # cases transparently, we first make sure tostring() returns
  444. # bytes by specifying utf-8 and then convert the result to a
  445. # plain string (code below assume it).
  446. xsl_string = str(xml.etree.ElementTree.tostring(xsd_root, encoding='utf-8'),
  447. encoding='us-ascii')
  448. self.xsl_body = self.open_template(XSL_TEMPLATE_LOCATION).substitute(
  449. xsl_string=xsl_string,
  450. xsd_namespace=XSD_NAMESPACE)
  451. assert self.xsl_body is not None
  452. return self.xsl_body
  453. def open_template(self, file_name):
  454. """It opens a template file, and it loads all lines to a
  455. string variable and returns string. Template object includes
  456. the variable. Limitation of a file size isn't needed there."""
  457. f = open(file_name, 'r')
  458. lines = "".join(f.readlines())
  459. f.close()
  460. assert lines is not None
  461. return string.Template(lines)
  462. if __name__ == "__main__":
  463. try:
  464. parser = OptionParser()
  465. parser.add_option(
  466. "-v", "--verbose", dest="verbose", action="store_true",
  467. help="display more about what is going on")
  468. (options, args) = parser.parse_args()
  469. if options.verbose:
  470. isc.log.init("b10-stats-httpd", "DEBUG", 99)
  471. stats_httpd = StatsHttpd()
  472. stats_httpd.start()
  473. except OptionValueError as ove:
  474. logger.fatal(STATHTTPD_BAD_OPTION_VALUE, ove)
  475. sys.exit(1)
  476. except isc.cc.session.SessionError as se:
  477. logger.fatal(STATHTTPD_CC_SESSION_ERROR, se)
  478. sys.exit(1)
  479. except HttpServerError as hse:
  480. logger.fatal(STATHTTPD_START_SERVER_ERROR, hse)
  481. sys.exit(1)
  482. except KeyboardInterrupt as kie:
  483. logger.info(STATHTTPD_STOPPED_BY_KEYBOARD)