Ad Widget

Collapse

I'm receiving the duplicated message in MS Teams.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yoori.H
    Junior Member
    • Jul 2023
    • 11

    #1

    I'm receiving the duplicated message in MS Teams.

    - Zabbix version: v6.0.0

    Hello. We configured to receive Zabbix notifications in MS Teams.
    We're receiving the notifications well, but the notifications is always duplicated.

    Click image for larger version

Name:	dataurl728323.jpg
Views:	354
Size:	57.1 KB
ID:	469798

    I found the same issue on the Zabbix Forum page.


    I modified the script according to the content of this site, but the issue was the same.
    Our zabbix version is 6.0.0 and I changed this part.

    Click image for larger version

Name:	dataurl728324.jpg
Views:	184
Size:	57.3 KB
ID:	469799

    Does anyone know a way to solve it?
    I shared AS-IS / TO-BE script though.​

    ################################################## #######
    [AS-IS Script]
    var SEVERITY_COLORS = [
    '#97AAB3', // Not classified.
    '#7499FF', // Information.
    '#FFC859', // Warning.
    '#FFA059', // Average.
    '#E97659', // High.
    '#E45959', // Disaster.
    '#009900', // Resolved.
    '#000000' // Default.
    ];
    try {
    var params = JSON.parse(value);
    if (typeof params.teams_endpoint !== 'string' || params.teams_endpoint.trim() === '') {
    throw 'Cannot get teams_endpoint';
    }
    else if (!params.teams_endpoint.startsWith('http')) {
    throw 'Invalid MS Teams webhook URL: ' + params.teams_endpoint;
    }
    params.zabbix_url = (params.zabbix_url.endsWith('/'))
    ? params.zabbix_url.slice(0, -1) : params.zabbix_url;
    if ([0, 1, 2, 3, 4].indexOf(parseInt(params.event_source)) === -1) {
    throw 'Incorrect "event_source" parameter given: "' + params.event_source + '".\nMust be 0-4.';
    }
    // Set "use_default_message" to true for non trigger-based events.
    if (params.event_source !== '0') {
    params.use_default_message = 'true';
    }
    // Check {EVENT.VALUE} for trigger-based and internal events.
    if (params.event_value !== '0' && params.event_value !== '1'
    && (params.event_source === '0' || params.event_source === '3')) {
    throw 'Incorrect "event_value" parameter given: "' + params.event_value + '".\nMust be 0 or 1.';
    }
    // Check {EVENT.UPDATE.STATUS} only for trigger-based events.
    if (params.event_update_status !== '0' && params.event_update_status !== '1' && params.event_source === '0') {
    throw 'Incorrect "event_update_status" parameter given: "' + params.event_update_status + '".\nMust be 0 or 1.';
    }
    if (params.event_value == 0) {
    params.event_nseverity = '6';
    }
    if (!SEVERITY_COLORS[params.event_nseverity]) {
    params.event_nseverity = '7';
    }
    var request = new HttpRequest(),
    facts = [],
    body = {
    themeColor: SEVERITY_COLORS[params.event_nseverity].replace('#', ''),
    summary: params.alert_subject,
    sections: [
    {
    markdown: 'false',
    activityTitle: params.alert_subject,
    text: (params.use_default_message.toLowerCase() == 'true')
    ? params.alert_message
    : params.trigger_description
    }
    ],
    potentialAction: [
    {
    '@type': 'OpenUri',
    name: (params.event_source === '0')
    ? 'Event Info'
    : 'Zabbix Home',
    targets: [
    {
    os: 'default',
    uri: (params.event_source === '0')
    ? params.zabbix_url + '/tr_events.php?triggerid=' +
    params.trigger_id + '&eventid=' + params.event_id
    : params.zabbix_url
    }
    ]
    }
    ]
    };
    if (params.use_default_message.toLowerCase() !== 'true') {
    // Problem message.
    if (params.event_value === '1' && params.event_update_status === '0') {
    facts.push({
    name: 'Event time',
    value: params.event_time + ' ' + params.event_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    // Update message.
    else if (params.event_update_status === '1') {
    body.sections[0].text = params.event_update_user + ' ' + params.event_update_action + '.';
    if (params.event_update_message) {
    body.sections[0].text += '<br>Message:<br>' + params.event_update_message;
    }
    facts.push({
    name: 'Event update time',
    value: params.event_update_time + ' ' + params.event_update_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    // Resolved message.
    else {
    facts.push({
    name: 'Recovery time',
    value: params.event_recovery_time + ' ' + params.event_recovery_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    if (params.event_severity && params.event_severity !== '{EVENT.SEVERITY}') {
    facts.push({
    name: 'Severity',
    value: params.event_severity
    });
    }
    if (params.event_opdata && params.event_opdata !== '{EVENT.OPDATA}') {
    facts.push({
    name: 'Operational data',
    value: params.event_opdata
    });
    }
    if (params.event_tags && params.event_tags !== '{EVENT.TAGS}') {
    facts.push({
    name: 'Event tags',
    value: params.event_tags
    });
    }
    Object.keys(params)
    .forEach(function (key) {
    if (key.startsWith('fact_') && params[key] !== '') {
    facts.push({
    name: key.substring(5),
    value: params[key]
    });
    }
    else if (key.startsWith('openUri_') && params[key] !== '' && !params[key].startsWith('{')) {
    body.potentialAction.push({
    '@type': 'OpenUri',
    name: key.substring(8),
    targets: [
    {
    os: 'default',
    uri: params[key]
    }
    ]
    });
    }
    });
    body.sections[0].facts = facts;
    }
    body.sections[0].text = body.sections[0].text.replace(/(?:\r\n|\r|\n)/g, '<br>');
    request.addHeader('Content-Type: application/json');
    if (typeof params.HTTPProxy === 'string' && params.HTTPProxy !== '') {
    request.setProxy(params.HTTPProxy);
    }
    Zabbix.log(4, '[ MS Teams Webhook ] JSON: ' + JSON.stringify(body));
    var response = request.post(params.teams_endpoint, JSON.stringify(body));
    Zabbix.log(4, '[ MS Teams Webhook ] Response: ' + response);
    if (response === '1') {
    return 'OK';
    }
    else {
    Zabbix.log(4, '[ MS Teams Webhook ] FAILED with response: ' + response);
    throw response;
    }
    }
    catch (error) {
    Zabbix.log(3, '[ MS Teams Webhook ] ERROR: ' + error);
    throw 'Sending failed: ' + error;
    }

    ################################################## #######################
    [TO-BE Script]
    var SEVERITY_COLORS = [
    '#97AAB3', // Not classified.
    '#7499FF', // Information.
    '#FFC859', // Warning.
    '#FFA059', // Average.
    '#E97659', // High.
    '#E45959', // Disaster.
    '#009900', // Resolved.
    '#000000' // Default.
    ];
    try {
    var params = JSON.parse(value);
    if (typeof params.teams_endpoint !== 'string' || params.teams_endpoint.trim() === '') {
    throw 'Cannot get teams_endpoint';
    }
    else if (!params.teams_endpoint.startsWith('http')) {
    throw 'Invalid MS Teams webhook URL: ' + params.teams_endpoint;
    }
    params.zabbix_url = (params.zabbix_url.endsWith('/'))
    ? params.zabbix_url.slice(0, -1) : params.zabbix_url;
    if ([0, 1, 2, 3, 4].indexOf(parseInt(params.event_source)) === -1) {
    throw 'Incorrect "event_source" parameter given: "' + params.event_source + '".\nMust be 0-4.';
    }
    // Set "use_default_message" to true for non trigger-based events.
    if (params.event_source !== '0') {
    params.use_default_message = 'true';
    }
    // Check {EVENT.VALUE} for trigger-based and internal events.
    if (params.event_value !== '0' && params.event_value !== '1'
    && (params.event_source === '0' || params.event_source === '3')) {
    throw 'Incorrect "event_value" parameter given: "' + params.event_value + '".\nMust be 0 or 1.';
    }
    // Check {EVENT.UPDATE.STATUS} only for trigger-based events.
    if (params.event_update_status !== '0' && params.event_update_status !== '1' && params.event_source === '0') {
    throw 'Incorrect "event_update_status" parameter given: "' + params.event_update_status + '".\nMust be 0 or 1.';
    }
    if (params.event_value == 0) {
    params.event_nseverity = '6';
    }
    if (!SEVERITY_COLORS[params.event_nseverity]) {
    params.event_nseverity = '7';
    }
    var request = new HttpRequest(),
    facts = [],
    body = {
    themeColor: SEVERITY_COLORS[params.event_nseverity].replace('#', ''),
    summary: params.alert_subject,
    sections: [
    {
    markdown: 'false',
    activityTitle: params.alert_subject,
    text: (params.use_default_message.toLowerCase() == 'true')
    ? params.alert_message
    : params.trigger_description
    }
    ],
    potentialAction: [
    {
    '@type': 'OpenUri',
    name: (params.event_source === '0')
    ? 'Event Info'
    : 'Zabbix Home',
    targets: [
    {
    os: 'default',
    uri: (params.event_source === '0')
    ? params.zabbix_url + '/tr_events.php?triggerid=' +
    params.trigger_id + '&eventid=' + params.event_id
    : params.zabbix_url
    }
    ]
    }
    ]
    };
    if (params.use_default_message.toLowerCase() !== 'true') {
    // Problem message.
    if (params.event_value === '1' && params.event_update_status === '0') {
    facts.push({
    name: 'Event time',
    value: params.event_time + ' ' + params.event_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    // Update message.
    else if (params.event_update_status === '1') {
    body.sections[0].text = params.event_update_user + ' ' + params.event_update_action + '.';
    if (params.event_update_message) {
    body.sections[0].text += '<br>Message:<br>' + params.event_update_message;
    }
    facts.push({
    name: 'Event update time',
    value: params.event_update_time + ' ' + params.event_update_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    // Resolved message.
    else {
    facts.push({
    name: 'Recovery time',
    value: params.event_recovery_time + ' ' + params.event_recovery_date
    });
    facts.push({
    name: 'Host',
    value: params.host_name + ' [' + params.host_ip + ']'
    });
    }
    if (params.event_severity && params.event_severity !== '{EVENT.SEVERITY}') {
    facts.push({
    name: 'Severity',
    value: params.event_severity
    });
    }
    if (params.event_opdata && params.event_opdata !== '{EVENT.OPDATA}') {
    facts.push({
    name: 'Operational data',
    value: params.event_opdata
    });
    }
    if (params.event_tags && params.event_tags !== '{EVENT.TAGS}') {
    facts.push({
    name: 'Event tags',
    value: params.event_tags
    });
    }
    Object.keys(params)
    .forEach(function (key) {
    if (key.startsWith('fact_') && params[key] !== '') {
    facts.push({
    name: key.substring(5),
    value: params[key]
    });
    }
    else if (key.startsWith('openUri_') && params[key] !== '' && !params[key].startsWith('{')) {
    body.potentialAction.push({
    '@type': 'OpenUri',
    name: key.substring(8),
    targets: [
    {
    os: 'default',
    uri: params[key]
    }
    ]
    });
    }
    });
    body.sections[0].facts = facts;
    }
    body.sections[0].text = body.sections[0].text.replace(/(?:\r\n|\r|\n)/g, '<br>');
    request.addHeader('Content-Type: application/json');
    if (typeof params.HTTPProxy === 'string' && params.HTTPProxy !== '') {
    request.setProxy(params.HTTPProxy);
    }
    Zabbix.log(4, '[ MS Teams Webhook ] JSON: ' + JSON.stringify(body));
    var response = request.post(params.teams_endpoint, JSON.stringify(body));
    Zabbix.log(4, '[ MS Teams Webhook ] Response: ' + response);
    if (response === '1') {
    return 'OK';
    }
    else {
    Zabbix.log(4, '[ MS Teams Webhook ] FAILED with response: ' + response);
    if (request.getStatus() != 200) {
    throw response;
    }
    return 'OK';
    }
    }
    catch (error) {
    Zabbix.log(3, '[ MS Teams Webhook ] ERROR: ' + error);
    Zabbix.log(3, '[ MS Teams Webhook ] FAILED with response: ' + error);
    throw 'Sending failed: ' + error;
    }​
Working...