Skip to content

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

SQL-Availability-Group-Sync-Monitor

AG-Status – SQL Availability Group Synchronization Monitor πŸ“Œ Overview

The AG-Status PowerShell script is designed to monitor SQL Server databases that are part of Availability Groups (AGs). It verifies their synchronization state, provides clear console output, and sends email alerts for failures or successes.

This script is ideal for:

Daily health checks

Automated job/pipeline monitoring

Proactive alerting to reduce downtime

βš™οΈ Features

Checks all databases on configured SQL Server instances.

Evaluates whether databases are:

βœ… Synchronized

⚠️ Not in an Availability Group

❌ Out of Sync

Sends email alerts for both success and failure states.

Exits with clear status codes (0 = success, 1 = failure) for easy integration with monitoring systems.

πŸ“‚ Requirements

PowerShell 5.1+ or PowerShell 7+

FailoverClusters module (included with Windows Server features)

SMTP relay (for sending notifications)

Custom function or module to check database sync state

The example uses Get-SITAGDatabaseStatus

You can replace this with your own logic (e.g., dbatools Get-DbaAgDatabase)

πŸ› οΈ Setup

Clone this repository or download the script.

git clone https://github.com//AG-Status.git cd AG-Status

Update script variables for your environment:

Import-Module path β†’ point to your module or replace with your own logic.

$smtpServer β†’ your SMTP relay.

$from β†’ email sender address.

$to β†’ recipient(s) for alerts.

Test the script manually:

.\AG-Status.ps1

Automate with Task Scheduler or DevOps pipeline:

Run on a schedule (e.g., every 15 minutes).

Use exit codes for CI/CD or monitoring pipelines.

πŸ“§ Email Alerts

Failure Email Subject: CRITICAL: SQL AG Databases Not Synchronized Body: List of affected databases with instance name and sync state.

Success Email Subject: SUCCESS: All SQL AG Databases Are Synchronized Body: Timestamp confirming successful synchronization.

🚦 Exit Codes Code Meaning Usage 0 All databases are synchronized Healthy state – no action required 1 One or more databases out of sync Critical issue – review failure email πŸ”§ Customization

Replace Get-SITAGDatabaseStatus with your own function or SQL query.

Adjust email configuration for SMTP authentication if required.

Expand notifications to include logging to file, Slack/Teams webhook alerts, or custom monitoring dashboards.

πŸ“œ Example Output

Console (Failure)

Critical: Found database(s) not synchronizing: SQL01 - SalesDB: Not in AG Group SQL02 - HRDB: Not Synchronized

Console (Success)

Success: All databases in Availability Groups are synchronized successfully as of 2025-06-06 14:30:12.

πŸ“„ License

MIT License – feel free to use and modify for your environment.


πŸ”„ Process Flow

The following diagram shows how the script evaluates database synchronization status and handles alerts:

flowchart TD
    A[Start Script] --> B[Collect DB Sync Status]
    B --> C{Any DB Not in AG?}
    C -- Yes --> D[Mark as Critical Issue]
    C -- No --> E{Any DB Not Synchronized?}
    E -- Yes --> D
    E -- No --> F[All DBs Synchronized]

    D --> G[Output Critical to Console]
    G --> H[Send Failure Email]
    H --> I[Exit 1]

    F --> J[Output Success to Console]
    J --> K[Send Success Email]
    K --> L[Exit 0]



Loading

About

PowerShell script to monitor SQL Server Availability Group database sync status and send email alerts

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages