/usr/lib/python2.7/dist-packages/mysql/fabric/error_log.py is in mysql-utilities 1.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | #
# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""Methods and classes to report servers' errors.
"""
import logging
from mysql.fabric import (
persistence as _persistence,
config as _config,
)
from mysql.fabric.utils import (
get_time,
)
_LOGGER = logging.getLogger(__name__)
class ErrorLog(_persistence.Persistable):
"""Errors reported by an entity that suspects that a server has failed
are processed and stored in a table (i.e. error_log) for future
analysis. Each entry has the following fields:
* erver_uuid - Server's UUID.
* reported - When the error has been reported.
* reporter - Who has reported the error.
* error - Error description.
See also:
* :class:`~mysql.fabric.server.Group`.
* :class:`~mysql.fabric.services.threat.ReportError`.
* :class:`~mysql.fabric.services.threat.ReportFailure`.
"""
_MIN_PRUNE_TIME = 60
_PRUNE_TIME = _DEFAULT_PRUNE_TIME = 3600
CREATE_SERVER_ERROR_LOG = (
"CREATE TABLE error_log "
"(server_uuid VARCHAR(40) NOT NULL, "
"reported TIMESTAMP /*!50604 (6) */ NOT NULL, "
"reporter VARCHAR(64) NOT NULL, "
"error TEXT, "
"INDEX key_server_uuid_reported (server_uuid, reported), "
"INDEX key_reporter (reporter)) DEFAULT CHARSET=utf8"
)
#SQL Statement for creating event used to prune error logs that are
#older than PRUNE_TIME.
CREATE_EVENT_ERROR_LOG = (
"CREATE EVENT prune_error_log "
"ON SCHEDULE EVERY %s SECOND "
"DO DELETE FROM error_log WHERE "
"TIMEDIFF(UTC_TIMESTAMP(), reported) > MAKETIME(%s,0,0)"
)
ADD_FOREIGN_KEY_CONSTRAINT_SERVER_UUID = (
"ALTER TABLE error_log ADD CONSTRAINT "
"fk_server_uuid_error_log FOREIGN KEY(server_uuid) "
"REFERENCES servers(server_uuid)"
)
QUERY_SERVER_ERROR_LOG = (
"SELECT reported, reporter FROM error_log WHERE "
"server_uuid = %s AND reported >= %s ORDER BY reported ASC"
)
REMOVE_SERVER_ERROR_LOG = (
"DELETE FROM error_log WHERE server_uuid = %s"
)
INSERT_SERVER_ERROR_LOG = (
"INSERT INTO error_log (server_uuid, reported, reporter, "
"error) VALUES(%s, %s, %s, %s)"
)
@staticmethod
def create(persister=None):
"""Create the objects(tables) that will store server's errors.
:param persister: Persister to persist the object to.
:raises: DatabaseError If the table already exists.
"""
persister.exec_stmt(ErrorLog.CREATE_SERVER_ERROR_LOG)
persister.exec_stmt(
ErrorLog.CREATE_EVENT_ERROR_LOG % (ErrorLog._PRUNE_TIME,
ErrorLog._PRUNE_TIME, )
)
@staticmethod
def add_constraints(persister=None):
"""Add the constraints to the error_log table.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(
ErrorLog.ADD_FOREIGN_KEY_CONSTRAINT_SERVER_UUID)
@staticmethod
def add(server, reported, reporter, error, persister=None):
"""Add a server's error log.
:param server: Server where the error has happened.
:param reported: When the error has happened.
:param reporter: Entity that has reported the error.
:param error: Error reported.
"""
from mysql.fabric.server import MySQLServer
assert(isinstance(server, MySQLServer))
persister.exec_stmt(ErrorLog.INSERT_SERVER_ERROR_LOG,
{"params": (str(server.uuid), reported, reporter, error)})
@staticmethod
def remove(server, persister=None):
"""Remove server's error logs from the state store.
:param server: A reference to a server.
:param persister: Persister to persist the object to.
"""
from mysql.fabric.server import MySQLServer
assert(isinstance(server, MySQLServer))
persister.exec_stmt(
ErrorLog.REMOVE_SERVER_ERROR_LOG,
{"params": (str(server.uuid), )}
)
@staticmethod
def fetch(server, interval, now=None, persister=None):
"""Return a ErrorLog object corresponding to the
server.
:param server: Server whose error has been reported.
:param interval: Interval of interest.
:param now: Consider from `now` until `now` - `interval`.
:param persister: Persister to persist the object to.
:return: ErrorLog object.
"""
from mysql.fabric.server import MySQLServer
assert(isinstance(server, MySQLServer))
now = now or get_time()
(whens, reporters) = ErrorLog.compute(
server.uuid, interval, now
)
return ErrorLog(server, interval, now, whens, reporters)
@staticmethod
def compute(uuid, interval, now, persister=None):
"""Compute information to build a ErrorLog object.
:param server: Server's UUID whose error has been reported.
:param interval: Interval of interest.
:param now: Consider from `now` until `now` - `interval`.
:param persister: Persister to persist the object to.
:return: ErrorLog object.
"""
cur = persister.exec_stmt(
ErrorLog.QUERY_SERVER_ERROR_LOG,
{"fetch" : False,
"params": (str(uuid), now - interval)
}
)
whens = []
reporters = []
for row in cur.fetchall():
when, reporter = row
whens.append(when)
reporters.append(reporter)
return (whens, reporters)
def __init__(self, server, interval, now, whens, reporters):
"""Constructor for ErrorLog object.
"""
from mysql.fabric.server import MySQLServer
assert(isinstance(server, MySQLServer))
assert(len(whens) == len(reporters))
super(ErrorLog, self).__init__()
self.__server_uuid = server.uuid
self.__now = now
self.__interval = interval
self.__whens = whens or []
self.__reporters = reporters or []
@property
def server_uuid(self):
"""Return the associated server's UUID.
"""
return self.__server_uuid
@property
def whens(self):
"""Return when errors were reported.
interval.
"""
return self.__whens
@property
def reporters(self):
"""Return who reported errors.
"""
return self.__reporters
@property
def now(self):
"""Base time to analyze errors.
"""
return self.__now
@property
def interval(self):
"""Interval upon which errors are analyzed.
"""
return self.__interval
def refresh(self):
"""Refresh set of errors.
"""
(whens, reporters) = ErrorLog.compute(
self.__server_uuid, self.__interval, self.__now
)
self.__whens = whens
self.__reporters = reporters
def is_unstable(self, n_notifications, n_reporters, filter_reporter=None):
"""Check whether a server is unstable or not.
:param n_notifications: Number of notifications threshold.
:param n_reporters: Number of reporters threshold.
:param filter_reporter: Filter errors by reporters before checking
whether a server is unstable or not.
:return: Whether the server can be considered faulty
or not. The outcome depends on the parameters.
"""
assert(
filter_reporter is None or isinstance(filter_reporter, tuple) or \
isinstance(filter_reporter, list)
)
reporters = self.__reporters
if filter_reporter is not None:
reporters = [
reporter for reporter in self.__reporters \
if reporter in filter_reporter
]
nreporters = len(set(reporters))
notifications = len(reporters)
if nreporters >= n_reporters and notifications >= n_notifications:
return True
return False
def configure(config):
"""Set configuration values.
"""
try:
prune_time = int(config.get("failure_tracking", "prune_time"))
if prune_time < ErrorLog._MIN_PRUNE_TIME:
_LOGGER.warning(
"Prune entries cannot be lower than %s.",
ErrorLog._MIN_PRUNE_TIME
)
prune_time = ErrorLog._MIN_PRUNE_TIME
ErrorLog._PRUNE_TIME = int(prune_time)
except (_config.NoOptionError, _config.NoSectionError, ValueError):
pass
|