MonitoringRunnable implements Runnable { List<MonitoringHandlerRun> extractRuleSections( File file, String fileContent, Properties params ) { StringBuilder sb = new StringBuilder(); List<String> sections = new ArrayList<String>(); for( String s : Arrays.asList( fileContent.trim().split( "\n" ))) { s = s.trim(); if( s.length() == 0 || s.startsWith( COMMENT_DELIMITER )) continue; if( s.toLowerCase().startsWith( RULE_BEGINNING )) { addSectionIfNotEmpty(sections, sb.toString()); sb.setLength( 0 ); } sb.append( Utils.expandTemplate(s, params) + "\n" ); } addSectionIfNotEmpty(sections, sb.toString()); List<MonitoringHandlerRun> result = new ArrayList<MonitoringHandlerRun> (); for( String s : sections ) { Matcher m = this.eventPattern.matcher( s ); if( ! m.find()) continue; s = s.substring( m.end()).trim(); MonitoringHandlerRun bean = new MonitoringHandlerRun(); bean.handlerName = m.group( 1 ); bean.eventId = m.group( 2 ); bean.rawRulesText = s; result.add( bean ); } return result; } MonitoringRunnable( AgentMessagingInterface agentInterface, List<IMonitoringHandler> handlers ); @Override void run();  }
@Test public void testExtractRuleSections_noSection() throws Exception { String fileContent = ""; MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( new File( "whatever" ), fileContent, null ); Assert.assertEquals( 0, handlers.size()); }
@Test public void testExtractRuleSections_file() throws Exception { File f = TestUtils.findTestFile( "/file-events.conf" ); String fileContent = Utils.readFileContent( f ); MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( f, fileContent, null ); Assert.assertEquals( 3, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "file", bean.handlerName ); Assert.assertEquals( "myRuleName-1", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "%TMP%/rbcf-test/ta-daaaaa" )); bean = handlers.get( 1 ); Assert.assertEquals( "file", bean.handlerName ); Assert.assertEquals( "myRuleName-2", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "%TMP%/rbcf-test/ta-daaaaa-2" )); bean = handlers.get( 2 ); Assert.assertEquals( "file", bean.handlerName ); Assert.assertEquals( "myRuleName", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "%TMP%/rbcf-test/a-directory-to-not-delete" )); }
@Test public void testExtractRuleSections_nagios() throws Exception { File f = TestUtils.findTestFile( "/nagios-events.conf" ); String fileContent = Utils.readFileContent( f ); MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( f, fileContent, null ); Assert.assertEquals( 3, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "nagios", bean.handlerName ); Assert.assertEquals( "myRuleName-80", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "host_name accept_passive_checks acknowledged" )); bean = handlers.get( 1 ); Assert.assertEquals( "nagios", bean.handlerName ); Assert.assertEquals( "myRuleName-2", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "http: bean = handlers.get( 2 ); Assert.assertEquals( "nagios", bean.handlerName ); Assert.assertEquals( "myRuleName-3", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "http: }
@Test public void testExtractRuleSections_rest() throws Exception { File f = TestUtils.findTestFile( "/rest-events.conf" ); String fileContent = Utils.readFileContent( f ); MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( f, fileContent, null ); Assert.assertEquals( 1, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "rest", bean.handlerName ); Assert.assertEquals( "myRuleName-1", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "http: }
@Test public void testExtractRuleSections_mixed() throws Exception { File f = TestUtils.findTestFile( "/mixed-events.conf" ); String fileContent = Utils.readFileContent( f ); MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( f, fileContent, null ); Assert.assertEquals( 2, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "nagios", bean.handlerName ); Assert.assertEquals( "myRuleName-nagios", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "Columns: host_name accept_passive_checks acknowledged" )); bean = handlers.get( 1 ); Assert.assertEquals( "file", bean.handlerName ); Assert.assertEquals( "myRuleName-for-file", bean.eventId ); Assert.assertTrue( bean.rawRulesText.contains( "/tmp/a-directory-to-not-delete" )); }
@Test public void testExtractRuleSections_mixedTemplating() throws Exception { File f = TestUtils.findTestFile( "/mixed-events-templating.conf" ); String fileContent = Utils.readFileContent( f ); MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); File propertiesFile = TestUtils.findTestFile( "/mixed-events-templating.properties" ); Assert.assertTrue( propertiesFile.exists()); Properties params = Utils.readPropertiesFile( propertiesFile ); List<MonitoringHandlerRun> handlers = task.extractRuleSections( f, fileContent, params ); Assert.assertEquals( 2, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "nagios", bean.handlerName ); Assert.assertTrue( bean.rawRulesText.endsWith( "= 1" )); bean = handlers.get( 1 ); Assert.assertEquals( "file", bean.handlerName ); Assert.assertTrue( bean.rawRulesText.contains( "/tmp/roboconf" )); }
@Test public void testExtractRuleSections_incompleteRule() throws Exception { String fileContent = MonitoringRunnable.RULE_BEGINNING; MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( new File( "test" ), fileContent, null ); Assert.assertEquals( 0, handlers.size()); }
@Test public void testExtractRuleSections_invalidRule() throws Exception { String fileContent = MonitoringRunnable.RULE_BEGINNING + " file event id with spaces]"; MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( new File( "test" ), fileContent, null ); Assert.assertEquals( 0, handlers.size()); }
@Test public void testExtractRuleSections_unknownParser() throws Exception { String fileContent = MonitoringRunnable.RULE_BEGINNING + " unknown event-id]\nok"; MonitoringRunnable task = new MonitoringRunnable( this.agentInterface, HANDLERS ); this.agentInterface.setScopedInstance( new Instance( "root" )); List<MonitoringHandlerRun> handlers = task.extractRuleSections( new File( "test" ), fileContent, null ); Assert.assertEquals( 1, handlers.size()); MonitoringHandlerRun bean = handlers.get( 0 ); Assert.assertEquals( "unknown", bean.handlerName ); Assert.assertEquals( "event-id", bean.eventId ); Assert.assertEquals( "ok", bean.rawRulesText ); }