View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.stats.listeners;
12  
13  import javax.inject.Inject;
14  import javax.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   * The listener interface for receiving memoryPoolMailing events. The class that is interested in
27   * processing a memoryPoolMailing event implements this interface, and the object created with that
28   * class is registered with a component using the component's {@code addMemoryPoolMailingListener}
29   * method. When the memoryPoolMailing event occurs, that object's appropriate method is invoked.
30   */
31  public class MemoryPoolMailingListener extends AbstractFlapListener
32      implements MessageSourceAware, InitializingBean {
33  
34    /** The Constant BASE_PROPERTY. */
35    private static final String BASE_PROPERTY = "probe.src.stats.listener.memory.pool.";
36  
37    /** The message source accessor. */
38    private MessageSourceAccessor messageSourceAccessor;
39  
40    /** The mailer. */
41    @Inject
42    private Mailer mailer;
43  
44    /**
45     * Gets the message source accessor.
46     *
47     * @return the message source accessor
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     * Gets the mailer.
60     *
61     * @return the mailer
62     */
63    public Mailer getMailer() {
64      return mailer;
65    }
66  
67    /**
68     * Sets the mailer.
69     *
70     * @param mailer the new mailer
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    * Send mail.
114    *
115    * @param sce the sce
116    * @param message the message
117    * @param flappingStop the flapping stop
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 }