NCBI C++ ToolKit
ns_alert.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ns_alert.cpp 89004 2020-02-11 19:47:28Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Sergey Satskiy
27  *
28  * File Description: NeSchedule affinity registry
29  *
30  */
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbistd.hpp>
33 
34 #include "ns_alert.hpp"
35 
36 
38 
39 struct AlertToId
40 {
41  enum EAlertType type;
42  string id;
43 };
44 
45 
47  { eStartupConfig, "StartupConfig" },
48  { eReconfigure, "Reconfigure" },
49  { ePidFile, "PidFile" },
50  { eStartAfterCrash, "StartedAfterCrash" },
51  { eAccess, "AccessDenied" },
52  { eConfigOutOfSync, "ConfigOutOfSync" },
53  { eReinit, "Reinit" },
54  { eNoDump, "NoDump" },
55  { eDumpError, "DumpError" },
56  { eMaxQueues, "MaxQueues" },
57  { eDumpLoadError, "DumpLoadError" },
58  { eDumpSpaceError, "DumpSpaceError" },
59  { eDataDirRemoveError, "DataDirRemoveError" } };
60 const size_t alertToIdMapSize = sizeof(alertToIdMap) / sizeof(AlertToId);
61 
62 
63 
65 {
66  string acknowledged;
67 
69  acknowledged = "n/a";
70  else
72 
73  return "OK:last_detected_time: " +
75  "OK:acknowledged_time: " + acknowledged + "\n"
76  "OK:on: " + NStr::BoolToString(m_On) + "\n"
77  "OK:count: " + to_string(m_Count) + "\n"
78  "OK:count_since_acknowledge: " + to_string(m_CountSinceAck) + "\n"
79  "OK:user: " + m_User + "\n"
80  "OK:message: \"" + NStr::PrintableString(m_Message) + "\"";
81 }
82 
83 
84 void CNSAlerts::Register(enum EAlertType alert_type,
85  const string & message)
86 {
87  map< enum EAlertType,
88  SNSAlertAttributes >::iterator found;
89  CFastMutexGuard guard(m_Lock);
90 
91  found = m_Alerts.find(alert_type);
92  if (found != m_Alerts.end()) {
93  // Alert has already been there
94  found->second.m_LastDetectedTimestamp = CNSPreciseTime::Current();
95  found->second.m_On = true;
96  ++found->second.m_Count;
97  ++found->second.m_CountSinceAck;
98  found->second.m_Message = message;
99  return;
100  }
101 
102  // Brand new alert
103  SNSAlertAttributes attrs;
104  attrs.m_Message = message;
105  m_Alerts[alert_type] = attrs;
106 }
107 
108 
109 enum EAlertAckResult CNSAlerts::Acknowledge(const string & alert_id,
110  const string & user)
111 {
112  EAlertType type = x_IdToType(alert_id);
113  if (type == eUnknown)
114  return eNotFound;
115 
116  return Acknowledge(type, user);
117 }
118 
119 
121  const string & user)
122 {
123  map< enum EAlertType,
124  SNSAlertAttributes >::iterator found;
125  CFastMutexGuard guard(m_Lock);
126 
127  found = m_Alerts.find(alert_type);
128  if (found == m_Alerts.end())
129  return eNotFound;
130 
131  if (!found->second.m_On)
132  return eAlreadyAcknowledged;
133 
134  found->second.m_AcknowledgedTimestamp = CNSPreciseTime::Current();
135  found->second.m_On = false;
136  found->second.m_User = user;
137  found->second.m_CountSinceAck = 0;
138  return eAcknowledged;
139 }
140 
141 
142 enum EAlertType CNSAlerts::x_IdToType(const string & alert_id) const
143 {
144  for (size_t k = 0; k < alertToIdMapSize; ++k) {
145  if (NStr::CompareNocase(alert_id, alertToIdMap[k].id) == 0)
146  return alertToIdMap[k].type;
147  }
148  return eUnknown;
149 }
150 
151 
153 {
154  for (size_t k = 0; k < alertToIdMapSize; ++k) {
155  if (alertToIdMap[k].type == type)
156  return alertToIdMap[k].id;
157  }
158  return "unknown";
159 }
160 
161 
162 // Provides the alerts which are on at the moment.
163 // It is a url encoded format
164 string CNSAlerts::GetURLEncoded(void) const
165 {
166  string result;
167  map< enum EAlertType,
168  SNSAlertAttributes >::const_iterator k;
169  CFastMutexGuard guard(m_Lock);
170 
171  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
172  if (!result.empty())
173  result += "&";
174  result += "alert_" + x_TypeToId(k->first) + "=";
175  if (!k->second.m_On)
176  result += "-";
177  result += to_string(k->second.m_CountSinceAck) +
178  "/" + to_string(k->second.m_Count);
179  }
180  return result;
181 }
182 
183 
184 // Provides the alerts for the STAT ALERTS command
185 string CNSAlerts::Serialize(void) const
186 {
187  string result;
188  map< enum EAlertType,
189  SNSAlertAttributes >::const_iterator k;
190  CFastMutexGuard guard(m_Lock);
191 
192  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
193  if (!result.empty())
194  result += "\n";
195  result += "OK:[alert " + x_TypeToId(k->first) + "]\n" +
196  k->second.Serialize();
197  }
198  return result;
199 }
200 
201 
203 
string GetURLEncoded(void) const
Definition: ns_alert.cpp:164
CFastMutex m_Lock
Definition: ns_alert.hpp:104
string Serialize(void) const
Definition: ns_alert.cpp:185
enum EAlertAckResult Acknowledge(const string &alert_id, const string &user)
Definition: ns_alert.cpp:109
enum EAlertType x_IdToType(const string &alert_id) const
Definition: ns_alert.cpp:142
string x_TypeToId(enum EAlertType type) const
Definition: ns_alert.cpp:152
void Register(enum EAlertType alert_type, const string &message)
Definition: ns_alert.cpp:84
map< enum EAlertType, SNSAlertAttributes > m_Alerts
Definition: ns_alert.hpp:106
static CNSPreciseTime Current(void)
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string PrintableString(const CTempString str, TPrintableMode mode=fNewLine_Quote|fNonAscii_Passthru)
Get a printable version of the specified string.
Definition: ncbistr.cpp:3949
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
static const string BoolToString(bool value)
Convert bool to string.
Definition: ncbistr.cpp:2813
const AlertToId alertToIdMap[]
Definition: ns_alert.cpp:46
const size_t alertToIdMapSize
Definition: ns_alert.cpp:60
EAlertAckResult
Definition: ns_alert.hpp:63
@ eAcknowledged
Definition: ns_alert.hpp:66
@ eAlreadyAcknowledged
Definition: ns_alert.hpp:65
EAlertType
Definition: ns_alert.hpp:46
@ eConfigOutOfSync
Definition: ns_alert.hpp:53
@ eMaxQueues
Definition: ns_alert.hpp:57
@ eReinit
Definition: ns_alert.hpp:54
@ eStartupConfig
Definition: ns_alert.hpp:48
@ ePidFile
Definition: ns_alert.hpp:50
@ eStartAfterCrash
Definition: ns_alert.hpp:51
@ eDumpSpaceError
Definition: ns_alert.hpp:59
@ eDumpLoadError
Definition: ns_alert.hpp:58
@ eAccess
Definition: ns_alert.hpp:52
@ eReconfigure
Definition: ns_alert.hpp:49
@ eDumpError
Definition: ns_alert.hpp:56
@ eNoDump
Definition: ns_alert.hpp:55
@ eDataDirRemoveError
Definition: ns_alert.hpp:60
const CNSPreciseTime kTimeZero
string NS_FormatPreciseTime(const CNSPreciseTime &t)
@ eNotFound
Not found.
string id
Definition: nc_utils.cpp:219
CNCAlerts::EAlertType type
Definition: nc_utils.cpp:218
string Serialize(void) const
Definition: ns_alert.cpp:64
CNSPreciseTime m_AcknowledgedTimestamp
Definition: ns_alert.hpp:73
CNSPreciseTime m_LastDetectedTimestamp
Definition: ns_alert.hpp:72
size_t m_CountSinceAck
Definition: ns_alert.hpp:76
Definition: type.c:6
else result
Definition: token2.c:20
Modified on Wed Dec 06 07:14:05 2023 by modify_doxy.py rev. 669887