santosh-mourya7/automated-incident-response

GitHub: santosh-mourya7/automated-incident-response

基于 GitHub Actions、ServiceNow 和 Slack 的自动化事件响应系统,实现服务故障自动检测、工单创建和团队告警的完整闭环。

Stars: 0 | Forks: 0

# 🚨 自动化事件响应系统 ![GitHub Actions](https://img.shields.io/badge/GitHub_Actions-2088FF?style=for-the-badge&logo=github-actions&logoColor=white) ![Python](https://img.shields.io/badge/Python-3.11-3776AB?style=for-the-badge&logo=python&logoColor=white) ![ServiceNow](https://img.shields.io/badge/ServiceNow-00C853?style=for-the-badge&logo=servicenow&logoColor=white) ![Slack](https://img.shields.io/badge/Slack-4A154B?style=for-the-badge&logo=slack&logoColor=white) ![Render](https://img.shields.io/badge/Render-46E3B7?style=for-the-badge&logo=render&logoColor=white) ## 📋 目录 • 概述 • 架构 • 功能 • 技术栈 • 项目结构 • 快速开始 • 配置 • 工作流 • API 参考 • 测试 ## 📌 概述 本项目自动化了完整的事件响应生命周期 —— 从检测到解决。当服务宕机时,系统会: 1. 通过定时健康检查**检测**故障 2. 自动**创建** ServiceNow 事件 3. 立即通过 Slack **警报**通知值班团队 4. 服务恢复时**解决**事件 检测和警报是完全自动化的。 事件解决可通过 GitHub Actions 工作流一键完成。 ## 🏗️ 架构 ``` ┌─────────────────────────────────────────────────────────┐ │ GitHub Actions │ │ │ │ ┌─────────────┐ Fails ┌──────────────────┐ │ │ │ health_ │ ─────────────► │ create_ │ │ │ │ check.py │ │ incident.py │ │ │ └─────────────┘ └────────┬─────────┘ │ │ (Every 15 mins) │ │ └───────────────────────────────────────────-│────────────┘ │ ┌────────────────────────┤ │ │ ▼ ▼ ┌───────────────────┐ ┌───────────────────────┐ │ ServiceNow │ │ Slack │ │ Incident Created │ │ Alert Delivered │ │ INC0010001 🎫 │ │ #incidents 🔔 │ └───────────────────┘ └───────────────────────┘ │ ▼ ┌───────────────────┐ │ resolve_ │ │ incident.py │ │ (Manual trigger) │ └───────────────────┘ │ ▼ ┌───────────────────┐ │ Incident Closed │ │ ✅ Resolved │ └───────────────────┘ ``` ## ✨ 功能 | 功能 | 描述 | |---|---| | 🔍 **自动健康监控** | 通过 GitHub Actions 每隔 15 分钟进行定时检查 | | 🎫 **ServiceNow 集成** | 通过 REST API 自动创建带有优先级和分类的事件 | | 🔔 **Slack 警报** | 将包含事件详情的实时通知发送至团队频道 | | ✅ **自动解决** | 一键工作流解决事件并通知团队服务已恢复 | | 🔒 **安全密钥管理** | 所有凭证均作为 GitHub Secrets 存储 —— 绝不硬编码 | | 🧪 **自动化测试** | 针对所有核心功能的 pytest 单元测试 | | 🚀 **CI/CD 流水线** | 完整的 GitHub Actions 流水线,无需管理基础设施 | ## 🛠️ 技术栈 | 层级 | 技术 | 用途 | |---|---|---| | **自动化** | GitHub Actions | 工作流编排 | | **后端** | Python 3.11 | 核心脚本和 API 集成逻辑 | | **ITSM** | ServiceNow PDI | 事件创建、跟踪和解决 | | **通知** | Slack Webhooks | 实时团队警报 | | **托管** | Render (免费层) | 被监控的 Web 服务端点 | | **REST 客户端** | requests 2.31.0 | 调用 ServiceNow 和 Slack API 的 HTTP 请求 | | **测试** | pytest 9.0.2 | 所有 Python 脚本的单元测试 | ## 📁 项目结构 ``` automated-incident-response/ │ ├── .github/ │ └── workflows/ │ ├── monitor.yml # Scheduled health check + incident creation │ └── resolve.yml # Manual incident resolution workflow │ ├── scripts/ │ ├── create_incident.py # ServiceNow incident creation via REST API │ ├── resolve_incident.py # ServiceNow incident resolution via REST API │ ├── notify_slack.py # Slack webhook notification handler │ └── health_check.py # HTTP health check for monitored service │ ├── tests/ │ └── test_incidents.py # pytest unit tests │ ├── docs/ │ └── architecture.txt # System architecture diagram │ ├── app.py # Flask health endpoint (deployed on Render) ├── render.yaml # Render deployment configuration ├── requirements.txt # Python dependencies └── README.md # Project documentation ``` ## 🚀 快速开始 ### 前置条件 - GitHub 账户 - ServiceNow 个人开发者实例 → [developer.servicenow.com](https://developer.servicenow.com) - 启用了传入 Webhooks 的 Slack 工作区 - Render 账户 → [render.com](https://render.com) - Python 3.11+ ### 安装 **1. 克隆仓库** ``` git clone https://github.com/YOUR-USERNAME/automated-incident-response.git cd automated-incident-response ``` **2. 安装依赖** ``` pip install -r requirements.txt ``` **3. 配置 GitHub Secrets** *(见下方配置部分)* **4. 在 Render 上部署被监控应用** ``` # Push to GitHub → 在 Render 上连接 repo → 自动 deploy git push origin main ``` ## ⚙️ 配置 所有敏感凭证均作为 **GitHub Repository Secrets** 存储。 操作路径:`Settings → Secrets and Variables → Actions → New Repository Secret` | Secret 名称 | 描述 | 示例 | |---|---|---| | `SNOW_INSTANCE` | ServiceNow 实例基础 URL | `https://devXXXXXX.service-now.com` | | `SNOW_USERNAME` | ServiceNow 管理员用户名 | `admin` | | `SNOW_PASSWORD` | ServiceNow 管理员密码 | `your-password` | | `SLACK_WEBHOOK_URL` | Slack 传入 Webhook URL | `https://hooks.slack.com/services/XXX/YYY/ZZZ` | | `MONITOR_URL` | 需监控的服务 URL | `https://your-app.onrender.com` | ## ⚡ 工作流 ### 1. 健康监控与事件创建器 **文件:** `.github/workflows/monitor.yml` **触发器:** - ⏰ 每 15 分钟自动运行 (`cron: '*/15 * * * *'`) - 🖱️ 通过 `workflow_dispatch` 手动触发 **流程:** ``` Run Health Check │ ├── ✅ PASS → No action taken │ └── ❌ FAIL → Create ServiceNow Incident + Send Slack Alert ``` ### 2. 自动解决事件 **文件:** `.github/workflows/resolve.yml` **触发器:** - 🖱️ 带输入参数的 `workflow_dispatch` 手动触发 **必需输入:** ``` incident_sys_id → ServiceNow internal SYS ID incident_number → Human-readable incident number (e.g. INC0010001) ``` **流程:** ``` Receive SYS ID + Incident Number │ ▼ Patch ServiceNow Incident → State: Resolved │ ▼ Send Slack Recovery Alert ✅ ``` ## 📡 API 参考 ### ServiceNow REST API **创建事件** ``` POST {SNOW_INSTANCE}/api/now/table/incident Authorization: Basic Auth Content-Type: application/json { "short_description": "Service Downtime Detected", "description": "Health check failed. Automated response triggered.", "urgency": "1", "impact": "2", "category": "software", "caller_id": "admin" } ``` **解决事件** ``` PATCH {SNOW_INSTANCE}/api/now/table/incident/{sys_id} Authorization: Basic Auth Content-Type: application/json { "state": "6", "close_code": "Solved (Permanently)", "close_notes": "Service recovered. Auto-resolved by monitoring system." } ``` ### Slack Webhook ``` POST https://hooks.slack.com/services/XXX/YYY/ZZZ Content-Type: application/json { "attachments": [{ "color": "danger", "title": "🚨 Incident Alert", "text": "Incident Created: INC0010001" }] } ``` ## 🧪 测试 在本地运行单元测试: ``` # Install dependencies pip install -r requirements.txt # Run all tests pytest tests/ -v # Expected output: # test_health_check_success PASSED # test_health_check_failure PASSED # test_health_check_timeout PASSED ``` ## 📊 实际运行效果 **ServiceNow 事件已创建:** ``` Number : INC0010001 Short Desc : Service Downtime Detected State : New Urgency : 1 - High Category : Software Opened : 2026-03-12 21:00:00 ``` **Slack 警报已接收:** ``` 🚨 Incident Alert ───────────────────────────── Incident Created: INC0010001 Summary: Service Downtime Detected Urgency: High Details: Health check failed. Automated response triggered. ───────────────────────────── Automated Incident Response System ``` ## 🙌 作者 本项目作为作品集构建,展示了使用 ServiceNow、GitHub Actions、Python REST API 和 Slack 集成的企业级自动化能力。
标签:ChatOps, GitHub Actions, ITSM, Python, Render, ServiceNow, Slack, SOAR, Webhook, 事件管理, 企业级安全, 健康监测, 力导向图, 告警通知, 安全规则引擎, 开源框架, 持续集成, 故障自愈, 无后门, 系统可用性, 自动化事件响应, 自动恢复, 自动笔记, 运维自动化, 逆向工具