DRV - Frag Monitoring
Summary
The DRV - Frag Monitoring monitor continuously checks the fragmentation level of eligible fixed HDD drives on Windows endpoints against the threshold and drive selection defined in the configuration file generated by the DRV Frag Monitoring Configuration Writer task. It does not read custom fields directly; it relies on the pre‑built JSON configuration to know which drives to monitor, what fragmentation threshold to enforce, and where to send ticket actions.
Depending on the effective mode, the monitor behaves differently:
-
AlertOnly mode: The monitor creates a ticket via webhook when a drive first breaches the threshold, and closes that ticket when the drive recovers. It returns a failure string only on the first detection so that the DRV Frag Autofix task runs once (and exits immediately without doing anything). Subsequent cycles return success while the breach remains open.
-
AutoFix mode (workstations only): The monitor never creates tickets. It records the breach state and returns a failure string to launch the DRV Frag Autofix task, which performs the actual defragmentation and manages ticket creation/comments/closure. The monitor tracks the remediation attempt count and only re‑triggers the autofix task when a retry is due (24 hours after the previous attempt) and the maximum attempts (4) have not been exhausted. When the drive eventually recovers, the monitor closes the ticket if one exists.
Server AutoFix policy note: Although the configuration writer includes logic to allow server AutoFix when the endpoint override is Enabled - Autofix, in practice this solution never monitors servers in AutoFix mode. The DRV Frag Monitoring - Active group, which is the only target for this monitor, excludes servers whose endpoint DRV_Frag_Mode is Enabled - Autofix. Therefore, servers are always in AlertOnly mode (or Disabled) and never trigger the autofix task. The server autofix policy is a safeguard in the scripts but is never exercised.
How It Works
-
Configuration File
At each scheduled run, the monitor reads:
C:\ProgramData\_Automation\Script\DRVFragmentationMonitoring\DRVFragmentationMonitoring.json.
This file contains:- Mode –
AutoFix,AlertOnly, orDisabled - Role –
ServerorWorkstation - Drives –
All,None, or a normalized drive letter string - Threshold – fragmentation percentage (1–100)
- AutoFixAllowed – boolean policy flag
- TicketWebhookUrl – the workflow webhook URL for ticket actions
If the file is missing or cannot be parsed, the monitor returns a failure message and skips the run without changing any state or firing webhooks. If the mode is
Disabled, the monitor closes any open ticket (if ticketing is available) and then returns success.Note on AutoFixAllowed for servers: The flag may be
truefor a server if the endpoint override isEnabled - Autofix, but such a server is not in the Active group and therefore never runs this monitor. In the context of this solution, the monitor will only encounter servers withMode = AlertOnlyorDisabled. - Mode –
-
Drive Discovery and Filtering
The monitor enumerates all local fixed drives (Win32_LogicalDisk,DriveType = 3), applies the drive selection from configuration (All,None, or specific letters), and checks media eligibility. Only fixed rotational HDDs are considered; SSDs, SCM, removable, and unknown media (unless$includeUnknownMediaTypeis true) are skipped. Drive eligibility usesMSFT_PhysicalDisk.MediaType: value3(HDD) is eligible; values4(SSD) and5(SCM) are excluded. -
Fragmentation Analysis and Caching
Fragmentation is measured usingWin32_Volume.DefragAnalysis, which is locale‑independent and returnsTotalPercentFragmentation. To avoid expensive repeated analyses, the monitor maintains a fragmentation cache (Drive_Fragmentation_Cache.json) with a configurable lifetime (default 4 hours). If a fresh cache entry exists, that value is used; otherwise a new analysis is performed and the cache is updated. The DRV Frag Autofix script also writes its post‑remediation measurement into this cache so the monitor observes the change immediately. -
Threshold Comparison
A drive is considered breached when its fragmentation percentage is greater than or equal to the configured threshold. For example, with a threshold of 30, 29% is healthy and 30% is a breach. -
State Machine (duplicate prevention and retry logic)
The monitor maintains three local state files:Drives_To_Create_Ticket.jsonDrives_With_Existing_Ticket.jsonDrives_To_Close_Ticket.json
For each drive letter (detected this run plus any letter already tracked), the monitor applies the following logic based on its current status and previous state:
AlertOnly mode:
- Breached and not tracked → add to Create list, return failure (first detection only)
- Breached and in Create list → keep in Create (webhook retry pending), return success
- Breached and in Existing list → keep in Existing, return success
- Healthy and in Create or Existing → move to Close list, return success
AutoFix mode:
- Breached and not tracked → add to Existing list with attempt count 0, return failure
- Breached and in Existing list:
- If attempt count ≥ 4 → keep in Existing, mark exhausted, return success
- If a failure string was already returned for the current attempt and within the launch request timeout (6 hours) → return success
- If the retry interval has not elapsed → return success
- Otherwise → increment failure tracking, return failure (triggers autofix task)
- Healthy and in Existing list → if ticket exists, move to Close list; else clear state
- Healthy and in Create or Close list → clear state
The monitor also handles drives that go out of scope or cannot be evaluated: their state is preserved untouched to avoid accidental ticket closure.
-
Webhook Execution
After updating the state files, the monitor processes any pending webhooks:- For drives in the Create list, it sends a
Createwebhook (only if ticketing is enabled). On success, the drive is moved to the Existing list withTicketExistsset totrue. - For drives in the Close list, it sends a
Closewebhook. On success, the drive is removed from the close list. - Failed webhooks remain in the appropriate list and are retried on later cycles.
The webhook payload always includes
Action,TicketSubject,TicketBody, andDeviceId(read from the endpoint registry). The webhook URL is taken from the configuration file. - For drives in the Create list, it sends a
-
Alert Signal
- If any failure strings were generated (new AlertOnly breach, new AutoFix breach, or an autofix retry due), the monitor returns a string starting with
Failure:. This triggers the RMM to launch the DRV Frag Autofix task (which exits immediately for AlertOnly). - Otherwise, the monitor returns
Success:with details. The monitor's success criteria is configured as Script Output containsSuccess:so that normal operation does not raise an alert. The RMM alert is only raised when aFailure:is present, which corresponds to a new incident or a required autofix action. Because RMM's built‑in ticketing is set to Do not Generate Ticket, the actual tickets are created and managed by the CWRMM Ticket Management for Monitors workflow via webhooks.
- If any failure strings were generated (new AlertOnly breach, new AutoFix breach, or an autofix retry due), the monitor returns a string starting with
-
Autofix Lock and Task Coordination
The DRV Frag Autofix script creates a lock file (Autofix.lock) while it runs. The monitor checks for this lock file and, if fresh, defers its evaluation for that cycle to avoid concurrent state modifications.
Scenario – AlertOnly New Breach
A workstation drive C: reaches 42% fragmentation (threshold 30%). The monitor detects it, creates a state entry in Create, fires a Create webhook to the workflow, and returns Failure: New fragmentation breach detected on drive C. Fragmentation is 42 percent. Threshold is 30 percent. Mode is AlertOnly.. The RMM raises an alert and launches the DRV Frag Autofix task, which sees AlertOnly and exits immediately. The ticket remains open in ConnectWise.
Scenario – AlertOnly Sustained Breach
On the next hourly run, drive C: is still at 42%. The state entry is now in Create (if the webhook is pending) or Existing (if the webhook succeeded). The monitor returns success and does not fire another webhook or trigger autofix again. No duplicate ticket is created.
Scenario – AlertOnly Recovery
After defragmentation (or manual action), drive C: drops to 10% fragmentation. The monitor detects it as healthy, moves the state to Close, and fires a Close webhook. The workflow closes the ticket. The monitor returns success.
Scenario – AutoFix New Breach and Retries (Workstation)
A workstation (with endpoint override Enabled - Autofix) has drive D: at 38% fragmentation (threshold 30%). The monitor detects the breach, creates an Existing state with attempt count 0, and returns a failure. The RMM launches the DRV Frag Autofix task. The task attempts defragmentation; if it fails, it creates a ticket via webhook and sets the next retry time to +24 hours. The monitor then returns success on subsequent runs until the retry time is reached. When the retry is due and the drive is still above threshold, the monitor returns failure again, triggering the autofix task for attempt 2. This repeats until the drive recovers or the maximum attempts (4) are exhausted. If exhausted, the monitor stops triggering autofix and returns success while keeping the ticket open.
Dependencies
- Custom Field: DRV_Frag_Mode_Wks
- Custom Field: DRV_Frag_Mode_Svr
- Custom Field: DRV_Frag_Drives_Wks
- Custom Field: DRV_Frag_Drives_Svr
- Custom Field: DRV_Frag_Threshold_Wks
- Custom Field: DRV_Frag_Threshold_Svr
- Custom Field: DRV_Frag_Mode_Wks_Site
- Custom Field: DRV_Frag_Mode_Svr_Site
- Custom Field: DRV_Frag_Drives_Wks_Site
- Custom Field: DRV_Frag_Drives_Svr_Site
- Custom Field: DRV_Frag_Threshold_Wks_Site
- Custom Field: DRV_Frag_Threshold_Svr_Site
- Custom Field: DRV_Frag_Mode
- Custom Field: DRV_Frag_Drives
- Custom Field: DRV_Frag_Threshold
- Custom Field: Ticket_Mgmt_Webhook_Url
- Device Group: DRV Frag Monitoring - Active
- Device Group: DRV Frag Monitoring - Alert Only [Workstations]
- Task: DRV Frag Monitoring Configuration Writer
- Task: DRV Frag Autofix
- Workflow: CWRMM Ticket Management for Monitors
- Trigger: CWRMM Ticket Management for Monitors
- Solution: Drive Fragmentation Monitoring
Monitor Setup Location
Monitors Path: ENDPOINTS ➞ Alerts ➞ Monitors
Monitor Summary
- Name:
DRV - Frag Monitoring - Description:
Monitors local drive fragmentation against configured thresholds and manages ConnectWise ticket creation and closure via webhooks. Triggers the DRV Frag Autofix task for AutoFix mode. - Type:
Script - Severity:
Others - Family:
Desktop Health

Targeted Resources
- Target Type:
Device Groups - Group Name: DRV Frag Monitoring - Active

Conditions
- Run script on:
Schedule - Repeat every:
1Hour(s) - Script Language:
PowerShell - Use Generative AI Assist for script creation:
False - PowerShell Script Editor:
- Criteria:
Contains - Operator:
AND - Script Output:
Failure: - Escalate ticket on script failure:
Disabled - Add Automation: DRV Frag Autofix

Ticket Resolution
- Automatically Resolve:
Disabled
Monitor Output
Output: Do not Generate Ticket
Completed Monitor

Changelog
2026-08-26
- Initial version of the document