<?xml version="1.0" encoding="UTF-8"?>

<!-- Differential patch for M/Monit database schema version 23 to 24 -->

<patch>

<!-- sqlite -->

<statements type="sqlite">

<comment>Add alertservice table</comment>
<statement>
CREATE TABLE alertservice (
id INTEGER PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE,
description VARCHAR(255),
endpoint VARCHAR(255),
apikey TEXT,
script TEXT NOT NULL,
expanded TEXT NOT NULL,
created_at INTEGER NOT NULL
);
</statement>

<comment>Add pre-defined Alert Script for Slack</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at) 
VALUES(1, 'Slack Alert', 'A Service for sending Slack Alerts',
'# Generate the Slack message payload
generate_payload() {
  printf ''{
  "channel": "%s",
  "text": "Alert from M/Monit",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Alert from M/Monit*"
      }
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Date:*\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Host:*\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Service:*\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Action:*\\n%s"
        }
      ]
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Event:*\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Description:*\\n%s"
        }
      ]
    }
  ]
}'' "$ENDPOINT" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}
# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST \
  -H "Authorization: Bearer $APIKEY" \
  -H "Content-type: application/json" \
  --data "$payload" \
  https://slack.com/api/chat.postMessage
', 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for Discord</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(2, 'Discord Alert', 'A Service for sending Discord Alerts',
'# Generate the Discord message payload
generate_payload() {
  printf ''{
  "content": "Alert from M/Monit",
  "embeds": [{
    "title": "M/Monit Alert",
    "color": 15158332,
    "fields": [
      { "name": "Date", "value": "%s", "inline": true },
      { "name": "Host", "value": "%s", "inline": true },
      { "name": "Service", "value": "%s", "inline": true },
      { "name": "Action", "value": "%s", "inline": true },
      { "name": "Event", "value": "%s", "inline": false },
      { "name": "Description", "value": "%s", "inline": false }
    ]
  }]
}'' "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -H "Content-Type: application/json" -d "$payload" "$ENDPOINT"
', 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for PagerDuty</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(3, 'PagerDuty Alert', 'A Service for sending PagerDuty Alerts',
'# The Integration Key should be set in the API Key field in the service configuration
INTEGRATION_KEY="$APIKEY"

# Generate the PagerDuty event payload
generate_payload() {
  printf ''{
  "routing_key": "%s",
  "event_action": "trigger",
  "payload": {
    "summary": "M/Monit Alert: %s on %s",
    "source": "M/Monit",
    "severity": "critical",
    "custom_details": {
      "date": "%s",
      "host": "%s",
      "service": "%s",
      "action": "%s",
      "event": "%s",
      "description": "%s"
    }
  }
}'' "$INTEGRATION_KEY" "$MONIT_EVENT" "$MONIT_HOST" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST -H "Content-Type: application/json" -d "$payload" \
  https://events.pagerduty.com/v2/enqueue
', 'noop', 1723456800);
</statement>

<comment>Enable welcome screen to show news</comment>
<statement>
UPDATE mmonit SET welcome=1;
</statement>

</statements>

<!-- mysql -->

<statements type="mysql">

<comment>Add alertservice table</comment>
<statement>
CREATE TABLE alertservice (
  id BIGINT PRIMARY KEY,
  name VARCHAR(64) NOT NULL UNIQUE,
  description VARCHAR(255),
  endpoint VARCHAR(255),
  apikey TEXT,
  script TEXT NOT NULL,
  expanded TEXT NOT NULL,
  created_at BIGINT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add pre-defined Alert Script for Slack</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(1, 'Slack Alert', 'A Service for sending Slack Alerts', CONCAT(
'# Generate the Slack message payload
generate_payload() {
  printf ''',
  '{
  "channel": "%s",
  "text": "Alert from M/Monit",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Alert from M/Monit*"
      }
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Date:*\\\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Host:*\\\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Service:*\\\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Action:*\\\\n%s"
        }
      ]
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Event:*\\\\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Description:*\\\\n%s"
        }
      ]
    }
  ]
}'' "$ENDPOINT" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}
# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST \\
  -H "Authorization: Bearer $APIKEY" \\
  -H "Content-type: application/json" \\
  --data "$payload" \\
  https://slack.com/api/chat.postMessage'
), 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for Discord</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(2, 'Discord Alert', 'A Service for sending Discord Alerts', CONCAT(
'# Generate the Discord message payload
generate_payload() {
  printf ''',
  '{
  "content": "Alert from M/Monit",
  "embeds": [{
    "title": "M/Monit Alert",
    "color": 15158332,
    "fields": [
      { "name": "Date", "value": "%s", "inline": true },
      { "name": "Host", "value": "%s", "inline": true },
      { "name": "Service", "value": "%s", "inline": true },
      { "name": "Action", "value": "%s", "inline": true },
      { "name": "Event", "value": "%s", "inline": false },
      { "name": "Description", "value": "%s", "inline": false }
    ]
  }]
}'' "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -H "Content-Type: application/json" -d "$payload" "$ENDPOINT"'
), 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for PagerDuty</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(3, 'PagerDuty Alert', 'A Service for sending PagerDuty Alerts', CONCAT(
'# The Integration Key should be set in the API Key field in the service configuration
INTEGRATION_KEY="$APIKEY"

# Generate the PagerDuty event payload
generate_payload() {
  printf ''',
  '{
  "routing_key": "%s",
  "event_action": "trigger",
  "payload": {
    "summary": "M/Monit Alert: %s on %s",
    "source": "M/Monit",
    "severity": "critical",
    "custom_details": {
      "date": "%s",
      "host": "%s",
      "service": "%s",
      "action": "%s",
      "event": "%s",
      "description": "%s"
    }
  }
}'' "$INTEGRATION_KEY" "$MONIT_EVENT" "$MONIT_HOST" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST -H "Content-Type: application/json" -d "$payload" \\
  https://events.pagerduty.com/v2/enqueue'
), 'noop', 1723456800);
</statement>

<comment>Enable welcome screen to show news</comment>
<statement>
UPDATE mmonit SET welcome=1;
</statement>

</statements>


<!-- postgresql -->

<statements type="postgresql">

<comment>Add pre-defined Alert Scripts</comment>
<statement>
CREATE TABLE alertservice (
  id BIGINT PRIMARY KEY,
  name VARCHAR(64) NOT NULL UNIQUE,
  description VARCHAR(255),
  endpoint VARCHAR(255),
  apikey TEXT,
  script TEXT NOT NULL,
  expanded TEXT NOT NULL,
  created_at BIGINT NOT NULL
);
</statement>

<comment>Add pre-defined Alert Script for Slack</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(1, 'Slack Alert', 'A Service for sending Slack Alerts', $script$
# Generate the Slack message payload
generate_payload() {
  printf '{
  "channel": "%s",
  "text": "Alert from M/Monit",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Alert from M/Monit*"
      }
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Date:*\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Host:*\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Service:*\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Action:*\n%s"
        }
      ]
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "text": "*Event:*\n%s"
        },
        {
          "type": "mrkdwn",
          "text": "*Description:*\n%s"
        }
      ]
    }
  ]
}' "$ENDPOINT" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}
# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST \
  -H "Authorization: Bearer $APIKEY" \
  -H "Content-type: application/json" \
  --data "$payload" \
  https://slack.com/api/chat.postMessage
$script$, 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for Discord</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(2, 'Discord Alert', 'A Service for sending Discord Alerts', $script$
# Generate the Discord message payload
generate_payload() {
  printf '{
  "content": "Alert from M/Monit",
  "embeds": [{
    "title": "M/Monit Alert",
    "color": 15158332,
    "fields": [
      { "name": "Date", "value": "%s", "inline": true },
      { "name": "Host", "value": "%s", "inline": true },
      { "name": "Service", "value": "%s", "inline": true },
      { "name": "Action", "value": "%s", "inline": true },
      { "name": "Event", "value": "%s", "inline": false },
      { "name": "Description", "value": "%s", "inline": false }
    ]
  }]
}' "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -H "Content-Type: application/json" -d "$payload" "$ENDPOINT"
$script$, 'noop', 1723456800);
</statement>

<comment>Add pre-defined Alert Script for PagerDuty</comment>
<statement>
INSERT INTO alertservice(id, name, description, script, expanded, created_at)
VALUES(3, 'PagerDuty Alert', 'A Service for sending PagerDuty Alerts', $script$
# The Integration Key should be set in the API Key field in the service configuration
INTEGRATION_KEY="$APIKEY"

# Generate the PagerDuty event payload
generate_payload() {
  printf '{
  "routing_key": "%s",
  "event_action": "trigger",
  "payload": {
    "summary": "M/Monit Alert: %s on %s",
    "source": "M/Monit",
    "severity": "critical",
    "custom_details": {
      "date": "%s",
      "host": "%s",
      "service": "%s",
      "action": "%s",
      "event": "%s",
      "description": "%s"
    }
  }
}' "$INTEGRATION_KEY" "$MONIT_EVENT" "$MONIT_HOST" "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
curl -s -X POST -H "Content-Type: application/json" -d "$payload" \
  https://events.pagerduty.com/v2/enqueue
$script$, 'noop', 1723456800);
</statement>

<comment>Enable welcome screen to show news</comment>
<statement>
UPDATE mmonit SET welcome=1;
</statement>

</statements>

</patch>

