stats_httpd.py.in 21 KB

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