1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans.stats.listeners;
12
13 import jakarta.inject.Inject;
14 import jakarta.mail.MessagingException;
15
16 import org.springframework.beans.factory.InitializingBean;
17 import org.springframework.beans.factory.annotation.Value;
18 import org.springframework.context.MessageSource;
19 import org.springframework.context.MessageSourceAware;
20 import org.springframework.context.support.MessageSourceAccessor;
21
22 import psiprobe.tools.MailMessage;
23 import psiprobe.tools.Mailer;
24
25
26
27
28
29
30
31 public class MemoryPoolMailingListener extends AbstractFlapListener
32 implements MessageSourceAware, InitializingBean {
33
34
35 private static final String BASE_PROPERTY = "probe.src.stats.listener.memory.pool.";
36
37
38 private MessageSourceAccessor messageSourceAccessor;
39
40
41 @Inject
42 private Mailer mailer;
43
44
45
46
47
48
49 public MessageSourceAccessor getMessageSourceAccessor() {
50 return messageSourceAccessor;
51 }
52
53 @Override
54 public void setMessageSource(MessageSource messageSource) {
55 this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
56 }
57
58
59
60
61
62
63 public Mailer getMailer() {
64 return mailer;
65 }
66
67
68
69
70
71
72 public void setMailer(Mailer mailer) {
73 this.mailer = mailer;
74 }
75
76 @Override
77 public void afterPropertiesSet() throws Exception {
78 if (getMailer().getSmtp() == null) {
79 logger.info("Mailer SMTP host is not set. Disabling listener.");
80 setEnabled(false);
81 } else if (getMailer().getDefaultTo() == null) {
82 logger.info("Mailer default recipient is not set. Disabling listener.");
83 setEnabled(false);
84 }
85 }
86
87 @Override
88 protected void flappingStarted(StatsCollectionEvent sce) {
89 sendMail(sce, "flappingStart", false);
90 }
91
92 @Override
93 protected void aboveThresholdFlappingStopped(StatsCollectionEvent sce) {
94 sendMail(sce, "aboveThreshold", true);
95 }
96
97 @Override
98 protected void belowThresholdFlappingStopped(StatsCollectionEvent sce) {
99 sendMail(sce, "belowThreshold", true);
100 }
101
102 @Override
103 protected void aboveThresholdNotFlapping(StatsCollectionEvent sce) {
104 sendMail(sce, "aboveThreshold", false);
105 }
106
107 @Override
108 protected void belowThresholdNotFlapping(StatsCollectionEvent sce) {
109 sendMail(sce, "belowThreshold", false);
110 }
111
112
113
114
115
116
117
118
119 protected void sendMail(StatsCollectionEvent sce, String message, boolean flappingStop) {
120 String name = sce.getName();
121 if (isSeriesDisabled(name)) {
122 return;
123 }
124 long value = sce.getValue();
125 long threshold = this.getThreshold(name);
126 String subjectInfix = "";
127 String bodyPrefix = "";
128 if (flappingStop) {
129 subjectInfix =
130 getMessageSourceAccessor().getMessage(BASE_PROPERTY + "flappingStop.subject.infix");
131 bodyPrefix =
132 getMessageSourceAccessor().getMessage(BASE_PROPERTY + "flappingStop.body.prefix");
133 }
134 String subject = getMessageSourceAccessor().getMessage(BASE_PROPERTY + message + ".subject",
135 new Object[] {subjectInfix, name, value, threshold});
136 String body = getMessageSourceAccessor().getMessage(BASE_PROPERTY + message + ".body",
137 new Object[] {bodyPrefix, name, value, threshold});
138 MailMessage mail = new MailMessage(null, subject, body);
139 try {
140 getMailer().send(mail);
141 } catch (MessagingException ex) {
142 logger.error("Cannot send message", ex);
143 }
144 }
145
146 @Value("${psiprobe.beans.stats.listeners.flapInterval}")
147 @Override
148 public void setDefaultFlapInterval(int defaultFlapInterval) {
149 super.setDefaultFlapInterval(defaultFlapInterval);
150 }
151
152 @Value("${psiprobe.beans.stats.listeners.flapStartThreshold}")
153 @Override
154 public void setDefaultFlapStartThreshold(float defaultFlapStartThreshold) {
155 super.setDefaultFlapStartThreshold(defaultFlapStartThreshold);
156 }
157
158 @Value("${psiprobe.beans.stats.listeners.flapStopThreshold}")
159 @Override
160 public void setDefaultFlapStopThreshold(float defaultFlapStopThreshold) {
161 super.setDefaultFlapStopThreshold(defaultFlapStopThreshold);
162 }
163
164 @Value("${psiprobe.beans.stats.listeners.flapLowWeight}")
165 @Override
166 public void setDefaultFlapLowWeight(float defaultFlapLowWeight) {
167 super.setDefaultFlapLowWeight(defaultFlapLowWeight);
168 }
169
170 @Value("${psiprobe.beans.stats.listeners.flapHighWeight}")
171 @Override
172 public void setDefaultFlapHighWeight(float defaultFlapHighWeight) {
173 super.setDefaultFlapHighWeight(defaultFlapHighWeight);
174 }
175
176 }