The MonitoringAgent is an intelligent monitoring system that watches data streams for anomalies and changes. It combines threshold-based alerting with LLM-powered anomaly detection to provide comprehensive monitoring capabilities for metrics, systems, and applications.
- 📊 Metric Analysis: Parse and analyze various metric formats
- 🎯 Threshold-Based Alerting: Configure custom thresholds for different metrics
- 🤖 LLM-Powered Anomaly Detection: Use Claude AI to detect unusual patterns
- 📈 Historical Tracking: Maintain metric history for trend analysis
- 🔄 Continuous Monitoring: Watch data sources in real-time
- 📝 Flexible Data Sources: Support for custom monitorable interfaces
- ⚡ Real-time Analysis: Immediate processing and alert generation
The MonitoringAgent is included in the claude-php-agent package:
composer require your-org/claude-php-agentuse ClaudeAgents\Agents\MonitoringAgent;
use ClaudeAgents\Monitoring\Metric;
use ClaudePhp\ClaudePhp;
$client = new ClaudePhp(apiKey: 'your-api-key');
$monitoringAgent = new MonitoringAgent($client, [
'thresholds' => [
'cpu_usage' => 80,
'memory_usage' => 90,
],
]);
// Analyze metrics
$task = "cpu_usage: 85\nmemory_usage: 75";
$result = $monitoringAgent->run($task);
if ($result->isSuccess()) {
echo $result->getAnswer();
$metadata = $result->getMetadata();
echo "Alerts: {$metadata['alerts_generated']}\n";
}The MonitoringAgent accepts configuration options in its constructor:
$monitoringAgent = new MonitoringAgent($client, [
'name' => 'production_monitor',
'check_interval' => 60, // Polling interval in seconds
'thresholds' => [ // Metric thresholds
'cpu_usage' => 80,
'memory_usage' => 90,
'disk_usage' => 85,
],
'max_history' => 1000, // Maximum history entries
'logger' => $logger, // PSR-3 logger instance
]);| Option | Type | Default | Description |
|---|---|---|---|
name |
string | 'monitoring_agent' |
Unique name for the agent |
check_interval |
int | 60 |
Polling interval in seconds for continuous monitoring |
thresholds |
array | [] |
Metric name => threshold value mappings |
max_history |
int | 1000 |
Maximum number of historical metric entries to keep |
logger |
LoggerInterface | NullLogger |
PSR-3 compatible logger |
Metrics represent measurable values that can be monitored and analyzed:
use ClaudeAgents\Monitoring\Metric;
// Basic metric
$metric = new Metric('cpu_usage', 75.5);
// Metric with metadata
$metric = new Metric('response_time', 250, [
'endpoint' => '/api/users',
'method' => 'GET',
'unit' => 'ms',
]);$metric = new Metric('cpu_usage', 85.5, ['host' => 'web-01']);
echo $metric->getName(); // 'cpu_usage'
echo $metric->getValue(); // 85.5
var_dump($metric->getMetadata()); // ['host' => 'web-01']
echo $metric->getTimestamp(); // Unix timestamp (float)$metric = new Metric('cpu_usage', 85);
// Check if exceeds threshold
if ($metric->exceedsThreshold(80, '>')) {
echo "CPU usage is high!";
}
// Available operators: '>', '>=', '<', '<=', '==', '!='
$metric->exceedsThreshold(90, '>='); // Greater than or equal
$metric->exceedsThreshold(50, '<'); // Less than
$metric->exceedsThreshold(85, '=='); // Equal toProcess metrics and generate alerts in a single check:
$task = <<<METRICS
cpu_usage: 85.5
memory_usage: 72.3
disk_usage: 90.0
METRICS;
$result = $monitoringAgent->run($task);
if ($result->isSuccess()) {
echo $result->getAnswer();
$metadata = $result->getMetadata();
echo "Metrics analyzed: {$metadata['metrics_analyzed']}\n";
echo "Alerts generated: {$metadata['alerts_generated']}\n";
foreach ($metadata['alerts'] as $alert) {
echo "[{$alert['severity']}] {$alert['title']}\n";
}
}Monitor a data source continuously:
use ClaudeAgents\Contracts\MonitorableInterface;
class SystemMonitor implements MonitorableInterface
{
public function getMetrics(): array
{
return [
new Metric('cpu_usage', $this->getCpuUsage()),
new Metric('memory_usage', $this->getMemoryUsage()),
];
}
public function getName(): string
{
return 'system';
}
private function getCpuUsage(): float
{
// Get actual CPU usage
return 45.5;
}
private function getMemoryUsage(): float
{
// Get actual memory usage
return 62.3;
}
}
// Start monitoring
$monitor = new SystemMonitor();
$monitoringAgent->watch($monitor, function (Alert $alert) {
echo "Alert: {$alert->getTitle()}\n";
echo "Severity: {$alert->getSeverity()}\n";
// Send notification, log, etc.
});
// Stop monitoring
$monitoringAgent->stop();The MonitoringAgent generates two types of alerts:
Generated when metrics exceed configured thresholds:
$monitoringAgent = new MonitoringAgent($client, [
'thresholds' => [
'cpu_usage' => 80,
'memory_usage' => 90,
],
]);
// This will generate threshold alerts
$result = $monitoringAgent->run("cpu_usage: 95\nmemory_usage: 92");Uses Claude AI to detect unusual patterns in historical data:
// Requires historical data (5+ data points)
// Anomalies are detected automatically when patterns deviate from history
$task = "cpu_usage: 45\nmemory_usage: 50";
// First few checks build history
for ($i = 0; $i < 10; $i++) {
$monitoringAgent->run($task);
sleep(60);
}
// Now LLM can detect anomalies
// e.g., sudden spike to cpu_usage: 95 would trigger anomaly alertCreate custom data sources by implementing the MonitorableInterface:
use ClaudeAgents\Contracts\MonitorableInterface;
use ClaudeAgents\Monitoring\Metric;
class DatabaseMonitor implements MonitorableInterface
{
public function getMetrics(): array
{
return [
new Metric('connections', $this->getConnectionCount(), [
'max' => 100,
'host' => 'db-primary',
]),
new Metric('query_time', $this->getAverageQueryTime(), [
'unit' => 'ms',
'host' => 'db-primary',
]),
new Metric('slow_queries', $this->getSlowQueryCount(), [
'threshold' => '1s',
'host' => 'db-primary',
]),
];
}
public function getName(): string
{
return 'database-primary';
}
private function getConnectionCount(): int
{
// Get actual connection count
return 45;
}
private function getAverageQueryTime(): float
{
// Get average query time in ms
return 25.5;
}
private function getSlowQueryCount(): int
{
// Get count of slow queries
return 2;
}
}
// Use the monitor
$dbMonitor = new DatabaseMonitor();
$monitoringAgent->watch($dbMonitor, function (Alert $alert) {
// Handle alerts
});When using run(), metrics should be formatted as:
metric_name: value
Examples:
cpu_usage: 75.5
memory_usage: 82
disk_space: 90.0
response_time: 250
error_count: 5
The parser automatically:
- Converts numeric strings to numbers
- Preserves string values for non-numeric data
- Skips invalid lines
Alerts are generated as Alert objects:
use ClaudeAgents\Monitoring\Alert;
// Alert properties
$alert->getId(); // Unique alert ID
$alert->getTitle(); // Alert title
$alert->getMessage(); // Alert message
$alert->getSeverity(); // Severity level
$alert->getMetric(); // Associated metric (if any)
$alert->getContext(); // Additional context
$alert->getTimestamp(); // When alert was created
// Severity check
if ($alert->isCritical()) {
// Handle critical alert
}
// Convert to array
$data = $alert->toArray();Alert severity levels:
Alert::SEVERITY_INFO // Informational
Alert::SEVERITY_WARNING // Warning condition
Alert::SEVERITY_ERROR // Error condition
Alert::SEVERITY_CRITICAL // Critical conditionCombine MonitoringAgent with AlertAgent for comprehensive monitoring and alerting:
use ClaudeAgents\Agents\MonitoringAgent;
use ClaudeAgents\Agents\AlertAgent;
$monitoringAgent = new MonitoringAgent($client, [
'thresholds' => ['cpu_usage' => 80],
]);
$alertAgent = new AlertAgent($client);
$alertAgent->registerChannel('slack', function ($alert, $message) {
// Send to Slack
});
// Monitor and alert
$result = $monitoringAgent->run("cpu_usage: 95");
if ($result->isSuccess()) {
$metadata = $result->getMetadata();
foreach ($metadata['alerts'] as $alertData) {
$alert = new Alert(
$alertData['title'],
$alertData['message'],
$alertData['severity']
);
$alertAgent->processAlert($alert);
}
}Monitor multiple data sources simultaneously:
$sources = [
new DatabaseMonitor('db-primary'),
new ApplicationMonitor('web-app'),
new InfrastructureMonitor('k8s-cluster'),
];
foreach ($sources as $source) {
$metrics = $source->getMetrics();
$metricLines = array_map(
fn($m) => "{$m->getName()}: {$m->getValue()}",
$metrics
);
$task = implode("\n", $metricLines);
$result = $monitoringAgent->run($task);
if ($result->isSuccess()) {
$metadata = $result->getMetadata();
echo "{$source->getName()}: {$metadata['alerts_generated']} alerts\n";
}
}Create a long-running monitoring service:
class MonitoringService
{
private MonitoringAgent $agent;
private array $sources = [];
public function __construct(MonitoringAgent $agent)
{
$this->agent = $agent;
}
public function addSource(MonitorableInterface $source): void
{
$this->sources[] = $source;
}
public function run(): void
{
foreach ($this->sources as $source) {
$this->agent->watch($source, function (Alert $alert) {
$this->handleAlert($alert);
});
}
}
private function handleAlert(Alert $alert): void
{
// Log alert
error_log("[{$alert->getSeverity()}] {$alert->getTitle()}");
// Send notifications based on severity
if ($alert->isCritical()) {
$this->sendCriticalNotification($alert);
}
}
}Set thresholds based on your system's normal operating range:
$monitoringAgent = new MonitoringAgent($client, [
'thresholds' => [
// Set based on historical data and capacity
'cpu_usage' => 80, // 80% CPU
'memory_usage' => 85, // 85% memory
'disk_usage' => 90, // 90% disk
'response_time' => 500, // 500ms response
'error_rate' => 1.0, // 1% error rate
],
]);Include relevant metadata with metrics:
$metric = new Metric('query_time', 350, [
'query_type' => 'SELECT',
'table' => 'users',
'endpoint' => '/api/users',
'host' => 'db-replica-2',
]);try {
$result = $monitoringAgent->run($task);
if ($result->isSuccess()) {
// Process result
} else {
error_log("Monitoring failed: {$result->getError()}");
}
} catch (\Throwable $e) {
error_log("Monitoring exception: {$e->getMessage()}");
}Include comprehensive metrics for better insights:
$task = <<<METRICS
cpu_usage: 75
memory_usage: 80
disk_usage: 65
network_latency: 45
active_connections: 150
request_rate: 1200
error_rate: 0.5
response_time_p95: 250
METRICS;Build sufficient history before relying on anomaly detection:
// Build baseline (run periodically)
for ($i = 0; $i < 20; $i++) {
$monitoringAgent->run($metricsTask);
sleep(60);
}
// Now anomaly detection will be effectiveThe MonitoringAgent uses Claude AI to detect anomalies when:
- Sufficient History: At least 5 historical data points exist
- Pattern Analysis: Claude analyzes current value against historical pattern
- Deviation Detection: Identifies sudden spikes, drops, or unusual deviations
Example anomaly detection prompt:
Analyze this metric for anomalies:
Metric: cpu_usage
Current Value: 95
Historical Values (recent): [45, 47, 43, 46, 44]
Is the current value anomalous compared to the historical pattern?
Claude will identify the spike from ~45 to 95 as an anomaly.
- Threshold Alerts: Instant, no API calls, predictable
- LLM Anomaly Detection: Requires API call, more intelligent, detects patterns
- Use Thresholds First: Configure thresholds for known limits
- Limit LLM Calls: Anomaly detection only runs when sufficient history exists
- Adjust Check Interval: Balance between responsiveness and API usage
- Batch Metrics: Analyze multiple metrics in one API call
// Efficient: Single API call for multiple metrics
$task = "cpu: 75\nmemory: 80\ndisk: 65";
$monitoringAgent->run($task);
// Inefficient: Multiple API calls
$monitoringAgent->run("cpu: 75");
$monitoringAgent->run("memory: 80");
$monitoringAgent->run("disk: 65");Create a new MonitoringAgent instance.
Analyze metrics and generate alerts. Returns result with:
answer: Formatted analysis reportmetadata['metrics_analyzed']: Number of metrics analyzedmetadata['alerts_generated']: Number of alerts generatedmetadata['alerts']: Array of alert data
Continuously monitor a data source. Calls $onAlert callback for each alert.
Stop continuous monitoring.
Get the agent name.
Get all generated alerts.
Get the metric name.
Get the metric value.
Get metric metadata.
Get the metric timestamp (Unix timestamp).
Check if metric exceeds threshold with given operator.
Convert metric to array representation.
See AlertAgent documentation for complete Alert API reference.
-
Check Thresholds: Ensure thresholds are configured
$monitoringAgent = new MonitoringAgent($client, [ 'thresholds' => ['cpu_usage' => 80], ]);
-
Verify Metric Format: Ensure metrics follow
name: valueformat// Correct "cpu_usage: 85" // Incorrect "cpu_usage=85" "85"
-
Check Metric Values: Ensure values are numeric for threshold checks
- Build History First: Need at least 5 data points
- Check API Key: Ensure Claude API key is valid
- Review Logs: Check logger output for errors
- Increase Check Interval: Reduce monitoring frequency
- Use Thresholds: Rely on thresholds instead of LLM when possible
- Batch Metrics: Combine multiple metrics in single calls
- AlertAgent Documentation - Alert processing and notifications
- MonitoringAgent Tutorial - Step-by-step guide
- Basic Example - Simple monitoring example
- Advanced Example - Multi-source monitoring