示例#1
0
    def testPsExcludeRegex(self):
        """
        test the search regex and excule regex
        """
        deviceConfig = Object()
        deviceConfig.device = 'localhost'
        deviceConfig.lastmodeltime = "a second ago"
        cmd = Object()
        cmd.deviceConfig = deviceConfig
        cmd.command = 'command'

        cmd.includeRegex = ".*myapp.*"
        cmd.excludeRegex = ".*(vim|tail|grep|tar|cat|bash).*"
        cmd.replaceRegex = ".*"
        cmd.replacement = "myapp"
        cmd.primaryUrlPath = "url"
        cmd.displayName = "myapp set name"
        cmd.eventKey = "bar"
        cmd.severity = 1
        cmd.generatedId = "url_" + md5("myapp").hexdigest().strip()

        p1 = Object()
        p1.id = 'cpu'
        p1.data = dict(id='url_myapp', alertOnRestart=True, failSeverity=3)
        p2 = Object()
        p2.id = 'mem'
        p2.data = dict(id='url_myapp', alertOnRestart=True, failSeverity=3)
        p3 = Object()
        p3.id = 'count'
        p3.data = dict(id='url_myapp', alertOnRestart=True, failSeverity=3)
        cmd.points = [p1, p2, p3]
        cmd.result = Object()

        cmd.result.output = """  PID   RSS     TIME COMMAND
120 1 00:00:01 myapp
121 1 00:00:02 vim myapp
122 1 00:00:03 vim /path/to/myapp
123 1 00:00:04 tail -f myapp.log
124 1 00:00:05 tail -f /path/to/myapp.log
125 1 00:00:06 /path/to/myapp
126 1 00:00:07 grep foo myapp
127 1 00:00:08 grep foo /path/to/myapp
128 1 00:00:09 tar cvfz bar.tgz /path/to/myapp
129 1 00:00:10 tar cvfz bar.tgz /path/to/myapp
130 1 00:00:11 cat /path/to/myapp
131 1 00:00:12 bash -c /path/to/myapp
132 1 00:00:13 bash -c myapp
"""
        #create empty data structure for results
        results = ParsedResults()

        parser = ps()
        # populate results
        parser.processResults(cmd, results)
        #print "results:", results

        self.assertEqual(len(results.values), 3)

        for val in results.values:
            if val[0].id == 'cpu':
                self.assertEqual(val[1], 7.0)
            elif val[0].id == 'count':
                self.assertEqual(val[1], 2)
            elif val[0].id == 'mem':
                self.assertEqual(val[1], 2.0)
            else:
                raise AssertionError("unexpected value:", val[0].id)

        results = ParsedResults()
        cmd.result.output = """  PID   RSS     TIME COMMAND
120 1 00:00:01 myapp
125 1 00:00:06 /path/to/myapp
"""
        parser.processResults(cmd, results)
        #print "results:", results

        if len(results.events
               ) != 2:  # only 2 made it through the exclude regex
            raise AssertionError("unexpected number of events: %s",
                                 len(results.events))

        for ev in results.events:
            summary = ev['summary']
            if summary.find(
                    'Process up'
            ) < 0:  # and they were still running with the same PIDs
                raise AssertionError("unexpected event")
示例#2
0
    def testPsExcludeRegex(self):
        """
        test the search regex and excule regex
        """
        deviceConfig = Object()
        deviceConfig.device = 'localhost'
        deviceConfig.lastmodeltime = "a second ago"
        cmd = Object()
        cmd.deviceConfig = deviceConfig
        cmd.command = 'command'
        
        cmd.includeRegex = ".*myapp.*"
        cmd.excludeRegex = ".*(vim|tail|grep|tar|cat|bash).*"
        cmd.replaceRegex = ".*"
        cmd.replacement  = "myapp"
        cmd.primaryUrlPath = "url"
        cmd.displayName = "myapp set name"
        cmd.eventKey = "bar"
        cmd.severity = 1
        cmd.generatedId = "url_" + md5("myapp").hexdigest().strip()

        p1 = Object()
        p1.id = 'cpu'
        p1.data = dict(id='url_myapp',
                       alertOnRestart=True,
                       failSeverity=3)
        p2 = Object()
        p2.id = 'mem'
        p2.data = dict(id='url_myapp',
                       alertOnRestart=True,
                       failSeverity=3)
        p3 = Object()
        p3.id = 'count'
        p3.data = dict(id='url_myapp',
                       alertOnRestart=True,
                       failSeverity=3)
        cmd.points = [p1, p2, p3]
        cmd.result = Object()

        cmd.result.output = """  PID   RSS     TIME COMMAND
120 1 00:00:01 myapp
121 1 00:00:02 vim myapp
122 1 00:00:03 vim /path/to/myapp
123 1 00:00:04 tail -f myapp.log
124 1 00:00:05 tail -f /path/to/myapp.log
125 1 00:00:06 /path/to/myapp
126 1 00:00:07 grep foo myapp
127 1 00:00:08 grep foo /path/to/myapp
128 1 00:00:09 tar cvfz bar.tgz /path/to/myapp
129 1 00:00:10 tar cvfz bar.tgz /path/to/myapp
130 1 00:00:11 cat /path/to/myapp
131 1 00:00:12 bash -c /path/to/myapp
132 1 00:00:13 bash -c myapp
"""
        #create empty data structure for results
        results = ParsedResults()
        
        parser = ps()
        # populate results
        parser.processResults(cmd, results)
        #print "results:", results

        self.assertEqual(len(results.values), 3)
        
        for val in results.values:
            if val[0].id == 'cpu':
                self.assertEqual(val[1], 7.0)
            elif val[0].id == 'count':
                self.assertEqual(val[1], 2)
            elif val[0].id == 'mem':
                self.assertEqual(val[1], 2.0)
            else:
                raise AssertionError("unexpected value:", val[0].id)

        results = ParsedResults()
        cmd.result.output = """  PID   RSS     TIME COMMAND
120 1 00:00:01 myapp
125 1 00:00:06 /path/to/myapp
"""
        parser.processResults(cmd, results)
        #print "results:", results

        if len(results.events) != 2: # only 2 made it through the exclude regex
            raise AssertionError("unexpected number of events: %s", len(results.events))

        for ev in results.events:
            summary = ev['summary']
            if summary.find('Process up') < 0: # and they were still running with the same PIDs
                raise AssertionError("unexpected event")