A Google Apps Script that monitors MX records for your domains and sends alerts via Google Chat webhook when changes are detected.
- π Monitors multiple domains for MX record changes
- π Real-time notifications via Google Chat webhook
- π Tracks MX record history in Google Sheets
- β° Automatic monitoring with configurable check intervals
- π‘οΈ Uses Google's DNS API for reliable lookups
- Google account with access to Google Sheets
- Google Chat space with webhook capability (or Slack as alternative)
- Domains you want to monitor
-
Create a new Google Sheets spreadsheet
- Go to Google Sheets
- Create a new blank spreadsheet
- Name it something like "MX Record Monitor"
-
Open the Apps Script editor
- In your spreadsheet, click Extensions β Apps Script
- Delete any default code in the editor
-
Add the script
- Copy the entire script from
Code.gsin this repository - Paste it into the Apps Script editor
- Click the save icon or press
Ctrl+S(Windows) /Cmd+S(Mac) - Name the project "MX Record Monitor"
- Copy the entire script from
Edit the CONFIG object in the script:
domains: [
'example.com',
'yourdomain.com',
'anotherdomain.com'
],Add all domains you want to monitor.
Create a webhook in Google Chat:
- Open Google Chat in your browser
- Create a new Space or select an existing one
- Click on the space name at the top
- Select Apps & integrations
- Click on Webhooks
- Click Add webhook
- Name it "MX Monitor" (or any name you prefer)
- Click Save
- Copy the webhook URL that appears
Add the webhook URL to the script:
webhookUrl: 'https://chat.googleapis.com/v1/spaces/XXXXXX/messages?key=YYYYYY&token=ZZZZZZ',By default, the script checks every 6 hours. To change this:
checkIntervalHours: 6, // Change to desired hours (1, 2, 4, 6, 8, 12, 24)In the Apps Script editor:
- Select the function dropdown at the top (shows "Select function")
- Choose
setupTrigger - Click the Run button (
βΆοΈ icon)
On first run, you'll need to authorize the script:
- Click Review permissions
- Select your Google account
- Click Advanced β Go to MX Record Monitor (unsafe)
- Click Allow
The script needs these permissions to:
- Access your spreadsheet to store MX record history
- Make external HTTP requests to check DNS records and send notifications
- Create time-based triggers for automatic monitoring
After running setupTrigger:
- Check your Google Sheet - a new tab called "MX_Records" should appear
- You should receive a Google Chat notification with initial MX records
- The script will now run automatically based on your configured interval
The script creates a "MX_Records" sheet with the following columns:
| Domain | MX Records | Last Checked | Last Changed |
|---|---|---|---|
| example.com | 10 mx1.example.com | 20 mx2.example.com | 2024-01-15 10:30:00 | 2024-01-15 10:30:00 |
π MX Record Monitor Alert
β οΈ example.com - MX RECORDS CHANGED
Old: 10 mx1.example.com | 20 mx2.example.com
New: 10 new-mx1.example.com | 20 new-mx2.example.com
π MX Record Monitor Alert
β example.com - ERROR
Failed to fetch MX records: DNS query failed
π MX Record Monitor Alert
β
example.com - Initial monitoring setup
Records: 10 mx1.example.com | 20 mx2.example.com
To manually test the monitoring:
- In the Apps Script editor, select the
testMXCheckfunction - Click Run (
βΆοΈ icon) - Check the Execution log (View β Logs) for results
- Verify you receive a notification if this is the first run
To use Slack webhooks instead:
- Create a Slack incoming webhook in your workspace
- Replace the
sendNotificationfunction with:
function sendNotification(results) {
if (!CONFIG.webhookUrl || CONFIG.webhookUrl === 'YOUR_WEBHOOK_URL_HERE') {
Logger.log('WARNING: Webhook URL not configured');
return;
}
let message = ':bell: *MX Record Monitor Alert*\n\n';
results.forEach(result => {
if (result.status === 'changed') {
message += `:warning: *${result.domain}* - MX RECORDS CHANGED\n`;
message += ` Old: ${result.oldRecords}\n`;
message += ` New: ${result.newRecords}\n\n`;
} else if (result.status === 'error') {
message += `:x: *${result.domain}* - ERROR\n`;
message += ` ${result.error}\n\n`;
} else if (result.status === 'initial') {
message += `:white_check_mark: *${result.domain}* - Initial monitoring setup\n`;
message += ` Records: ${result.records}\n\n`;
}
});
const payload = {
text: message
};
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
try {
const response = UrlFetchApp.fetch(CONFIG.webhookUrl, options);
Logger.log('Notification sent: ' + response.getContentText());
} catch (error) {
Logger.log('Failed to send notification: ' + error.toString());
}
}- In Apps Script editor, click the clock icon (Triggers) in the left sidebar
- You'll see all active triggers for this project
- Go to Triggers
- Click the three dots next to the
checkMXRecordstrigger - Select Delete trigger
Run the setupTrigger function again
- Verify webhook URL is correct in the
CONFIGobject - Check the Execution log (View β Executions) for errors
- Ensure the Google Chat space allows webhooks
- Run
testMXCheckmanually to see error messages
- Re-run the authorization process
- Make sure you clicked "Allow" for all permissions
- Check that domains are spelled correctly
- Verify domains have MX records (use
nslookup -type=mx yourdomain.com) - Check Execution log for DNS query errors
- Go to Triggers and verify a trigger exists for
checkMXRecords - Check execution history for any failures
- Re-run
setupTriggerto recreate the trigger
- DNS Lookup: Uses Google's public DNS API (
dns.google) to query MX records - Storage: Stores current MX records in a Google Sheet
- Comparison: Compares new results with stored records
- Notification: Sends webhook notification when changes detected
- Scheduling: Time-based trigger runs the check automatically
- All data is stored in your personal Google Sheet
- DNS queries use Google's public DNS API
- Webhook notifications are sent only to your configured endpoint
- No third-party services have access to your data
MIT License - Feel free to use and modify as needed
Issues and pull requests are welcome! Please feel free to submit improvements or report bugs.
For issues or questions:
- Check the Troubleshooting section above
- Review the Execution log in Apps Script
- Open an issue on GitHub with error details
Note: This script is provided as-is. Always test thoroughly before relying on it for critical monitoring.