Kafka智能管理工具

KafkaIQ是一款通过Model Control Protocol (MCP)实现的智能Kafka管理工具,允许AI助手通过自然语言命令无缝交互和管理Kafka集群,适用于DevOps、数据工程和开发工作流等多种场景。

  • by Ashfaqbs
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Ashfaqbs/KafkaIQ

使用说明

KafkaIQ 🧠⚡(可译为“KafkaIQ 智慧闪电”或根据语境简化为“KafkaIQ 智能”)

通过模型控制协议(MCP)实现AI驱动的Kafka管理

KafkaIQ是一款智能的Kafka管理工具,它通过模型控制协议(MCP)暴露Apache Kafka的操作,使AI助手能够通过自然语言命令无缝地与Kafka集群进行交互和管理。

🚀 概述

利用人工智能的力量,彻底改变您的Kafka操作体验。KafkaIQ作为连接AI系统与Kafka基础设施的智能中间件层,能够将自然语言请求转化为精确的Kafka操作。

AI Assistant → MCP Protocol → KafkaIQ → Apache Kafka Cluster

✨ 特点/功能

  • 🤖 原生AI集成专为使用MCP协议的AI助手而设计
  • ⚡ 实时操作即时主题管理、配置和监控
  • 🔍 智能检测智能聚类分析与健康监测
  • 📊 高级分析消费者滞后分析与分区领导跟踪
  • 🛡️ 主动预警自动健康检查,状态分为CRITICAL(严重)/WARN(警告)/OK(正常)
  • 📧 电子邮件通知内置Gmail SMTP集成以发送警报
  • 🌐 HTTP API用于轻松集成的RESTful流式HTTP接口

🛠️ 可用工具

工具 描述 示例用法
initialize_kafka_connection 连接到 Kafka 集群 连接到 localhost:9092
list_kafka_topics 列出所有主题 显示我所有主题
describe_kafka_topic 获取主题详情 描述“订单”主题
create_kafka_topic 创建新主题 创建名为 'events' 的主题,包含3个分区
delete_kafka_topic 删除主题 移除“旧日志”主题
get_kafka_topic_config 获取主题配置 显示“user-data”的配置
kafka_health_check 检查连接状态 Kafka 是否正常?
get_cluster_details 获取集群信息 显示集群概览
get_consumer_lag 分析消费者滞后 检查“处理器”组的滞后情况
kafka_alert_summary 智能健康分析 给我集群健康状态
broker_leadership_distribution 分区领导权分析 显示代理分区分布
get_offline_partitions 查找离线分区 列出所有离线分区
get_broker_resources 获取代理资源 显示代理-1的资源
send_email_notification 发送邮件提醒 向管理员发送集群状态邮件

📦 安装

先决条件

  • Python 3.8及以上版本
  • Apache Kafka 集群
  • Gmail账户(用于电子邮件通知)

设置

# Clone the repository
git clone https://github.com/ashfaqbs/kafkaiq.git
cd kafkaiq

# Install dependencies
pip install fastmcp kafka-python

# Set email credentials (optional)
export KAFKAIQ_GMAIL_USER="your-email@gmail.com"
export KAFKAIQ_GMAIL_PASSWORD="your-app-password"

# Run the server
python kafka_mcp_server.py

🚀 快速入门

1. 启动KafkaIQ服务器

python kafka_mcp_server.py

服务器开始运行于 http://127.0.0.1:8080

2. 连接到您的AI助手

将您的AI助手配置为使用位于(此处)的MCP终端 http://127.0.0.1:8080

3. 开始用自然语言管理Kafka!

💬 示例对话

基本操作

连接设置:

Human: Connect to my local Kafka cluster server : localhost:9092
AI → KafkaIQ: initialize_kafka_connection("localhost:9092")
KafkaIQ Response: "Successfully connected to Kafka cluster at localhost:9092"
AI → Human: "Connected to your Kafka cluster successfully!"

主题管理:

Human: What topics exist in my cluster?
AI → KafkaIQ: list_kafka_topics()
KafkaIQ Response: {
  "topics": ["orders", "users", "events", "payments"],
  "count": 4,
  "include_internal": false
}
AI → Human: "Your cluster has 4 topics: orders, users, events, and payments."

创建新主题:

Human: Create a topic called 'notifications' with 5 partitions
AI → KafkaIQ: create_kafka_topic("notifications", 5, 1)
KafkaIQ Response: {
  "status": "success",
  "message": "Topic 'notifications' created successfully",
  "topic_name": "notifications",
  "num_partitions": 5,
  "replication_factor": 1
}
AI → Human: "Successfully created topic 'notifications' with 5 partitions!"

健康监测

集群健康检查:

