class Threshold(db.Model): '''阀值表''' __tablename__ = 'thresholds' id = db.Column(db.Integer, primary_key=True) category_id = db.Column(db.Integer, db.ForeignKey('categories.id')) metric_id = db.Column(db.Integer, db.ForeignKey('metrics.id')) name = db.Column(db.String(60)) # 后台生成 alias = db.Column(db.String(100)) enabled = db.Column(db.Integer, default=1) alarm_class_id = db.Column(db.Integer, db.ForeignKey('alarm_classes.id')) occur_count = db.Column(db.Integer, default=1) summary = db.Column(db.String(200)) occur_cond1 = db.Column(db.String(60)) restore_cond1 = db.Column(db.String(60)) severity1 = db.Column(db.Integer, db.ForeignKey('alarm_severities.id'), default=4 ) occur_cond2 = db.Column(db.String(60)) restore_cond2 = db.Column(db.String(60)) severity2 = db.Column(db.Integer, db.ForeignKey('alarm_severities.id'), default=2) severity1_obj = db.relationship('AlarmSeverity', primaryjoin="AlarmSeverity.id==Threshold.severity1") severity2_obj = db.relationship('AlarmSeverity', primaryjoin="AlarmSeverity.id==Threshold.severity2") category = db.relationship('Category') metric = db.relationship('Metric') alarm_class = db.relationship('AlarmClass') def __unicode__(self): return u'<阀值 %s>' % self.alias
class AlarmJournal(db.Model): __tablename__ = 'alarm_journals' id = db.Column(db.Integer, primary_key=True) uid = db.Column(db.Integer, db.ForeignKey("users.id")) alarm_id = db.Column(db.Integer, db.ForeignKey("alarms.id")) title = db.Column(db.String(200)) summary = db.Column(db.String(200)) created_at = db.Column(db.DateTime) user = db.relationship('User') alarm = db.relationship('Alarm', backref=db.backref('journals', order_by=id))
class NodeEoc(NodeMixin, db.Model): """ Eocs """ __tablename__ = 'node_eocs' esn = db.Column(db.String(50)) # ESN contact_tel = db.Column(db.String(50)) # 联系电话 install_time = db.Column(db.DateTime) # 安装时间 ctrl_id = db.Column(db.Integer, db.ForeignKey('node_onus.id')) cpes = db.relationship("NodeCpe", backref="eoc") ports = db.relationship("PortEoc", backref="eoc") @property def cpe_count_plan(self): return object_session(self).\ scalar( select([func.count(NodeCpe.id)]).\ where(and_(NodeCpe.ctrl_id==self.id, NodeCpe.area_id != None)) ) @property def cpe_count_unplan(self): return object_session(self).\ scalar( select([func.count(NodeCpe.id)]).\ where(and_(NodeCpe.ctrl_id==self.id, NodeCpe.area_id == None)) ) def __unicode__(self): return u'<EOC %s>' % self.alias @staticmethod def export_columns(): return [ 'name', 'alias', 'addr', 'area.branch_name', 'vendor.alias', 'mask', 'snmp_comm', 'snmp_wcomm', 'snmp_ver', 'remark' ] @staticmethod def eoc_entrance(): eoc_entrance = db.session.query(NodeEoc.addr, Area.alias).outerjoin( Area, Area.branch == NodeEoc.area_id).filter(Area.area_type == 4).all() eoc_entrance_dict = {} for ip, entrance_name in eoc_entrance: if eoc_entrance_dict.get(ip): eoc_entrance_dict[ip] = eoc_entrance_dict[ip] + [entrance_name] else: eoc_entrance_dict[ip] = [entrance_name] return eoc_entrance_dict
class NodeOnu(NodeMixin, db.Model): """ ONU """ __tablename__ = 'node_onus' ctrl_id = db.Column(db.Integer, db.ForeignKey('node_olts.id')) eocs = db.relationship("NodeEoc", backref="onu") ports = db.relationship("PortOnu", backref="onu") def __unicode__(self): return u'<ONU %s>' % self.alias @staticmethod def export_columns(): return [ 'name', 'alias', 'addr', 'mac', 'olt.addr', 'area.entrance_name', 'snmp_comm', 'snmp_wcomm', 'snmp_ver', 'remark' ]
class AlarmVar(db.Model): __tablename__ = 'alarm_vars' id = db.Column(db.Integer, primary_key=True) alarm_id = db.Column(db.Integer, db.ForeignKey("alarms.id")) name = db.Column(db.String(60)) value = db.Column(db.String(100)) alarm = db.relationship('Alarm', backref=db.backref('vars', order_by=id))
class Vendor(db.Model): """Device Vendor""" __tablename__ = 'vendors' id = db.Column(db.Integer, primary_key=True) cityid = db.Column(db.Integer) type_id = db.Column(db.Integer) # 貌似没用? name = db.Column(db.String(100)) alias = db.Column(db.String(100)) url = db.Column(db.String(100)) # 厂商主页 is_valid = db.Column(db.Integer) models = db.relationship("Model", backref="vendor") @property def node_count(self): return object_session(self).\ scalar( select([func.count(Node.id)]).\ where(Node.vendor_id==self.id) ) @property def node_status1_count(self): return object_session(self).\ scalar( select([func.count(Node.id)]).\ where(and_(Node.vendor_id==self.id, Node.status == 1)) ) @property def node_status2_count(self): return object_session(self).\ scalar( select([func.count(Node.id)]).\ where(and_(Node.vendor_id==self.id, Node.status == 2)) ) @property def node_status3_count(self): return object_session(self).\ scalar( select([func.count(Node.id)]).\ where(and_(Node.vendor_id==self.id, Node.status == 3)) ) @property def node_status4_count(self): return object_session(self).\ scalar( select([func.count(Node.id)]).\ where(and_(Node.vendor_id==self.id, Node.status == 4)) ) def __unicode__(self): return u'<厂商 %s>' % self.alias
class NodeSwitch(NodeMixin, db.Model): """ Switchs """ __tablename__ = 'node_switchs' ports = db.relationship("PortSwitch", backref="switch") def __unicode__(self): return u'<交换机 %s>' % self.alias @staticmethod def export_columns(): return [ 'name', 'alias', 'addr', 'area.entrance_name', 'mask', 'snmp_comm', 'snmp_wcomm', 'location', 'remark' ]
class OperationLog(db.Model): __tablename__ = 'oplogs' id = db.Column(db.Integer, primary_key=True) session = db.Column(db.String(50)) uid = db.Column(db.Integer, db.ForeignKey("users.id")) action = db.Column(db.String(200)) success = db.Column(db.Integer) summary = db.Column(db.String(200)) module = db.Column(db.String(200)) terminal_ip = db.Column(db.String(20)) created_at = db.Column(db.DateTime, default=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now) user = db.relationship("User") def __unicode__(self): return u'<操作日志 %s>' % self.user.name
class Board(db.Model): """板卡""" __tablename__ = 'boards' node_id = db.Column(db.Integer) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), unique=True) alias = db.Column(db.String(40)) vendor_id = db.Column(db.Integer) # 厂商 shelf_no = db.Column(db.Integer) # 机架号 slot_no = db.Column(db.Integer) # 槽位号 board_no = db.Column(db.Integer) # 板卡号 board_type = db.Column(db.Integer) # 板卡类型 board_status = db.Column(db.Integer) # 板卡状态 software_vsn = db.Column(db.String(200)) #软件版本 hardversion = db.Column(db.String(200)) #硬件版本 fpga_vsn = db.Column(db.String(100)) #FPGA版本 cpld_vsn = db.Column(db.String(100)) #CPLD版本 max_ports = db.Column(db.Integer) # 端口数量 adminstatus = db.Column(db.Integer) # 管理状态 operstatus = db.Column(db.Integer) # 运行状态 standbystatus = db.Column(db.Integer) #主备状态 lockstatus = db.Column(db.Integer) # 锁状态 cpuload = db.Column(db.Integer) # CPU负载 memusage = db.Column(db.Integer) # 内存占用 temperature = db.Column(db.Integer) # 温度 serial_no = db.Column(db.String(100)) # 序列号 uptime = db.Column(db.Integer) # 运行时间 remark = db.Column(db.String(100)) last_update = db.Column(db.DateTime) created_at = db.Column(db.DateTime) updated_at = db.Column(db.DateTime) category_id = db.Column(db.Integer, db.ForeignKey('categories.id')) category = db.relationship("Category") @property def port_count(self): return object_session(self).\ scalar( select([func.count(Port.id)]).\ where(Port.board_id==self.id) )
class SecurityLog(db.Model): __tablename__ = 'seclogs' id = db.Column(db.Integer, primary_key=True) session = db.Column(db.String(50)) uid = db.Column(db.Integer, db.ForeignKey("users.id")) success = db.Column(db.Integer) summary = db.Column(db.String(200)) terminal_ip = db.Column(db.String(20)) login_at = db.Column(db.DateTime) logout_at = db.Column(db.DateTime) user = db.relationship("User") @hybrid_property def time(self): if u'登录' in self.summary: return self.login_at else: return self.logout_at def __unicode__(self): return u'<安全日志 %s>' % self.user.name
def timeperiod(cls): return db.relationship("TimePeriod")
def maintain(cls): return db.relationship("Maintain")
def area(cls): return db.relationship("Area")
def vendor(cls): return db.relationship("Vendor")
def category(cls): return db.relationship("Category")
def model(cls): return db.relationship("Model")
class NodeOlt(NodeMixin, db.Model): """ OLT """ __tablename__ = 'node_olts' onus = db.relationship("NodeOnu", backref="olt") ports = db.relationship("PortOlt", backref="olt") def __unicode__(self): return u'<OLT %s>' % self.alias @property def onu_count_plan(self): return object_session(self).\ scalar( select([func.count(NodeOnu.id)]).\ where(and_(NodeOnu.ctrl_id==self.id, NodeOnu.area_id != None)) ) @property def onu_count_unplan(self): return object_session(self).\ scalar( select([func.count(NodeOnu.id)]).\ where(and_(NodeOnu.ctrl_id==self.id, NodeOnu.area_id == None)) ) @staticmethod def export_columns(): return [ 'name', 'alias', 'addr', 'area.branch_name', 'vendor.alias', 'mask', 'snmp_comm', 'snmp_wcomm', 'snmp_ver', 'remark' ] @staticmethod def olt_entrance(): olt_entrance = db.session.query(NodeOlt.addr, Area.alias).outerjoin( Area, Area.branch == NodeOlt.area_id).filter(Area.area_type == 4).all() olt_entrance_dict = {} for ip, entrance_name in olt_entrance: if olt_entrance_dict.get(ip): olt_entrance_dict[ip] = olt_entrance_dict[ip] + [entrance_name] else: olt_entrance_dict[ip] = [entrance_name] return olt_entrance_dict def get_traffic(self): data_ifInOctets, data_ifOutOctets = [], [] try: client = errdb.Client(host=db.app.config.get("ERRDB_HOST"), port=db.app.config.get("ERRDB_PORT")) start_time = time.mktime( (datetime.now() - timedelta(days=2)).timetuple()) data = client.fetch(self.dn + ":ping", ['rtmax', 'rtmax'], start_time, time.time()) data_ifInOctets = [{ 'x': data_dict.keys()[0], 'y': data_dict.values()[0][0] } for data_dict in data] data_ifOutOctets = [{ 'x': data_dict.keys()[0], 'y': data_dict.values()[0][1] } for data_dict in data] except: pass return data_ifInOctets, data_ifOutOctets def get_ping_delay(self): data_ping = [] try: client = errdb.Client(host=db.app.config.get("ERRDB_HOST"), port=db.app.config.get("ERRDB_PORT")) start_time = time.mktime( (datetime.now() - timedelta(days=2)).timetuple()) data = client.fetch(self.dn + ":ping", ['rtmax'], start_time, time.time()) data_ping = [{ 'x': data_dict.keys()[0], 'y': data_dict.values()[0][0] } for data_dict in data] except: pass return data_ping def get_errdb_data(self, key, dn=None): if not dn: dn = self.dn + ":ping" try: client = errdb.Client(host=db.app.config.get("ERRDB_HOST"), port=db.app.config.get("ERRDB_PORT")) start_time = time.mktime( (datetime.now() - timedelta(days=2)).timetuple()) data = client.fetch(dn, key, start_time, time.time()) result = data[-1].values() if len(result) == 0: return None if len(result) == 1: return result[0] return result except: pass