-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-graylog-cluster-id.py
executable file
·83 lines (69 loc) · 2.17 KB
/
update-graylog-cluster-id.py
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
#!/usr/bin/env python3
import argparse
from pymongo import MongoClient
DEFAULTS = {
"mongodb_host": "localhost",
"mongodb_port": 27017,
"mongodb_username": "",
"mongodb_password": "",
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--host",
"-H",
help="MongoDB hostname",
type=str,
default=DEFAULTS["mongodb_host"],
)
parser.add_argument(
"--port", "-p", help="MongoDB port", type=int, default=DEFAULTS["mongodb_port"]
)
parser.add_argument(
"--username",
"-U",
help="MongoDB username",
type=str,
default=DEFAULTS["mongodb_username"],
)
parser.add_argument(
"--password",
"-P",
help="MongoDB password",
type=str,
default=DEFAULTS["mongodb_password"],
)
parser.add_argument(
"--cluster-id", help="Graylog Cluster ID", required=True, type=str
)
args = parser.parse_args()
return args
def get_mongodb_collection(host, port, username, password):
if username != "" and password != "":
mongodb_url = f"mongodb://{username}:{password}@{host}:{port}"
else:
mongodb_url = f"mongodb://{host}:{port}"
mongo_client = MongoClient(mongodb_url)
db = mongo_client.graylog
return db["cluster_config"]
def get_cluster_id(collection):
return collection.find_one({"type": "org.graylog2.plugin.cluster.ClusterId"})[
"payload"
]["cluster_id"]
def update_cluster_id(collection, cluster_id):
print(f"Setting Graylog Cluster ID to {cluster_id}")
result = collection.find_one_and_update(
{"type": "org.graylog2.plugin.cluster.ClusterId"},
{"$set": {"payload": {"cluster_id": cluster_id}}},
)
return True if result["payload"]["cluster_id"] == cluster_id else False
def main():
args = parse_args()
collection = get_mongodb_collection(
args.host, args.port, args.username, args.password
)
if get_cluster_id(collection) != args.cluster_id:
if update_cluster_id(collection, args.cluster_id) == True:
print("Cluster ID successfully updated!")
if __name__ == "__main__":
main()