add server code for zookeeper monitor

This commit is contained in:
Aayush Gupta 2023-11-30 12:07:41 +05:30
parent dabe34a90b
commit 82b5b84610
3 changed files with 48 additions and 30 deletions

View file

@ -6,7 +6,7 @@ const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MI
SQL_DATETIME_FORMAT SQL_DATETIME_FORMAT
} = require("../../src/util"); } = require("../../src/util");
const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, mqttAsync, setSetting, httpNtlm, radius, grpcQuery, const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, mqttAsync, setSetting, httpNtlm, radius, grpcQuery,
redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal, zookeeperConnect
} = require("../util-server"); } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model"); const { BeanModel } = require("redbean-node/dist/bean-model");
@ -182,6 +182,8 @@ class Monitor extends BeanModel {
tlsCert: this.tlsCert, tlsCert: this.tlsCert,
tlsKey: this.tlsKey, tlsKey: this.tlsKey,
kafkaProducerSaslOptions: JSON.parse(this.kafkaProducerSaslOptions), kafkaProducerSaslOptions: JSON.parse(this.kafkaProducerSaslOptions),
zookeeperHost: this.zookeeperHost,
zookeeperTimeout: this.zookeeperTimeout,
}; };
} }
@ -884,6 +886,12 @@ class Monitor extends BeanModel {
bean.status = UP; bean.status = UP;
bean.ping = dayjs().valueOf() - startTime; bean.ping = dayjs().valueOf() - startTime;
} else if (this.type === "zookeeper") {
let startTime = dayjs().valueOf();
bean.msg = await zookeeperConnect(this.zookeeperHost, this.zookeeperTimeout);
bean.status = UP;
bean.ping = dayjs().valueOf() - startTime;
} else { } else {
throw new Error("Unknown Monitor Type"); throw new Error("Unknown Monitor Type");
} }

View file

@ -827,6 +827,8 @@ let needSetup = false;
bean.kafkaProducerAllowAutoTopicCreation = bean.kafkaProducerAllowAutoTopicCreation =
monitor.kafkaProducerAllowAutoTopicCreation; monitor.kafkaProducerAllowAutoTopicCreation;
bean.gamedigGivenPortOnly = monitor.gamedigGivenPortOnly; bean.gamedigGivenPortOnly = monitor.gamedigGivenPortOnly;
bean.zookeeperHost = monitor.zookeeperHost;
bean.zookeeperTimeout = monitor.zookeeperTimeout;
bean.validate(); bean.validate();

View file

@ -526,35 +526,6 @@ exports.mongodbPing = async function (connectionString) {
} }
}; };
exports.zookeeperConnect = function (zookeeperHost, timeoutMs = 5000) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(Error("Zookeeper operation timed out"));
}, timeoutMs);
const config = {
connect: zookeeperHost,
timeout: 1000,
debug_level: ZooKeeper.ZOO_LOG_LEVEL_WARN,
host_order_deterministic: false,
};
const client = new ZooKeeper(config);
client.connect(config, (err, _) => {
if (err) {
clearTimeout(timer);
reject(err);
} else {
clearTimeout(timer);
resolve("Successfully connected");
}
client.close();
});
});
};
/** /**
* Query radius server * Query radius server
* @param {string} hostname Hostname of radius server * @param {string} hostname Hostname of radius server
@ -1194,3 +1165,40 @@ module.exports.axiosAbortSignal = (timeoutMs) => {
} }
} }
}; };
/**
* Attempt to connect to the given zookeeper host under the given timeout.
* @param {string} zookeeperHost - The connection string for a single host in the form 'host:port'
* @param {number} timeoutMs - Timeout in milliseconds under which connection must be established.
* @returns {Promise<any>} The result of connection attempt
*/
exports.zookeeperConnect = function (zookeeperHost, timeoutMs = 5000) {
return new Promise((resolve, reject) => {
// Seems like zookeeper client behavior is to retry indefitiely on timeout.
// So, an explicit timer is needed to prevent the monitor from getting stuck.
const timer = setTimeout(() => {
reject(Error("Zookeeper operation timed out"));
}, timeoutMs);
const config = {
connect: zookeeperHost,
timeout: timeoutMs, // Causes the client to retry and does not fail.
debug_level: ZooKeeper.ZOO_LOG_LEVEL_WARN,
host_order_deterministic: false,
};
const client = new ZooKeeper(config);
client.connect(config, (err, _) => {
if (err) {
clearTimeout(timer);
reject(err);
} else {
clearTimeout(timer);
resolve("Successfully connected");
}
client.close();
});
});
};