mirror of
				https://github.com/louislam/uptime-kuma.git
				synced 2025-11-04 05:36:13 +08:00 
			
		
		
		
	Merge pull request #1892 from Computroniks/feature/#1891-set-ping-packet-size
Added #1891: Set ping packet size
This commit is contained in:
		
							
								
								
									
										5
									
								
								db/patch-ping-packet-size.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								db/patch-ping-packet-size.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db.
 | 
			
		||||
BEGIN TRANSACTION;
 | 
			
		||||
ALTER TABLE monitor
 | 
			
		||||
    ADD packet_size INTEGER DEFAULT 56 NOT NULL;
 | 
			
		||||
COMMIT;
 | 
			
		||||
@@ -65,6 +65,7 @@ class Database {
 | 
			
		||||
        "patch-grpc-monitor.sql": true,
 | 
			
		||||
        "patch-add-radius-monitor.sql": true,
 | 
			
		||||
        "patch-monitor-add-resend-interval.sql": true,
 | 
			
		||||
        "patch-ping-packet-size.sql": true,
 | 
			
		||||
        "patch-maintenance-table2.sql": true,
 | 
			
		||||
        "patch-add-gamedig-monitor.sql": true,
 | 
			
		||||
    };
 | 
			
		||||
 
 | 
			
		||||