Human: How is my Kafka cluster doing?
AI → KafkaIQ: kafka_alert_summary()
KafkaIQ Response: {
  "status": "OK",
  "signals": [],
  "summary": {
    "cluster_id": "kafka-cluster-1",
    "broker_count": 3,
    "topics_count": 15,
    "under_replicated": 0,
    "offline_partitions": 0,
    "single_broker": false,
    "lag": [
      {"group": "processors", "topic": "orders", "total_lag": 50, "level": "OK"},
      {"group": "analytics", "topic": "events", "total_lag": 1250, "level": "OK"}
    ]
  }
}
AI → Human: "Your Kafka cluster is healthy! 3 brokers running, 15 topics, no offline partitions or under-replicated partitions. Consumer lag looks good across all groups."

消费者滞后分析

Human: Check consumer lag for the 'analytics' group on 'events' topic
AI → KafkaIQ: get_consumer_lag("analytics", "events")
KafkaIQ Response: {
  "group_id": "analytics",
  "topic": "events",
  "total_lag": 1250,
  "partitions": [
    {"partition": 0, "consumer_offset": 10000, "high_water_mark": 10500, "lag": 500},
    {"partition": 1, "consumer_offset": 8000, "high_water_mark": 8750, "lag": 750}
  ]
}
AI → Human: "The 'analytics' group has a total lag of 1,250 messages on the 'events' topic. Partition 0 has 500 messages lag, partition 1 has 750 messages lag."

高级分析

分区领导权:

Human: Show me how partitions are distributed across brokers
AI → KafkaIQ: broker_leadership_distribution()
KafkaIQ Response: {
  "total_partitions": 45,
  "distribution": {
    "1": 15,
    "2": 15, 
    "3": 15
  },
  "by_topic": {
    "orders": {"1": [0, 3], "2": [1, 4], "3": [2, 5]},
    "events": {"1": [0, 2], "2": [1, 3], "3": [4, 5]}
  }
}
AI → Human: "Partition leadership is well-balanced! Each of your 3 brokers leads exactly 15 partitions out of 45 total. The distribution looks optimal."

离线分区检查:

Human: Are there any offline partitions?
AI → KafkaIQ: get_offline_partitions()
KafkaIQ Response: {
  "offline_count": 0,
  "by_topic": {}
}
AI → Human: "Great news! There are no offline partitions in your cluster. All partitions have active leaders."

资源监控

经纪人资源:

Human: What are the resources for broker-1?
AI → KafkaIQ: get_broker_resources("broker-1")
KafkaIQ Response: {
  "broker_id": "broker-1",
  "resources": {
    "ram_gb": 32,
    "storage_gb": 1000,
    "cores": 8
  },
  "status": "active",
  "note": "Hardcoded values - integrate with monitoring system"
}
AI → Human: "Broker-1 has 32GB RAM, 1TB storage, and 8 CPU cores. Status is active."

电子邮件通知

发送警报:

Human: Email the cluster status to admin@company.com
AI → KafkaIQ: send_email_notification("admin@company.com", "Cluster Status: OK\nBrokers: 3\nTopics: 15\nNo issues detected", "KafkaIQ Daily Report")
KafkaIQ Response: {
  "status": "sent",
  "message": "Email sent successfully",
  "recipient": "admin@company.com",
  "subject": "KafkaIQ Daily Report",
  "timestamp": "2025-09-28T10:30:00"
}
AI → Human: "Successfully sent the cluster status report to admin@company.com!"

🎯 应用场景

DevOps与基础设施管理

  • 自动化监控“如果任何分区下线,请提醒我”
  • 健康仪表板“生成每日集群健康报告”
  • 事件响应“是什么导致了消费滞后现象?”
  • 容量规划“展示我经纪人资源的利用率”

数据工程

  • 管道管理“为新数据流创建主题”
  • 性能调优“哪些消费者落后了?”
  • 数据质量“监控主题配置以确保合规性”
  • 流处理“为实时分析设置主题”

开发工作流程

  • 环境设置“创建带有适当配置的开发主题”
  • 测试“集成测试后清理测试主题”
  • 调试“为什么消息没有被消费?”
  • 配置管理“备份主题配置”

商业智能

  • 运营洞察“生成每周Kafka使用报告”
  • 成本优化“找出利用不足的话题”
  • SLA 监控“跟踪消费者延迟情况,对照SLA(服务级别协议)”
  • 趋势分析“显示分区分布趋势”

🏗️ 建筑学

核心组件

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   AI Assistant  │    │     KafkaIQ      │    │  Kafka Cluster  │
│                 │◄──►│   MCP Server     │◄──►│                 │
│ (Claude, GPT)   │    │   (Port 8080)    │    │  (Brokers)      │
└─────────────────┘    └──────────────────┘    └─────────────────┘
         │                        │                       │
         │                        │                       │
         ▼                        ▼                       ▼
┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│ Natural Language│    │  Tool Execution  │    │   Operations    │
│   Commands      │    │   & Response     │    │  & Monitoring   │
└─────────────────┘    └──────────────────┘    └─────────────────┘

关键技术

  • FastMCP模型控制协议实现
  • kafka-pythonKafka客户端库
  • **Gmail SMTP(服务器)**电子邮件通知系统
  • **JSON(JavaScript对象表示法)**结构化数据交换
  • **HTTP/REST(超文本传输协议/表述性状态传递)**通信协议

