def doCron(watch): if config.Config.cronenabled == "0": return config.Config.config[ "cron"] = "1" # Flag to indicate that we're running from cron. if not util.lock(): return util.bufferOutput() if watch: # Check whether nodes are still running an restart if neccessary. for (node, isrunning) in control.isRunning(config.Config.nodes()): if not isrunning and node.hasCrashed(): control.start([node]) # Check for dead hosts. _checkHosts() # Generate statistics. _logStats(5) # Check available disk space. _checkDiskSpace() # Expire old log files. _expireLogs() # Update the HTTP stats directory. _updateHTTPStats() # Run external command if we have one. if config.Config.croncmd: execute.runLocalCmd(config.Config.croncmd) # Mail potential output. output = util.getBufferedOutput() if output: util.sendMail("cron: " + output.split("\n")[0], output) util.unlock() config.Config.config["cron"] = "0" util.debug(1, "cron done")
def doCron(watch): if config.Config.cronenabled == "0": return config.Config.config["cron"] = "1" # Flag to indicate that we're running from cron. if not util.lock(): return util.bufferOutput() if watch: # Check whether nodes are still running an restart if neccessary. for (node, isrunning) in control.isRunning(config.Config.nodes()): if not isrunning and node.hasCrashed(): control.start([node]) # Check for dead hosts. _checkHosts() # Generate statistics. _logStats(5) # Check available disk space. _checkDiskSpace() # Expire old log files. _expireLogs() # Update the HTTP stats directory. _updateHTTPStats() # Run external command if we have one. if config.Config.croncmd: execute.runLocalCmd(config.Config.croncmd) # Mail potential output. output = util.getBufferedOutput() if output: util.sendMail("cron: " + output.split("\n")[0], output) util.unlock() config.Config.config["cron"] = "0" util.debug(1, "cron done")
def sendMail(subject, body): if not config.Config.sendmail: return cmd = "%s '%s'" % (os.path.join(config.Config.scriptsdir, "send-mail"), subject) (success, output) = execute.runLocalCmd(cmd, "", body) if not success: warn("cannot send mail")
def __init__(self, config, basedir, broscriptdir, version): global Config Config = self self.config = {} self.state = {} # Read broctl.cfg. self.config = self._readConfig(config) # Set defaults for options we get passed in. self._setOption("brobase", basedir) self._setOption("broscriptdir", broscriptdir) self._setOption("version", version) # Initialize options. for opt in options.options: if not opt.dontinit: self._setOption(opt.name, opt.default) # Set defaults for options we derive dynamically. self._setOption("mailto", "%s" % os.getenv("USER")) self._setOption("mailfrom", "Big Brother <bro@%s>" % socket.gethostname()) self._setOption("home", os.getenv("HOME")) self._setOption("mailalarmsto", self.config["mailto"]) # Determine operating system. (success, output) = execute.runLocalCmd("uname") if not success: util.error("cannot run uname") self._setOption("os", output[0].lower().strip()) if self.config["os"] == "linux": self._setOption("pin_command", "taskset -c") elif self.config["os"] == "freebsd": self._setOption("pin_command", "cpuset -l") else: self._setOption("pin_command", "") # Find the time command (should be a GNU time for best results). (success, output) = execute.runLocalCmd("which time") if success: self._setOption("time", output[0].lower().strip()) else: self._setOption("time", "")
def _expireLogs(): i = int(config.Config.logexpireinterval) if not i: return (success, output) = execute.runLocalCmd(os.path.join(config.Config.scriptsdir, "expire-logs")) if not success: util.output("error running expire-logs\n\n") util.output(output)
def processTrace(trace, bro_options, bro_scripts): if not os.path.isfile(trace): util.output("Error: trace file not found: %s" % trace) return False if not os.path.exists(os.path.join(config.Config.scriptsdir, "broctl-config.sh")): util.output("error: broctl-config.sh not found (try 'broctl install')") return False standalone = config.Config.standalone == "1" if standalone: tag = "standalone" else: tag = "workers" node = config.Config.nodes(tag=tag)[0] cwd = os.path.join(config.Config.tmpdir, "testing") if not execute.rmdir(config.Config.manager(), cwd): util.output("cannot remove directory %s on manager" % cwd) return False if not execute.mkdir(config.Config.manager(), cwd): util.output("cannot create directory %s on manager" % cwd) return False env = _makeEnvParam(node) bro_args = " ".join(bro_options + _makeBroParams(node, False)) bro_args += " broctl/process-trace" if bro_scripts: bro_args += " " + " ".join(bro_scripts) cmd = os.path.join(config.Config.scriptsdir, "run-bro-on-trace") + " %s %s %s %s" % (0, cwd, trace, bro_args) print cmd (success, output) = execute.runLocalCmd(cmd, env, donotcaptureoutput=True) for line in output: util.output(line) util.output("") util.output("### Bro output in %s" % cwd) return success
def getCFlowStatus(): (success, output) = execute.runLocalCmd(os.path.join(config.Config.scriptsdir, "cflow-stats")) if not success or not output: util.warn("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. util.warn("failed to get cFlow statistics: %s" % line) return None return vals
def processTrace(trace, bro_options, bro_scripts): standalone = (config.Config.standalone == "1") if standalone: tag = "standalone" else: tag = "workers" node = config.Config.nodes(tag=tag)[0] cwd = os.path.join(config.Config.tmpdir, "testing") if not execute.rmdir(config.Config.manager(), cwd): util.output("cannot remove directory %s on manager" % cwd) return False if not execute.mkdir(config.Config.manager(), cwd): util.output("cannot create directory %s on manager" % cwd) return False env = _makeEnvParam(node) bro_args = " ".join( bro_options + _makeBroParams(node, False, add_manager=(not standalone))) if bro_scripts: bro_args += " " + " ".join(bro_scripts) cmd = os.path.join( config.Config.scriptsdir, "run-bro-on-trace") + " %s %s %s %s" % (0, cwd, trace, bro_args) cmd += " broctl/process-trace" print cmd (success, output) = execute.runLocalCmd(cmd, env, donotcaptureoutput=True) for line in output: util.output(line) util.output("") util.output("### Bro output in %s" % cwd) return success
def getCFlowStatus(): (success, output) = execute.runLocalCmd( os.path.join(config.Config.scriptsdir, "cflow-stats")) if not success or not output: util.warn("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. util.warn("failed to get cFlow statistics: %s" % line) return None return vals
def _updateHTTPStats(): # Get the prof.logs. # FIXME: Disabled for now. This currently copies the complete prof.log # each time. As these can get huge, that can take a while. We should # change that to only copy the most recent chunk and then also expire old # prof logs on the manager. # _getProfLogs() # Create meta file. if not os.path.exists(config.Config.statsdir): util.warn("creating directory for stats file: %s" % config.Config.statsdir) os.makedirs(config.Config.statsdir) meta = open(os.path.join(config.Config.statsdir, "meta.dat"), "w") for node in config.Config.hosts(): print >> meta, "node", node, node.type, node.host print >> meta, "time", time.asctime() print >> meta, "version", config.Config.version try: print >> meta, "os", execute.captureCmd("uname -a")[1][0] except IndexError: print >> meta, "os <error>" try: print >> meta, "host", execute.captureCmd("hostname")[1][0] except IndexError: print >> meta, "host <error>" meta.close() # Run the update-stats script. (success, output) = execute.runLocalCmd( os.path.join(config.Config.scriptsdir, "update-stats")) if not success: util.output("error running update-stats\n\n") util.output(output)
def _getBroVersion(self): version = "" bro = self.subst("${bindir}/bro") if execute.exists(None, bro): (success, output) = execute.runLocalCmd("%s -v" % bro) if success and output: version = output[-1] else: util.error("cannot find Bro binary to determine version") m = re.search(".* version ([^ ]*).*$", version) if not m: util.error("cannot determine Bro version [%s]" % version.strip()) version = m.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 _updateHTTPStats(): # Get the prof.logs. # FIXME: Disabled for now. This currently copies the complete prof.log # each time. As these can get huge, that can take a while. We should # change that to only copy the most recent chunk and then also expire old # prof logs on the manager. # _getProfLogs() # Create meta file. if not os.path.exists(config.Config.statsdir): util.warn("creating directory for stats file: %s" % config.Config.statsdir) os.makedirs(config.Config.statsdir) meta = open(os.path.join(config.Config.statsdir, "meta.dat"), "w") for node in config.Config.hosts(): print >>meta, "node", node, node.type, node.host print >>meta, "time", time.asctime() print >>meta, "version", config.Config.version try: print >>meta, "os", execute.captureCmd("uname -a")[1][0] except IndexError: print >>meta, "os <error>" try: print >>meta, "host", execute.captureCmd("hostname")[1][0] except IndexError: print >>meta, "host <error>" meta.close() # Run the update-stats script. (success, output) = execute.runLocalCmd(os.path.join(config.Config.scriptsdir, "update-stats")) if not success: util.output("error running update-stats\n\n") util.output(output)
metadat = os.path.join(config.Config.statsdir, "meta.dat") try: meta = open(metadat, "w") except IOError, err: util.output("error creating file: %s" % err) return for node in config.Config.hosts(): print >>meta, "node", node, node.type, node.host print >>meta, "time", time.asctime() print >>meta, "version", config.Config.version try: print >>meta, "os", execute.runLocalCmd("uname -a")[1][0] except IndexError: print >>meta, "os <error>" try: print >>meta, "host", execute.runLocalCmd("hostname")[1][0] except IndexError: print >>meta, "host <error>" meta.close() wwwdir = os.path.join(config.Config.statsdir, "www") if not os.path.isdir(wwwdir): try: os.makedirs(wwwdir) except OSError, err:
util.warn("creating directory for stats file: %s" % config.Config.statsdir) try: meta = open(os.path.join(config.Config.statsdir, "meta.dat"), "w") except IOError, err: util.output("error creating file: %s" % err) return for node in config.Config.hosts(): print >>meta, "node", node, node.type, node.host print >>meta, "time", time.asctime() print >>meta, "version", config.Config.version try: print >>meta, "os", execute.runLocalCmd("uname -a")[1][0] except IndexError: print >>meta, "os <error>" try: print >>meta, "host", execute.runLocalCmd("hostname")[1][0] except IndexError: print >>meta, "host <error>" meta.close() # Run the update-stats script. (success, output) = execute.runLocalCmd(os.path.join(config.Config.scriptsdir, "update-stats")) if not success: util.output("error running update-stats\n\n")