|
@@ -34,6 +34,17 @@ import isc.cc
|
|
|
import isc.config
|
|
|
import isc.util.process
|
|
|
|
|
|
+import isc.log
|
|
|
+from stats_httpd_messages import *
|
|
|
+
|
|
|
+isc.log.init("b10-stats-httpd")
|
|
|
+logger = isc.log.Logger("stats-httpd")
|
|
|
+
|
|
|
+# Some constants for debug levels, these should be removed when we
|
|
|
+# have #1074
|
|
|
+DBG_STATS_HTTPD_INIT = 10
|
|
|
+DBG_STATS_HTTPD_MESSAGING = 30
|
|
|
+
|
|
|
# If B10_FROM_SOURCE is set in the environment, we use data files
|
|
|
# from a directory relative to that, otherwise we use the ones
|
|
|
# installed on the system
|
|
@@ -169,8 +180,7 @@ class StatsHttpd:
|
|
|
def open_mccs(self):
|
|
|
"""Opens a ModuleCCSession object"""
|
|
|
# create ModuleCCSession
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Starting CC Session\n")
|
|
|
+ logger.debug(DBG_STATS_HTTPD_INIT, STATS_HTTPD_STARTING_CC_SESSION)
|
|
|
self.mccs = isc.config.ModuleCCSession(
|
|
|
SPECFILE_LOCATION, self.config_handler, self.command_handler)
|
|
|
self.cc_session = self.mccs._session
|
|
@@ -183,8 +193,8 @@ class StatsHttpd:
|
|
|
"""Closes a ModuleCCSession object"""
|
|
|
if self.mccs is None:
|
|
|
return
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Closing CC Session\n")
|
|
|
+
|
|
|
+ logger.debug(DBG_STATS_HTTPD_INIT, STATS_HTTPD_CLOSING_CC_SESSION)
|
|
|
self.mccs.close()
|
|
|
self.mccs = None
|
|
|
|
|
@@ -233,10 +243,8 @@ class StatsHttpd:
|
|
|
(server_address[0], server_address[1],
|
|
|
err.__class__.__name__, err))
|
|
|
else:
|
|
|
- if self.verbose:
|
|
|
- self.write_log(
|
|
|
- "[b10-stats-httpd] Started on address %s, port %s\n" %
|
|
|
- server_address)
|
|
|
+ logger.info(STATS_HTTPD_STARTED, server_address[0],
|
|
|
+ server_address[1])
|
|
|
return httpd
|
|
|
|
|
|
def close_httpd(self):
|
|
@@ -244,11 +252,8 @@ class StatsHttpd:
|
|
|
if len(self.httpd) == 0:
|
|
|
return
|
|
|
for ht in self.httpd:
|
|
|
- if self.verbose:
|
|
|
- self.write_log(
|
|
|
- "[b10-stats-httpd] Closing address %s, port %s\n" %
|
|
|
- (ht.server_address[0], ht.server_address[1])
|
|
|
- )
|
|
|
+ logger.info(STATS_HTTPD_CLOSING, ht.server_address[0],
|
|
|
+ ht.server_address[1])
|
|
|
ht.server_close()
|
|
|
self.httpd = []
|
|
|
|
|
@@ -285,8 +290,7 @@ class StatsHttpd:
|
|
|
def stop(self):
|
|
|
"""Stops the running StatsHttpd objects. Closes CC session and
|
|
|
HTTP handling sockets"""
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Shutting down\n")
|
|
|
+ logger.info(STATS_HTTPD_SHUTDOWN)
|
|
|
self.close_httpd()
|
|
|
self.close_mccs()
|
|
|
|
|
@@ -303,13 +307,11 @@ class StatsHttpd:
|
|
|
def config_handler(self, new_config):
|
|
|
"""Config handler for the ModuleCCSession object. It resets
|
|
|
addresses and ports to listen HTTP requests on."""
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Loading config : %s\n" % str(new_config))
|
|
|
+ logger.debug(DBG_STATS_HTTPD_MESSAGING, STATS_HTTPD_HANDLE_CONFIG,
|
|
|
+ new_config)
|
|
|
for key in new_config.keys():
|
|
|
if key not in DEFAULT_CONFIG:
|
|
|
- if self.verbose:
|
|
|
- self.write_log(
|
|
|
- "[b10-stats-httpd] Unknown known config: %s" % key)
|
|
|
+ logger.error(STATS_HTTPD_UNKNOWN_CONFIG_ITEM, key)
|
|
|
return isc.config.ccsession.create_answer(
|
|
|
1, "Unknown known config: %s" % key)
|
|
|
# backup old config
|
|
@@ -319,9 +321,7 @@ class StatsHttpd:
|
|
|
try:
|
|
|
self.open_httpd()
|
|
|
except HttpServerError as err:
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] %s\n" % err)
|
|
|
- self.write_log("[b10-stats-httpd] Restoring old config\n")
|
|
|
+ logger.error(STATS_HTTPD_SERVER_ERROR, err)
|
|
|
# restore old config
|
|
|
self.config_handler(old_config)
|
|
|
return isc.config.ccsession.create_answer(
|
|
@@ -333,19 +333,19 @@ class StatsHttpd:
|
|
|
"""Command handler for the ModuleCCSesson object. It handles
|
|
|
"status" and "shutdown" commands."""
|
|
|
if command == "status":
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Received 'status' command\n")
|
|
|
+ logger.debug(DBG_STATS_HTTPD_MESSAGING,
|
|
|
+ STATS_HTTPD_RECEIVED_STATUS_COMMAND)
|
|
|
return isc.config.ccsession.create_answer(
|
|
|
0, "Stats Httpd is up. (PID " + str(os.getpid()) + ")")
|
|
|
elif command == "shutdown":
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Received 'shutdown' command\n")
|
|
|
+ logger.debug(DBG_STATS_HTTPD_MESSAGING,
|
|
|
+ STATS_HTTPD_RECEIVED_SHUTDOWN_COMMAND)
|
|
|
self.running = False
|
|
|
return isc.config.ccsession.create_answer(
|
|
|
0, "Stats Httpd is shutting down.")
|
|
|
else:
|
|
|
- if self.verbose:
|
|
|
- self.write_log("[b10-stats-httpd] Received unknown command\n")
|
|
|
+ logger.debug(DBG_STATS_HTTPD_MESSAGING,
|
|
|
+ STATS_HTTPD_RECEIVED_UNKNOWN_COMMAND, command)
|
|
|
return isc.config.ccsession.create_answer(
|
|
|
1, "Unknown command: " + str(command))
|
|
|
|
|
@@ -481,12 +481,14 @@ if __name__ == "__main__":
|
|
|
(options, args) = parser.parse_args()
|
|
|
stats_httpd = StatsHttpd(verbose=options.verbose)
|
|
|
stats_httpd.start()
|
|
|
- except OptionValueError:
|
|
|
- sys.exit("[b10-stats-httpd] Error parsing options")
|
|
|
+ except OptionValueError as ove:
|
|
|
+ logger.fatal(STATS_HTTPD_BAD_OPTION_VALUE, ove)
|
|
|
+ sys.exit(1)
|
|
|
except isc.cc.session.SessionError as se:
|
|
|
- sys.exit("[b10-stats-httpd] Error creating module, "
|
|
|
- + "is the command channel daemon running?")
|
|
|
+ logger.fatal(STATS_HTTPD_CC_SESSION_ERROR, se)
|
|
|
+ sys.exit(1)
|
|
|
except HttpServerError as hse:
|
|
|
- sys.exit("[b10-stats-httpd] %s" % hse)
|
|
|
+ logger.fatal(STATS_HTTPD_START_SERVER_ERROR, hse)
|
|
|
+ sys.exit(1)
|
|
|
except KeyboardInterrupt as kie:
|
|
|
- sys.exit("[b10-stats-httpd] Interrupted, exiting")
|
|
|
+ logger.info(STATS_HTTPD_STOPPED_BY_KEYBOARD)
|