CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlarmHandler.cc
Go to the documentation of this file.
1 //$Id: AlarmHandler.cc,v 1.12 2011/11/08 10:48:40 mommsen Exp $
3 
4 
5 #include "sentinel/utils/version.h"
6 #include "sentinel/utils/Alarm.h"
7 
8 #include "xcept/tools.h"
9 #include "xdata/InfoSpaceFactory.h"
10 
16 
17 
18 namespace stor {
19 
21  (
22  xdaq::Application* app
23  ) :
24  app_(app)
25  {
26  try
27  {
28  alarmInfoSpace_ = xdata::getInfoSpaceFactory()->get("urn:xdaq-sentinel:alarms");
29  }
31  {
32  // sentinel is not available
33  alarmInfoSpace_ = 0;
34  }
35  }
36 
37 
39  (
40  xdaq::Application* app,
42  ) :
43  app_(app),
44  sharedResources_(sr)
45  {
46  try
47  {
48  alarmInfoSpace_ = xdata::getInfoSpaceFactory()->get("urn:xdaq-sentinel:alarms");
49  }
51  {
52  // sentinel is not available
53  alarmInfoSpace_ = 0;
54  }
55  }
56 
57 
59  (
60  const std::string name,
61  const ALARM_LEVEL level,
63  )
64  {
65 
66  switch( level )
67  {
68  case OKAY:
69  revokeAlarm(name);
70  break;
71 
72  case WARNING:
73  if ( raiseAlarm(name, "warning", exception) )
74  LOG4CPLUS_WARN(app_->getApplicationLogger(),
75  "Raising warning alarm " << name << ": " << exception.message());
76  break;
77 
78  case ERROR:
79  if ( raiseAlarm(name, "error", exception) )
80  LOG4CPLUS_ERROR(app_->getApplicationLogger(),
81  "Raising error alarm " << name << ": " << exception.message());
82  break;
83 
84  case FATAL:
85  if ( raiseAlarm(name, "fatal", exception) )
86  LOG4CPLUS_FATAL(app_->getApplicationLogger(),
87  "Raising fatal alarm " << name << ": " << exception.message());
88  break;
89 
90  default:
91  LOG4CPLUS_WARN(app_->getApplicationLogger(),
92  "Unknown alarm level received for " << name << ": " << exception.message());
93  }
94  }
95 
96 
98  (
99  const ALARM_LEVEL level,
100  xcept::Exception& exception
101  )
102  {
103 
104  switch( level )
105  {
106  case OKAY:
107  LOG4CPLUS_INFO(app_->getApplicationLogger(),
108  xcept::stdformat_exception_history(exception));
109  break;
110 
111  case WARNING:
112  LOG4CPLUS_WARN(app_->getApplicationLogger(),
113  xcept::stdformat_exception_history(exception));
114  app_->notifyQualified("warning", exception);
115  break;
116 
117  case ERROR:
118  LOG4CPLUS_ERROR(app_->getApplicationLogger(),
119  xcept::stdformat_exception_history(exception));
120  app_->notifyQualified("error", exception);
121  break;
122 
123  case FATAL:
124  LOG4CPLUS_FATAL(app_->getApplicationLogger(),
125  xcept::stdformat_exception_history(exception));
126  app_->notifyQualified("fatal", exception);
127  break;
128 
129  default:
130  LOG4CPLUS_WARN(app_->getApplicationLogger(),
131  "Unknown alarm level received for exception: " <<
132  xcept::stdformat_exception_history(exception));
133  }
134  }
135 
136 
138  (
139  const std::string name,
140  const std::string level,
141  xcept::Exception& exception
142  )
143  {
144 
145  if (!alarmInfoSpace_) return false;
146 
147  boost::mutex::scoped_lock sl( mutex_ );
148 
149  sentinel::utils::Alarm *alarm =
150  new sentinel::utils::Alarm(level, exception, app_);
151  try
152  {
153  alarmInfoSpace_->fireItemAvailable(name, alarm);
154  }
156  {
157  // Alarm is already set or sentinel not available
158  return false;
159  }
160  return true;
161  }
162 
163 
165  (
166  const std::string name
167  )
168  {
169  if (!alarmInfoSpace_) return;
170 
171  boost::mutex::scoped_lock sl( mutex_ );
172 
173  sentinel::utils::Alarm *alarm;
174  try
175  {
176  alarm = dynamic_cast<sentinel::utils::Alarm*>( alarmInfoSpace_->find( name ) );
177  }
179  {
180  // Alarm has not been set or sentinel not available
181  return;
182  }
183 
184  LOG4CPLUS_INFO(app_->getApplicationLogger(), "Revoking alarm " << name);
185 
186  alarmInfoSpace_->fireItemRevoked(name, app_);
187  delete alarm;
188  }
189 
190 
192  {
193  if (!alarmInfoSpace_) return;
194 
195  boost::mutex::scoped_lock sl( mutex_ );
196 
197  typedef std::map<std::string, xdata::Serializable*, std::less<std::string> > alarmList;
198  alarmList alarms = alarmInfoSpace_->match(".*");
199  for (alarmList::const_iterator it = alarms.begin(), itEnd = alarms.end();
200  it != itEnd; ++it)
201  {
202  sentinel::utils::Alarm* alarm = dynamic_cast<sentinel::utils::Alarm*>(it->second);
203  alarmInfoSpace_->fireItemRevoked(it->first, app_);
204  delete alarm;
205  }
206  }
207 
208 
210  {
211  std::string errorMsg = "Failed to process FAIL exception: "
212  + xcept::stdformat_exception_history(exception) + " due to ";
213 
214  try
215  {
217  sharedResources_->statisticsReporter_->getStateMachineMonitorCollection().setStatusMessage(
218  xcept::stdformat_exception_history(exception)
219  );
220  EventPtr_t stMachEvent( new Fail() );
221  // wait maximum 5 seconds until enqueuing succeeds
222  if ( ! sharedResources_->commandQueue_->enqTimedWait( stMachEvent, boost::posix_time::seconds(5) ) )
223  {
224  XCEPT_DECLARE_NESTED( stor::exception::StateTransition,
225  sentinelException, "Failed to enqueue FAIL event", exception );
226  notifySentinel(AlarmHandler::FATAL, sentinelException);
227  }
228  }
229  catch(xcept::Exception &e)
230  {
231  errorMsg += xcept::stdformat_exception_history(e);
232  localDebug( errorMsg );
233  }
234  catch(std::exception &e)
235  {
236  errorMsg += e.what();
237  localDebug( errorMsg );
238  }
239  catch( ... )
240  {
241  errorMsg += "an unknown exception.";
242  localDebug( errorMsg );
243  }
244  }
245 
246 
247  void AlarmHandler::localDebug( const std::string& message ) const
248  {
249  std::ostringstream fname_oss;
250  fname_oss << "/tmp/storage_manager_debug_" <<
251  sharedResources_->configuration_->getDiskWritingParams().smInstanceString_ <<
252  "_" << getpid();
253  const std::string fname = fname_oss.str();
254  std::ofstream f( fname.c_str(), std::ios_base::ate | std::ios_base::out | std::ios_base::app );
255  if( f.is_open() )
256  {
257  try
258  {
259  f << message << std::endl;
260  f.close();
261  }
262  catch(...)
263  {}
264  }
265  }
266 
267 } // namespace stor
268 
269 
virtual void notifySentinel(const ALARM_LEVEL, xcept::Exception &)
Definition: AlarmHandler.cc:98
double seconds()
xdaq::Application * app_
Definition: AlarmHandler.h:108
xdata::InfoSpace * alarmInfoSpace_
Definition: AlarmHandler.h:110
reject
Definition: HLTenums.h:23
virtual void raiseAlarm(const std::string name, const ALARM_LEVEL, xcept::Exception &)
Definition: AlarmHandler.cc:59
virtual void revokeAlarm(const std::string name)
static const int WARNING
boost::shared_ptr< SharedResources > SharedResourcesPtr
void localDebug(const std::string &message) const
boost::shared_ptr< boost::statechart::event_base > EventPtr_t
Definition: CommandQueue.h:21
double f[11][100]
boost::shared_ptr< SharedResources > sharedResources_
Definition: AlarmHandler.h:109
virtual void moveToFailedState(xcept::Exception &)
tuple out
Definition: dbtoconf.py:99
string fname
main script
boost::mutex mutex_
Definition: AlarmHandler.h:112
tuple level
Definition: testEve_cfg.py:34
static const int ERROR