<!-- Auto-generated for OpenClaw by pack-openclaw. Notes for OpenClaw users:
- Claude Code dynamic expressions (!`...`) in this file are NOT evaluated by OpenClaw
and appear as literal text. Run them manually at the start of the workflow.
- Invoke this skill only via slash command (e.g. /amg-check-pg-flex). Auto-invocation is
disabled on Claude Code but not on OpenClaw. -->
<h2>OpenClaw Setup (one-time)</h2>
<p>This skill calls MCP tools prefixed with <code>mcp__amg__*</code>, so OpenClaw must have an MCP server registered under the exact name <strong><code>amg</code></strong>. Run this once per workspace before invoking the skill:</p>
<pre><code class="language-bash">openclaw mcp set amg '{"url":"https://<your-grafana-instance>/api/azure-mcp","transport":"streamable-http","headers":{"Authorization":"Bearer <your-token>"}}'
</code></pre>
<p>Replace <code><your-grafana-instance></code> with your Azure Managed Grafana endpoint and <code><your-token></code> with a valid Grafana service-account token (starts with <code>glsa_</code>). The server name <strong>must</strong> be <code>amg</code> — the skill's <code>allowed-tools</code> reference <code>mcp__amg__*</code> and will not find tools under any other name.</p>
<p>Verify the server is registered:</p>
<pre><code class="language-bash">openclaw mcp list
</code></pre>
<blockquote>
<p>Official skill source: https://github.com/Azure/amg-skills</p>
</blockquote>
<h2>Runtime Context</h2>
<ul>
<li>Current UTC time: !<code>date -u +%Y-%m-%dT%H:%M:%SZ</code></li>
<li>Config: !<code>cat memory/amg-check-pg-flex/config.md 2>/dev/null || echo "NOT_CONFIGURED"</code></li>
<li>Prior report: !<code>[ -f memory/amg-check-pg-flex/report.md ] && echo "exists ($(grep -c '^### BUG-' memory/amg-check-pg-flex/report.md) bugs documented)" || echo "not found"</code></li>
<li>Arguments: time-range=$0, subscription-override=$1</li>
</ul>
<blockquote>
<p><strong>Known Issues</strong>: Before presenting findings, cross-reference results against <code>memory/amg-check-pg-flex/report.md</code>.</p>
</blockquote>
<h2>AMG PostgreSQL Flexible Server Health Check</h2>
<h2>Critical Constraints</h2>
<ul>
<li><strong>No subagents for MCP.</strong> The Agent tool cannot access MCP tools — all MCP calls must be made from the main context.</li>
<li><strong>Scan every resource.</strong> No sampling or early stopping.</li>
<li><strong>Time format</strong>: ISO 8601 UTC with explicit <code>from</code>/<code>to</code> — NEVER use <code>timespan</code> (it causes errors).</li>
<li><strong>Parallelism cap</strong>: 30 concurrent MCP calls per batch. Reduce to 4-5 if rate-limited.</li>
<li><strong>Result too large</strong>: Save to temp file and parse outside the context window. Prefer <code>node -e "..."</code> if installed; otherwise fall back to <code>python -c "..."</code>, <code>jq</code>, or <code>pwsh -Command "..."</code>. Bash permission for the chosen interpreter will be prompted on first use.</li>
</ul>
<h2>Progress Tracking</h2>
<p>Update checkboxes as you complete each phase:</p>
<ul>
<li><input disabled="" type="checkbox"> Phase 1a: Datasource validated</li>
<li><input disabled="" type="checkbox"> Phase 1b: Servers discovered (N=?)</li>
<li><input disabled="" type="checkbox"> Phase 1c: Non-ready servers investigated (if any)</li>
<li><input disabled="" type="checkbox"> Phase 2: Pulse check completed (N scanned, N findings)</li>
<li><input disabled="" type="checkbox"> Phase 3: Deep metrics for abnormal servers</li>
<li><input disabled="" type="checkbox"> Phase 4: Resource logs for abnormal servers</li>
<li><input disabled="" type="checkbox"> Report presented</li>
<li><input disabled="" type="checkbox"> Known issues updated in <code>memory/amg-check-pg-flex/report.md</code></li>
</ul>
<h2>Configuration</h2>
<p><strong>If Config shows <code>NOT_CONFIGURED</code></strong>: Run First-Run Setup at the bottom of this file, then return here.</p>
<p><strong>If Config is populated</strong>: Extract the datasource UID and subscription ID from the pre-loaded Runtime Context above and use them for all queries. Use <code>$1</code> as the subscription override if provided.</p>
<ul>
<li><strong>Datasource UID</strong>: from <code>## Azure Monitor Datasource</code> > <code>UID</code></li>
<li><strong>Subscription ID</strong>: from <code>## Subscription</code> (or <code>$1</code> if provided)</li>
<li><strong>Resource Type</strong>: <code>microsoft.dbforpostgresql/flexibleservers</code> (lowercase)</li>
<li><strong>ARM ID template</strong>: <code>/subscriptions/{SUB}/resourceGroups/{RG}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{name}</code></li>
</ul>
<h2>Time Range</h2>
<p>Default: 7 days for metrics, 24 hours for logs. Override with <code>$0</code> (e.g., <code>3d</code>). Keep log queries to 1-2 days to avoid timeouts.</p>
<h2>Workflow</h2>
<h3>Phase 1a: Validate Datasource</h3>
<p>Call <code>amgmcp_datasource_list</code> (no parameters). Find entry with <code>type == "grafana-azure-monitor-datasource"</code>.</p>
<ul>
<li>Matches configured UID → proceed.</li>
<li>Different UID → update <code>memory/amg-check-pg-flex/config.md</code>, warn user, use new UID.</li>
<li>Not found → abort with error.</li>
</ul>
<h3>Phase 1b: Discover All PostgreSQL Flexible Servers</h3>
<pre><code>azureMonitorDatasourceUid: {DATASOURCE_UID}
query: |
resources
| where type == 'microsoft.dbforpostgresql/flexibleservers'
| where subscriptionId == '{SUBSCRIPTION_ID}'
| project name, resourceGroup, location, properties.state, sku.name, sku.tier
| order by location asc, name asc
</code></pre>
<p>If multiple subscriptions are configured, query each separately and merge results. Derive region summary by counting servers per <code>location</code>. Flag servers not in "Ready" state. Stop if zero servers found.</p>
<h3>Phase 1c: Activity Log for Non-Ready Servers</h3>
<p>If any servers are not in "Ready" state, query the activity log for up to 3 of them:</p>
<pre><code>azureMonitorDatasourceUid: {DATASOURCE_UID}
scope: /subscriptions/{SUB}/resourceGroups/{RG}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{name}
startTime: now-3d
endTime: now
select: eventTimestamp,operationName,status,caller,subStatus
</code></pre>
<p>If the response exceeds 500 KB, retry with <code>startTime: now-6h</code>. Summarize: operations performed, caller type (service principal vs human), success/in-progress status, likely cause.</p>
<h3>Phase 2: Tier 1 — Fleet-Wide Pulse Check</h3>
<pre><code>azureMonitorDatasourceUid: {DATASOURCE_UID}
pastDays: 7
scenarios: pg_flex
</code></pre>
<p>Scans all servers across 5 scenarios: <code>pg_flex_cpu</code>, <code>pg_flex_memory</code>, <code>pg_flex_storage</code>, <code>pg_flex_disk_iops</code>, <code>pg_flex_disk_bandwidth</code>.</p>
<p><strong>Before moving to Phase 3, verify:</strong></p>
<ol>
<li><code>scanSummary.totalResourcesScanned</code> matches Phase 1 server count.</li>
<li>All 5 scenarios show <code>status: "completed"</code> in <code>scenarioResults</code>.</li>
<li>If <code>errors</code> non-empty, retry affected scenarios individually (e.g., <code>scenarios: pg_flex_cpu</code>).</li>
<li>If >10% servers missing, fall back to batched <code>amgmcp_query_resource_metric</code> for unscanned servers.</li>
</ol>
<p><strong>Severity thresholds (findings array):</strong></p>
<table>
<thead>
<tr>
<th>Severity</th>
<th>CPU</th>
<th>Memory</th>
<th>Storage</th>
<th>Disk IOPS</th>
<th>Disk BW</th>
</tr>
</thead>
<tbody><tr>
<td>Critical</td>
<td>>90%</td>
<td>>90%</td>
<td>>85%</td>
<td>>90%</td>
<td>>90%</td>
</tr>
<tr>
<td>Warning</td>
<td>>80%</td>
<td>>80%</td>
<td>>75%</td>
<td>>80%</td>
<td>>80%</td>
</tr>
</tbody></table>
<h3>Phase 3: Tier 2 — Deep Metrics for Abnormal Servers</h3>
<p>Read <strong>reference/phase3-deep-dive.md</strong> before starting Phase 3. It contains:</p>
<ul>
<li>Core metrics table (always query) and secondary metrics table (query on anomaly)</li>
<li>Batch strategy: up to 50 comma-separated resource IDs per call, up to 5 parallel metric calls</li>
<li>Correlation analysis patterns (use ultrathink)</li>
</ul>
<h3>Phase 4: Resource Logs for Abnormal Servers</h3>
<p>Read <strong>reference/phase4-resource-logs.md</strong> before starting Phase 4. It contains:</p>
<ul>
<li>5 KQL query templates: error distribution, top errors, warning messages, log volume, session duration</li>
<li>Time filter rules (always required to avoid timeouts)</li>
</ul>
<h2>Output</h2>
<p>Present the report using the structure in <strong>reference/output-format.md</strong>.</p>
<p><strong>Classification:</strong></p>
<table>
<thead>
<tr>
<th>Severity</th>
<th>Criteria</th>
</tr>
</thead>
<tbody><tr>
<td><strong>CRITICAL</strong></td>
<td><code>is_db_alive</code> sustained 0, OR CPU max >90%, OR Memory max >90%, OR Storage >85%</td>
</tr>
<tr>
<td><strong>WARNING</strong></td>
<td>CPU avg >80%, Memory avg >85%, sustained >60% for 6h+, spike >30pp in 1h, Storage >70%, <code>connections_failed</code> >0, <code>deadlocks</code> >0, disk IOPS/BW >80%, transactionIDs >1B, longest query >300s, replication delay >30s</td>
</tr>
<tr>
<td><strong>HEALTHY</strong></td>
<td>All metrics within normal ranges</td>
</tr>
</tbody></table>
<h2>Update Known Issues</h2>
<p>After presenting findings, update <code>memory/amg-check-pg-flex/report.md</code>:</p>
<ol>
<li>Read the current file.</li>
<li>Rebuild the Resource Inventory table at the end: every server, full ARM ID, region, SKU, state. Group by region, sorted alphabetically.</li>
<li>Update existing bug status from today's telemetry (resolved / improving / worsening / still active).</li>
<li>Add new bugs with: severity, server name, region, metric evidence, log evidence, root cause, recommended action.</li>
<li>Update the "Updated" date header.</li>
</ol>
<p>Only add genuine issues: sustained high utilization, crash patterns, connection storms, persistent errors. Skip transient single-hour spikes or expected maintenance windows.</p>
<h2>Error Handling</h2>
<p>See <strong>reference/error-handling.md</strong> for the full recovery table.</p>
<h2>Analysis Guidance</h2>
<ul>
<li>Known patterns, signals, root causes: reference/analysis-patterns.md</li>
<li>Optional deep-dive KQL queries: reference/deep-dive-queries.md</li>
</ul>
<h2>First-Run Setup</h2>
<p>Run only when Config shows <code>NOT_CONFIGURED</code>. After completing, return to the Workflow above.</p>
<p><strong>1. Discover Datasource UID</strong>: Call <code>amgmcp_datasource_list</code>. Filter <code>type == "grafana-azure-monitor-datasource"</code>. Prefer <code>uid == "azure-monitor-oob"</code> if multiple match. Abort if zero match.</p>
<p><strong>2. Discover Subscription ID</strong>: Run this Resource Graph query to list all subscriptions with PostgreSQL Flexible Servers, then present the results as a table and ask the user which subscription(s) to use:</p>
<pre><code>resources
| where type == 'microsoft.dbforpostgresql/flexibleservers'
| join kind=inner (
resourcecontainers
| where type == 'microsoft.resources/subscriptions'
| project subscriptionId, subscriptionName=name
) on subscriptionId
| summarize ServerCount=count() by subscriptionId, subscriptionName
| order by ServerCount desc
</code></pre>
<p>Present the results as a table with columns: <strong>Subscription Name</strong>, <strong>Subscription ID</strong>, <strong>Server Count</strong>. Then ask the user: <em>"Which subscription ID(s) should I configure for this health check?"</em></p>
<p><strong>3. Write config</strong>: Write <code>memory/amg-check-pg-flex/config.md</code>:</p>
<pre><code class="language-markdown"># amg-check-pg-flex Configuration
User-specific values for the PostgreSQL Flexible Server health check skill.
This file is auto-generated on first run and can be edited manually.
Azure Monitor Datasource
- **UID**: {discovered_uid}
- **Name**: {discovered_name}
Subscription
- {subscription_id}
</code></pre>
<p><strong>4. Confirm</strong>: Show the resolved config and ask for confirmation before proceeding.</p>



