def _get_bro_version(self): from BroControl import execute bro = self.subst("${bindir}/bro") if not os.path.lexists(bro): raise ConfigurationError("cannot find Bro binary: %s" % bro) version = "" (success, output) = execute.run_localcmd("%s -v" % bro) if success and output: version = output[-1] else: msg = "" if output: msg = " with output:\n%s" % "\n".join(output) raise ConfigurationError("running \"bro -v\" failed%s" % msg) match = re.search(".* version ([^ ]*).*$", version) if not match: raise ConfigurationError("cannot determine Bro version [%s]" % version.strip()) version = match.group(1) # If bro is built with the "--enable-debug" configure option, then it # appends "-debug" to the version string. if version.endswith("-debug"): version = version[:-6] return version
def _get_bro_version(self): from BroControl import execute bro = self.config["bro"] if not os.path.lexists(bro): raise ConfigurationError("cannot find Bro binary: %s" % bro) version = "" (success, output) = execute.run_localcmd("%s -v" % bro) if success and output: version = output[-1] else: msg = " with no output" if output: msg = " with output:\n%s" % "\n".join(output) raise RuntimeEnvironmentError('running "bro -v" failed%s' % msg) match = re.search(".* version ([^ ]*).*$", version) if not match: raise RuntimeEnvironmentError('cannot determine Bro version ("bro -v" output: %s)' % version.strip()) version = match.group(1) # If bro is built with the "--enable-debug" configure option, then it # appends "-debug" to the version string. if version.endswith("-debug"): version = version[:-6] return version
def _sendmail(self, subject, body): if not self.config.sendmail: return True, "" cmd = "%s '%s'" % (os.path.join(self.config.scriptsdir, "send-mail"), subject) (success, output) = execute.run_localcmd(cmd, "", body) return success, output
def _break_lock(cmdout): from BroControl import execute try: # Check whether lock is stale. with open(config.Config.lockfile, "r") as f: pid = f.readline().strip() except (OSError, IOError) as err: cmdout.error("failed to read lock file: %s" % err) return -1 success, output = execute.run_localcmd("%s %s" % (os.path.join(config.Config.helperdir, "check-pid"), pid)) if success and output.strip() == "running": # Process still exists. try: return int(pid) except ValueError: return -1 cmdout.info("removing stale lock") try: # Break lock. os.unlink(config.Config.lockfile) except (OSError, IOError) as err: cmdout.error("failed to remove lock file: %s" % err) return -1 return 0
def process(self, trace, bro_options, bro_scripts): results = cmdresult.CmdResult() if not os.path.isfile(trace): self.ui.error("trace file not found: %s" % trace) results.ok = False return results standalone = (self.config.standalone == "1") if standalone: tag = "standalone" else: tag = "workers" node = self.config.nodes(tag=tag)[0] cwd = os.path.join(self.config.tmpdir, "testing") if os.path.isdir(cwd): try: shutil.rmtree(cwd) except OSError as err: self.ui.error("cannot remove directory: %s" % err) results.ok = False return results try: os.makedirs(cwd) except OSError as err: self.ui.error("cannot create directory: %s" % err) results.ok = False return results env = _make_env_params(node) bro_args = " ".join(bro_options + _make_bro_params(node, False)) bro_args += " broctl/process-trace" if bro_scripts: bro_args += " " + " ".join(bro_scripts) cmd = os.path.join( self.config.scriptsdir, "run-bro-on-trace") + " %s %s %s %s" % (0, cwd, trace, bro_args) self.ui.info(cmd) (success, output) = execute.run_localcmd(cmd, env, donotcaptureoutput=True) if not success: results.ok = False for line in output: self.ui.info(line) self.ui.info("\n### Bro output in %s" % cwd) return results
def _break_lock(cmdout): from BroControl import execute try: # Check whether lock is stale. with open(config.Config.lockfile, "r") as f: pid = f.readline().strip() except (OSError, IOError) as err: cmdout.error("failed to read lock file: %s" % err) return -1 success, output = execute.run_localcmd( "%s %s" % (os.path.join(config.Config.helperdir, "check-pid"), pid)) if success and output.strip() == "running": # Process still exists. try: return int(pid) except ValueError: return -1 cmdout.info("removing stale lock") try: # Break lock. os.unlink(config.Config.lockfile) except (OSError, IOError) as err: cmdout.error("failed to remove lock file: %s" % err) return -1 return 0
def _get_bro_version(self): from BroControl import execute bro = self.config["bro"] if not os.path.lexists(bro): raise ConfigurationError("cannot find Bro binary: %s" % bro) version = "" success, output = execute.run_localcmd("%s -v" % bro) if success and output: version = output.splitlines()[-1] else: msg = " with no output" if output: msg = " with output:\n%s" % output raise RuntimeEnvironmentError('running "bro -v" failed%s' % msg) match = re.search(".* version ([^ ]*).*$", version) if not match: raise RuntimeEnvironmentError( 'cannot determine Bro version ("bro -v" output: %s)' % version.strip()) version = match.group(1) # If bro is built with the "--enable-debug" configure option, then it # appends "-debug" to the version string. if version.endswith("-debug"): version = version[:-6] return version
def run_cron_cmd(self): # Run external command if we have one. if self.config.croncmd: success, output = execute.run_localcmd(self.config.croncmd) if not success: self.ui.error("failure running croncmd: %s" % self.config.croncmd)
def _initialize_options(self): from BroControl import execute # Set defaults for options we get passed in. self.init_option("brobase", self.basedir) self.init_option("broscriptdir", self.broscriptdir) self.init_option("version", VERSION) # Initialize options that are not already set. for opt in options.options: if not opt.dontinit: self.init_option(opt.name, opt.default) # Set defaults for options we derive dynamically. self.init_option("mailto", "%s" % os.getenv("USER")) self.init_option("mailfrom", "Big Brother <bro@%s>" % socket.gethostname()) self.init_option("mailalarmsto", self.config["mailto"]) # Determine operating system. success, output = execute.run_localcmd("uname") if not success or not output: raise RuntimeEnvironmentError("failed to run uname: %s" % output) self.init_option("os", output.strip()) # Determine the CPU pinning command. pin_cmd = "" if self.config["os"] == "Linux": pin_cmd = "taskset -c" elif self.config["os"] == "FreeBSD": pin_cmd = "cpuset -l" self.init_option("pin_command", pin_cmd) # Find the time command (should be a GNU time for best results). time_cmd = "" success, output = execute.run_localcmd("which time") if success and output: # On redhat-based systems, path to cmd is prefixed with '\t' on 2nd # line when alias is defined. time_cmd = output.splitlines()[-1].strip() self.init_option("time", time_cmd) # Calculate the log expire interval (in minutes). minutes = self._get_interval_minutes("logexpireinterval") self.init_option("logexpireminutes", minutes)
def expire_logs(self): if self.config.logexpireminutes == 0 and self.config.statslogexpireinterval == 0: return success, output = execute.run_localcmd(os.path.join(self.config.scriptsdir, "expire-logs")) if not success: self.ui.error("expire-logs failed\n%s" % output)
def expire_logs(self): if self.config.logexpireinterval == "0" and self.config.statslogexpireinterval == "0": return (success, output) = execute.run_localcmd(os.path.join(self.config.scriptsdir, "expire-logs")) if not success: self.ui.error("expire-logs failed\n") for line in output: self.ui.error(line)
def expire_logs(self): if self.config.logexpireminutes == 0 and self.config.statslogexpireinterval == 0: return (success, output) = execute.run_localcmd(os.path.join(self.config.scriptsdir, "expire-logs")) if not success: self.ui.error("expire-logs failed\n") for line in output: self.ui.error(line)
def process(self, trace, bro_options, bro_scripts): results = cmdresult.CmdResult() if not os.path.isfile(trace): self.ui.error("trace file not found: %s" % trace) results.ok = False return results if self.config.standalone: tag = "standalone" else: tag = "workers" node = self.config.nodes(tag=tag)[0] cwd = os.path.join(self.config.tmpdir, "testing") if os.path.isdir(cwd): try: shutil.rmtree(cwd) except OSError as err: self.ui.error("cannot remove directory: %s" % err) results.ok = False return results try: os.makedirs(cwd) except OSError as err: self.ui.error("cannot create directory: %s" % err) results.ok = False return results env = _make_env_params(node) bro_args = " ".join(bro_options + _make_bro_params(node, False)) bro_args += " broctl/process-trace" if bro_scripts: bro_args += " " + " ".join(bro_scripts) cmd = os.path.join(self.config.scriptsdir, "run-bro-on-trace") + " %s %s %s %s" % (0, cwd, trace, bro_args) self.ui.info(cmd) (success, output) = execute.run_localcmd(cmd, env, donotcaptureoutput=True) if not success: results.ok = False for line in output: self.ui.info(line) self.ui.info("\n### Bro output in %s" % cwd) return results
def _initialize_options(self): from BroControl import execute # Set defaults for options we get passed in. self._set_option("brobase", self.basedir) self._set_option("broscriptdir", self.broscriptdir) self._set_option("version", VERSION) # Initialize options that are not already set. for opt in options.options: if not opt.dontinit: self._set_option(opt.name, opt.default) # Set defaults for options we derive dynamically. self._set_option("mailto", "%s" % os.getenv("USER")) self._set_option("mailfrom", "Big Brother <bro@%s>" % socket.gethostname()) self._set_option("mailalarmsto", self.config["mailto"]) # Determine operating system. (success, output) = execute.run_localcmd("uname") if not success: raise RuntimeError("cannot run uname") self._set_option("os", output[0].lower().strip()) if self.config["os"] == "linux": self._set_option("pin_command", "taskset -c") elif self.config["os"] == "freebsd": self._set_option("pin_command", "cpuset -l") else: self._set_option("pin_command", "") # Find the time command (should be a GNU time for best results). (success, output) = execute.run_localcmd("which time") if success: self._set_option("time", output[0].lower().strip()) else: self._set_option("time", "") minutes = self._get_interval_minutes("logexpireinterval") self._set_option("logexpireminutes", minutes)
def _initialize_options(self, basedir, broscriptdir): from BroControl import execute # Set defaults for options we get passed in. self._set_option("brobase", basedir) self._set_option("broscriptdir", broscriptdir) self._set_option("version", VERSION) # Initialize options that are not already set. for opt in options.options: if not opt.dontinit: self._set_option(opt.name, opt.default) # Set defaults for options we derive dynamically. self._set_option("mailto", "%s" % os.getenv("USER")) self._set_option("mailfrom", "Big Brother <bro@%s>" % socket.gethostname()) self._set_option("mailalarmsto", self.config["mailto"]) # Determine operating system. (success, output) = execute.run_localcmd("uname") if not success: raise RuntimeError("cannot run uname") self._set_option("os", output[0].lower().strip()) if self.config["os"] == "linux": self._set_option("pin_command", "taskset -c") elif self.config["os"] == "freebsd": self._set_option("pin_command", "cpuset -l") else: self._set_option("pin_command", "") # Find the time command (should be a GNU time for best results). (success, output) = execute.run_localcmd("which time") if success: self._set_option("time", output[0].lower().strip()) else: self._set_option("time", "")
def getCFlowStatus(self): (success, output) = execute.run_localcmd( os.path.join(self.config.scriptsdir, "cflow-stats")) if not success or not output: self.ui.error("failed to run cflow-stats") return None vals = {} for line in output: try: (port, pps, bps, pkts, bytes) = line.split() vals[port] = (float(pkts), float(bytes)) except ValueError: # Probably an error message because we can't connect. self.ui.error("failed to get cFlow statistics: %s" % line) return None return vals
def expire_logs(self): if self.config.logexpireminutes == 0 and self.config.statslogexpireinterval == 0: return if self.config.standalone: success, output = execute.run_localcmd(os.path.join(self.config.scriptsdir, "expire-logs")) if not success: self.ui.error("expire-logs failed\n%s" % output) else: nodes = self.config.hosts(tag=node_mod.logger_group()) if not nodes: nodes = self.config.hosts(tag=node_mod.manager_group()) expirelogs = os.path.join(self.config.scriptsdir, "expire-logs") cmds = [(node, expirelogs, []) for node in nodes] for (node, success, output) in self.executor.run_cmds(cmds): if not success: self.ui.error("expire-logs failed for node %s\n" % node) if output: self.ui.error(output)
def expire_logs(self): if self.config.logexpireminutes == 0 and self.config.statslogexpireinterval == 0: return if self.config.standalone: success, output = execute.run_localcmd( os.path.join(self.config.scriptsdir, "expire-logs")) if not success: self.ui.error("expire-logs failed\n%s" % output) else: nodes = self.config.hosts(tag=node_mod.logger_group()) if not nodes: nodes = self.config.hosts(tag=node_mod.manager_group()) expirelogs = os.path.join(self.config.scriptsdir, "expire-logs") cmds = [(node, expirelogs, []) for node in nodes] for (node, success, output) in self.executor.run_cmds(cmds): if not success: self.ui.error("expire-logs failed for node %s\n" % node) if output: self.ui.error(output)
def _sendmail(self, subject, body): if not self.config.sendmail: return True, "" cmd = "%s '%s'" % (os.path.join(self.config.scriptsdir, "send-mail"), subject) return execute.run_localcmd(cmd, inputtext=body)
def update_http_stats(self): if not self.config.statslogenable: return # Create meta file. if not os.path.exists(self.config.statsdir): try: os.makedirs(self.config.statsdir) except OSError as err: self.ui.error("failure creating directory in broctl option statsdir: %s" % err) return self.ui.info("creating directory for stats file: %s" % self.config.statsdir) metadat = os.path.join(self.config.statsdir, "meta.dat") try: with open(metadat, "w") as meta: for node in self.config.hosts(): meta.write("node %s %s %s\n" % (node, node.type, node.host)) meta.write("time %s\n" % time.asctime()) meta.write("version %s\n" % self.config.version) try: meta.write("os %s\n" % execute.run_localcmd("uname -a")[1][0]) except IndexError: meta.write("os <error>\n") try: meta.write("host %s\n" % execute.run_localcmd("hostname")[1][0]) except IndexError: meta.write("host <error>\n") except IOError as err: self.ui.error("failure creating file: %s" % err) return wwwdir = os.path.join(self.config.statsdir, "www") if not os.path.isdir(wwwdir): try: os.makedirs(wwwdir) except OSError as err: self.ui.error("failed to create directory: %s" % err) return # Update the WWW data statstocsv = os.path.join(self.config.scriptsdir, "stats-to-csv") (success, output) = execute.run_localcmd("%s %s %s %s" % (statstocsv, self.config.statslog, metadat, wwwdir)) if success: shutil.copy(metadat, wwwdir) else: self.ui.error("error reported by stats-to-csv") for line in output: self.ui.error(line) # Append the current stats.log in spool to the one in ${statsdir} dst = os.path.join(self.config.statsdir, os.path.basename(self.config.statslog)) try: with open(self.config.statslog, "r") as fsrc: with open(dst, "a") as fdst: shutil.copyfileobj(fsrc, fdst) except IOError as err: self.ui.error("failed to append file: %s" % err) return os.unlink(self.config.statslog)
def run_cron_cmd(self): # Run external command if we have one. if self.config.croncmd: (success, output) = execute.run_localcmd(self.config.croncmd) if not success: self.ui.error("failure running croncmd: %s" % self.config.croncmd)
def update_http_stats(self): if not self.config.statslogenable: return # Create meta file. if not os.path.exists(self.config.statsdir): try: os.makedirs(self.config.statsdir) except OSError as err: self.ui.error( "failure creating directory in broctl option statsdir: %s" % err) return self.ui.info("creating directory for stats file: %s" % self.config.statsdir) metadat = os.path.join(self.config.statsdir, "meta.dat") try: with open(metadat, "w") as meta: for node in self.config.hosts(): meta.write("node %s %s %s\n" % (node, node.type, node.host)) meta.write("time %s\n" % time.asctime()) meta.write("version %s\n" % self.config.version) success, output = execute.run_localcmd("uname -a") if success and output: # Note: "output" already has a '\n' meta.write("os %s" % output) else: meta.write("os <error>\n") success, output = execute.run_localcmd("hostname") if success and output: # Note: "output" already has a '\n' meta.write("host %s" % output) else: meta.write("host <error>\n") except IOError as err: self.ui.error("failure creating file: %s" % err) return wwwdir = os.path.join(self.config.statsdir, "www") if not os.path.isdir(wwwdir): try: os.makedirs(wwwdir) except OSError as err: self.ui.error("failed to create directory: %s" % err) return # Update the WWW data statstocsv = os.path.join(self.config.scriptsdir, "stats-to-csv") success, output = execute.run_localcmd( "%s %s %s %s" % (statstocsv, self.config.statslog, metadat, wwwdir)) if success: shutil.copy(metadat, wwwdir) else: self.ui.error("error reported by stats-to-csv\n%s" % output) # Append the current stats.log in spool to the one in ${statsdir} dst = os.path.join(self.config.statsdir, os.path.basename(self.config.statslog)) try: with open(self.config.statslog, "r") as fsrc: with open(dst, "a") as fdst: shutil.copyfileobj(fsrc, fdst) except IOError as err: self.ui.error("failed to append file: %s" % err) return os.unlink(self.config.statslog)