@@ -87,6 +87,7 @@ class Monitor extends BeanModel {
 | 
			
		||||
            expiryNotification: this.isEnabledExpiryNotification(),
 | 
			
		||||
            ignoreTls: this.getIgnoreTls(),
 | 
			
		||||
            upsideDown: this.isUpsideDown(),
 | 
			
		||||
            packetSize: this.packetSize,
 | 
			
		||||
            maxredirects: this.maxredirects,
 | 
			
		||||
            accepted_statuscodes: this.getAcceptedStatuscodes(),
 | 
			
		||||
            dns_resolve_type: this.dns_resolve_type,
 | 
			
		||||
@@ -375,7 +376,7 @@ class Monitor extends BeanModel {
 | 
			
		||||
                    bean.status = UP;
 | 
			
		||||
 | 
			
		||||
                } else if (this.type === "ping") {
 | 
			
		||||
                    bean.ping = await ping(this.hostname);
 | 
			
		||||
                    bean.ping = await ping(this.hostname, this.packetSize);
 | 
			
		||||
                    bean.msg = "";
 | 
			
		||||
                    bean.status = UP;
 | 
			
		||||
                } else if (this.type === "dns") {
 | 
			
		||||
@@ -485,7 +486,7 @@ class Monitor extends BeanModel {
 | 
			
		||||
                        bean.msg = res.data.response.servers[0].name;
 | 
			
		||||
 | 
			
		||||
                        try {
 | 
			
		||||
                            bean.ping = await ping(this.hostname);
 | 
			
		||||
                            bean.ping = await ping(this.hostname, this.packetSize);
 | 
			
		||||
                        } catch (_) { }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        throw new Error("Server not found on Steam");
 | 
			
		||||
 
 | 
			
		||||
@@ -696,6 +696,7 @@ let needSetup = false;
 | 
			
		||||
                bean.ignoreTls = monitor.ignoreTls;
 | 
			
		||||
                bean.expiryNotification = monitor.expiryNotification;
 | 
			
		||||
                bean.upsideDown = monitor.upsideDown;
 | 
			
		||||
                bean.packetSize = monitor.packetSize;
 | 
			
		||||
                bean.maxredirects = monitor.maxredirects;
 | 
			
		||||
                bean.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes);
 | 
			
		||||
                bean.dns_resolve_type = monitor.dns_resolve_type;
 | 
			
		||||
 
 | 
			
		||||
@@ -79,15 +79,16 @@ exports.tcping = function (hostname, port) {
 | 
			
		||||
/**
 | 
			
		||||
 * Ping the specified machine
 | 
			
		||||
 * @param {string} hostname Hostname / address of machine
 | 
			
		||||
 * @param {number} [size=56] Size of packet to send
 | 
			
		||||
 * @returns {Promise<number>} Time for ping in ms rounded to nearest integer
 | 
			
		||||
 */
 | 
			
		||||
exports.ping = async (hostname) => {
 | 
			
		||||
exports.ping = async (hostname, size = 56) => {
 | 
			
		||||
    try {
 | 
			
		||||
        return await exports.pingAsync(hostname);
 | 
			
		||||
        return await exports.pingAsync(hostname, false, size);
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        // If the host cannot be resolved, try again with ipv6
 | 
			
		||||
        if (e.message.includes("service not known")) {
 | 
			
		||||
            return await exports.pingAsync(hostname, true);
 | 
			
		||||
            return await exports.pingAsync(hostname, true, size);
 | 
			
		||||
        } else {
 | 
			
		||||
            throw e;
 | 
			
		||||
        }
 | 
			
		||||
@@ -98,14 +99,16 @@ exports.ping = async (hostname) => {
 | 
			
		||||
 * Ping the specified machine
 | 
			
		||||
 * @param {string} hostname Hostname / address of machine to ping
 | 
			
		||||
 * @param {boolean} ipv6 Should IPv6 be used?
 | 
			
		||||
 * @param {number} [size = 56] Size of ping packet to send
 | 
			
		||||
 * @returns {Promise<number>} Time for ping in ms rounded to nearest integer
 | 
			
		||||
 */
 | 
			
		||||
exports.pingAsync = function (hostname, ipv6 = false) {
 | 
			
		||||
exports.pingAsync = function (hostname, ipv6 = false, size = 56) {
 | 
			
		||||
    return new Promise((resolve, reject) => {
 | 
			
		||||
        ping.promise.probe(hostname, {
 | 
			
		||||
            v6: ipv6,
 | 
			
		||||
            min_reply: 1,
 | 
			
		||||
            deadline: 10,
 | 
			
		||||
            packetSize: size,
 | 
			
		||||
        }).then((res) => {
 | 
			
		||||
            // If ping failed, it will set field to unknown
 | 
			
		||||
            if (res.alive) {
 | 
			
		||||
 
 | 
			
		||||
@@ -358,6 +358,7 @@
 | 
			
		||||
    "Docker Hosts": "Docker Hosts",
 | 
			
		||||
    "Domain": "Domain",
 | 
			
		||||
    "Workstation": "Workstation",
 | 
			
		||||
    "Packet Size": "Packet Size",
 | 
			
		||||
    "telegram": "Telegram",
 | 
			
		||||
    "ZohoCliq": "ZohoCliq",
 | 
			
		||||
    "Bot Token": "Bot Token",
 | 
			
		||||
 
 | 
			
		||||
@@ -365,6 +365,12 @@
 | 
			
		||||
                                </div>
 | 
			
		||||
                            </div>
 | 
			
		||||
 | 
			
		||||
                            <!-- Ping packet size -->
 | 
			
		||||
                            <div v-if="monitor.type === 'ping'" class="my-3">
 | 
			
		||||
                                <label for="packet-size" class="form-label">{{ $t("Packet Size") }}</label>
 | 
			
		||||
                                <input id="packet-size" v-model="monitor.packetSize" type="number" class="form-control" required min="1" max="65500" step="1">
 | 
			
		||||
                            </div>
 | 
			
		||||
 | 
			
		||||
                            <!-- HTTP / Keyword only -->
 | 
			
		||||
                            <template v-if="monitor.type === 'http' || monitor.type === 'keyword' || monitor.type === 'grpc-keyword' ">
 | 
			
		||||
                                <div class="my-3">
 | 
			
		||||
@@ -842,6 +848,7 @@ message HealthCheckResponse {
 | 
			
		||||
                    notificationIDList: {},
 | 
			
		||||
                    ignoreTls: false,
 | 
			
		||||
                    upsideDown: false,
 | 
			
		||||
                    packetSize: 56,
 | 
			
		||||
                    expiryNotification: false,
 | 
			
		||||
                    maxredirects: 10,
 | 
			
		||||
                    accepted_statuscodes: [ "200-299" ],
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user