View Javadoc

1   /*
2    * #%L
3    * Bitrepository Monitoring Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.monitoringservice.alarm;
23  
24  import java.math.BigInteger;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  import org.bitrepository.bitrepositoryelements.Alarm;
29  import org.bitrepository.bitrepositoryelements.AlarmCode;
30  import org.bitrepository.common.settings.Settings;
31  import org.bitrepository.monitoringservice.status.ComponentStatus;
32  import org.bitrepository.monitoringservice.status.StatusStore;
33  import org.bitrepository.protocol.messagebus.MessageSender;
34  import org.bitrepository.service.AlarmDispatcher;
35  import org.bitrepository.settings.referencesettings.AlarmLevel;
36  
37  /**
38   * Class for the monitoring service keep a watch on non responding components, and send alarms if needed.
39   */
40  public class BasicMonitoringServiceAlerter extends AlarmDispatcher implements MonitorAlerter {
41      /** The store for the status results from the components.*/
42      private final StatusStore statusStore;
43      /** The maximum number of missing replies before an alarm is dispatched.*/
44      private final BigInteger maxRetries;
45      
46      /**
47       * @param statusStore The store for the status results from the components.
48       * @param sender Used for sending the alarms.
49       * @param alarmLevel Only send alarms at this alarms level or higher.
50       * @param statusStore Used to maintain the status of the components.
51       */
52      public BasicMonitoringServiceAlerter(
53          Settings settings, MessageSender sender, AlarmLevel alarmLevel, StatusStore statusStore) {
54          super(settings, sender, alarmLevel);
55          this.statusStore = statusStore;
56          maxRetries = settings.getReferenceSettings().getMonitoringServiceSettings().getMaxRetries();
57      }
58      
59      @Override
60      public void checkStatuses() {
61          Map<String, ComponentStatus> statusMap = statusStore.getStatusMap();
62          List<String> nonRespondingComponents = new ArrayList<String>();
63          for(String ID : statusMap.keySet()) {
64              ComponentStatus componentStatus = statusMap.get(ID);
65              if(componentStatus.getNumberOfMissingReplies() >= maxRetries.intValue()) {
66                  componentStatus.markAsUnresponsive();
67                  if(!componentStatus.hasAlarmed()) {
68                  	nonRespondingComponents.add(ID);
69                  	componentStatus.alarmed();
70                  }
71              }
72          }
73          
74          if(!nonRespondingComponents.isEmpty()) {
75              Alarm alarm = new Alarm();
76              alarm.setAlarmCode(AlarmCode.COMPONENT_FAILURE);
77              alarm.setAlarmText("The following components has become unresponsive: " 
78                      + nonRespondingComponents.toString());
79              error(alarm);
80          }
81      }
82  }