sensu-plugins-mongodb-mrtrotl 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1 -0
  3. data/LICENSE +22 -0
  4. data/README.md +27 -0
  5. data/bin/check-mongodb-metric.rb +144 -0
  6. data/bin/check-mongodb-query-count.rb +267 -0
  7. data/bin/check-mongodb.py +1644 -0
  8. data/bin/check-mongodb.rb +5 -0
  9. data/bin/metrics-mongodb-replication.rb +254 -0
  10. data/bin/metrics-mongodb.rb +133 -0
  11. data/lib/bson/__init__.py +1347 -0
  12. data/lib/bson/__pycache__/__init__.cpython-310.pyc +0 -0
  13. data/lib/bson/__pycache__/_helpers.cpython-310.pyc +0 -0
  14. data/lib/bson/__pycache__/binary.cpython-310.pyc +0 -0
  15. data/lib/bson/__pycache__/code.cpython-310.pyc +0 -0
  16. data/lib/bson/__pycache__/codec_options.cpython-310.pyc +0 -0
  17. data/lib/bson/__pycache__/dbref.cpython-310.pyc +0 -0
  18. data/lib/bson/__pycache__/decimal128.cpython-310.pyc +0 -0
  19. data/lib/bson/__pycache__/errors.cpython-310.pyc +0 -0
  20. data/lib/bson/__pycache__/int64.cpython-310.pyc +0 -0
  21. data/lib/bson/__pycache__/json_util.cpython-310.pyc +0 -0
  22. data/lib/bson/__pycache__/max_key.cpython-310.pyc +0 -0
  23. data/lib/bson/__pycache__/min_key.cpython-310.pyc +0 -0
  24. data/lib/bson/__pycache__/objectid.cpython-310.pyc +0 -0
  25. data/lib/bson/__pycache__/raw_bson.cpython-310.pyc +0 -0
  26. data/lib/bson/__pycache__/regex.cpython-310.pyc +0 -0
  27. data/lib/bson/__pycache__/son.cpython-310.pyc +0 -0
  28. data/lib/bson/__pycache__/timestamp.cpython-310.pyc +0 -0
  29. data/lib/bson/__pycache__/tz_util.cpython-310.pyc +0 -0
  30. data/lib/bson/_cbson.cpython-310-x86_64-linux-gnu.so +0 -0
  31. data/lib/bson/_helpers.py +41 -0
  32. data/lib/bson/binary.py +364 -0
  33. data/lib/bson/code.py +101 -0
  34. data/lib/bson/codec_options.py +414 -0
  35. data/lib/bson/codec_options.pyi +100 -0
  36. data/lib/bson/dbref.py +133 -0
  37. data/lib/bson/decimal128.py +314 -0
  38. data/lib/bson/errors.py +35 -0
  39. data/lib/bson/int64.py +39 -0
  40. data/lib/bson/json_util.py +874 -0
  41. data/lib/bson/max_key.py +55 -0
  42. data/lib/bson/min_key.py +55 -0
  43. data/lib/bson/objectid.py +286 -0
  44. data/lib/bson/py.typed +2 -0
  45. data/lib/bson/raw_bson.py +175 -0
  46. data/lib/bson/regex.py +135 -0
  47. data/lib/bson/son.py +208 -0
  48. data/lib/bson/timestamp.py +124 -0
  49. data/lib/bson/tz_util.py +52 -0
  50. data/lib/gridfs/__init__.py +1015 -0
  51. data/lib/gridfs/__pycache__/__init__.cpython-310.pyc +0 -0
  52. data/lib/gridfs/__pycache__/errors.cpython-310.pyc +0 -0
  53. data/lib/gridfs/__pycache__/grid_file.cpython-310.pyc +0 -0
  54. data/lib/gridfs/errors.py +33 -0
  55. data/lib/gridfs/grid_file.py +907 -0
  56. data/lib/gridfs/py.typed +2 -0
  57. data/lib/pymongo/__init__.py +185 -0
  58. data/lib/pymongo/__pycache__/__init__.cpython-310.pyc +0 -0
  59. data/lib/pymongo/__pycache__/_csot.cpython-310.pyc +0 -0
  60. data/lib/pymongo/__pycache__/aggregation.cpython-310.pyc +0 -0
  61. data/lib/pymongo/__pycache__/auth.cpython-310.pyc +0 -0
  62. data/lib/pymongo/__pycache__/auth_aws.cpython-310.pyc +0 -0
  63. data/lib/pymongo/__pycache__/bulk.cpython-310.pyc +0 -0
  64. data/lib/pymongo/__pycache__/change_stream.cpython-310.pyc +0 -0
  65. data/lib/pymongo/__pycache__/client_options.cpython-310.pyc +0 -0
  66. data/lib/pymongo/__pycache__/client_session.cpython-310.pyc +0 -0
  67. data/lib/pymongo/__pycache__/collation.cpython-310.pyc +0 -0
  68. data/lib/pymongo/__pycache__/collection.cpython-310.pyc +0 -0
  69. data/lib/pymongo/__pycache__/command_cursor.cpython-310.pyc +0 -0
  70. data/lib/pymongo/__pycache__/common.cpython-310.pyc +0 -0
  71. data/lib/pymongo/__pycache__/compression_support.cpython-310.pyc +0 -0
  72. data/lib/pymongo/__pycache__/cursor.cpython-310.pyc +0 -0
  73. data/lib/pymongo/__pycache__/daemon.cpython-310.pyc +0 -0
  74. data/lib/pymongo/__pycache__/database.cpython-310.pyc +0 -0
  75. data/lib/pymongo/__pycache__/driver_info.cpython-310.pyc +0 -0
  76. data/lib/pymongo/__pycache__/encryption.cpython-310.pyc +0 -0
  77. data/lib/pymongo/__pycache__/encryption_options.cpython-310.pyc +0 -0
  78. data/lib/pymongo/__pycache__/errors.cpython-310.pyc +0 -0
  79. data/lib/pymongo/__pycache__/event_loggers.cpython-310.pyc +0 -0
  80. data/lib/pymongo/__pycache__/hello.cpython-310.pyc +0 -0
  81. data/lib/pymongo/__pycache__/helpers.cpython-310.pyc +0 -0
  82. data/lib/pymongo/__pycache__/max_staleness_selectors.cpython-310.pyc +0 -0
  83. data/lib/pymongo/__pycache__/message.cpython-310.pyc +0 -0
  84. data/lib/pymongo/__pycache__/mongo_client.cpython-310.pyc +0 -0
  85. data/lib/pymongo/__pycache__/monitor.cpython-310.pyc +0 -0
  86. data/lib/pymongo/__pycache__/monitoring.cpython-310.pyc +0 -0
  87. data/lib/pymongo/__pycache__/network.cpython-310.pyc +0 -0
  88. data/lib/pymongo/__pycache__/ocsp_cache.cpython-310.pyc +0 -0
  89. data/lib/pymongo/__pycache__/ocsp_support.cpython-310.pyc +0 -0
  90. data/lib/pymongo/__pycache__/operations.cpython-310.pyc +0 -0
  91. data/lib/pymongo/__pycache__/periodic_executor.cpython-310.pyc +0 -0
  92. data/lib/pymongo/__pycache__/pool.cpython-310.pyc +0 -0
  93. data/lib/pymongo/__pycache__/pyopenssl_context.cpython-310.pyc +0 -0
  94. data/lib/pymongo/__pycache__/read_concern.cpython-310.pyc +0 -0
  95. data/lib/pymongo/__pycache__/read_preferences.cpython-310.pyc +0 -0
  96. data/lib/pymongo/__pycache__/response.cpython-310.pyc +0 -0
  97. data/lib/pymongo/__pycache__/results.cpython-310.pyc +0 -0
  98. data/lib/pymongo/__pycache__/saslprep.cpython-310.pyc +0 -0
  99. data/lib/pymongo/__pycache__/server.cpython-310.pyc +0 -0
  100. data/lib/pymongo/__pycache__/server_api.cpython-310.pyc +0 -0
  101. data/lib/pymongo/__pycache__/server_description.cpython-310.pyc +0 -0
  102. data/lib/pymongo/__pycache__/server_selectors.cpython-310.pyc +0 -0
  103. data/lib/pymongo/__pycache__/server_type.cpython-310.pyc +0 -0
  104. data/lib/pymongo/__pycache__/settings.cpython-310.pyc +0 -0
  105. data/lib/pymongo/__pycache__/socket_checker.cpython-310.pyc +0 -0
  106. data/lib/pymongo/__pycache__/srv_resolver.cpython-310.pyc +0 -0
  107. data/lib/pymongo/__pycache__/ssl_context.cpython-310.pyc +0 -0
  108. data/lib/pymongo/__pycache__/ssl_support.cpython-310.pyc +0 -0
  109. data/lib/pymongo/__pycache__/topology.cpython-310.pyc +0 -0
  110. data/lib/pymongo/__pycache__/topology_description.cpython-310.pyc +0 -0
  111. data/lib/pymongo/__pycache__/typings.cpython-310.pyc +0 -0
  112. data/lib/pymongo/__pycache__/uri_parser.cpython-310.pyc +0 -0
  113. data/lib/pymongo/__pycache__/write_concern.cpython-310.pyc +0 -0
  114. data/lib/pymongo/_cmessage.cpython-310-x86_64-linux-gnu.so +0 -0
  115. data/lib/pymongo/_csot.py +118 -0
  116. data/lib/pymongo/aggregation.py +229 -0
  117. data/lib/pymongo/auth.py +549 -0
  118. data/lib/pymongo/auth_aws.py +94 -0
  119. data/lib/pymongo/bulk.py +513 -0
  120. data/lib/pymongo/change_stream.py +457 -0
  121. data/lib/pymongo/client_options.py +302 -0
  122. data/lib/pymongo/client_session.py +1112 -0
  123. data/lib/pymongo/collation.py +224 -0
  124. data/lib/pymongo/collection.py +3204 -0
  125. data/lib/pymongo/command_cursor.py +353 -0
  126. data/lib/pymongo/common.py +984 -0
  127. data/lib/pymongo/compression_support.py +149 -0
  128. data/lib/pymongo/cursor.py +1345 -0
  129. data/lib/pymongo/daemon.py +141 -0
  130. data/lib/pymongo/database.py +1202 -0
  131. data/lib/pymongo/driver_info.py +42 -0
  132. data/lib/pymongo/encryption.py +884 -0
  133. data/lib/pymongo/encryption_options.py +221 -0
  134. data/lib/pymongo/errors.py +365 -0
  135. data/lib/pymongo/event_loggers.py +221 -0
  136. data/lib/pymongo/hello.py +219 -0
  137. data/lib/pymongo/helpers.py +259 -0
  138. data/lib/pymongo/max_staleness_selectors.py +114 -0
  139. data/lib/pymongo/message.py +1440 -0
  140. data/lib/pymongo/mongo_client.py +2144 -0
  141. data/lib/pymongo/monitor.py +440 -0
  142. data/lib/pymongo/monitoring.py +1801 -0
  143. data/lib/pymongo/network.py +311 -0
  144. data/lib/pymongo/ocsp_cache.py +87 -0
  145. data/lib/pymongo/ocsp_support.py +372 -0
  146. data/lib/pymongo/operations.py +507 -0
  147. data/lib/pymongo/periodic_executor.py +183 -0
  148. data/lib/pymongo/pool.py +1660 -0
  149. data/lib/pymongo/py.typed +2 -0
  150. data/lib/pymongo/pyopenssl_context.py +383 -0
  151. data/lib/pymongo/read_concern.py +75 -0
  152. data/lib/pymongo/read_preferences.py +609 -0
  153. data/lib/pymongo/response.py +109 -0
  154. data/lib/pymongo/results.py +217 -0
  155. data/lib/pymongo/saslprep.py +113 -0
  156. data/lib/pymongo/server.py +247 -0
  157. data/lib/pymongo/server_api.py +170 -0
  158. data/lib/pymongo/server_description.py +285 -0
  159. data/lib/pymongo/server_selectors.py +153 -0
  160. data/lib/pymongo/server_type.py +32 -0
  161. data/lib/pymongo/settings.py +159 -0
  162. data/lib/pymongo/socket_checker.py +104 -0
  163. data/lib/pymongo/srv_resolver.py +126 -0
  164. data/lib/pymongo/ssl_context.py +39 -0
  165. data/lib/pymongo/ssl_support.py +99 -0
  166. data/lib/pymongo/topology.py +890 -0
  167. data/lib/pymongo/topology_description.py +639 -0
  168. data/lib/pymongo/typings.py +39 -0
  169. data/lib/pymongo/uri_parser.py +624 -0
  170. data/lib/pymongo/write_concern.py +129 -0
  171. data/lib/pymongo-4.2.0.dist-info/INSTALLER +1 -0
  172. data/lib/pymongo-4.2.0.dist-info/LICENSE +201 -0
  173. data/lib/pymongo-4.2.0.dist-info/METADATA +250 -0
  174. data/lib/pymongo-4.2.0.dist-info/RECORD +167 -0
  175. data/lib/pymongo-4.2.0.dist-info/REQUESTED +0 -0
  176. data/lib/pymongo-4.2.0.dist-info/WHEEL +6 -0
  177. data/lib/pymongo-4.2.0.dist-info/top_level.txt +3 -0
  178. data/lib/sensu-plugins-mongodb/metrics.rb +391 -0
  179. data/lib/sensu-plugins-mongodb/version.rb +9 -0
  180. data/lib/sensu-plugins-mongodb.rb +1 -0
  181. metadata +407 -0
