DB系统预警联系人API
Author:Skate
Time:2014/12/16
DB系统预警联系人API
在我们维护系统时,须要把系统的报警信息即时传递给对应同学。假设把联系方式直接写到脚本里。对以后的维护变更将埋下祸根,尤其是成百上千的系统。
为此这里写了个获取联系人信息的API
数据库配置中心表:
CREATE TABLE `db_alertcontact` (
`id` INT(11) NULL DEFAULT NULL,
`levelid` INT(11) NULL DEFAULT NULL COMMENT 'contact level',
`contact` VARCHAR(50) NULL DEFAULT NULL COMMENT 'email or phone information',
`type` VARCHAR(50) NULL DEFAULT NULL COMMENT 'phone/email',
`username` VARCHAR(100) NULL DEFAULT NULL,
`group` VARCHAR(80) NULL DEFAULT NULL COMMENT 'contact group'
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TABLE `db_alertlevel` (
`id` INT(11) NULL DEFAULT NULL,
`levelname` VARCHAR(50) NULL DEFAULT NULL COMMENT 'info/warn/err'
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
使用方法帮助:
[root@skatedb55 pytest]# python contactlist.py --help
usage: Contanct API v0.1 ,(C) Copyright Skate 2014 [-h] --group GROUP --type
TYPE --level LEVEL
[--interval INTERVAL]
[--load LOAD]
optional arguments:
-h, --help show this help message and exit
--group GROUP = The contact group
--type TYPE = The mode of contact
--level LEVEL = alarm level,info/warn/err
--interval INTERVAL = Database query interval(s)
--load LOAD = The configure center database,eg:
load=user/pass@ip:port:dbname
[root@skatedb55 pytest]#
样例:
INSERT INTO `db_alertcontact` (`id`, `levelid`, `contact`, `type`, `username`, `group`) VALUES
(1, 1, 'skate1@163.com', 'email', 'skate1', 'p1'),
(2, 2, 'skate2@163.com', 'email', 'skate2', 'p2'),
(3, 1, '1300000000', 'phone', 'skate3', 'p2'),
(4, 1, '1311111111', 'phone', 'skate4', 'p2'),
(5, 1, '1322222222', 'phone', 'skate5', 'p2'),
(6, 2, 'skate6@163.com', 'email', 'skate6', 'p2');
INSERT INTO `db_alertlevel` (`id`, `levelname`) VALUES
(1, 'info'),
(2, 'warn'),
(3, 'error');
[root@skatedb55 pytest]# python contactlist.py --group=p2 --type=phone --level=info --interval=10--load=root/root@10.20.0.55:3306:test6
1300000000,1311111111,1322222222,
[root@skatedb55 pytest]#
[root@skatedb55 pytest]# python contactlist.py --group=p2 --type=email --level=warn --interval=10--load=root/root@10.20.0.55:3306:test6
skate2@163.com,skate6@163.com,
[root@skatedb55 pytest]#
长处:
1.在变更联系人或联系方式不须要改动代码
2.联系人的相关信息存储在配置中心数据库,为了降低对数据库的查询,默认每天查一次数据库(自己能够指定),把联系信息放在本地。既提高了速度,也降低了对配置中心的依赖
3.假设想在变更联系信息及时生效。仅仅需把本地的暂时文件"/tmp/contact_dbinfo"删除就可以
contactlist.py:
# -*- coding: utf-8 -*-
#!/usr/bin/python
#
# Author:Skate
# Time:2014/12/10
# Function: Contact API import MySQLdb,sys
import argparse
import os
import datetime class database:
def __int__(self,host,user,passwd,port,dbname):
self.conn = None
pass
def conn(self,host,user,passwd,port,dbname):
self.host=host
self.user=user
self.passwd=passwd
self.port=port
self.dbname=dbname
try:
self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbname,port=self.port) except MySQLdb.Error, e:
print "MySQL Connect Error: %s" % (e.args[1])
return self.conn
def closeConn(self):
self.conn.close()
def execute(self,sql,param):
if self.conn==None or self.conn.open==False :
return -1
sys.exit
cur = self.conn.cursor()
cur.execute(sql,param)
self.closeConn()
return cur def contactlist(group,type,level,host,user,passwd,port,dbname,interval=86400):
tfile='/tmp/contact_dbinfo'
list=''
if os.path.isfile(tfile):
a1=datetime.datetime.fromtimestamp(os.path.getctime(tfile))
a2=datetime.datetime.now()
diffsec = (a2 - a1).seconds
if diffsec > interval:
os.remove(tfile)
f=open(tfile,'a')
db=database()
db.conn(host,user,passwd,port,dbname)
sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s"
param=(level,group,type)
cur=db.execute(sql,param)
results=cur.fetchall()
for row in results:
if row[3] =='phone':
#for r in row:
list = list + row[0] + ','
elif row[3] == 'email':
#for r in row:
list = list + row[0] + ','
if type =='phone':
f.write('phonelist='+ group + ':' + list + '\n')
f.close()
elif type == 'email':
f.write('emaillist='+ group + ':' +list + '\n')
f.close()
else:
strsearch = type + 'list='+ group
istype = os.popen('cat '+ tfile +' | grep ' + strsearch + ' | wc -l').readline().strip()
if int(istype) > 0:
line = os.popen('cat '+ tfile +' | grep ' + strsearch).readline().strip()
b=line.split('=')
a=b[1].split(":")
if b[0]=='phonelist':
list=a[1]
elif b[0]=='emaillist':
list=a[1]
elif int(istype) < 1:
f=open(tfile,'a')
db=database()
db.conn(host,user,passwd,port,dbname)
sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s"
param=(level,group,type)
cur=db.execute(sql,param)
results=cur.fetchall()
#list=''
for row in results:
if row[3] =='phone':
list = list + row[0] + ','
elif row[3] == 'email':
list = list + row[0] + ','
if type =='phone':
f.write('phonelist='+ group + ':' + list + '\n')
f.close()
elif type == 'email':
f.write('emaillist='+ group + ':' + list + '\n')
f.close() else:
f=open(tfile,'a')
db=database()
db.conn(host,user,passwd,port,dbname)
sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s"
param=(level,group,type)
cur=db.execute(sql,param)
results=cur.fetchall() for row in results:
if row[3] =='phone':
#for r in row:
list = list + row[0] + ','
elif row[3] == 'email':
#for r in row:
list = list + row[0] + ',' if type =='phone': f.write('phonelist='+ group + ':' + list + '\n')
f.close()
elif type == 'email':
f.write('emaillist='+ group + ':' + list + '\n')
f.close() return list if __name__ == "__main__":
parser = argparse.ArgumentParser("Contanct API v0.1 ,(C) Copyright Skate 2014")
parser.add_argument('--group', action='store', dest='group',required=True,
help=" = The contact group") parser.add_argument('--type', action='store', dest='type',required=True,
help=" = The mode of contact") parser.add_argument('--level', action='store', dest='level',required=True,
help=" = alarm level,info/warn/err") parser.add_argument('--interval', action='store', dest='interval',type=int,default=86400,
help=" = Database query interval") parser.add_argument('--load', action='store', dest='load',default='',
help=" = The configure center database,eg: \n load=user/pass@ip:port:dbname") results = parser.parse_args() load = results.load
group = results.group
type = results.type
level = results.level
interval = results.interval if (load !=''):
user_info,url = load.split("@")
host,port,db = url.split(":")
port=int(port)
user,passwd = user_info.split("/",1) str = contactlist(group,type,level,host,user,passwd,port,db,interval)
print str
大家有好的意见,欢迎提出
------end-------
DB系统预警联系人API的更多相关文章
- Android 获取系统的联系人
本文主要介绍android中怎样获取系统的联系人数据 首先打开模拟器 点击联系人图标按钮 说明系统联系人数据库是空的,打开File explorer,找到data/data下面的文件夹: 将conta ...
- 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)
1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...
- 用ContentProvider向系统增加联系人
发现对系统的联系人进行操作的api很乱,感觉逻辑有点不清楚...... 主要用到这4个类: android.provider.ContactsContract.CommonDataKinds.Emai ...
- Oracle DB 查看预警日志
“Database(数据库)”主页>“Related Links相关链接)”区域> “Alert Log Content (预警日志内容)” 查看预警日志每个数据库都有一个alert_&l ...
- 最新Android系统版本与API等级对应关系表
最新Android系统版本与API等级对应关系表 从Android官网拷过来的,方便查阅... 官网地址:https://developer.android.com/guide/topics/mani ...
- Android 系统版本和API level的关系表
Android 系统版本和API level的关系表 wiki: https://zh.wikipedia.org/wiki/Android%E6%AD%B7%E5%8F%B2%E7%89%88%E6 ...
- [android] 获取系统的联系人信息
内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db ...
- android该系统的应用API选择演示版本
转载请注明出处[http://blog.csdn.net/y150481863/article/details/41280045] 首先我们在开发一个应用之前,特别是一个android应用.首先要考虑 ...
- 用户创建,删除and并发注册and系统登陆的API研究(学习汇总网上资料)
一.系统登陆链接实现 比如有一个外围支持系统,用户需要在外围系统登录之后点个link就可以登录到Oracle ERP系统中,那么我们需要先把外围系统的用户创建在Oracle ERP中,并且分配职责给他 ...
随机推荐
- C语言误区
int date[10] ; //定义一个字长10的数组,从date[0]....date[9] static char c ; //静态变量(有静态外部变量和静态局部变量),静态外部变量 ...
- Web开发者宝典:10款流行前沿矢量图形素材
矢量图形以其鲜亮.无杂斑和醒目的外观而深受网页设计师们的喜爱.本文整理了网页设计中最为流行的20款矢量设计素材,如网页按钮,社交媒体图标和联系人图标等,希望Web开发人员会喜欢. 1. Web But ...
- Mybatis中模糊查询的各种写法
1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%'); 2. 使用 ${...} ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- MacOS 10.8更新SVN到1.8.4的问题和解决方法
因为要导入以前的项目,但以前项目里内含有的svn信息,所以xcode默认安装的svn1.6是无法删除svn信息,据说需要svn1.7才能清除掉svn信息.所以必须要升级svn的版本. 我在网上找了各种 ...
- RabbitMq基本使用
1.新建一个vhost : rabbitmqctl add_vhost test 2.新建一个用户: rabbitmqctl add_user news news 3.对这个news用户增加test ...
- autorelease的对象何时被释放
autorelease的对象何时被释放 参考答案: 如果了解一点点Run Loop的知道,应该了解到:Run Loop在每个事件循环结束后会去自动释放池将所有自动释放对象的引用计数减一,若引用计数变成 ...
- VR介绍
VR(Virtual Reality,即虚拟现实,简称VR),是由美国VPL公司创建人拉尼尔在20世纪80年代初提出的.其具体内涵是:综合利用计算机图形系统和各种现实及控制等接口设备,在计算机上生成的 ...
- NBIbatis 框架体系说明
框架体系说明 Application 表现层 表现层必须通过Business业务规则层操作数据库,不能直接调用DataAccess数据访问. Sqlmap.config配置: connectionSt ...
- [JAVA] 一个可以编辑、编译、运行Java简单文件的记事本java实现
本来是Java课做一个仿windows记事本的实验,后来突然脑子一热,结果就给它加了一个编译运行Java文件的功能. 本工程总共大约3000行代码,基本上把所学的java界面.文件.控件的功能都包含在 ...