/** * Make step result UI element. * * @param {array} step Step object returned from server. * * @return {jQuery} */ function makeStepResult(step) { if (typeof step.error !== 'undefined') { return jQuery(new Template(jQuery('#preprocessing-step-error-icon').html()).evaluate( {error: step.error || ')) ?>} )); } else if (typeof step.result === 'undefined' || step.result === null) { return jQuery('', {'class': ''}).text(); } else if (step.result === '') { return jQuery('', {'class': ''}).text(')) ?>); } else if (step.result.indexOf("\n") != -1 || step.result.length > 25) { return jQuery(new Template(jQuery('#preprocessing-step-result').html()).evaluate( jQuery.extend({result: step.result}) )); } else { return jQuery('').text(step.result); } } /** * Disable item test form. */ function disableItemTestForm() { jQuery('#value, #time, [name^=macros]').prop('disabled', true); jQuery('#get_value, #get_value_btn').prop('disabled', true); jQuery('#interface_address').prop('disabled', true); jQuery('#interface_port').prop('disabled', true); jQuery('#proxyid').prop('disabled', true); jQuery('#get_value, #get_value_btn').prop('disabled', true); jQuery('#prev_time').prop('readonly', true); jQuery('#eol input').prop('disabled', true); } /** * Enable item test form. */ function enableItemTestForm() { jQuery('#value, #time, [name^=macros]').prop('disabled', false); jQuery('#get_value, #get_value_btn').prop('disabled', false); jQuery('#interface_address').prop('disabled', false); jQuery('#interface_port').prop('disabled', false); jQuery('#proxyid').prop('disabled', false); jQuery('#get_value, #get_value_btn').prop('disabled', false); if (!jQuery('#get_value').is(':checked')) { jQuery('#prev_value').multilineInput('unsetReadOnly'); jQuery('#prev_time').prop('readonly', false); } jQuery('#eol input').prop('disabled', false); } /** * Clear previous test results. */ function cleanPreviousTestResults() { var $form = jQuery('#preprocessing-test-form'); jQuery('[id^="preproc-test-step-"][id$="-result"]', $form).empty(); jQuery('[id^="preproc-test-step-"][id$="-name"] > div', $form).remove(); jQuery('.js-final-result', $form) .hide() .next() .empty() .hide(); } /** * Send item get value request and display retrieved results. * * @param {object} overlay Overlay dialog object. */ function itemGetValueTest(overlay) { var $body = overlay.$dialogue.$body, $form = overlay.$dialogue.find('form'), form_data = $form.serializeJSON(), post_data = getItemTestProperties('#preprocessing-test-form'), interface = (typeof form_data['interface'] !== 'undefined') ? form_data['interface'] : null, url = new Curl('zabbix.php'); url.setArgument('action', 'popup.itemtest.getvalue'); url.setArgument('', ); post_data = jQuery.extend(post_data, { interface: { address: interface ? interface['address'].trim() : '', port: (interface && interface['port']) ? interface['port'].trim() : '', interfaceid: interface ? interface['interfaceid'] : null, useip: interface ? interface['useip'] : null, details: interface ? interface['details'] : null }, macros: form_data['macros'], proxyid: form_data['proxyid'], test_type: , hostid: , value: form_data['value'] }); post_data['time_change'] = (form_data['upd_prev'] !== '') ? parseInt(form_data['upd_last']) - parseInt(form_data['upd_prev']) : Math.ceil(+new Date() / 1000) - parseInt(form_data['upd_last']); delete post_data.interfaceid; delete post_data.delay; overlay.xhr = jQuery.ajax({ url: url.getUrl(), data: post_data, beforeSend: function() { jQuery('#get_value_btn').blur().addClass('is-loading'); overlay.setLoading(); disableItemTestForm(); cleanPreviousTestResults(); }, complete: function() { jQuery('#get_value_btn').removeClass('is-loading'); enableItemTestForm(); overlay.unsetLoading(); }, success: function(ret) { overlay.$dialogue.find('.msg-bad, .msg-good, .msg-warning').remove(); if ('error' in ret) { const message_box = makeMessageBox('bad', ret.error.messages, ret.error.title); jQuery($body).prepend(message_box); return; } if (typeof ret.prev_value !== 'undefined') { jQuery('#prev_value', $form).multilineInput('value', ret.prev_value); jQuery('#prev_time', $form).val(ret.prev_time); jQuery('#upd_prev', $form).val(form_data['upd_last']); jQuery('#upd_last', $form).val(Math.ceil(+new Date() / 1000)); } jQuery('#value', $form).multilineInput('value', ret.value); if (typeof ret.eol !== 'undefined') { jQuery("input[value=" + ret.eol + "]", jQuery("#eol")).prop("checked", "checked"); } }, dataType: 'json', type: 'post' }); } /** * Send item preprocessing test details and display results in table. * * @param {object} overlay Overlay dialog object. */ function itemCompleteTest(overlay) { var $body = overlay.$dialogue.$body, $form = overlay.$dialogue.find('form'), form_data = $form.serializeJSON(), post_data = getItemTestProperties('#preprocessing-test-form'), interface = (typeof form_data['interface'] !== 'undefined') ? form_data['interface'] : null, url = new Curl('zabbix.php'); url.setArgument('action', 'popup.itemtest.send'); url.setArgument('', ); post_data = jQuery.extend(post_data, { get_value: form_data['get_value'] || 0, steps: form_data['steps'], interface: { address: interface ? interface['address'].trim() : '', port: (interface && interface['port']) ? interface['port'].trim() : '', interfaceid: interface ? interface['interfaceid'] : null, useip: interface ? interface['useip'] : null, details: interface ? interface['details'] : null }, macros: form_data['macros'], proxyid: form_data['proxyid'], show_final_result: , test_type: , hostid: , valuemapid: , value: form_data['value'], not_supported: form_data['not_supported'] }); if (post_data.get_value) { post_data['time_change'] = (form_data['upd_prev'] !== '') ? parseInt(form_data['upd_last']) - parseInt(form_data['upd_prev']) : Math.ceil(+new Date() / 1000) - parseInt(form_data['upd_last']); } post_data = jQuery.extend(post_data, { prev_time: form_data['prev_time'], prev_value: form_data['prev_value'] }); overlay.xhr = jQuery.ajax({ url: url.getUrl(), data: post_data, beforeSend: function() { overlay.setLoading(); disableItemTestForm(); cleanPreviousTestResults(); }, complete: function() { enableItemTestForm(); overlay.unsetLoading(); }, success: function(ret) { overlay.$dialogue.find('.msg-bad, .msg-good, .msg-warning').remove(); if ('error' in ret) { const message_box = makeMessageBox('bad', ret.error.messages, ret.error.title); jQuery($body).prepend(message_box); } processItemPreprocessingTestResults(ret.steps ?? []); if (typeof ret.prev_value !== 'undefined') { jQuery('#prev_value', $form).multilineInput('value', ret.prev_value); jQuery('#prev_time', $form).val(ret.prev_time); jQuery('#upd_prev', $form).val(post_data['upd_last']); jQuery('#upd_last', $form).val(Math.ceil(+new Date() / 1000)); } jQuery('#value', $form).multilineInput('value', ret.value); if (typeof ret.eol !== 'undefined') { jQuery("input[value=" + ret.eol + "]", jQuery("#eol")).prop("checked", "checked"); } if (typeof ret.final !== 'undefined') { var result = makeStepResult(ret.final); if (result !== null) { $result = jQuery(result).css('float', 'right'); } $result_row = jQuery('
', {'class': ''}) .css({whiteSpace: 'normal'}) .append(jQuery('
').append(ret.final.action, $result)) .css({display: 'block', width: '675px'}); if (typeof ret.mapped_value !== 'undefined') { $mapped_value = makeStepResult({result: ret.mapped_value}); $mapped_value.css('float', 'right'); $result_row.append(jQuery('
') .append( jQuery('', {'class': ''}) .text(''), $mapped_value ) ); } jQuery('.js-final-result') .show() .next() .append($result_row) .show(); } }, dataType: 'json', type: 'post' }); return false; } /** * Process test results and make visual changes in test dialog results block. * * @param {array} steps Array of objects containing details about each preprocessing step test results. */ function processItemPreprocessingTestResults(steps) { var tmpl_gray_label = new Template(jQuery('#preprocessing-gray-label').html()), tmpl_act_done = new Template(jQuery('#preprocessing-step-action-done').html()); steps.forEach(function(step, i) { if (typeof step.action !== 'undefined') { switch (step.action) { case : step.action = null; break; case : step.action = jQuery(tmpl_gray_label.evaluate( _('Discard value') ]) ?>)); break; case : step.action = jQuery(tmpl_act_done.evaluate(jQuery.extend( _('Set value to') ]) ?>, {failed: step.result}))); break; case : step.action = jQuery(tmpl_act_done.evaluate(jQuery.extend( _('Set error to') ]) ?>, {failed: step.failed}))); break; } } step.result = makeStepResult(step); if (typeof step.action !== 'undefined' && step.action !== null) { jQuery('#preproc-test-step-' + i + '-name').append(jQuery(tmpl_gray_label.evaluate( _('Custom on fail') ]) ?>))); } jQuery('#preproc-test-step-' + i + '-result').append(step.result, step.action); }); } /** * Collect values from opened item test dialog and save input values for repeated use. */ function saveItemTestInputs() { var $form = jQuery('#preprocessing-test-form'), $test_obj, input_values = { value: jQuery('#value').multilineInput('value'), eol: jQuery('#eol').find(':checked').val() }, form_data = $form.serializeJSON(), interface = (typeof form_data['interface'] !== 'undefined') ? form_data['interface'] : null, macros = {}; input_values = jQuery.extend(input_values, { get_value: jQuery('#get_value', $form).is(':checked') ? 1 : 0, proxyid: jQuery('#proxyid', $form).val(), interfaceid: || 0, address: jQuery('#interface_address', $form).val(), port: jQuery('#interface_port', $form).val(), interface_details: (interface && 'details' in interface) ? interface['details'] : null }); input_values = jQuery.extend(input_values, { prev_value: jQuery('#prev_value').multilineInput('value'), prev_time: jQuery('#prev_time').val() }); jQuery('[name^=macros]').each(function(i, macro) { var name = macro.name.toString(); macros[name.substr(7, name.length - 8)] = macro.value; }); input_values.macros = macros; $test_obj = jQuery('.tfoot-buttons'); $test_obj = jQuery('.preprocessing-list-foot', jQuery('#preprocessing')); $test_obj = jQuery('.preprocessing-list-item[data-step=]', jQuery('#preprocessing')); $test_obj.data('test-data', input_values); } jQuery(document).ready(function($) { $('.js-final-result').hide().next().hide(); jQuery('#upd_last').val(Math.ceil(+new Date() / 1000)); $('#value').multilineInput({ placeholder: , value: , monospace_font: false, autofocus: true, readonly: false, grow: 'auto', rows: 0 }); $('#prev_value').multilineInput({ placeholder: , value: , monospace_font: false, disabled: , grow: 'auto', rows: 0 }); $('#not_supported').on('change', function() { var $form = $('#preprocessing-test-form'); if ($(this).is(':checked')) { $('#value', $form).multilineInput('setReadOnly'); } else { $('#value', $form).multilineInput('unsetReadOnly'); } }); $('#get_value').on('change', function() { var $rows = $('.js-host-address-row, .js-proxy-hostid-row, .js-get-value-row, [class*=js-popup-row-snmp]'), $form = $('#preprocessing-test-form'), $submit_btn = overlays_stack.getById('item-test').$btn_submit, $not_supported = $('#not_supported', $form); if ($(this).is(':checked')) { $('#value', $form).multilineInput('setReadOnly'); $not_supported.prop('disabled', true); $('#prev_value', $form).multilineInput('setReadOnly'); $('#prev_time', $form).prop('readonly', true); $('#proxyid').prop('disabled', false); $('#interface_address').prop('disabled', false); $('#interface_port').prop('disabled', false); $submit_btn.html(''); $rows.show(); $('#interface_details_version').on('change', function (e) { $(`.js-popup-row-snmp-community, .js-popup-row-snmp-max-repetition, .js-popup-row-snmpv3-contextname, .js-popup-row-snmpv3-securityname, .js-popup-row-snmpv3-securitylevel, .js-popup-row-snmpv3-authprotocol, .js-popup-row-snmpv3-authpassphrase, .js-popup-row-snmpv3-privprotocol, .js-popup-row-snmpv3-privpassphrase`).hide(); switch (e.target.value) { case '': $('#interface_details_securitylevel').off('change'); $('.js-popup-row-snmp-community').show(); break; case '': $('#interface_details_securitylevel').off('change'); $('.js-popup-row-snmp-community').show(); $('.js-popup-row-snmp-max-repetition').show(); break; case '': $(`.js-popup-row-snmpv3-contextname, .js-popup-row-snmpv3-securityname, .js-popup-row-snmpv3-securitylevel, .js-popup-row-snmp-max-repetition`).show(); $('#interface_details_securitylevel').on('change', function (e) { $(`.js-popup-row-snmpv3-authprotocol, .js-popup-row-snmpv3-authpassphrase, .js-popup-row-snmpv3-privprotocol, .js-popup-row-snmpv3-privpassphrase`).hide(); switch (e.target.value) { case '': $(`.js-popup-row-snmpv3-authprotocol, .js-popup-row-snmpv3-authpassphrase`) .show(); break; case '': $(`.js-popup-row-snmpv3-authprotocol, .js-popup-row-snmpv3-authpassphrase, .js-popup-row-snmpv3-privprotocol, .js-popup-row-snmpv3-privpassphrase`) .show(); break; } overlays_stack.end().centerDialog(); }).trigger('change'); break; } overlays_stack.end().centerDialog(); }).trigger('change'); } else { !$not_supported.is(':checked') && $('#value', $form).multilineInput('unsetReadOnly'); $not_supported.prop('disabled', false); $('#prev_value', $form).multilineInput('unsetReadOnly'); $('#prev_time', $form).prop('readonly', false); $('#proxyid').prop('disabled', true); $('#interface_address').prop('disabled', false); $('#interface_port').prop('disabled', false); $submit_btn.html(''); $rows.hide(); } }).trigger('change'); $('#get_value_btn').on('click', function() { itemGetValueTest(overlays_stack.getById('item-test')); }); $('#preprocessing-test-form .').textareaFlexible(); });