Esempio n. 1
0
def testRule(appServer, ruleId=False, ruleData=False):
    """Usage: testRule.py appServer [ruleId=ruleId, ruleData=ruleData]"""
    myHandler = incidentHandler(appServer)
    sys_rule_id = ''
    pause = 0
    if ruleData:
        sys_rule_id = ruleData['ruleId']
    elif ruleId:
        myRule = myHandler.getRule(ruleId)
        if myRule:
            sys_rule_id = myRule.attribute['id']
            fb_type = myRule.filterOperators.type
            if fb_type == 'FOLLOWED_BY':
                pause = 5
    dataMap = {}
    dataMap['pause'] = pause
    count = 0
    if ruleData:
        dataMap['reportIp'] = ruleData['reportIp']
        dataMap['rawData'] = ruleData['rawMsg']
    else:
        myParam = myHandler.getTestRuleParameter(ruleId)
        count = int(myParam.count)
        reportIp = ''
        if myParam:
            reportIp = myParam.reptDevIpAddr
            if reportIp == '$localhost':
                reportIp = getLocalhostIp()
            if reportIp == '$appServer':
                reportIp = appServer
        myData = myHandler.getRawData(rule_id)
        if myData:
            dataList = []
            for item in myData.dataMap['default'].eventMsg:
                if '$reporter' in item:
                    item = item.replace('$reporter', reportIp)
                if '$localhost' in item:
                    item = item.replace('$localhost', getLocalhostIp())
                if '$appServer' in item:
                    item = item.replace('$appServer', appServer)
                dataList.append(item)
            dataMap = {}
            dataMap['pause'] = 0
            dataMap['reportIp'] = reportIp
            dataMap['rawData'] = dataList
    status = ''
    msg = ''
    if dataMap and sys_rule_id:
        status, msg = myHandler.testRule(sys_rule_id, count, dataMap)

    return status, msg
Esempio n. 2
0
 def trigger(self, param, msgs):
     rept_ip = ''
     if param.reptDevIpAddr == '$localhost':
         rept_ip = getLocalhostIp()
     elif param.reptDevIpAddr == '$appServer':
         rept_ip = self.appServer
     else:
         rept_ip = param.reptDevIpAddr
     if param.createDevice:
         self.createDevice(param.reptDevIpAddr,
                           param.deviceName,
                           param.deviceType,
                           domain=param.domainController)
     rawSend = False
     if self.posix and param.reptDevIpAddr != '$localhost':
         rawSend = True
     if rawSend:
         mySendEvent = rawUdpSendHandler.rawUdpSendHandler(
             self.appServer, PORTS[param.method])
     else:
         mySendEvent = sendEventHandler.sendEventHandler(
             param.method, self.appServer)
     rept = {}
     rept['$localhost'] = getLocalhostIp()
     rept['$dataCollector'] = self.appServer
     rept['$reporter'] = rept_ip
     print rept_ip
     for i in range(int(param.count)):
         for line in msgs:
             msg = generalUtility.multiReplace(line, rept)
             if '$randomIP' in line:
                 ip = getRandomIPAddr()
                 msg = msg.replace('$randomIP', ip)
             if '$randomNum' in line:
                 num = getRandomNum(1, 100)
                 msg = msg.replace('$randomNum', num)
             if rawSend:
                 if param.method == 'netflow':
                     temp_msg = GenerateNetFlowData.getNetFlowPacket(msg)
                 else:
                     temp_msg = msg.strip()
                 send_msg = GenerateRawIPData.getRawIpPacket(
                     temp_msg, rept_ip, self.appServer, PORTS[param.method])
             else:
                 send_msg = msg
             print msg
             mySendEvent.sendoutEvent(send_msg)
             time.sleep(1)
Esempio n. 3
0
 def __init__(self, appServer):
     self.rest=restApiDataHandler(appServer)
     self.parsers={}
     self.parser_names={}
     if not os.path.exists(result_path):
         os.mkdir(result_path)
     self.localhost=getLocalhostIp()
     self.dbHandler=caseDbHandler('EventParsing')
     self.needCoverEvents=[]
     self.testEvents=[]
Esempio n. 4
0
 def __init__(self, server, mod=False):
     self.udphandler=udpSendHandler(server)
     self.appHandler=appHandler(server)
     self.module_name=mod
     self.localhost=getLocalhostIp()
