mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-04-13 09:42:01 +00:00
add querystring logic to mongo db health check to accommodate consul vs haproxy checking
This commit is contained in:
parent
0bbff92c73
commit
dd13af5bf1
5 changed files with 36 additions and 35 deletions
|
@ -8,7 +8,7 @@
|
|||
"port": 27017,
|
||||
"checks": [{
|
||||
"id": "mongo-ping",
|
||||
"http": "http://{{ ansible_ssh_host }}:5579/db_check/mongo",
|
||||
"http": "http://{{ ansible_ssh_host }}:5579/db_check/mongo?consul=1",
|
||||
"interval": "15s",
|
||||
"failures_before_critical": 4
|
||||
}]
|
||||
|
|
|
@ -124,7 +124,7 @@ backend postgres
|
|||
option httpchk GET /db_check/postgres
|
||||
server postgres-db01 db_pgsql:5000 check inter 2000ms
|
||||
backend mongo
|
||||
option httpchk GET /db_check/mongo
|
||||
option httpchk GET /db_check/mongo?haproxy=1
|
||||
server mongo-db22 db_mongo:5000 check inter 2000ms
|
||||
backend redis
|
||||
option httpchk GET /db_check/redis
|
||||
|
|
|
@ -146,7 +146,7 @@ backend postgres
|
|||
server postgres-db02 db_pgsql:5000 check inter 2000ms
|
||||
|
||||
backend mongo
|
||||
option httpchk GET /db_check/mongo
|
||||
option httpchk GET /db_check/mongo?haproxy=1
|
||||
server mongo-db20d db20d:5000 check inter 2000ms
|
||||
server mongo-db22 db22:5000 check inter 2000ms
|
||||
server mongo-db23a db23a:5000 check inter 2000ms
|
||||
|
|
|
@ -167,7 +167,7 @@ backend postgres
|
|||
server db-postgres db-postgres.node.nyc1.consul:5579 check inter 2000ms resolvers consul resolve-prefer ipv4 resolve-opts allow-dup-ip init-addr none
|
||||
|
||||
backend mongo
|
||||
option httpchk GET /db_check/mongo
|
||||
option httpchk GET /db_check/mongo?haproxy=1
|
||||
default-server check inter 2000ms resolvers consul resolve-prefer ipv4 resolve-opts allow-dup-ip init-addr none
|
||||
{% for host in groups.mongo %}
|
||||
server {{host}} {{host}}.node.nyc1.consul:5579
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from flask import Flask, abort
|
||||
from flask import Flask, abort, request
|
||||
import os
|
||||
import psycopg2
|
||||
import pymysql
|
||||
|
@ -80,42 +80,43 @@ def db_check_mongo():
|
|||
# The `mongo` hostname below is a reference to the newsblurnet docker network, where 172.18.0.0/16 is defined
|
||||
client = pymongo.MongoClient(f"mongodb://{settings.MONGO_DB['username']}:{settings.MONGO_DB['password']}@{settings.SERVER_NAME}/?authSource=admin")
|
||||
db = client.newsblur
|
||||
return str(1)
|
||||
if request.args.get('consul') == '1':
|
||||
return str(1)
|
||||
except:
|
||||
abort(503)
|
||||
|
||||
try:
|
||||
stories = db.stories.estimated_document_count()
|
||||
except (pymongo.errors.NotMasterError, pymongo.errors.ServerSelectionTimeoutError):
|
||||
abort(504)
|
||||
except pymongo.errors.OperationFailure as e:
|
||||
if 'Authentication failed' in str(e):
|
||||
abort(505)
|
||||
abort(506)
|
||||
if request.args.get('haproxy') == '1':
|
||||
try:
|
||||
stories = db.stories.estimated_document_count()
|
||||
except (pymongo.errors.NotMasterError, pymongo.errors.ServerSelectionTimeoutError):
|
||||
abort(504)
|
||||
except pymongo.errors.OperationFailure as e:
|
||||
if 'Authentication failed' in str(e):
|
||||
abort(505)
|
||||
abort(506)
|
||||
|
||||
if not stories:
|
||||
abort(510)
|
||||
|
||||
if not stories:
|
||||
abort(510)
|
||||
|
||||
status = client.admin.command('replSetGetStatus')
|
||||
members = status['members']
|
||||
primary_optime = None
|
||||
oldest_secondary_optime = None
|
||||
for member in members:
|
||||
member_state = member['state']
|
||||
optime = member['optime']
|
||||
if member_state == PRIMARY_STATE:
|
||||
primary_optime = optime['ts'].time
|
||||
elif member_state == SECONDARY_STATE:
|
||||
if not oldest_secondary_optime or optime['ts'].time < oldest_secondary_optime:
|
||||
oldest_secondary_optime = optime['ts'].time
|
||||
status = client.admin.command('replSetGetStatus')
|
||||
members = status['members']
|
||||
primary_optime = None
|
||||
oldest_secondary_optime = None
|
||||
for member in members:
|
||||
member_state = member['state']
|
||||
optime = member['optime']
|
||||
if member_state == PRIMARY_STATE:
|
||||
primary_optime = optime['ts'].time
|
||||
elif member_state == SECONDARY_STATE:
|
||||
if not oldest_secondary_optime or optime['ts'].time < oldest_secondary_optime:
|
||||
oldest_secondary_optime = optime['ts'].time
|
||||
|
||||
if not primary_optime or not oldest_secondary_optime:
|
||||
abort(511)
|
||||
if not primary_optime or not oldest_secondary_optime:
|
||||
abort(511)
|
||||
|
||||
# if primary_optime - oldest_secondary_optime > 100:
|
||||
# abort(512)
|
||||
# if primary_optime - oldest_secondary_optime > 100:
|
||||
# abort(512)
|
||||
|
||||
return str(stories)
|
||||
return str(stories)
|
||||
|
||||
@app.route("/db_check/mongo_analytics")
|
||||
def db_check_mongo_analytics():
|
||||
|
|
Loading…
Add table
Reference in a new issue