{
  "name": "MZPanel: resource-cap alert -> owner",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mzpanel-events",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "227f643c-dfb1-5784-8ef1-189cbd6dd56c",
      "name": "MZPanel webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "webhookId": "7e05c358-b1eb-52c2-848c-305e2b2f6595"
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "1d77a7fb-daae-5443-9976-4bdcbe98d321",
              "name": "webhookSecret",
              "value": "PASTE-THE-ENDPOINT-SECRET",
              "type": "string"
            },
            {
              "id": "eb1653bc-557e-5c5e-9d56-f8e2730785ee",
              "name": "ownerEmail",
              "value": "owner@example.com",
              "type": "string"
            },
            {
              "id": "6274cf70-3ff2-573e-907b-30c731a9cd05",
              "name": "fromEmail",
              "value": "alerts@example.com",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "fd71e4d2-ec17-598a-8683-369dc645d199",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Verify the MZPanel webhook signature over the RAW body:\n// X-MZ-Signature-V2 = \"sha256=\" + hex(HMAC-SHA256(secret, X-MZ-Timestamp + \".\" + rawBody)).\n// Needs NODE_FUNCTION_ALLOW_BUILTIN=crypto on the n8n instance.\nconst crypto = require('crypto');\nconst s = $('Settings').first().json;\nconst item = $input.first();\nconst h = item.json.headers || {};\nconst raw = Buffer.from(item.binary.data.data, 'base64').toString('utf8');\nconst ts = h['x-mz-timestamp'] || '';\nif (Math.abs(Date.now() / 1000 - Number(ts)) > 300) throw new Error('stale or missing X-MZ-Timestamp');\nconst want = 'sha256=' + crypto.createHmac('sha256', s.webhookSecret).update(ts + '.' + raw).digest('hex');\nconst got = String(h['x-mz-signature-v2'] || '');\nif (got.length !== want.length || !crypto.timingSafeEqual(Buffer.from(got), Buffer.from(want))) throw new Error('bad signature');\nconst evt = JSON.parse(raw);\nif (evt.event !== 'resource.cap') return [];\nconst d = evt.data || {};\nreturn [{ json: {\n  subject: '[MZPanel] ' + d.domain + ' hit its ' + (d.resource || d.kind || 'resource') + ' cap',\n  text: 'Site ' + d.domain + ' on server ' + d.server_id + ': ' + (d.kind || '') + ' ' + (d.resource || '') +\n        (d.percent != null ? ' at ' + d.percent + ' %' : '') + ' (severity ' + (d.severity || '?') + ').\\n' +\n        'Event ' + evt.id + ' at ' + evt.at + '. Open the site in MZPanel to raise its limit or find the cause.',\n} }];\n"
      },
      "id": "f1e3f844-be9f-51d2-a8f8-a904cb7d0510",
      "name": "Verify signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $('Settings').first().json.fromEmail }}",
        "toEmail": "={{ $('Settings').first().json.ownerEmail }}",
        "subject": "={{ $json.subject }}",
        "emailFormat": "text",
        "text": "={{ $json.text }}",
        "options": {}
      },
      "id": "778e23c0-42d8-54b0-a063-e7d46db2664c",
      "name": "Email the owner",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        660,
        300
      ],
      "credentials": {
        "smtp": {
          "id": "smtp",
          "name": "SMTP account"
        }
      }
    }
  ],
  "connections": {
    "MZPanel webhook": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Verify signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify signature": {
      "main": [
        [
          {
            "node": "Email the owner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "mzpanel": "Developer webhook endpoint (Pro+, event resource.cap) with X-MZ-Signature-V2 verification; emails the owner. On Free/Plus poll GET /v1/events?types=resource.cap instead (see the docs)."
  }
}
