前几部分写的都是围绕着报警中心的一些外围功能。本篇开始描述报警中心的各个功能。
概述
报警中心需要的一些功能模块:
- 用户管理:用户信息,登录接入,权限等
- 报警记录:记录报警的各项信息
- 通知规则:配置报警通知规则
- 计划任务:设定计划任务,根据通知规则进行通知行为的触发
- 主机信息:记录报警主机信息,从CMDB中按需同步
- 项目(分组)信息:记录项目(分组)信息,从CMDB中按需同步
- 数据统计:各个维度统计记录报警频率数据等
数据库设计
从概述中可梳理出一些需要处理的数据:
- 用户 accounts
- 报警记录 events
- 主机 hosts
- 项目 projects
- 规则 rules
使用Flask-SQLAlchemy
from db import db
class Base(db.Model):
"""基类"""
# 作为父类被继承,不会被创建成表(抽象)
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
def save(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def update(self):
db.session.commit()
class Account(Base):
"""
"""
__tablename__ = "accounts"
account_name = db.Column(db.String(20)) # name
account_nickname = db.Column(db.String(20))
dingding_id = db.Column(db.String(50)) # dingding id
mobile = db.Column(db.String(15)) # mobile phone
account_dept = db.Column(db.String(50))
account_project = db.relationship('Account_Project', backref='account_project', lazy='dynamic'')
class Event(Base):
"""
"""
__tablename__ = "events"
start_time = db.Column(db.DateTime)
end_time = db.Column(db.DateTime)
info = db.Column(db.String(600))
status = db.Column(db.String(10))
event_id = db.Column(db.Integer) # 操作id,用秒的时间戳
class Project(Base):
"""
"""
__tablename__ = "projects"
project_name = db.Column(db.String(10))
project_webhook = db.Column(db.String(150))
host = db.Column(db.Integer, db.ForeignKey('hosts.id'))
account_project = db.relationship('Account_Project', backref='account_project', lazy='dynamic'')
class Account_Project(Base):
"""
"""
__tablename__ = "account_project"
account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'))
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
accout = db.relationship('Account', backref='accounts', lazy=True)
project = db.relationship('Project', backref='projects', lazy=True)
class Host(Base):
"""
"""
__tablename__ = "hosts"
ip = db.Column(db.String(15))
hostname = db.Column(db.String(50))
project = db.relationship('Project', backref='projects', lazy=True)
class Rule(Base):
"""
"""
__tablename__ = "rules"
rule_name = db.Column(db.String(20))
rule_content = db.Column(db.String(200))