AIOps combines machine learning with IT operations to automate incident detection, diagnosis, and resolution.
What is AIOps?
AIOps platforms aggregate and analyze IT operations data to:
- Detect anomalies automatically
- Predict incidents before they occur
- Automate remediation
- Reduce MTTR (Mean Time To Resolve)
Key Components
Data Collection
typescript
// metrics-collector.ts
import Prometheus from 'prom-client';
import { v4 as uuidv4 } from 'uuid';
class MetricsCollector {
private metrics = new Map();
collect(metricName: string, value: number, labels: Record<string, any>) {
const key = `${metricName}:${JSON.stringify(labels)}`;
if (!this.metrics.has(key)) {
this.metrics.set(key, {
name: metricName,
labels,
values: [],
timestamps: []
});
}
const metric = this.metrics.get(key);
metric.values.push(value);
metric.timestamps.push(Date.now());
}
getMetrics(metricName: string) {
return Array.from(this.metrics.values())
.filter(m => m.name === metricName);
}
}Anomaly Detection
python
# anomaly_detection.py
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
class AnomalyDetector:
def __init__(self, contamination=0.1):
self.scaler = StandardScaler()
self.model = IsolationForest(contamination=contamination)
self.fitted = False
def fit(self, X):
"""Train on normal data"""
X_scaled = self.scaler.fit_transform(X)
self.model.fit(X_scaled)
self.fitted = True
def predict(self, X):
"""Return -1 for anomaly, 1 for normal"""
if not self.fitted:
raise Exception("Model not fitted")
X_scaled = self.scaler.transform(X)
predictions = self.model.predict(X_scaled)
return predictions
def anomaly_score(self, X):
"""Return anomaly scores"""
X_scaled = self.scaler.transform(X)
scores = self.model.score_samples(X_scaled)
return -scores # Negative because lower is more anomalousPredictive Alerting
python
# predictive_alerting.py
import pandas as pd
from sklearn.linear_model import LinearRegression
from datetime import datetime, timedelta
class PredictiveAlerter:
def __init__(self, lookback_hours=24):
self.lookback = timedelta(hours=lookback_hours)
self.model = LinearRegression()
def train(self, metric_data):
"""Train model on historical data"""
# Convert timestamps to relative hours
X = (metric_data['timestamp'] - metric_data['timestamp'].min()).dt.total_seconds() / 3600
X = X.values.reshape(-1, 1)
y = metric_data['value'].values
self.model.fit(X, y)
def predict_future(self, current_time, hours_ahead=1):
"""Predict metric value hours_ahead from now"""
future_time = current_time + timedelta(hours=hours_ahead)
X_future = ((future_time - current_time).total_seconds() / 3600).reshape(-1, 1)
return self.model.predict(X_future)[0]
def will_alert(self, current_time, threshold, hours_ahead=1):
"""Check if predicted value exceeds threshold"""
predicted = self.predict_future(current_time, hours_ahead)
return predicted > thresholdIncident Automation
typescript
// incident-automation.ts
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
interface Incident {
id: string;
type: string;
severity: 'low' | 'medium' | 'high';
description: string;
metrics: any;
}
class IncidentResponder {
async restartService(serviceName: string) {
const { stdout, stderr } = await execAsync(
`kubectl rollout restart deployment/${serviceName}`
);
return { stdout, stderr };
}
async scaleUp(deploymentName: string, replicas: number) {
const { stdout, stderr } = await execAsync(
`kubectl scale deployment/${deploymentName} --replicas=${replicas}`
);
return { stdout, stderr };
}
async clearCache() {
// Redis cache clearing logic
const { stdout, stderr } = await execAsync(
'redis-cli FLUSHALL'
);
return { stdout, stderr };
}
async autoRemediate(incident: Incident) {
const actions = [];
if (incident.type === 'high_memory') {
const result = await this.restartService('memory-hog-service');
actions.push({ action: 'restart', result });
}
if (incident.type === 'high_latency') {
const result = await this.scaleUp('api-server', 6);
actions.push({ action: 'scale', result });
}
if (incident.type === 'cache_stale') {
const result = await this.clearCache();
actions.push({ action: 'clear_cache', result });
}
return actions;
}
}ChatOps Integration
typescript
// chatops.ts
import WebClient from '@slack/web-api';
const slack = new WebClient(process.env.SLACK_TOKEN);
class ChatOps {
async notifyIncident(incident: Incident) {
await slack.chat.postMessage({
channel: '#incidents',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `🚨 *Incident Detected*
`
`*Type:* ${incident.type}
`
`*Severity:* ${incident.severity}
`
`*Description:* ${incident.description}`
}
},
{
type: 'actions',
elements: [
{
type: 'button',
text: { type: 'plain_text', text: 'Auto-Remediate' },
action_id: 'auto_remediate',
value: incident.id
},
{
type: 'button',
text: { type: 'plain_text', text: 'Acknowledge' },
action_id: 'acknowledge',
value: incident.id
}
]
}
]
});
}
}Best Practices
- 1Start with data collection
- 2Use unsupervised learning for anomaly detection
- 3Implement human-in-the-loop for critical actions
- 4Monitor the AIOps system itself
- 5Document all automated actions
- 6Maintain rollback procedures
Conclusion
AIOps transforms IT operations from reactive to predictive. Start small, prove value, and expand systematically.