2021-08-21 02:46:11 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# keepalive.py - Utility functions for pvcnoded Keepalives
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2023-12-29 11:16:59 -05:00
|
|
|
# Copyright (C) 2018-2024 Joshua M. Boniface <joshua@boniface.me>
|
2021-08-21 02:46:11 -04:00
|
|
|
#
|
|
|
|
# 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 3.
|
|
|
|
#
|
|
|
|
# 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import pvcnoded.util.fencing
|
|
|
|
|
|
|
|
import daemon_lib.common as common
|
|
|
|
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
from rados import Rados
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
from queue import Queue
|
|
|
|
from threading import Thread
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import libvirt
|
|
|
|
import psutil
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
# State table for pretty stats
|
|
|
|
libvirt_vm_states = {
|
|
|
|
0: "NOSTATE",
|
|
|
|
1: "RUNNING",
|
|
|
|
2: "BLOCKED",
|
|
|
|
3: "PAUSED",
|
|
|
|
4: "SHUTDOWN",
|
|
|
|
5: "SHUTOFF",
|
|
|
|
6: "CRASHED",
|
2021-11-06 03:02:43 -04:00
|
|
|
7: "PMSUSPENDED",
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-21 15:12:20 -05:00
|
|
|
def start_keepalive_timer(logger, config, zkhandler, this_node, netstats):
|
2021-11-06 03:02:43 -04:00
|
|
|
keepalive_interval = config["keepalive_interval"]
|
|
|
|
logger.out(
|
|
|
|
f"Starting keepalive timer ({keepalive_interval} second interval)", state="s"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
keepalive_timer = BackgroundScheduler()
|
|
|
|
keepalive_timer.add_job(
|
|
|
|
node_keepalive,
|
2023-12-21 15:12:20 -05:00
|
|
|
args=(logger, config, zkhandler, this_node, netstats),
|
2021-11-06 03:02:43 -04:00
|
|
|
trigger="interval",
|
|
|
|
seconds=keepalive_interval,
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
keepalive_timer.start()
|
|
|
|
return keepalive_timer
|
|
|
|
|
|
|
|
|
|
|
|
def stop_keepalive_timer(logger, keepalive_timer):
|
|
|
|
try:
|
|
|
|
keepalive_timer.shutdown()
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Stopping keepalive timer", state="s")
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to stop keepalive timer", state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Ceph stats update function
|
|
|
|
def collect_ceph_stats(logger, config, zkhandler, this_node, queue):
|
2021-11-06 03:02:43 -04:00
|
|
|
pool_list = zkhandler.children("base.pool")
|
|
|
|
osd_list = zkhandler.children("base.osd")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Thread starting", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Connect to the Ceph cluster
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_conn = Rados(
|
|
|
|
conffile=config["ceph_config_file"],
|
|
|
|
conf=dict(keyring=config["ceph_admin_keyring"]),
|
|
|
|
)
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Connecting to cluster", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
ceph_conn.connect(timeout=1)
|
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to open connection to Ceph cluster: {}".format(e), state="e")
|
2021-08-21 02:46:11 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Primary-only functions
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2023-02-15 15:16:02 -05:00
|
|
|
# Get Ceph status information (pretty)
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Set Ceph status information in zookeeper (primary only)",
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
command = {"prefix": "status", "format": "pretty"}
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_status = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[
|
|
|
|
1
|
|
|
|
].decode("ascii")
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write([("base.storage", str(ceph_status))])
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to set Ceph status data: {}".format(e), state="e")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2023-02-15 15:16:02 -05:00
|
|
|
# Get Ceph health information (JSON)
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Set Ceph health information in zookeeper (primary only)",
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2023-02-15 15:16:02 -05:00
|
|
|
|
|
|
|
command = {"prefix": "health", "format": "json"}
|
|
|
|
ceph_health = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[
|
|
|
|
1
|
|
|
|
].decode("ascii")
|
|
|
|
try:
|
|
|
|
zkhandler.write([("base.storage.health", str(ceph_health))])
|
|
|
|
except Exception as e:
|
|
|
|
logger.out("Failed to set Ceph health data: {}".format(e), state="e")
|
|
|
|
|
|
|
|
# Get Ceph df information (pretty)
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Set Ceph rados df information in zookeeper (primary only)",
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Get rados df info
|
|
|
|
command = {"prefix": "df", "format": "pretty"}
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_df = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[1].decode(
|
|
|
|
"ascii"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write([("base.storage.util", str(ceph_df))])
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to set Ceph utilization data: {}".format(e), state="e")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Set pool information in zookeeper (primary only)",
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Get pool info
|
|
|
|
command = {"prefix": "df", "format": "json"}
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_df_output = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[
|
|
|
|
1
|
|
|
|
].decode("ascii")
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2024-07-22 13:26:27 -04:00
|
|
|
ceph_pool_df_raw = sorted(
|
|
|
|
json.loads(ceph_df_output)["pools"], key=lambda x: x["name"]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to obtain Pool data (ceph df): {}".format(e), state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
ceph_pool_df_raw = []
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
retcode, stdout, stderr = common.run_os_command(
|
|
|
|
"rados df --format json", timeout=1
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2024-07-22 13:26:27 -04:00
|
|
|
rados_pool_df_raw = sorted(
|
|
|
|
json.loads(stdout)["pools"], key=lambda x: x["name"]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to obtain Pool data (rados df): {}".format(e), state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
rados_pool_df_raw = []
|
|
|
|
|
|
|
|
pool_count = len(ceph_pool_df_raw)
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Getting info for {} pools".format(pool_count),
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
for pool_idx in range(0, pool_count):
|
|
|
|
try:
|
|
|
|
# Combine all the data for this pool
|
|
|
|
ceph_pool_df = ceph_pool_df_raw[pool_idx]
|
|
|
|
rados_pool_df = rados_pool_df_raw[pool_idx]
|
|
|
|
pool = ceph_pool_df
|
|
|
|
pool.update(rados_pool_df)
|
|
|
|
|
|
|
|
# Ignore any pools that aren't in our pool list
|
2021-11-06 03:02:43 -04:00
|
|
|
if pool["name"] not in pool_list:
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Pool {} not in pool list {}".format(pool["name"], pool_list),
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
continue
|
|
|
|
else:
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Parsing data for pool {}".format(pool["name"]),
|
|
|
|
state="d",
|
|
|
|
prefix="ceph-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Assemble a useful data structure
|
|
|
|
pool_df = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"id": pool["id"],
|
|
|
|
"stored_bytes": pool["stats"]["stored"],
|
|
|
|
"free_bytes": pool["stats"]["max_avail"],
|
|
|
|
"used_bytes": pool["stats"]["bytes_used"],
|
|
|
|
"used_percent": pool["stats"]["percent_used"],
|
|
|
|
"num_objects": pool["stats"]["objects"],
|
|
|
|
"num_object_clones": pool["num_object_clones"],
|
|
|
|
"num_object_copies": pool["num_object_copies"],
|
|
|
|
"num_objects_missing_on_primary": pool[
|
|
|
|
"num_objects_missing_on_primary"
|
|
|
|
],
|
|
|
|
"num_objects_unfound": pool["num_objects_unfound"],
|
|
|
|
"num_objects_degraded": pool["num_objects_degraded"],
|
|
|
|
"read_ops": pool["read_ops"],
|
|
|
|
"read_bytes": pool["read_bytes"],
|
|
|
|
"write_ops": pool["write_ops"],
|
|
|
|
"write_bytes": pool["write_bytes"],
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Write the pool data to Zookeeper
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[(("pool.stats", pool["name"]), str(json.dumps(pool_df)))]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
|
|
|
# One or more of the status commands timed out, just continue
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"Failed to format and send pool data: {}".format(e), state="w"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
# Only grab OSD stats if there are OSDs to grab (otherwise `ceph osd df` hangs)
|
|
|
|
osds_this_node = 0
|
|
|
|
if len(osd_list) > 0:
|
|
|
|
# Get data from Ceph OSDs
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Get data from Ceph OSDs", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Parse the dump data
|
|
|
|
osd_dump = dict()
|
|
|
|
|
|
|
|
command = {"prefix": "osd dump", "format": "json"}
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_dump_output = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[
|
|
|
|
1
|
|
|
|
].decode("ascii")
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_dump_raw = json.loads(osd_dump_output)["osds"]
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to obtain OSD data: {}".format(e), state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
osd_dump_raw = []
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Loop through OSD dump", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
for osd in osd_dump_raw:
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_dump.update(
|
|
|
|
{
|
|
|
|
str(osd["osd"]): {
|
|
|
|
"uuid": osd["uuid"],
|
|
|
|
"up": osd["up"],
|
|
|
|
"in": osd["in"],
|
|
|
|
"primary_affinity": osd["primary_affinity"],
|
|
|
|
}
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
2021-11-06 03:02:43 -04:00
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Parse the df data
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Parse the OSD df data", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
osd_df = dict()
|
|
|
|
|
|
|
|
command = {"prefix": "osd df", "format": "json"}
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_df_raw = json.loads(
|
|
|
|
ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[1]
|
|
|
|
)["nodes"]
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to obtain OSD data: {}".format(e), state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
osd_df_raw = []
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Loop through OSD df", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
for osd in osd_df_raw:
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_df.update(
|
|
|
|
{
|
|
|
|
str(osd["id"]): {
|
|
|
|
"utilization": osd["utilization"],
|
|
|
|
"var": osd["var"],
|
|
|
|
"pgs": osd["pgs"],
|
|
|
|
"kb": osd["kb"],
|
2022-08-11 11:06:36 -04:00
|
|
|
"kb_used": osd["kb_used"],
|
|
|
|
"kb_used_data": osd["kb_used_data"],
|
|
|
|
"kb_used_omap": osd["kb_used_omap"],
|
|
|
|
"kb_used_meta": osd["kb_used_meta"],
|
|
|
|
"kb_avail": osd["kb_avail"],
|
2021-11-06 03:02:43 -04:00
|
|
|
"weight": osd["crush_weight"],
|
|
|
|
"reweight": osd["reweight"],
|
2022-08-11 11:06:36 -04:00
|
|
|
"class": osd["device_class"],
|
2021-11-06 03:02:43 -04:00
|
|
|
}
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
2021-11-06 03:02:43 -04:00
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Parse the status data
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Parse the OSD status data", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
osd_status = dict()
|
|
|
|
|
|
|
|
command = {"prefix": "osd status", "format": "pretty"}
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
osd_status_raw = ceph_conn.mon_command(json.dumps(command), b"", timeout=1)[
|
|
|
|
1
|
|
|
|
].decode("ascii")
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to obtain OSD status data: {}".format(e), state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
osd_status_raw = []
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Loop through OSD status data", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
for line in osd_status_raw.split("\n"):
|
2021-08-21 02:46:11 -04:00
|
|
|
# Strip off colour
|
2021-11-06 03:02:43 -04:00
|
|
|
line = re.sub(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))", "", line)
|
2021-08-21 02:46:11 -04:00
|
|
|
# Split it for parsing
|
|
|
|
line = line.split()
|
2023-08-31 00:40:33 -04:00
|
|
|
|
|
|
|
# Ceph 14 format:
|
|
|
|
# ['|', '0', '|', 'hv1.p.u.bonilan.net', '|', '318G', '|', '463G', '|', '213', '|', '1430k', '|', '22', '|', '124k', '|', 'exists,up', '|']
|
|
|
|
# Ceph 16 format:
|
|
|
|
# ['0', 'hv1.t.u.bonilan.net', '2489M', '236G', '0', '0', '0', '0', 'exists,up']
|
|
|
|
|
|
|
|
# Bypass obviously invalid lines
|
|
|
|
if len(line) < 1:
|
|
|
|
continue
|
|
|
|
elif line[0] == "+":
|
|
|
|
continue
|
|
|
|
|
2023-11-01 21:33:40 -04:00
|
|
|
try:
|
|
|
|
# If line begins with | and second entry is a digit (i.e. OSD ID)
|
|
|
|
if line[0] == "|" and line[1].isdigit():
|
|
|
|
# Parse the line in Ceph 14 format
|
|
|
|
osd_id = line[1]
|
|
|
|
node = line[3].split(".")[0]
|
|
|
|
used = line[5]
|
|
|
|
avail = line[7]
|
|
|
|
wr_ops = line[9]
|
|
|
|
wr_data = line[11]
|
|
|
|
rd_ops = line[13]
|
|
|
|
rd_data = line[15]
|
|
|
|
state = line[17]
|
|
|
|
# If first entry is a digit (i.e. OSD ID)
|
|
|
|
elif line[0].isdigit():
|
|
|
|
# Parse the line in Ceph 16 format
|
|
|
|
osd_id = line[0]
|
|
|
|
node = line[1].split(".")[0]
|
|
|
|
used = line[2]
|
|
|
|
avail = line[3]
|
|
|
|
wr_ops = line[4]
|
|
|
|
wr_data = line[5]
|
|
|
|
rd_ops = line[6]
|
|
|
|
rd_data = line[7]
|
|
|
|
state = line[8]
|
|
|
|
# Otherwise, it's the header line and is ignored
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
except IndexError:
|
2023-08-31 00:40:33 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# I don't know why 2018 me used this construct instead of a normal
|
|
|
|
# dictionary update, but it works so not changing it.
|
|
|
|
# ref: bfbe9188ce830381f3f2fa1da11f1973f08eca8c
|
|
|
|
osd_status.update(
|
|
|
|
{
|
|
|
|
str(osd_id): {
|
|
|
|
"node": node,
|
|
|
|
"used": used,
|
|
|
|
"avail": avail,
|
|
|
|
"wr_ops": wr_ops,
|
|
|
|
"wr_data": wr_data,
|
|
|
|
"rd_ops": rd_ops,
|
|
|
|
"rd_data": rd_data,
|
|
|
|
"state": state,
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
2023-08-31 00:40:33 -04:00
|
|
|
}
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Merge them together into a single meaningful dict
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Merge OSD data together", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
osd_stats = dict()
|
|
|
|
|
|
|
|
for osd in osd_list:
|
2021-11-06 03:02:43 -04:00
|
|
|
if zkhandler.read(("osd.node", osd)) == config["node_hostname"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
osds_this_node += 1
|
|
|
|
try:
|
|
|
|
this_dump = osd_dump[osd]
|
|
|
|
this_dump.update(osd_df[osd])
|
|
|
|
this_dump.update(osd_status[osd])
|
|
|
|
osd_stats[osd] = this_dump
|
|
|
|
except KeyError as e:
|
|
|
|
# One or more of the status commands timed out, just continue
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"Failed to parse OSD stats into dictionary: {}".format(e), state="w"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Upload OSD data for the cluster (primary-only)
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Trigger updates for each OSD", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
for osd in osd_list:
|
|
|
|
try:
|
|
|
|
stats = json.dumps(osd_stats[osd])
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write([(("osd.stats", osd), str(stats))])
|
2021-08-21 02:46:11 -04:00
|
|
|
except KeyError as e:
|
|
|
|
# One or more of the status commands timed out, just continue
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"Failed to upload OSD stats from dictionary: {}".format(e),
|
|
|
|
state="w",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
ceph_conn.shutdown()
|
|
|
|
|
|
|
|
queue.put(osds_this_node)
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Thread finished", state="d", prefix="ceph-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
# VM stats update function
|
|
|
|
def collect_vm_stats(logger, config, zkhandler, this_node, queue):
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Thread starting", state="d", prefix="vm-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Connect to libvirt
|
|
|
|
libvirt_name = "qemu:///system"
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Connecting to libvirt", state="d", prefix="vm-thread")
|
2021-09-26 20:42:01 -04:00
|
|
|
try:
|
|
|
|
lv_conn = libvirt.open(libvirt_name)
|
|
|
|
if lv_conn is None:
|
|
|
|
raise Exception
|
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out('Failed to open connection to "{}"'.format(libvirt_name), state="e")
|
2021-09-26 20:42:01 -04:00
|
|
|
return
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
memalloc = 0
|
|
|
|
memprov = 0
|
|
|
|
vcpualloc = 0
|
|
|
|
# Toggle state management of dead VMs to restart them
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Toggle state management of dead VMs to restart them",
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
# Make a copy of the d_domain; if not, and it changes in flight, this can fail
|
|
|
|
fixed_d_domain = this_node.d_domain.copy()
|
|
|
|
for domain, instance in fixed_d_domain.items():
|
|
|
|
if domain in this_node.domain_list:
|
2023-12-11 11:52:59 -05:00
|
|
|
# Add the allocated memory to our memalloc value
|
|
|
|
memalloc += instance.getmemory()
|
|
|
|
memprov += instance.getmemory()
|
|
|
|
vcpualloc += instance.getvcpus()
|
2021-11-06 03:02:43 -04:00
|
|
|
if instance.getstate() == "start" and instance.getnode() == this_node.name:
|
2021-08-21 02:46:11 -04:00
|
|
|
if instance.getdom() is not None:
|
|
|
|
try:
|
|
|
|
if instance.getdom().state()[0] != libvirt.VIR_DOMAIN_RUNNING:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"VM {} has failed".format(instance.domname),
|
|
|
|
state="w",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
# Toggle a state "change"
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"Resetting state to {} for VM {}".format(
|
|
|
|
instance.getstate(), instance.domname
|
|
|
|
),
|
|
|
|
state="i",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
|
|
|
zkhandler.write(
|
|
|
|
[(("domain.state", domain), instance.getstate())]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
elif instance.getnode() == this_node.name:
|
|
|
|
memprov += instance.getmemory()
|
|
|
|
|
|
|
|
# Get list of running domains from Libvirt
|
|
|
|
running_domains = lv_conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE)
|
|
|
|
|
|
|
|
# Get statistics from any running VMs
|
|
|
|
for domain in running_domains:
|
|
|
|
try:
|
|
|
|
# Get basic information about the VM
|
|
|
|
tree = ElementTree.fromstring(domain.XMLDesc())
|
|
|
|
domain_uuid = domain.UUIDString()
|
|
|
|
domain_name = domain.name()
|
|
|
|
|
|
|
|
# Get all the raw information about the VM
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Getting general statistics for VM {}".format(domain_name),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-11-06 03:02:43 -04:00
|
|
|
(
|
|
|
|
domain_state,
|
|
|
|
domain_maxmem,
|
|
|
|
domain_mem,
|
|
|
|
domain_vcpus,
|
|
|
|
domain_cputime,
|
|
|
|
) = domain.info()
|
2021-08-21 02:46:11 -04:00
|
|
|
# We can't properly gather stats from a non-running VMs so continue
|
|
|
|
if domain_state != libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
continue
|
|
|
|
domain_memory_stats = domain.memoryStats()
|
|
|
|
domain_cpu_stats = domain.getCPUStats(True)[0]
|
|
|
|
except Exception as e:
|
2023-12-27 12:58:36 -05:00
|
|
|
try:
|
|
|
|
logger.out(
|
|
|
|
"Failed getting VM information for {}: {}".format(domain.name(), e),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-08-21 02:46:11 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Ensure VM is present in the domain_list
|
|
|
|
if domain_uuid not in this_node.domain_list:
|
|
|
|
this_node.domain_list.append(domain_uuid)
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Getting disk statistics for VM {}".format(domain_name),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
domain_disk_stats = []
|
2021-09-12 19:41:07 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
for disk in tree.findall("devices/disk"):
|
|
|
|
disk_name = disk.find("source").get("name")
|
2021-09-12 19:41:07 -04:00
|
|
|
if not disk_name:
|
2021-11-06 03:02:43 -04:00
|
|
|
disk_name = disk.find("source").get("file")
|
|
|
|
disk_stats = domain.blockStats(disk.find("target").get("dev"))
|
|
|
|
domain_disk_stats.append(
|
|
|
|
{
|
|
|
|
"name": disk_name,
|
|
|
|
"rd_req": disk_stats[0],
|
|
|
|
"rd_bytes": disk_stats[1],
|
|
|
|
"wr_req": disk_stats[2],
|
|
|
|
"wr_bytes": disk_stats[3],
|
|
|
|
"err": disk_stats[4],
|
|
|
|
}
|
|
|
|
)
|
2021-09-12 19:41:07 -04:00
|
|
|
except Exception as e:
|
2023-12-27 12:58:36 -05:00
|
|
|
try:
|
|
|
|
logger.out(
|
|
|
|
"Failed getting disk stats for {}: {}".format(domain.name(), e),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-09-12 19:41:07 -04:00
|
|
|
continue
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Getting network statistics for VM {}".format(domain_name),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
domain_network_stats = []
|
2021-09-12 19:41:07 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
for interface in tree.findall("devices/interface"):
|
|
|
|
interface_type = interface.get("type")
|
|
|
|
if interface_type not in ["bridge"]:
|
2021-09-12 19:41:07 -04:00
|
|
|
continue
|
2021-11-06 03:02:43 -04:00
|
|
|
interface_name = interface.find("target").get("dev")
|
|
|
|
interface_bridge = interface.find("source").get("bridge")
|
2021-09-12 19:41:07 -04:00
|
|
|
interface_stats = domain.interfaceStats(interface_name)
|
2021-11-06 03:02:43 -04:00
|
|
|
domain_network_stats.append(
|
|
|
|
{
|
|
|
|
"name": interface_name,
|
|
|
|
"bridge": interface_bridge,
|
|
|
|
"rd_bytes": interface_stats[0],
|
|
|
|
"rd_packets": interface_stats[1],
|
|
|
|
"rd_errors": interface_stats[2],
|
|
|
|
"rd_drops": interface_stats[3],
|
|
|
|
"wr_bytes": interface_stats[4],
|
|
|
|
"wr_packets": interface_stats[5],
|
|
|
|
"wr_errors": interface_stats[6],
|
|
|
|
"wr_drops": interface_stats[7],
|
|
|
|
}
|
|
|
|
)
|
2021-09-12 19:41:07 -04:00
|
|
|
except Exception as e:
|
2023-12-27 12:58:36 -05:00
|
|
|
try:
|
|
|
|
logger.out(
|
|
|
|
"Failed getting network stats for {}: {}".format(domain.name(), e),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-09-12 19:41:07 -04:00
|
|
|
continue
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Create the final dictionary
|
|
|
|
domain_stats = {
|
|
|
|
"state": libvirt_vm_states[domain_state],
|
|
|
|
"maxmem": domain_maxmem,
|
|
|
|
"livemem": domain_mem,
|
|
|
|
"cpus": domain_vcpus,
|
|
|
|
"cputime": domain_cputime,
|
|
|
|
"mem_stats": domain_memory_stats,
|
|
|
|
"cpu_stats": domain_cpu_stats,
|
|
|
|
"disk_stats": domain_disk_stats,
|
2021-11-06 03:02:43 -04:00
|
|
|
"net_stats": domain_network_stats,
|
2021-08-21 02:46:11 -04:00
|
|
|
}
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Writing statistics for VM {} to Zookeeper".format(domain_name),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[(("domain.stats", domain_uuid), str(json.dumps(domain_stats)))]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception as e:
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Failed to write domain statistics: {}".format(e),
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Close the Libvirt connection
|
|
|
|
lv_conn.close()
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
f"VM stats: doms: {len(running_domains)}; memalloc: {memalloc}; memprov: {memprov}; vcpualloc: {vcpualloc}",
|
|
|
|
state="d",
|
|
|
|
prefix="vm-thread",
|
|
|
|
)
|
2023-11-10 10:29:32 -05:00
|
|
|
|
2021-08-21 02:46:11 -04:00
|
|
|
queue.put(len(running_domains))
|
|
|
|
queue.put(memalloc)
|
|
|
|
queue.put(memprov)
|
|
|
|
queue.put(vcpualloc)
|
|
|
|
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Thread finished", state="d", prefix="vm-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Keepalive update function
|
2023-12-21 15:12:20 -05:00
|
|
|
def node_keepalive(logger, config, zkhandler, this_node, netstats):
|
2023-02-24 10:38:24 -05:00
|
|
|
# Display node information to the terminal
|
|
|
|
if config["log_keepalives"]:
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2023-02-24 10:38:24 -05:00
|
|
|
cst_colour = logger.fmt_green
|
2023-09-15 16:47:56 -04:00
|
|
|
elif this_node.coordinator_state == "secondary":
|
2023-02-24 10:38:24 -05:00
|
|
|
cst_colour = logger.fmt_blue
|
|
|
|
else:
|
|
|
|
cst_colour = logger.fmt_cyan
|
2023-09-15 22:40:09 -04:00
|
|
|
|
|
|
|
active_coordinator_state = this_node.coordinator_state
|
|
|
|
|
|
|
|
runtime_start = datetime.now()
|
2023-11-29 16:29:43 -05:00
|
|
|
logger.out(
|
2023-12-11 11:29:49 -05:00
|
|
|
f"Starting node keepalive run at {datetime.now()}",
|
2023-11-29 16:29:43 -05:00
|
|
|
state="t",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Set the migration selector in Zookeeper for clients to read
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_hypervisor"]:
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
if (
|
|
|
|
zkhandler.read("base.config.migration_target_selector")
|
|
|
|
!= config["migration_target_selector"]
|
|
|
|
):
|
2022-10-03 14:04:12 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"base.config.migration_target_selector",
|
|
|
|
config["migration_target_selector"],
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2022-10-03 14:04:12 -04:00
|
|
|
logger.out(
|
|
|
|
"Failed to set migration target selector in Zookeeper",
|
|
|
|
state="e",
|
|
|
|
prefix="main-thread",
|
2021-11-06 03:02:43 -04:00
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Set the upstream IP in Zookeeper for clients to read
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_networking"]:
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
if (
|
|
|
|
zkhandler.read("base.config.upstream_ip")
|
|
|
|
!= config["upstream_floating_ip"]
|
|
|
|
):
|
2022-10-03 14:04:12 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[("base.config.upstream_ip", config["upstream_floating_ip"])]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2022-10-03 14:04:12 -04:00
|
|
|
logger.out(
|
|
|
|
"Failed to set upstream floating IP in Zookeeper",
|
|
|
|
state="e",
|
|
|
|
prefix="main-thread",
|
2021-11-06 03:02:43 -04:00
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Get past state and update if needed
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Get past state and update if needed", state="d", prefix="main-thread")
|
2021-11-06 03:02:43 -04:00
|
|
|
|
|
|
|
past_state = zkhandler.read(("node.state.daemon", this_node.name))
|
|
|
|
if past_state != "run" and past_state != "shutdown":
|
|
|
|
this_node.daemon_state = "run"
|
|
|
|
zkhandler.write([(("node.state.daemon", this_node.name), "run")])
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
this_node.daemon_state = "run"
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Ensure the primary key is properly set
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Ensure the primary key is properly set", state="d", prefix="main-thread"
|
|
|
|
)
|
2023-09-15 16:47:56 -04:00
|
|
|
if this_node.coordinator_state == "primary":
|
2021-11-06 03:02:43 -04:00
|
|
|
if zkhandler.read("base.config.primary_node") != this_node.name:
|
|
|
|
zkhandler.write([("base.config.primary_node", this_node.name)])
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Run VM statistics collection in separate thread for parallelization
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_hypervisor"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
vm_thread_queue = Queue()
|
2021-11-06 03:02:43 -04:00
|
|
|
vm_stats_thread = Thread(
|
|
|
|
target=collect_vm_stats,
|
|
|
|
args=(logger, config, zkhandler, this_node, vm_thread_queue),
|
|
|
|
kwargs={},
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
vm_stats_thread.start()
|
|
|
|
|
|
|
|
# Run Ceph status collection in separate thread for parallelization
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_storage"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
ceph_thread_queue = Queue()
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_stats_thread = Thread(
|
|
|
|
target=collect_ceph_stats,
|
|
|
|
args=(logger, config, zkhandler, this_node, ceph_thread_queue),
|
|
|
|
kwargs={},
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
ceph_stats_thread.start()
|
|
|
|
|
|
|
|
# Get node performance statistics
|
|
|
|
this_node.memtotal = int(psutil.virtual_memory().total / 1024 / 1024)
|
|
|
|
this_node.memused = int(psutil.virtual_memory().used / 1024 / 1024)
|
2024-04-19 10:25:33 -04:00
|
|
|
this_node.memfree = int(psutil.virtual_memory().available / 1024 / 1024)
|
2023-08-29 21:41:44 -04:00
|
|
|
this_node.cpuload = round(os.getloadavg()[0], 2)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2023-12-21 15:12:20 -05:00
|
|
|
# Get node network statistics via netstats instance
|
|
|
|
netstats.set_interfaces()
|
|
|
|
netstats.set_data()
|
|
|
|
|
2021-08-21 02:46:11 -04:00
|
|
|
# Join against running threads
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_hypervisor"]:
|
2024-10-10 15:33:47 -04:00
|
|
|
vm_stats_thread.join(timeout=config["keepalive_interval"] - 1)
|
2021-08-21 02:46:11 -04:00
|
|
|
if vm_stats_thread.is_alive():
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("VM stats gathering exceeded timeout, continuing", state="w")
|
|
|
|
if config["enable_storage"]:
|
2024-10-10 15:33:47 -04:00
|
|
|
ceph_stats_thread.join(timeout=config["keepalive_interval"] - 1)
|
2021-08-21 02:46:11 -04:00
|
|
|
if ceph_stats_thread.is_alive():
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Ceph stats gathering exceeded timeout, continuing", state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Get information from thread queues
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_hypervisor"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2024-10-10 15:33:47 -04:00
|
|
|
this_node.domains_count = vm_thread_queue.get(timeout=0.1)
|
|
|
|
this_node.memalloc = vm_thread_queue.get(timeout=0.1)
|
|
|
|
this_node.memprov = vm_thread_queue.get(timeout=0.1)
|
|
|
|
this_node.vcpualloc = vm_thread_queue.get(timeout=0.1)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("VM stats queue get exceeded timeout, continuing", state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
|
|
|
this_node.domains_count = 0
|
|
|
|
this_node.memalloc = 0
|
|
|
|
this_node.memprov = 0
|
|
|
|
this_node.vcpualloc = 0
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["enable_storage"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2024-10-10 15:33:47 -04:00
|
|
|
osds_this_node = ceph_thread_queue.get(timeout=0.1)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Ceph stats queue get exceeded timeout, continuing", state="w")
|
|
|
|
osds_this_node = "?"
|
2023-02-13 12:13:56 -05:00
|
|
|
else:
|
|
|
|
osds_this_node = "0"
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Set our information in zookeeper
|
|
|
|
keepalive_time = int(time.time())
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out("Set our information in zookeeper", state="d", prefix="main-thread")
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[
|
|
|
|
(("node.memory.total", this_node.name), str(this_node.memtotal)),
|
|
|
|
(("node.memory.used", this_node.name), str(this_node.memused)),
|
|
|
|
(("node.memory.free", this_node.name), str(this_node.memfree)),
|
|
|
|
(("node.memory.allocated", this_node.name), str(this_node.memalloc)),
|
|
|
|
(("node.memory.provisioned", this_node.name), str(this_node.memprov)),
|
|
|
|
(("node.vcpu.allocated", this_node.name), str(this_node.vcpualloc)),
|
|
|
|
(("node.cpu.load", this_node.name), str(this_node.cpuload)),
|
|
|
|
(
|
|
|
|
("node.count.provisioned_domains", this_node.name),
|
|
|
|
str(this_node.domains_count),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.running_domains", this_node.name),
|
|
|
|
" ".join(this_node.domain_list),
|
|
|
|
),
|
|
|
|
(("node.keepalive", this_node.name), str(keepalive_time)),
|
|
|
|
]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to set keepalive data", state="e")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["log_keepalives"]:
|
2023-09-15 22:40:09 -04:00
|
|
|
runtime_end = datetime.now()
|
|
|
|
runtime_delta = runtime_end - runtime_start
|
|
|
|
runtime = "{:0.02f}".format(runtime_delta.total_seconds())
|
|
|
|
|
|
|
|
logger.out(
|
|
|
|
"{start_colour}{hostname} keepalive @ {starttime}{nofmt} [{cst_colour}{costate}{nofmt}] in {runtime} seconds".format(
|
|
|
|
start_colour=logger.fmt_purple,
|
|
|
|
cst_colour=logger.fmt_bold + cst_colour,
|
|
|
|
nofmt=logger.fmt_end,
|
|
|
|
hostname=config["node_hostname"],
|
|
|
|
starttime=runtime_start,
|
|
|
|
costate=active_coordinator_state,
|
|
|
|
runtime=runtime,
|
|
|
|
),
|
|
|
|
state="t",
|
|
|
|
)
|
|
|
|
|
2023-02-24 10:38:24 -05:00
|
|
|
if this_node.maintenance is True:
|
|
|
|
maintenance_colour = logger.fmt_blue
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
2023-02-24 10:38:24 -05:00
|
|
|
maintenance_colour = logger.fmt_green
|
|
|
|
|
|
|
|
if isinstance(this_node.health, int):
|
|
|
|
if this_node.health > 90:
|
|
|
|
health_colour = logger.fmt_green
|
|
|
|
elif this_node.health > 50:
|
|
|
|
health_colour = logger.fmt_yellow
|
|
|
|
else:
|
|
|
|
health_colour = logger.fmt_red
|
|
|
|
health_text = str(this_node.health) + "%"
|
|
|
|
|
|
|
|
else:
|
|
|
|
health_colour = logger.fmt_blue
|
|
|
|
health_text = "N/A"
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["log_keepalive_cluster_details"]:
|
2021-08-21 02:46:11 -04:00
|
|
|
logger.out(
|
2023-02-24 10:38:24 -05:00
|
|
|
"{bold}Maintenance:{nofmt} {maintenance_colour}{maintenance}{nofmt} "
|
|
|
|
"{bold}Health:{nofmt} {health_colour}{health}{nofmt} "
|
|
|
|
"{bold}VMs:{nofmt} {domcount} "
|
|
|
|
"{bold}OSDs:{nofmt} {osdcount} "
|
2021-11-06 03:02:43 -04:00
|
|
|
"{bold}Load:{nofmt} {load} "
|
2023-02-24 10:38:24 -05:00
|
|
|
"{bold}Memory [MiB]: "
|
2021-11-06 03:02:43 -04:00
|
|
|
"{bold}Used:{nofmt} {usedmem} "
|
|
|
|
"{bold}Free:{nofmt} {freemem}".format(
|
2021-08-21 02:46:11 -04:00
|
|
|
bold=logger.fmt_bold,
|
2023-02-24 10:38:24 -05:00
|
|
|
maintenance_colour=maintenance_colour,
|
|
|
|
health_colour=health_colour,
|
2021-08-21 02:46:11 -04:00
|
|
|
nofmt=logger.fmt_end,
|
2023-02-24 10:38:24 -05:00
|
|
|
maintenance=this_node.maintenance,
|
|
|
|
health=health_text,
|
2021-08-21 02:46:11 -04:00
|
|
|
domcount=this_node.domains_count,
|
2023-02-13 12:13:56 -05:00
|
|
|
osdcount=osds_this_node,
|
2021-08-21 02:46:11 -04:00
|
|
|
load=this_node.cpuload,
|
|
|
|
freemem=this_node.memfree,
|
|
|
|
usedmem=this_node.memused,
|
|
|
|
),
|
2021-11-06 03:02:43 -04:00
|
|
|
state="t",
|
2021-08-21 02:46:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Look for dead nodes and fence them
|
|
|
|
if not this_node.maintenance:
|
2023-12-27 12:58:36 -05:00
|
|
|
logger.out(
|
|
|
|
"Look for dead nodes and fence them", state="d", prefix="main-thread"
|
|
|
|
)
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["daemon_mode"] == "coordinator":
|
|
|
|
for node_name in zkhandler.children("base.node"):
|
2021-08-21 02:46:11 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
node_daemon_state = zkhandler.read(("node.state.daemon", node_name))
|
|
|
|
node_keepalive = int(zkhandler.read(("node.keepalive", node_name)))
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
node_daemon_state = "unknown"
|
2021-08-21 02:46:11 -04:00
|
|
|
node_keepalive = 0
|
|
|
|
|
|
|
|
# Handle deadtime and fencng if needed
|
|
|
|
# (A node is considered dead when its keepalive timer is >6*keepalive_interval seconds
|
|
|
|
# out-of-date while in 'start' state)
|
2021-11-06 03:02:43 -04:00
|
|
|
node_deadtime = int(time.time()) - (
|
|
|
|
int(config["keepalive_interval"]) * int(config["fence_intervals"])
|
|
|
|
)
|
|
|
|
if node_keepalive < node_deadtime and node_daemon_state == "run":
|
|
|
|
logger.out(
|
|
|
|
"Node {} seems dead - starting monitor for fencing".format(
|
|
|
|
node_name
|
|
|
|
),
|
|
|
|
state="w",
|
|
|
|
)
|
|
|
|
zk_lock = zkhandler.writelock(("node.state.daemon", node_name))
|
2021-08-21 02:46:11 -04:00
|
|
|
with zk_lock:
|
|
|
|
# Ensures that, if we lost the lock race and come out of waiting,
|
|
|
|
# we won't try to trigger our own fence thread.
|
2021-11-06 03:02:43 -04:00
|
|
|
if zkhandler.read(("node.state.daemon", node_name)) != "dead":
|
|
|
|
fence_thread = Thread(
|
|
|
|
target=pvcnoded.util.fencing.fence_node,
|
|
|
|
args=(node_name, zkhandler, config, logger),
|
|
|
|
kwargs={},
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
fence_thread.start()
|
|
|
|
# Write the updated data after we start the fence thread
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[(("node.state.daemon", node_name), "dead")]
|
|
|
|
)
|