8
n8n 中文网amn8n.com

Meraki丢包和延迟告警至Microsoft Teams

高级

这是一个IT Ops, SecOps领域的自动化工作流,包含 23 个节点。主要使用 Set, Code, Merge, Redis, HttpRequest 等节点。 Meraki丢包和延迟告警推送至Microsoft Teams

前置要求
  • Redis 服务器连接信息
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "257476b1ef58bf3cb6a46e65fac7ee34a53a5e1a8492d5c6e4da5f87c9b82833",
    "templateId": "2054"
  },
  "nodes": [
    {
      "id": "3b18d784-eded-4b74-ac44-b25565049e13",
      "name": "当点击\"执行工作流\"时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        380,
        400
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "96eadd32-17c2-44b0-a00b-8e2ddcecaafa",
      "name": "获取 Meraki 组织",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "This node uses an API call to Meraki using the URL https://api.meraki.com/api/v1/organizations\n\nthe Authorization header is what is used to authenticate. You also have to set it to accept json",
      "position": [
        620,
        320
      ],
      "parameters": {
        "url": "https://api.meraki.com/api/v1/organizations",
        "options": {
          "redirect": {
            "redirect": {}
          }
        },
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "12",
          "name": "Header Auth account"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "444071ea-a364-45a6-9430-0338d2752b18",
      "name": "获取网络 ID",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "This node uses a URL with an expression in the middle to do an API call for each ORG ID that was pulled to pull all the network's for each org",
      "position": [
        1020,
        240
      ],
      "parameters": {
        "url": "=https://api.meraki.com/api/v1/organizations/{{ $json.OrgID }}/networks  ",
        "options": {
          "redirect": {
            "redirect": {}
          }
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization"
            },
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "42deea02-e2d2-4ba6-97f7-698c219715a5",
      "name": "获取组织名称和 ID",
      "type": "n8n-nodes-base.set",
      "notes": "This takes the output data from the previous node and changes the variables to better suit what we'll be using. ",
      "position": [
        840,
        240
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "CompanyName",
              "stringValue": "={{ $json.name }}"
            },
            {
              "name": "OrgID",
              "stringValue": "={{ $json.id }}"
            }
          ]
        },
        "include": "selected",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "3d5ce6a8-bf05-4457-88e6-c5ad5a995b1d",
      "name": "将延迟与其对应的网络合并",
      "type": "n8n-nodes-base.merge",
      "notes": "This node matches on the NetworkID field, so that the networks we pulled earlier and the Loss / Latency can be combined into one dataset",
      "position": [
        1500,
        400
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "joinMode": "enrichInput1",
        "mergeByFields": {
          "values": [
            {
              "field1": "NetworkID",
              "field2": "networkId"
            }
          ]
        }
      },
      "notesInFlow": false,
      "typeVersion": 2.1
    },
    {
      "id": "caafd6dd-a2b2-405d-a078-cea3bb615788",
      "name": "使延迟和丢包可筛选",
      "type": "n8n-nodes-base.set",
      "notes": "Like before, This takes the output data from the previous node and changes the variables to better suit what we'll be using. ",
      "position": [
        1680,
        400
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "networkId",
              "stringValue": "={{ $json.networkId }}"
            },
            {
              "name": "NetworkName",
              "stringValue": "={{ $json.NetworkName }}"
            },
            {
              "name": "networkURL",
              "stringValue": "={{ $json.networkURL }}"
            },
            {
              "name": "Serial",
              "stringValue": "={{ $json.serial }}"
            },
            {
              "name": "TS0-Loss",
              "stringValue": "={{ $json.timeSeries[0].lossPercent }}"
            },
            {
              "name": "TS1-Loss",
              "stringValue": "={{ $json.timeSeries[1].lossPercent }}"
            },
            {
              "name": "TS2-Loss",
              "stringValue": "={{ $json.timeSeries[2].lossPercent }}"
            },
            {
              "name": "TS3-Loss",
              "stringValue": "={{ $json.timeSeries[3].lossPercent }}"
            },
            {
              "name": "TS4-Loss",
              "stringValue": "={{ $json.timeSeries[4].lossPercent }}"
            },
            {
              "name": "TS0-Latency",
              "stringValue": "={{ $json.timeSeries[0].latencyMs }}"
            },
            {
              "name": "TS1-Latency",
              "stringValue": "={{ $json.timeSeries[1].latencyMs }}"
            },
            {
              "name": "TS2-Latency",
              "stringValue": "={{ $json.timeSeries[2].latencyMs }}"
            },
            {
              "name": "TS3-Latency",
              "stringValue": "={{ $json.timeSeries[3].latencyMs }}"
            },
            {
              "name": "TS4-Latency",
              "stringValue": "={{ $json.timeSeries[4].latencyMs }}"
            }
          ]
        },
        "include": "selected",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "c695fd89-c884-4794-b0e5-51b91a71ac5c",
      "name": "筛选有问题的站点",
      "type": "n8n-nodes-base.code",
      "notes": "This node uses JavaScript to look at the calculated averages and if they pass the threshold for 300ms Latency or 2% Loss it will pass that site info forward",
      "position": [
        2040,
        400
      ],
      "parameters": {
        "jsCode": "// Function to filter items based on averageLatency and averageLoss\nfunction filterItems(items) {\n  return items.filter(item =>\n    item.AverageLatency >300 || item.AverageLoss > 2\n  );\n}\n\n// Get the input items from the previous node\nconst inputItems = items.map(item => item.json); // Adjust based on your actual data structure\n\n// Filter the items based on the conditions\nconst filteredItems = filterItems(inputItems);\n\n// Return the filtered items to the workflow\nreturn filteredItems.map(item => {\n  return { json: item }; // Format each filtered item as JSON\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "6db219fd-f54d-4bf8-b150-9c4f3069cf92",
      "name": "5 分钟内的平均延迟和丢包率",
      "type": "n8n-nodes-base.code",
      "notes": "This node uses JavaScript to calculate the average over the last 5 entries of packet loss and latency",
      "position": [
        1860,
        400
      ],
      "parameters": {
        "jsCode": "// Assuming $input.all() is an array of items and each item has a json property\nfunction calculateAverages(inputItems) {\n  return inputItems.map(item => {\n    // Calculate total and average loss\n    const totalLoss = \n      parseFloat(item.json['TS0-Loss']) +\n      parseFloat(item.json['TS1-Loss']) +\n      parseFloat(item.json['TS2-Loss']) +\n      parseFloat(item.json['TS3-Loss']) +\n      parseFloat(item.json['TS4-Loss']);\n    const averageLoss = totalLoss / 5;\n    item.json['AverageLoss'] = averageLoss;\n\n    // Calculate total and average latency\n    const totalLatency = \n      parseFloat(item.json['TS0-Latency']) +\n      parseFloat(item.json['TS1-Latency']) +\n      parseFloat(item.json['TS2-Latency']) +\n      parseFloat(item.json['TS3-Latency']) +\n      parseFloat(item.json['TS4-Latency']);\n    const averageLatency = totalLatency / 5;\n    item.json['AverageLatency'] = averageLatency;\n\n    // Return the modified item\n    return item;\n  });\n}\n\nreturn calculateAverages($input.all());\n"
      },
      "typeVersion": 2
    },
    {
      "id": "f3831843-2596-492d-b800-7d349e443293",
      "name": "获取上行链路丢包和延迟",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "This uses a URL with an expression in the middle so that for each org ID it will pull the Loss and Latency for their uplinks. ",
      "position": [
        840,
        400
      ],
      "parameters": {
        "url": "=https://api.meraki.com/api/v1/organizations/{{ $json.id }}/devices/uplinksLossAndLatency?timespan=300",
        "options": {
          "redirect": {
            "redirect": {}
          }
        },
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "12",
          "name": "Header Auth account"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "87c3f32c-de4f-48fd-a711-3521a008c245",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "notes": "schedules the workflow to run every 5 minutes mon-fri 8am-5pm",
      "position": [
        380,
        240
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/5 8-16 * * 1-5"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "572adb5c-6745-4b4e-89d8-a97835c3d486",
      "name": "设置网络变量",
      "type": "n8n-nodes-base.set",
      "notes": "Like before, This takes the output data from the previous node and changes the variables to better suit what we'll be using. ",
      "position": [
        1220,
        240
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "NetworkID",
              "stringValue": "={{ $json.id }}"
            },
            {
              "name": "NetworkName",
              "stringValue": "={{ $json.name }}"
            },
            {
              "name": "networkURL",
              "stringValue": "={{ $json.url }}"
            }
          ]
        },
        "include": "selected",
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "01717ed3-1012-4eda-9da9-567038132e06",
      "name": "合并",
      "type": "n8n-nodes-base.merge",
      "notes": "This looks at the problematic sites as well as the info from the database. It will pass on all non-matching as if the site name matches with the database then that means we have an open alert for that site already.",
      "position": [
        2720,
        300
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "joinMode": "keepNonMatches",
        "mergeByFields": {
          "values": [
            {
              "field1": "NetworkName",
              "field2": "NetworkName"
            }
          ]
        },
        "outputDataFrom": "input2"
      },
      "typeVersion": 2.1
    },
    {
      "id": "a20f6f7c-0c92-4db9-903b-e322d67b536d",
      "name": "检查警报是否存在",
      "type": "n8n-nodes-base.redis",
      "notes": "This node Looks to see if the alert already exists in the Redis database. If it does exist it won't alert us in Teams",
      "position": [
        2300,
        240
      ],
      "parameters": {
        "key": "={{ $json.NetworkName }}",
        "options": {
          "dotNotation": "={{ true }}"
        },
        "operation": "get",
        "propertyName": "NetworkName"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "63590446-2938-443a-adbe-bfc0ba89008c",
      "name": "创建响应",
      "type": "n8n-nodes-base.code",
      "notes": "If the alert isn't in the database all the Redis will respond with is \"null\" \n\nother n8n nodes see null as (no data) and show blank which is fair. So I made this node to look at the null responses and make a response of \"false\" for a \"alertExists\" variable. that way we have something to filter on",
      "position": [
        2500,
        240
      ],
      "parameters": {
        "jsCode": "return items.map(item => {\n  // Check if the 'NetworkName' property is not null, indicating an alert exists.\n  // If 'NetworkName' is null, no alert exists for this network.\n  const alertExists = item.json.NetworkName !== null;\n\n  // Set the alertExists property correctly based on the condition.\n  item.json.alertExists = alertExists;\n\n  return item;\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "c63e2a45-bf5e-455f-8012-9868d55aa3e2",
      "name": "通知技术人员",
      "type": "n8n-nodes-base.microsoftTeams",
      "notes": "sends an alert to Dispatch with info related to the site.",
      "position": [
        2920,
        300
      ],
      "parameters": {
        "chatId": "19:bfd41d9621e544c88ae9f2f275e373b5@thread.v2",
        "message": "=<strong>Loss & Latency Alert</strong> <br><br>\n<strong>Network Name:</strong> <a href=\"{{ $json.networkURL }}\">{{ $json.NetworkName }}</a> <br>\n<strong>Average Loss:</strong> {{ $json.AverageLoss }}% <br>\n<strong>Average Latency:</strong> {{ $json.AverageLatency }} <br> ",
        "options": {},
        "resource": "chatMessage"
      },
      "typeVersion": 1.1
    },
    {
      "id": "b4ae54cd-8345-4611-9fbe-32e7537855a9",
      "name": "记录警报",
      "type": "n8n-nodes-base.redis",
      "notes": "Logs the alert and sets the TTL to 3h, after 3h Redis will delete the entry and if the site is still having issues, the next run of the workflow will notify us again",
      "position": [
        3120,
        300
      ],
      "parameters": {
        "key": "={{ $('Merge').item.json.NetworkName }}",
        "ttl": 10800,
        "value": "={{ $('Merge').item.json.NetworkName }}",
        "expire": true,
        "keyType": "string",
        "operation": "set"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "05416de5-a0c0-4ff8-bfc5-3d26f0d88139",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        573.394087003982,
        143.69977924944794
      ],
      "parameters": {
        "width": 791.5865288559442,
        "height": 462.84878343542437,
        "content": "## 拉取信息"
      },
      "typeVersion": 1
    },
    {
      "id": "aeb1c8a6-3cec-4899-83e2-098d0b1a9703",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1464,
        211
      ],
      "parameters": {
        "width": 688.5000872281419,
        "height": 411.1258278145692,
        "content": "## 处理数据"
      },
      "typeVersion": 1
    },
    {
      "id": "9ca9e89b-61ce-4b3f-af8e-bf03daff6710",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2260,
        60
      ],
      "parameters": {
        "width": 1015.6997792494475,
        "height": 614.8167770419421,
        "content": "## 通知"
      },
      "typeVersion": 1
    },
    {
      "id": "801879be-e83b-4c74-b8ac-b20157ca32d1",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        600,
        640
      ],
      "parameters": {
        "width": 673.6064168725538,
        "height": 394.26386951839356,
        "content": "## 说明"
      },
      "typeVersion": 1
    },
    {
      "id": "2b028326-0776-4b9b-bdf2-d79949809092",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1480,
        660
      ],
      "parameters": {
        "width": 645.9603701592033,
        "height": 389.89870424786454,
        "content": "## 说明"
      },
      "typeVersion": 1
    },
    {
      "id": "f7c1448d-3a03-4196-bde6-a1aea92f28ad",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2300,
        700
      ],
      "parameters": {
        "width": 913.6905067516504,
        "height": 523.763772544089,
        "content": "## 说明"
      },
      "typeVersion": 1
    },
    {
      "id": "4f4d40ef-8e7d-420a-8273-4cfefd3af6e9",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1460,
        -320
      ],
      "parameters": {
        "width": 670.6963066922013,
        "height": 366.61782280504275,
        "content": "## 其他用例"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Message Techs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message Techs": {
      "main": [
        [
          {
            "node": "Log the Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Response": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Network IDs": {
      "main": [
        [
          {
            "node": "Sets Network Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Meraki Organizations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Org Name & ID": {
      "main": [
        [
          {
            "node": "Get Network IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if Alert Exists": {
      "main": [
        [
          {
            "node": "Create Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sets Network Variables": {
      "main": [
        [
          {
            "node": "Combine latency to its respective Network",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Meraki Organizations": {
      "main": [
        [
          {
            "node": "Get Org Name & ID",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Uplink Loss and Latency",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filters Problematic sites": {
      "main": [
        [
          {
            "node": "Check if Alert Exists",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Get Uplink Loss and Latency": {
      "main": [
        [
          {
            "node": "Combine latency to its respective Network",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Average Latency & Loss over 5m": {
      "main": [
        [
          {
            "node": "Filters Problematic sites",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \"Execute Workflow\"": {
      "main": [
        [
          {
            "node": "Get Meraki Organizations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Makes Latency and Loss Filterable": {
      "main": [
        [
          {
            "node": "Average Latency & Loss over 5m",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine latency to its respective Network": {
      "main": [
        [
          {
            "node": "Makes Latency and Loss Filterable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - IT 运维, 安全运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量23
分类2
节点类型9
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流