8
n8n 中文网amn8n.com

通过邮件警报自动清理磁盘 - 服务器日志清除工作流

中级

这是一个DevOps领域的自动化工作流,包含 8 个节点。主要使用 Set, Ssh, Code, EmailReadImap 等节点。 通过SSH和邮件警报实现Nginx、Docker和系统日志的自动化清理

前置要求
  • 无特殊前置要求,导入即可使用
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "4Y5VQwUUo7uqPPL1",
  "meta": {
    "instanceId": "dd69efaf8212c74ad206700d104739d3329588a6f3f8381a46a481f34c9cc281",
    "templateCredsSetupCompleted": true
  },
  "name": "通过邮件警报自动清理磁盘 - 服务器日志清除工作流",
  "tags": [],
  "nodes": [
    {
      "id": "8c1aac94-9e6a-4b14-a177-3a00644ea232",
      "name": "检查磁盘警报邮件",
      "type": "n8n-nodes-base.emailReadImap",
      "position": [
        0,
        0
      ],
      "parameters": {
        "options": {
          "customEmailConfig": "[\"UNSEEN\", [\"SUBJECT\", \"disk\"]]"
        }
      },
      "credentials": {
        "imap": {
          "id": "D3nLrSlP1dHWcIDE",
          "name": "IMAP account"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "025c2c15-f693-4b53-8433-4d641526d11a",
      "name": "从邮件提取服务器 IP",
      "type": "n8n-nodes-base.code",
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "const subject = $input.first().json.subject;\n\nif (subject && (subject.toLowerCase().includes(\"disk-utilization\") || subject.toLowerCase().includes(\"disk utilization\"))) {\n  const ipMatch = subject.match(/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):9100/);\n  const server_ip = ipMatch ? ipMatch[1] : '';\n  return [{ json: { server_ip } }];\n} else {\n  // Stop workflow if subject doesn't match\n  return [];\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "723c7067-fe88-4b8b-b1ff-38c9eb662641",
      "name": "准备 SSH 变量",
      "type": "n8n-nodes-base.set",
      "position": [
        440,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "49c16fb1-4b84-46a7-bb50-5e1fa7f59664",
              "name": "pwd",
              "type": "string",
              "value": "password"
            },
            {
              "id": "c16557bd-7ef6-4049-a634-a5b207a92fd6",
              "name": "server_user",
              "type": "string",
              "value": "user"
            },
            {
              "id": "fd8c4253-8ab2-4094-9a72-78ef782d89d3",
              "name": "server_ip",
              "type": "string",
              "value": "={{ $input.first().json.server_ip }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "7ef2b2b6-5880-4fc1-93fa-b76c3c75effa",
      "name": "通过 SSH 运行日志清理命令",
      "type": "n8n-nodes-base.ssh",
      "position": [
        660,
        0
      ],
      "parameters": {
        "command": "=echo '{{ $json.pwd }}' | sudo -S su - jenkins -c \"ssh {{ $json.server_user }}@{{ $json.server_ip }} 'sudo rm -rf /var/log/*.gz && sudo rm -rf /var/log/*.1 && sudo rm -rf /var/log/nginx/*.log && sudo rm -rf /var/log/apache2/*.log && sudo journalctl --vacuum-size=200M && sudo apt-get clean && sudo apt-get autoclean && docker builder prune -af && docker system df && sudo apt-get autoremove -y'\""
      },
      "credentials": {
        "sshPassword": {
          "id": "e5pmM8y0fJOZytlE",
          "name": "Jenkins Server SSH"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0935c618-237c-4630-9087-80b757a78a9c",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -20,
        -140
      ],
      "parameters": {
        "width": 160,
        "height": 300,
        "content": "当收到关于磁盘使用情况的警报邮件时触发"
      },
      "typeVersion": 1
    },
    {
      "id": "d4efc5ab-9003-4d42-a49c-6ac4c376c9cb",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        180,
        -140
      ],
      "parameters": {
        "color": 3,
        "width": 170,
        "height": 300,
        "content": "解析邮件正文以提取目标服务器的 IP 地址"
      },
      "typeVersion": 1
    },
    {
      "id": "adf3341f-4319-40cc-bf97-8917a2bb5c44",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        -140
      ],
      "parameters": {
        "color": 4,
        "width": 170,
        "height": 300,
        "content": "手动设置或映射凭据、路径或其他配置值"
      },
      "typeVersion": 1
    },
    {
      "id": "4c933879-5907-4616-8902-42cfb2d73edc",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        620,
        -140
      ],
      "parameters": {
        "color": 6,
        "width": 170,
        "height": 300,
        "content": "执行清理命令(Nginx、PM2、Docker、系统日志)"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fbb7d480-4bbd-4240-9e95-459b563adba4",
  "connections": {
    "Prepare SSH Variables\t": {
      "main": [
        [
          {
            "node": "Run Log Cleanup Commands via SSH\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Disk Alert Emails\t": {
      "main": [
        [
          {
            "node": "Extract Server IP from Email\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Server IP from Email\t": {
      "main": [
        [
          {
            "node": "Prepare SSH Variables\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 开发运维

需要付费吗?

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

工作流信息
难度等级
中级
节点数量8
分类1
节点类型5
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Oneclick AI Squad

Oneclick AI Squad

@oneclick-ai

The AI Squad Initiative is a pioneering effort to build, automate and scale AI-powered workflows using n8n.io. Our mission is to help individuals and businesses integrate AI agents seamlessly into their daily operations from automating tasks and enhancing productivity to creating innovative, intelligent solutions. We design modular, reusable AI workflow templates that empower creators, developers and teams to supercharge their automation with minimal effort and maximum impact.

外部链接
在 n8n.io 查看

分享此工作流