Esempio n. 5
0
def testRule(appServer, rule_id):
    """Usage: testRule.py appServer, ruleId"""
    myHandler = incidentHandler(appServer)
    myRules = {}
    autoParams = myHandler.getAutoParams()
    if rule_id.lower() == 'all':
        myRules = myHandler.getAllRules()
    else:
        for item in rule_id.split(','):
            myRules[item] = myHandler.getRule(item)
    test_result = {}
    netflow_rules = {}
    not_implemented_rules = {}
    if len(myRules):
        for rule_key in myRules.keys():
            if rule_key not in autoParams.keys():
                not_implemented_rules[rule_key] = myRules[rule_key].name
            elif autoParams[rule_key].method == 'netflow':
                netflow_rules[rule_key] = myRules[rule_key].name
            else:
                print 'Testing rule %s' % rule_key
                sys_rule_id = myRules[rule_key].attribute['id']
                pause = 0
                fb_type = myRules[rule_key].filterOperators.type
                if fb_type == 'FOLLOWED_BY':
                    pause = 5
                myParam = autoParams[rule_key]
                reportIp = ''
                if myParam:
                    reportIp = myParam.reptDevIpAddr
                    if reportIp == '$localhost':
                        reportIp = getLocalhostIp()
                    if reportIp == '$appServer':
                        reportIp = appServer
                    if myParam.createDevice:
                        myHandler.createDevice(myParam.reptDevIpAddr,
                                               myParam.deviceName,
                                               myParam.deviceType,
                                               domain=myParam.domainController)
                    myData = myHandler.getRawData(rule_key)
                if myData:
                    dataList = []
                    for item in myData.dataMap['default'].eventMsg:
                        if '$reporter' in item:
                            item = item.replace('$reporter', reportIp)
                        if '$localhost' in item:
                            item = item.replace('$localhost', getLocalhostIp())
                        if '$appServer' in item:
                            item = item.replace('$appServer', appServer)
                        dataList.append(item)
                    dataMap = {}
                    dataMap['pause'] = pause
                    dataMap['reportIp'] = reportIp
                    dataMap['rawData'] = dataList
                    status, msg = myHandler.testRule(sys_rule_id,
                                                     int(myParam.count),
                                                     dataMap)
                    reason = ''
                    if status == 'Failure':
                        matched = ''
                        for key in test_rule_exps.keys():
                            match = test_rule_exps[key].search(msg)
                            print key, match
                            if match:
                                matched = key
                                break
                        reason = matched
                    elif status == 'Unfinish':
                        rasson = 'timeout (10 minutes)'
                    elif status == 'Success':
                        reason = 'pass'
                map = {}
                map['name'] = myRules[rule_key].name
                map['status'] = status
                map['reason'] = reason
                test_result[rule_key] = map
                print '%s %s %s' % (rule_key, status, reason)
    writeHtml(test_result, netflow_rules, not_implemented_rules)
Esempio n. 6
0
def getTestConfig(config, src=None):
    testConfig = XmlHandler().XmlFileToObj(config)
    if testConfig is None:
        print 'testConfig file has problem: %s' % config
        sys.exit()
    testConfig.localhost = getLocalhostIp()
    testConfig.runTime = getTimeNowFormat()
    server = ''
    if getattr(testConfig.testServer, 'allInOne'):
        if src:
            server = src
        else:
            server = testConfig.testServer.allInOne
        testConfig.testServer.dbServer = server
        testConfig.testServer.appServer = server
        testConfig.testServer.dataCollector = server
    else:
        server = testConfig.testServer.appServer + '-' + testConfig.testServer.dataCollector
    if hasattr(testConfig, 'noSend') and testConfig.noSend == 'true':
        testConfig.noSend = True
    else:
        testConfig.noSend = False
    if os.name == 'posix':
        myPass = False
        if hasattr(testConfig, 'rootCredential'):
            myPass = testConfig.rootCredential.password
        print 'pass %s' % myPass
        myCheck = checkTimer(testConfig.testServer.dataCollector, pwd=myPass)
        myCheck.compareTime()
        setattr(testConfig, 'posix', True)
    if hasattr(testConfig, 'credential'):
        my_user = testConfig.credential.user
        my_password = testConfig.credential.password
    else:
        my_user = '******'
        my_password = '******'
    setattr(testConfig, 'user', my_user)
    setattr(testConfig, 'password', my_password)
    systemInfo = restApiDataHandler(server.split('-')[0],
                                    user=my_user,
                                    password=my_password).getData(
                                        'health', module='cloudStatus')
    if systemInfo:
        try:
            buildDate = convertTime(int(systemInfo.buildDate))
        except ValueError:
            buildDate = systemInfo.buildDate
        testConfig.buildVersion = systemInfo.version + ' (build on ' + buildDate + ')'
        ver_num = int(int(systemInfo.version.replace('.', '')) / 10000)
        if ver_num >= 371:
            setattr(testConfig, 'ruleTestSupport', True)
        else:
            setattr(testConfig, 'ruleTestSupport', False)
    else:
        print 'Cannot get System Info. Exit'
        sys.exit()
    tasks = []
    for task in testConfig.testTask:
        tasks.append(task.taskName)
        if not task.taskFiles:
            task.taskFiles = 'all'
        if not task.taskOption:
            task.taskOption = config_constant.test_option_check
        if not task.waitTime:
            task.waitTime = 120
    testConfig.name = ''.join(tasks) + '-' + testConfig.runTime + '-' + server

    return testConfig