OSSIM-agent源代码分析(三)

2021/10/24 23:12:45

本文主要是介绍OSSIM-agent源代码分析(三),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

2021SC@SDUSC

OSSIM-agent源代码分析(三)

OSSIM-agent-config.py

简述

OSSIM Agent的主要职责是收集网络上存在的各种设备发送的所有数据,然后按照一种标准方式(standardized way)有序的发送给OSSIM Server,Agent收集到数据后在发送给Server之前要对这些数据进行标准化处理,这样Server就可以依一种统一的方式来处理这些信息,并且也简化了Server的处理过程

agent的配置文件是config.py,作为大量数据的标准化处理位置,配置文件的对各种数据、文件的处理方式的

config配置类

导入相关引用文件

import os, sys, string, re
import codecs
from ConfigParser import ConfigParser
from optparse import OptionParser
from Exceptions import AgentCritical
from Logger import Logger
logger = Logger.logger
import ParserUtil

相关配置参数在config.cfg

    _NEEDED_CONFIG_ENTRIES = {
        'daemon': [],
        'log': [],
        'plugin-defaults': ['sensor', 'interface', 'tzone'],
        'watchdog': ['enable', 'interval'],
        'output-server': ['enable', 'ip', 'port'],
        'plugins': [],
    }

通过正则表达式,覆盖OPTCRE选项,使得允许在选项名称中使用:

    OPTCRE = re.compile(
        r'(?P<option>[^:=\s][^=]*)'          
        r'\s*(?P<vi>[:=])\s*'                                            
        r'(?P<value>.*)$'                    
        )

检查相关配置文件

 def read(self, filenames, encoding='latin1',check_neededEntries = True):
        self.__validConfig = True
        fp=None
        for filename in filenames:
            if not os.path.isfile(filename):
                AgentCritical("Configuration file (%s) does not exist!" % (filename))
            try:
                fp = codecs.open(filename, 'r', encoding=encoding)
                self.readfp(fp)
            except Exception,e:
                logger.error("Invalid plugin file:%s",str(e))
                self.__validConfig = False
            if check_neededEntries:
                self.check_needed_config_entries()

在.cfg文件中检查所需的文件,此函数使用变量_NEEDED_CONFIG_ENTRIES,如果没有所需文件,则退出

 def check_needed_config_entries(self):
        for section, values in self._NEEDED_CONFIG_ENTRIES.iteritems():
            if not self.has_section(section):
                logger.critical (
                    "Needed section [%s] not found!" % (section))
                self.__validConfig = False
                if self._EXIT_IF_MALFORMED_CONFIG:
                    sys.exit()
            for value in values:
                if not self.has_option(section, value):
                    self.__validConfig = False
                    logger.critical (
                        "Needed option [%s->%s] not found!" % (section, value))
                    if self._EXIT_IF_MALFORMED_CONFIG:
                        sys.exit()

避免配置值中的混淆

    def _strip_value(self, value):
        from string import strip
        return strip(strip(value, '"'), "'")

这个方法与RawConfigParser.items()相同,但返回哈希值而不是列表

    def hitems(self, section,braw=False):
        hash = {}
        for item in self.items(section,braw):
            hash[item[0]] = self._strip_value(item[1])
        return hash

这个方法与ConfigParser.get()相同,但使用and剥离值

    def get(self, section, option):
        try:
            value = ConfigParser.get(self, section, option)
            value = self._strip_value(value)
        except:
            value = ""
        return value

    def getboolean(self, section, option):
        try:
            value = ConfigParser.getboolean(self, section, option)
        except ValueError: # not a boolean
            logger.warning("Value %s->%s is not a boolean" % (section, option))
            return False
        return value

打印配置对象的表示形式,用于调试

    def __repr__(self):
        conf_str = '<sensor-config>\n'
        for section in sorted(self.sections()):
            conf_str += '  <section name="%s">\n' % (section)
            for i in self.items(section):
                conf_str += '    <item name="%s" value="%s" />\n' % (i[0], i[1])
            conf_str += '  </section>\n'
        conf_str += '</sensor-config>'
        return conf_str


这篇关于OSSIM-agent源代码分析(三)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程