def __init__(self, cfgFile=None): self._configFile = cfgFile if self._configFile == None: #self._configFile = sys_config.getDir() + "/conf/mdstack.conf" self._configFile = os.path.split( os.path.realpath(__file__))[0] + "/conf/mdstack.conf" #print self._configFile if os.path.exists(self._configFile) == False: self._configFile = "/opt/mdstack/conf/mdstackd/mdstack.conf" if os.path.exists(self._configFile) == False: raise NoConfigFile # 暂不捕获该错误,直接退出 conf = sys_config.SysConfig(self._configFile) eshost = conf.getConfig("esflow", "esHost") esport = conf.getConfig("esflow", "esPort") self._url = 'http://' + eshost + ":" + esport # 日志文件 self._logFile = conf.getConfig("datastats", "logFile") # 实例名 self._instance = conf.getConfig("datastats", "instanceName") # 抓包配置文件 # 在配置文件mdstack.conf的datastats段,增加一个抓包配置文件位置的配置项 self._capConf = conf.getConfig("datastats", "captureConfigFile") if self._capConf == None: self._capConf = "/usr/local/etc/capture/config.ini" self._confCap = sys_config.SysConfig(self._capConf)
def main(): # 配置文件 #configFile = sys_config.getDir() + "/mdstack/conf/mdstack.conf" configFile = os.path.split( os.path.realpath(__file__))[0] + "/mdstack/conf/mdstack.conf" if os.path.exists(configFile) == False: configFile = "/opt/mdstack/conf/mdstackd/mdstack.conf" serviceName = "datastats" confZone = "datastats" conf = sys_config.SysConfig(configFile) # 进程号文件名 pidFile = conf.getConfig(confZone, "pidFile") # 日志文件 logFile = conf.getConfig(confZone, "logFile") # 实例名 instance = conf.getConfig(confZone, "instanceName") daemon1 = daemon.Daemon(serviceName, pidFile, logFile, instance) try: lstPar, lstErrPar = getopt( sys.argv[1:], "", ["start", "autostart", "stop", "restart", "status", "help"]) if len(lstErrPar) > 0: print "Unknown command, try \"python run_datastats.py --help\" for more information." elif len(lstPar) <> 1: print "Unknown command, try \"python run_datastats.py --help\" for more information." else: if lstPar[0][0] == "--start": sys_log.SysLog(logFile, instance).writeLog( "info", "The Data Statistics Service is starting ...") daemon1.start() elif lstPar[0][0] == "--autostart": sys_log.SysLog(logFile, instance).writeLog( "info", "The Data Statistics Service is starting ...") daemon1.start() elif lstPar[0][0] == "--stop": daemon1.stop() sys_log.SysLog(logFile, instance).writeLog( "info", "The Data Statistics Service is stopped") elif lstPar[0][0] == "--restart": sys_log.SysLog(logFile, instance).writeLog( "info", "The Data Statistics Service is restarting ...") daemon1.restart() elif lstPar[0][0] == "--status": daemon1.status() elif lstPar[0][0] == "--help": print "Usage: python run_datastats.py {Options}" print "Options:" print " --start: Start the Data Statistics Service" print " --stop: Stop the Data Statistics Service" print " --restart: Restart the Data Statistics Service" print " --status: Show the status of the Data Statistics Service" print " --help: Show the help" except GetoptError, e: print e.msg + ", try \"run_datastats.py --help\" for more information."
def stats_firewall_dashboard(): """ 自定义防火墙dashboard,统一调用入口 """ # 配置文件 configFile = "/opt/mdstack/conf/mdstackd/mdstack.conf" conf = sys_config.SysConfig(configFile) logFile = conf.getConfig("datastats", "logFile") instance = conf.getConfig("datastats", "instanceName") es_host = conf.getConfig("elasticsearch", "esHost") es_port = conf.getConfig("elasticsearch", "esPort") post_host = conf.getConfig("postgresql", "dbHost") post_port = conf.getConfig("postgresql", "dbPort") post_user = conf.getConfig("postgresql", "dbUser") post_pwd = conf.getConfig("postgresql", "dbPwd") post_db = conf.getConfig("postgresql", "dbName") # 删除过期的统计数据 post_url = "host=" + post_host + " port=" + post_port + " user="******" password="******" dbname=" + post_db days = post_data.get_expiry_date(post_url, "flows") + 1 idx_name = "firewall_dashboard" url = 'http://' + es_host + ":" + es_port print url conn = ES(url, timeout=120) localtz = timezone('Asia/Shanghai') # 基础查询条件,这里一般设置查询防火墙的条件 base_condition = "(msg.dn:\"hw:venustech:usg_fw_3610d\")" # 统计各台防火墙在每个小时内被pass/deny的访问次数、字节(发送/接收/总数)、数据包(发送/接收/总数) if conf.getConfig("custom_dashboard", "firewall.fw_stats_per_hour") == "Y": try: doc_type = "fw_stats_per_hour" # 删除过期的统计数据 clear_index(idx_name, doc_type, conn, days) day = datetime.now() - timedelta(1) fw_stats_per_hour(idx_name, doc_type, conn, day, localtz, base_condition) day = datetime.now() - timedelta(2) fw_stats_per_hour(idx_name, doc_type, conn, day, localtz, base_condition) except Exception, e: sys_log.SysLog(logFile, instance).writeLog("error", str(traceback.format_exc()))
def stats_flows_dashboard(): """ 自定义流量dashboard,统一调用入口 """ # 配置文件 configFile = "/opt/mdstack/conf/mdstackd/mdstack.conf" conf = sys_config.SysConfig(configFile) logFile = conf.getConfig("datastats", "logFile") instance = conf.getConfig("datastats", "instanceName") es_host = conf.getConfig("elasticsearch", "esHost") es_port = conf.getConfig("elasticsearch", "esPort") post_host = conf.getConfig("postgresql", "dbHost") post_port = conf.getConfig("postgresql", "dbPort") post_user = conf.getConfig("postgresql", "dbUser") post_pwd = conf.getConfig("postgresql", "dbPwd") post_db = conf.getConfig("postgresql", "dbName") # 删除过期的统计数据 post_url = "host=" + post_host + " port=" + post_port + " user="******" password="******" dbname=" + post_db days = post_data.get_expiry_date(post_url, "flows") + 1 idx_name = "flows_dashboard" url = 'http://' + es_host + ":" + es_port print url conn = ES(url, timeout=120) localtz = timezone('Asia/Shanghai') # 基础查询条件,这里一般设置通用查询的条件 base_condition = "" # 统计昨天、前天每个小时内网络流量的session次数、包数、字节数、数据字节数、时延 if conf.getConfig("custom_dashboard", "flows.flows_stats_per_hour") == "Y": try: doc_type = "flows_stats_per_hour" # 删除过期的统计数据 clear_index(idx_name, doc_type, conn, days) day = datetime.now() - timedelta(1) flows_stats_per_hour(idx_name, doc_type, conn, day, localtz, base_condition) day = datetime.now() - timedelta(2) flows_stats_per_hour(idx_name, doc_type, conn, day, localtz, base_condition) except Exception, e: sys_log.SysLog(logFile, instance).writeLog("error", str(traceback.format_exc()))
def stats_dashboard(pd): """ 统一调用入口函数 """ try: if pd != None and pd.is_master() == False: return # 自定义防火墙dashboard统计 firewall_dashboard.stats_firewall_dashboard() flows_dashboard.stats_flows_dashboard() except Exception, e: # 配置文件 configFile = "/opt/mdstack/conf/mdstackd/mdstack.conf" conf = sys_config.SysConfig(configFile) # 错误日志 logFile = conf.getConfig("datastats", "logFile") instance = conf.getConfig("datastats", "instanceName") sys_log.SysLog(logFile, instance).writeLog("error", str(traceback.format_exc()))