🔧 配置

环境变量

# Email Configuration (Optional)
KAFKAIQ_GMAIL_USER=your-email@gmail.com
KAFKAIQ_GMAIL_PASSWORD=your-app-password

警报阈值

自定义警报阈值 kafka_inspector.alert_summary():

# Built-in thresholds (edit as needed)
offline_crit = 1              # Any offline partition = CRITICAL
urp_warn, urp_crit = 1, 5     # Under-replicated partitions
lag_warn, lag_crit = 10_000, 100_000  # Consumer lag thresholds
single_broker_warn = True     # Warn on single broker setup

🔐 安全

认证

  • 卡夫卡支持SASL/PLAIN、SASL/SCRAM、SSL/TLS
  • 电子邮件Gmail 应用程序密码(需要 2FA)
  • MCP可以翻译为“最小成本生产”或“最低成本路径”,具体含义取决于上下文。在商业或生产管理领域,它通常指的是以最低成本实现生产目标或完成任务的方法或路径基于HTTP,具备API密钥集成潜力

最佳实践

  • 使用环境变量存储凭据
  • 在生产环境中启用Kafka的SSL/TLS
  • 实施网络级别的访问控制
  • 定期更换凭证
  • 监控访问日志

📊 监控与可观测性

内置指标

  • 集群健康状态(正常/警告/严重)
  • 分区领导分配
  • 各群体消费者的消费滞后
  • 离线分区检测
  • 分区监控不足

集成点

  • 普罗米修斯指标导出(未来增强功能)
  • **Grafana(音译为“格拉夫纳”,但通常直接使用其英文名,因其是一个知名的数据可视化工具)**仪表盘集成
  • **Slack/Teams(保持原样,因为这是专有名词,通常不翻译,直接作为软件名称使用)**警报转发
  • **PagerDuty(注:这是一个专有名词,通常直接音译,表示一个用于监控和告警管理的软件平台)**事件管理
  • SIEM安全事件日志记录

🤝 AI助手集成

支持的平台

  • 克劳德 (Anthropic) - 主要开发平台
  • ChatGPT (OpenAI) - 兼容MCP
  • 定制化人工智能系统 - 任何兼容MCP的助手

集成步骤

  1. 启动KafkaIQ服务器
  2. 配置具有MCP端点的人工智能助手
  3. 使用基本命令测试连接
  4. 建立监控和告警工作流程

🚀 高级用法

批量操作

Human: Create 5 topics for microservices: user-service, order-service, payment-service, notification-service, audit-service
AI: I'll create all 5 topics for your microservices architecture...

自动化工作流

Human: Set up monitoring that emails me if any consumer lag exceeds 50,000 messages
AI: I'll configure automated monitoring with email alerts for high consumer lag...

自定义配置

# Custom topic configuration example
topic_configs = {
    "retention.ms": "604800000",  # 7 days
    "compression.type": "lz4",
    "max.message.bytes": "1048576"  # 1MB
}

🐛 故障排除

常见问题

连接失败

Error: "Failed to connect to Kafka cluster"
Solution: Check if Kafka is running on specified port

邮件无法发送

Error: "Gmail authentication failed"
Solution: Enable 2FA and generate App Password

消费者延迟超时

Error: "Consumer lag check timed out"
Solution: Increase timeout in KafkaConsumer config

权限被拒绝

Error: "Not authorized to perform operation"
Solution: Check Kafka ACLs and user permissions

🤝 贡献(或“参与贡献”)

我们欢迎投稿!

开发环境设置

# Clone and setup development environment
git clone https://github.com/yourusername/kafkaiq.git
cd kafkaiq

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate     # Windows

# Install development dependencies
pip install -r requirements.txt

# Run tests
python -m pytest tests/

# Start development server
python kafka_mcp_server.py

贡献领域

  • 🐛 修复漏洞并提高稳定性
  • ✨ 新工具的实现
  • 📚 文档和示例
  • 🧪 测试覆盖率扩展
  • 🔧 性能优化

📄 许可证

这个项目遵循MIT许可证授权——详见 许可证 文件中有详细信息。

🙏 致谢

  • FastMCP团队对于卓越的MCP框架
  • Kafka 社区对于强大的kafka-python库
  • **Anthropic(公司名,可译为“安萨里奇”或根据具体语境保留原名)**为Claude和MCP协议开发

📞 支持与社区

  • 🐛 这个符号在中文里通常被用作表示“虫子”或者“错误(bug)”的简写,尤其是在编程或技术领域中。所以,可以翻译为“虫子”或“错误(bug)”。 问题: GitHub 问题(或议题)
  • 💡(灯泡图标,常用于表示灵感、创意或想法) 讨论: GitHub 讨论区

🏷️ 标签

kafka ai mcp devops monitoring alerts data-engineering stream-processing automation python fastmcp claude chatgpt infrastructure


为人工智能和数据工程社区倾心打造

利用AI的力量,革新您的Kafka运维体验——即刻体验KafkaIQ!