UNPKG

zabbix-utils

Version:

TypeScript port of zabbix-utils - Python library for working with Zabbix API, Sender, and Getter protocols

165 lines 6.8 kB
"use strict"; // zabbix_utils // // Copyright (C) 2001-2023 Zabbix SIA (Original Python library) // Copyright (C) 2024-2025 Han Yong Lim <hanyong.lim@gmail.com> (TypeScript adaptation) // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, // merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software // is furnished to do so, subject to the following conditions: var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Getter = void 0; // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. const net = __importStar(require("net")); const common_1 = require("./common"); const exceptions_1 = require("./exceptions"); const types_1 = require("./types"); class Getter { constructor(options = {}) { const { host = '127.0.0.1', port = 10050, timeout = 10, useIpv6 = false, sourceIp, socketWrapper } = options; this.host = host; this.port = port; this.timeout = timeout * 1000; // Convert to milliseconds this.useIpv6 = useIpv6; this.sourceIp = sourceIp; if (socketWrapper && typeof socketWrapper !== 'function') { throw new TypeError('Value "socketWrapper" should be a function.'); } this.socketWrapper = socketWrapper; } async getResponse(conn) { const result = await common_1.ZabbixProtocol.parseSyncPacket(conn, console, exceptions_1.ProcessingError); console.debug('Received data:', result); return result; } /** * Gets item value from Zabbix agent by specified key. * * @param key - Zabbix item key. * @returns Value from Zabbix agent for specified key. */ async get(key) { const packet = common_1.ZabbixProtocol.createPacket(key, console); let connection; try { connection = new net.Socket(); } catch (error) { throw new exceptions_1.ProcessingError(`Error creating socket for ${this.host}:${this.port}`); } connection.setTimeout(this.timeout); try { await new Promise((resolve, reject) => { const onConnect = () => { connection.removeListener('error', onError); connection.removeListener('timeout', onTimeout); resolve(); }; const onError = (err) => { connection.removeListener('connect', onConnect); connection.removeListener('timeout', onTimeout); reject(err); }; const onTimeout = () => { connection.removeListener('connect', onConnect); connection.removeListener('error', onError); reject(new Error(`Connection timeout after ${this.timeout}ms`)); }; connection.once('connect', onConnect); connection.once('error', onError); connection.once('timeout', onTimeout); const connectOptions = { host: this.host, port: this.port, family: this.useIpv6 ? 6 : 4 }; if (this.sourceIp) { connectOptions.localAddress = this.sourceIp; } connection.connect(connectOptions); }); if (this.socketWrapper) { connection = this.socketWrapper(connection); } await new Promise((resolve, reject) => { connection.write(packet, (err) => { if (err) reject(err); else resolve(); }); }); } catch (error) { console.error(`An error occurred while trying to connect/send to ${this.host}:${this.port}:`, error); connection.destroy(); throw error; } let response; try { response = await this.getResponse(connection); } catch (error) { console.debug('Get value error:', error); console.warn('Check access restrictions in Zabbix agent configuration.'); connection.destroy(); throw error; } console.debug(`Response from [${this.host}:${this.port}]:`, response); try { connection.destroy(); } catch (error) { // Ignore socket close errors } return new types_1.AgentResponse(response); } } exports.Getter = Getter; //# sourceMappingURL=getter.js.map