@@ -0,0 +1,170 @@
1
+ # Copyright 2020-present MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
4
+ # may not use this file except in compliance with the License. You
5
+ # may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
+ # implied. See the License for the specific language governing
13
+ # permissions and limitations under the License.
14
+
15
+ """Support for MongoDB Stable API.
16
+
17
+ .. _versioned-api-ref:
18
+
19
+ MongoDB Stable API
20
+ =====================
21
+
22
+ Starting in MongoDB 5.0, applications can specify the server API version
23
+ to use when creating a :class:`~pymongo.mongo_client.MongoClient`. Doing so
24
+ ensures that the driver behaves in a manner compatible with that server API
25
+ version, regardless of the server's actual release version.
26
+
27
+ Declaring an API Version
28
+ ````````````````````````
29
+
30
+ .. attention:: Stable API requires MongoDB >=5.0.
31
+
32
+ To configure MongoDB Stable API, pass the ``server_api`` keyword option to
33
+ :class:`~pymongo.mongo_client.MongoClient`::
34
+
35
+ >>> from pymongo.mongo_client import MongoClient
36
+ >>> from pymongo.server_api import ServerApi
37
+ >>>
38
+ >>> # Declare API version "1" for MongoClient "client"
39
+ >>> server_api = ServerApi('1')
40
+ >>> client = MongoClient(server_api=server_api)
41
+
42
+ The declared API version is applied to all commands run through ``client``,
43
+ including those sent through the generic
44
+ :meth:`~pymongo.database.Database.command` helper.
45
+
46
+ .. note:: Declaring an API version on the
47
+ :class:`~pymongo.mongo_client.MongoClient` **and** specifying stable
48
+ API options in :meth:`~pymongo.database.Database.command` command document
49
+ is not supported and will lead to undefined behaviour.
50
+
51
+ To run any command without declaring a server API version or using a different
52
+ API version, create a separate :class:`~pymongo.mongo_client.MongoClient`
53
+ instance.
54
+
55
+ Strict Mode
56
+ ```````````
57
+
58
+ Configuring ``strict`` mode will cause the MongoDB server to reject all
59
+ commands that are not part of the declared :attr:`ServerApi.version`. This
60
+ includes command options and aggregation pipeline stages.
61
+
62
+ For example::
63
+
64
+ >>> server_api = ServerApi('1', strict=True)
65
+ >>> client = MongoClient(server_api=server_api)
66
+ >>> client.test.command('count', 'test')
67
+ Traceback (most recent call last):
68
+ ...
69
+ pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'
70
+
71
+ Detecting API Deprecations
72
+ ``````````````````````````
73
+
74
+ The ``deprecationErrors`` option can be used to enable command failures
75
+ when using functionality that is deprecated from the configured
76
+ :attr:`ServerApi.version`. For example::
77
+
78
+ >>> server_api = ServerApi('1', deprecation_errors=True)
79
+ >>> client = MongoClient(server_api=server_api)
80
+
81
+ Note that at the time of this writing, no deprecated APIs exist.
82
+
83
+ Classes
84
+ =======
85
+ """
86
+
87
+
88
+ class ServerApiVersion:
89
+ """An enum that defines values for :attr:`ServerApi.version`.
90
+
91
+ .. versionadded:: 3.12
92
+ """
93
+
94
+ V1 = "1"
95
+ """Server API version "1"."""
96
+
97
+
98
+ class ServerApi(object):
99
+ """MongoDB Stable API."""
100
+
101
+ def __init__(self, version, strict=None, deprecation_errors=None):
102
+ """Options to configure MongoDB Stable API.
103
+
104
+ :Parameters:
105
+ - `version`: The API version string. Must be one of the values in
106
+ :class:`ServerApiVersion`.
107
+ - `strict` (optional): Set to ``True`` to enable API strict mode.
108
+ Defaults to ``None`` which means "use the server's default".
109
+ - `deprecation_errors` (optional): Set to ``True`` to enable
110
+ deprecation errors. Defaults to ``None`` which means "use the
111
+ server's default".
112
+
113
+ .. versionadded:: 3.12
114
+ """
115
+ if version != ServerApiVersion.V1:
116
+ raise ValueError("Unknown ServerApi version: %s" % (version,))
117
+ if strict is not None and not isinstance(strict, bool):
118
+ raise TypeError(
119
+ "Wrong type for ServerApi strict, value must be an instance "
120
+ "of bool, not %s" % (type(strict),)
121
+ )
122
+ if deprecation_errors is not None and not isinstance(deprecation_errors, bool):
123
+ raise TypeError(
124
+ "Wrong type for ServerApi deprecation_errors, value must be "
125
+ "an instance of bool, not %s" % (type(deprecation_errors),)
126
+ )
127
+ self._version = version
128
+ self._strict = strict
129
+ self._deprecation_errors = deprecation_errors
130
+
131
+ @property
132
+ def version(self):
133
+ """The API version setting.
134
+
135
+ This value is sent to the server in the "apiVersion" field.
136
+ """
137
+ return self._version
138
+
139
+ @property
140
+ def strict(self):
141
+ """The API strict mode setting.
142
+
143
+ When set, this value is sent to the server in the "apiStrict" field.
144
+ """
145
+ return self._strict
146
+
147
+ @property
148
+ def deprecation_errors(self):
149
+ """The API deprecation errors setting.
150
+
151
+ When set, this value is sent to the server in the
152
+ "apiDeprecationErrors" field.
153
+ """
154
+ return self._deprecation_errors
155
+
156
+
157
+ def _add_to_command(cmd, server_api):
158
+ """Internal helper which adds API versioning options to a command.
159
+
160
+ :Parameters:
161
+ - `cmd`: The command.
162
+ - `server_api` (optional): A :class:`ServerApi` or ``None``.
163
+ """
164
+ if not server_api:
165
+ return
166
+ cmd["apiVersion"] = server_api.version
167
+ if server_api.strict is not None:
168
+ cmd["apiStrict"] = server_api.strict
169
+ if server_api.deprecation_errors is not None:
170
+ cmd["apiDeprecationErrors"] = server_api.deprecation_errors
@@ -0,0 +1,285 @@
1
+ # Copyright 2014-present MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Represent one server the driver is connected to."""
16
+
17
+ import time
18
+ from typing import Any, Dict, Mapping, Optional, Set, Tuple
19
+
20
+ from bson import EPOCH_NAIVE
21
+ from bson.objectid import ObjectId
22
+ from pymongo.hello import Hello
23
+ from pymongo.server_type import SERVER_TYPE
24
+ from pymongo.typings import _Address
25
+
26
+
27
+ class ServerDescription(object):
28
+ """Immutable representation of one server.
29
+
30
+ :Parameters:
31
+ - `address`: A (host, port) pair
32
+ - `hello`: Optional Hello instance
33
+ - `round_trip_time`: Optional float
34
+ - `error`: Optional, the last error attempting to connect to the server
35
+ """
36
+
37
+ __slots__ = (
38
+ "_address",
39
+ "_server_type",
40
+ "_all_hosts",
41
+ "_tags",
42
+ "_replica_set_name",
43
+ "_primary",
44
+ "_max_bson_size",
45
+ "_max_message_size",
46
+ "_max_write_batch_size",
47
+ "_min_wire_version",
48
+ "_max_wire_version",
49
+ "_round_trip_time",
50
+ "_me",
51
+ "_is_writable",
52
+ "_is_readable",
53
+ "_ls_timeout_minutes",
54
+ "_error",
55
+ "_set_version",
56
+ "_election_id",
57
+ "_cluster_time",
58
+ "_last_write_date",
59
+ "_last_update_time",
60
+ "_topology_version",
61
+ )
62
+
63
+ def __init__(
64
+ self,
65
+ address: _Address,
66
+ hello: Optional[Hello] = None,
67
+ round_trip_time: Optional[float] = None,
68
+ error: Optional[Exception] = None,
69
+ ) -> None:
70
+ self._address = address
71
+ if not hello:
72
+ hello = Hello({})
73
+
74
+ self._server_type = hello.server_type
75
+ self._all_hosts = hello.all_hosts
76
+ self._tags = hello.tags
77
+ self._replica_set_name = hello.replica_set_name
78
+ self._primary = hello.primary
79
+ self._max_bson_size = hello.max_bson_size
80
+ self._max_message_size = hello.max_message_size
81
+ self._max_write_batch_size = hello.max_write_batch_size
82
+ self._min_wire_version = hello.min_wire_version
83
+ self._max_wire_version = hello.max_wire_version
84
+ self._set_version = hello.set_version
85
+ self._election_id = hello.election_id
86
+ self._cluster_time = hello.cluster_time
87
+ self._is_writable = hello.is_writable
88
+ self._is_readable = hello.is_readable
89
+ self._ls_timeout_minutes = hello.logical_session_timeout_minutes
90
+ self._round_trip_time = round_trip_time
91
+ self._me = hello.me
92
+ self._last_update_time = time.monotonic()
93
+ self._error = error
94
+ self._topology_version = hello.topology_version
95
+ if error:
96
+ details = getattr(error, "details", None)
97
+ if isinstance(details, dict):
98
+ self._topology_version = details.get("topologyVersion")
99
+
100
+ self._last_write_date: Optional[float]
101
+ if hello.last_write_date:
102
+ # Convert from datetime to seconds.
103
+ delta = hello.last_write_date - EPOCH_NAIVE
104
+ self._last_write_date = delta.total_seconds()
105
+ else:
106
+ self._last_write_date = None
107
+
108
+ @property
109
+ def address(self) -> _Address:
110
+ """The address (host, port) of this server."""
111
+ return self._address
112
+
113
+ @property
114
+ def server_type(self) -> int:
115
+ """The type of this server."""
116
+ return self._server_type
117
+
118
+ @property
119
+ def server_type_name(self) -> str:
120
+ """The server type as a human readable string.
121
+
122
+ .. versionadded:: 3.4
123
+ """
124
+ return SERVER_TYPE._fields[self._server_type]
125
+
126
+ @property
127
+ def all_hosts(self) -> Set[Tuple[str, int]]:
128
+ """List of hosts, passives, and arbiters known to this server."""
129
+ return self._all_hosts
130
+
131
+ @property
132
+ def tags(self) -> Mapping[str, Any]:
133
+ return self._tags
134
+
135
+ @property
136
+ def replica_set_name(self) -> Optional[str]:
137
+ """Replica set name or None."""
138
+ return self._replica_set_name
139
+
140
+ @property
141
+ def primary(self) -> Optional[Tuple[str, int]]:
142
+ """This server's opinion about who the primary is, or None."""
143
+ return self._primary
144
+
145
+ @property
146
+ def max_bson_size(self) -> int:
147
+ return self._max_bson_size
148
+
149
+ @property
150
+ def max_message_size(self) -> int:
151
+ return self._max_message_size
152
+
153
+ @property
154
+ def max_write_batch_size(self) -> int:
155
+ return self._max_write_batch_size
156
+
157
+ @property
158
+ def min_wire_version(self) -> int:
159
+ return self._min_wire_version
160
+
161
+ @property
162
+ def max_wire_version(self) -> int:
163
+ return self._max_wire_version
164
+
165
+ @property
166
+ def set_version(self) -> Optional[int]:
167
+ return self._set_version
168
+
169
+ @property
170
+ def election_id(self) -> Optional[ObjectId]:
171
+ return self._election_id
172
+
173
+ @property
174
+ def cluster_time(self) -> Optional[Mapping[str, Any]]:
175
+ return self._cluster_time
176
+
177
+ @property
178
+ def election_tuple(self) -> Tuple[Optional[int], Optional[ObjectId]]:
179
+ return self._set_version, self._election_id
180
+
181
+ @property
182
+ def me(self) -> Optional[Tuple[str, int]]:
183
+ return self._me
184
+
185
+ @property
186
+ def logical_session_timeout_minutes(self) -> Optional[int]:
187
+ return self._ls_timeout_minutes
188
+
189
+ @property
190
+ def last_write_date(self) -> Optional[float]:
191
+ return self._last_write_date
192
+
193
+ @property
194
+ def last_update_time(self) -> float:
195
+ return self._last_update_time
196
+
197
+ @property
198
+ def round_trip_time(self) -> Optional[float]:
199
+ """The current average latency or None."""
200
+ # This override is for unittesting only!
201
+ if self._address in self._host_to_round_trip_time:
202
+ return self._host_to_round_trip_time[self._address]
203
+
204
+ return self._round_trip_time
205
+
206
+ @property
207
+ def error(self) -> Optional[Exception]:
208
+ """The last error attempting to connect to the server, or None."""
209
+ return self._error
210
+
211
+ @property
212
+ def is_writable(self) -> bool:
213
+ return self._is_writable
214
+
215
+ @property
216
+ def is_readable(self) -> bool:
217
+ return self._is_readable
218
+
219
+ @property
220
+ def mongos(self) -> bool:
221
+ return self._server_type == SERVER_TYPE.Mongos
222
+
223
+ @property
224
+ def is_server_type_known(self) -> bool:
225
+ return self.server_type != SERVER_TYPE.Unknown
226
+
227
+ @property
228
+ def retryable_writes_supported(self) -> bool:
229
+ """Checks if this server supports retryable writes."""
230
+ return (
231
+ self._ls_timeout_minutes is not None
232
+ and self._server_type in (SERVER_TYPE.Mongos, SERVER_TYPE.RSPrimary)
233
+ ) or self._server_type == SERVER_TYPE.LoadBalancer
234
+
235
+ @property
236
+ def retryable_reads_supported(self) -> bool:
237
+ """Checks if this server supports retryable writes."""
238
+ return self._max_wire_version >= 6
239
+
240
+ @property
241
+ def topology_version(self) -> Optional[Mapping[str, Any]]:
242
+ return self._topology_version
243
+
244
+ def to_unknown(self, error: Optional[Exception] = None) -> "ServerDescription":
245
+ unknown = ServerDescription(self.address, error=error)
246
+ unknown._topology_version = self.topology_version
247
+ return unknown
248
+
249
+ def __eq__(self, other: Any) -> bool:
250
+ if isinstance(other, ServerDescription):
251
+ return (
252
+ (self._address == other.address)
253
+ and (self._server_type == other.server_type)
254
+ and (self._min_wire_version == other.min_wire_version)
255
+ and (self._max_wire_version == other.max_wire_version)
256
+ and (self._me == other.me)
257
+ and (self._all_hosts == other.all_hosts)
258
+ and (self._tags == other.tags)
259
+ and (self._replica_set_name == other.replica_set_name)
260
+ and (self._set_version == other.set_version)
261
+ and (self._election_id == other.election_id)
262
+ and (self._primary == other.primary)
263
+ and (self._ls_timeout_minutes == other.logical_session_timeout_minutes)
264
+ and (self._error == other.error)
265
+ )
266
+
267
+ return NotImplemented
268
+
269
+ def __ne__(self, other: Any) -> bool:
270
+ return not self == other
271
+
272
+ def __repr__(self):
273
+ errmsg = ""
274
+ if self.error:
275
+ errmsg = ", error=%r" % (self.error,)
276
+ return "<%s %s server_type: %s, rtt: %s%s>" % (
277
+ self.__class__.__name__,
278
+ self.address,
279
+ self.server_type_name,
280
+ self.round_trip_time,
281
+ errmsg,
282
+ )
283
+
284
+ # For unittesting only. Use under no circumstances!
285
+ _host_to_round_trip_time: Dict = {}
@@ -0,0 +1,153 @@
1
+ # Copyright 2014-2016 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
4
+ # may not use this file except in compliance with the License. You
5
+ # may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
+ # implied. See the License for the specific language governing
13
+ # permissions and limitations under the License.
14
+
15
+ """Criteria to select some ServerDescriptions from a TopologyDescription."""
16
+
17
+ from pymongo.server_type import SERVER_TYPE
18
+
19
+
20
+ class Selection(object):
21
+ """Input or output of a server selector function."""
22
+
23
+ @classmethod
24
+ def from_topology_description(cls, topology_description):
25
+ known_servers = topology_description.known_servers
26
+ primary = None
27
+ for sd in known_servers:
28
+ if sd.server_type == SERVER_TYPE.RSPrimary:
29
+ primary = sd
30
+ break
31
+
32
+ return Selection(
33
+ topology_description,
34
+ topology_description.known_servers,
35
+ topology_description.common_wire_version,
36
+ primary,
37
+ )
38
+
39
+ def __init__(self, topology_description, server_descriptions, common_wire_version, primary):
40
+ self.topology_description = topology_description
41
+ self.server_descriptions = server_descriptions
42
+ self.primary = primary
43
+ self.common_wire_version = common_wire_version
44
+
45
+ def with_server_descriptions(self, server_descriptions):
46
+ return Selection(
47
+ self.topology_description, server_descriptions, self.common_wire_version, self.primary
48
+ )
49
+
50
+ def secondary_with_max_last_write_date(self):
51
+ secondaries = secondary_server_selector(self)
52
+ if secondaries.server_descriptions:
53
+ return max(secondaries.server_descriptions, key=lambda sd: sd.last_write_date)
54
+
55
+ @property
56
+ def primary_selection(self):
57
+ primaries = [self.primary] if self.primary else []
58
+ return self.with_server_descriptions(primaries)
59
+
60
+ @property
61
+ def heartbeat_frequency(self):
62
+ return self.topology_description.heartbeat_frequency
63
+
64
+ @property
65
+ def topology_type(self):
66
+ return self.topology_description.topology_type
67
+
68
+ def __bool__(self):
69
+ return bool(self.server_descriptions)
70
+
71
+ def __getitem__(self, item):
72
+ return self.server_descriptions[item]
73
+
74
+
75
+ def any_server_selector(selection):
76
+ return selection
77
+
78
+
79
+ def readable_server_selector(selection):
80
+ return selection.with_server_descriptions(
81
+ [s for s in selection.server_descriptions if s.is_readable]
82
+ )
83
+
84
+
85
+ def writable_server_selector(selection):
86
+ return selection.with_server_descriptions(
87
+ [s for s in selection.server_descriptions if s.is_writable]
88
+ )
89
+
90
+
91
+ def secondary_server_selector(selection):
92
+ return selection.with_server_descriptions(
93
+ [s for s in selection.server_descriptions if s.server_type == SERVER_TYPE.RSSecondary]
94
+ )
95
+
96
+
97
+ def arbiter_server_selector(selection):
98
+ return selection.with_server_descriptions(
99
+ [s for s in selection.server_descriptions if s.server_type == SERVER_TYPE.RSArbiter]
100
+ )
101
+
102
+
103
+ def writable_preferred_server_selector(selection):
104
+ """Like PrimaryPreferred but doesn't use tags or latency."""
105
+ return writable_server_selector(selection) or secondary_server_selector(selection)
106
+
107
+
108
+ def apply_single_tag_set(tag_set, selection):
109
+ """All servers matching one tag set.
110
+
111
+ A tag set is a dict. A server matches if its tags are a superset:
112
+ A server tagged {'a': '1', 'b': '2'} matches the tag set {'a': '1'}.
113
+
114
+ The empty tag set {} matches any server.
115
+ """
116
+
117
+ def tags_match(server_tags):
118
+ for key, value in tag_set.items():
119
+ if key not in server_tags or server_tags[key] != value:
120
+ return False
121
+
122
+ return True
123
+
124
+ return selection.with_server_descriptions(
125
+ [s for s in selection.server_descriptions if tags_match(s.tags)]
126
+ )
127
+
128
+
129
+ def apply_tag_sets(tag_sets, selection):
130
+ """All servers match a list of tag sets.
131
+
132
+ tag_sets is a list of dicts. The empty tag set {} matches any server,
133
+ and may be provided at the end of the list as a fallback. So
134
+ [{'a': 'value'}, {}] expresses a preference for servers tagged
135
+ {'a': 'value'}, but accepts any server if none matches the first
136
+ preference.
137
+ """
138
+ for tag_set in tag_sets:
139
+ with_tag_set = apply_single_tag_set(tag_set, selection)
140
+ if with_tag_set:
141
+ return with_tag_set
142
+
143
+ return selection.with_server_descriptions([])
144
+
145
+
146
+ def secondary_with_tags_server_selector(tag_sets, selection):
147
+ """All near-enough secondaries matching the tag sets."""
148
+ return apply_tag_sets(tag_sets, secondary_server_selector(selection))
149
+
150
+
151
+ def member_with_tags_server_selector(tag_sets, selection):
152
+ """All near-enough members matching the tag sets."""
153
+ return apply_tag_sets(tag_sets, readable_server_selector(selection))
@@ -0,0 +1,32 @@
1
+ # Copyright 2014-2015 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Type codes for MongoDB servers."""
16
+
17
+ from typing import NamedTuple
18
+
19
+
20
+ class _ServerType(NamedTuple):
21
+ Unknown: int
22
+ Mongos: int
23
+ RSPrimary: int
24
+ RSSecondary: int
25
+ RSArbiter: int
26
+ RSOther: int
27
+ RSGhost: int
28
+ Standalone: int
29
+ LoadBalancer: int
30
+
31
+
32
+ SERVER_TYPE = _ServerType(*range(9))