1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package ch.syabru.nagios.broker.jmx;
17
18 import java.util.regex.Pattern;
19 import java.util.regex.PatternSyntaxException;
20
21 import org.apache.commons.lang.Validate;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import ch.syabru.nagios.broker.ExternalCommand;
26 import ch.syabru.nagios.broker.ExternalCommandFactory;
27 import ch.syabru.nagios.broker.Matcher;
28 import ch.syabru.nagios.broker.MatcherException;
29 import ch.syabru.nagios.broker.Message;
30
31
32
33
34
35
36
37 public class JmxMatcher implements Matcher {
38 private static final Logger logger =
39 LoggerFactory.getLogger(JmxMatcher.class);
40
41 private static final Pattern PATTERN_ANY = Pattern.compile(".*", Pattern.DOTALL);
42
43 private Pattern classNamePattern = PATTERN_ANY;
44 private Pattern messagePattern = PATTERN_ANY;
45 private Pattern typePattern = PATTERN_ANY;
46 private Pattern sourcePattern = PATTERN_ANY;
47 private ExternalCommandFactory commandFactory;
48
49
50
51
52
53
54
55
56
57
58 public JmxMatcher(String classNameRegex, String messageRegex,
59 String typeRegex, String sourceRegex,
60 ExternalCommandFactory commandFactory)
61 throws MatcherException
62 {
63 Validate.notNull(commandFactory, "commandFactory must not be null");
64 try {
65 if (classNameRegex != null)
66 this.classNamePattern = Pattern.compile(classNameRegex);
67 if (messageRegex != null)
68 this.messagePattern = Pattern.compile(messageRegex);
69 if (typeRegex != null)
70 this.typePattern = Pattern.compile(typeRegex);
71 if (sourceRegex != null)
72 this.sourcePattern = Pattern.compile(sourceRegex);
73 } catch (PatternSyntaxException e) {
74 throw new MatcherException("Error compiling matcher regex", e);
75 }
76 this.commandFactory = commandFactory;
77 }
78
79 @Override
80 public ExternalCommand matches(Message message) {
81 ExternalCommand matchingCommand = null;
82 if (message instanceof JmxMessage) {
83 JmxMessage jmxMsg = (JmxMessage) message;
84 java.util.regex.Matcher classNameMatcher =
85 classNamePattern.matcher(jmxMsg.getClassName());
86 String msgText = jmxMsg.getMessage() != null ?
87 jmxMsg.getMessage() : "";
88 java.util.regex.Matcher messageMatcher =
89 messagePattern.matcher(msgText);
90 java.util.regex.Matcher typeMatcher =
91 typePattern.matcher(jmxMsg.getType());
92 java.util.regex.Matcher sourceMatcher =
93 sourcePattern.matcher(jmxMsg.getSource());
94 if (classNameMatcher.matches() &&
95 messageMatcher.matches() &&
96 typeMatcher.matches() &&
97 sourceMatcher.matches())
98 {
99 matchingCommand = commandFactory.createCommand();
100 logger.debug("Message matched [classNamePattern={}, " +
101 "messagePattern={}, typePattern={}, " +
102 "sourcePattern={}, " + "message={}]",
103 new Object[] { classNamePattern, messagePattern,
104 typePattern, sourcePattern, message });
105 } else {
106 logger.debug("Message did not match [classNamePattern={}, " +
107 "messagePattern={}, typePattern={}, " +
108 "sourcePattern={}, message={}]", new Object[] {
109 classNamePattern, messagePattern, typePattern,
110 sourcePattern, message });
111 }
112 }
113 return matchingCommand;
114 }
115 }