import repository from arizona
[raven.git] / apps / gacks / gackslog_mysql.py
1 import pickle
2 import time
3 import MySQLdb
4 import MySQLdb.cursors
5
6 from gackslog import *
7
8 class GacksMysqlLogger(GacksLogger):
9     def __init__(self, directory = "./log", name = "gacks.log", enable_stdout=False, tablename="gackslog", readonly=False):
10         self.tablename = tablename
11         self.tablename_bigobjs = tablename + "_objs"
12         self.dbname = "gacks"
13         self.address = "localhost"
14         self.user = "gacks"
15         self.passwordfile = "/root/mysql-gacks-pass.txt"
16         self.rootpasswordfile = "/root/mysql-root-pass.txt"
17         self.open()
18
19         GacksLogger.__init__(self, directory, name, enable_stdout, readonly)
20
21     def get_password(self):
22         return open(self.passwordfile,"r").readline().strip()
23
24     def get_root_password(self):
25         return open(self.rootpasswordfile,"r").readline().strip()
26
27     def reset(self):
28         self.execute("DROP TABLE IF EXISTS `" + self.tablename + "`;")
29         self.execute("DROP TABLE IF EXISTS `" + self.tablename_bigobjs + "`;")
30         self.create()
31
32     def open(self):
33         self.connect()
34         self.create()
35
36     def lock(self):
37         self.execute("SELECT GET_LOCK('queue_lock', 300);")  # 5 minute duration
38
39     def unlock(self):
40         self.cursor.execute("SELECT RELEASE_LOCK('queue_lock');")
41
42     def connect(self, db=None, user=None, passwd=None, address=None):
43         if not db:\r
44             db = self.dbname\r
45         if not user:\r
46             user = self.user\r
47         if not passwd:\r
48             passwd = self.get_password()\r
49         if not address:\r
50             address = self.address\r
51         self.conn = MySQLdb.connect(host=address, user=user, passwd=passwd, db=db, cursorclass=MySQLdb.cursors.DictCursor)\r
52         self.cursor = self.conn.cursor()\r
53 \r
54     def create(self):\r
55         querystr = "CREATE TABLE IF NOT EXISTS `" + self.tablename + "`" + """\r
56                        (`id`            INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
57                         `timestamp`     DOUBLE NOT NULL, INDEX(`timestamp`),
58                         `kind`          VARCHAR(32),
59                         `funcname`      VARCHAR(48),
60                         `args`          TEXT,
61                         `result`        TEXT,
62                         `msg`           TEXT);"""
63         print querystr
64         self.execute(querystr);
65         querystr = "CREATE TABLE IF NOT EXISTS `" + self.tablename_bigobjs + "`" + """
66                        (`id`            INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
67                         `name`          VARCHAR(48) NOT NULL, INDEX(`name`),
68                         `kind`          VARCHAR(32),
69                         `value`         TEXT NOT NULL);"""
70         self.execute(querystr);
71
72     def execute(self, query, args=None):
73         if args is not None:
74              query = query % self.conn.literal(args)
75         print "Query:", query
76         self.cursor.execute(query)
77
78     def execute_dict(self, query, args=None):
79         self.execute(query, args)
80         return self.cursor.fetchall()
81
82     def store_big(self, id, kind, val):
83         self.lock()
84         dicts = self.execute_dict("SELECT name FROM `" + self.tablename_bigobjs + "` WHERE value='%s' AND kind='%s';" % (val, kind))
85         if not dicts:
86             self.execute_dict("INSERT INTO `" + self.tablename_bigobjs + "` (name,value,kind) VALUES('%s','%s','%s');" % (id, val, kind))
87             dicts = self.execute_dict("SELECT name FROM `" + self.tablename_bigobjs + "` WHERE value='%s' AND kind='%s';" % (val, kind))
88         self.unlock()
89
90     def log_line(self, timestamp, kind, func_name, argStrs=None, resultStr=None, msg=None):
91         GacksLogger.log_line(self, timestamp, kind, func_name, argStrs, resultStr, msg)
92
93         if self.readonly:
94             return
95
96         argStrs = pickle.dumps(argStrs)
97         resultStr = pickle.dumps(resultStr)
98
99         if msg==None:
100             msg="None"
101
102         self.execute("INSERT INTO `" + self.tablename + "` (timestamp, kind, funcname, args, result, msg) VALUES('%f','%s','%s','%s','%s','%s');" %
103                      (timestamp,
104                       kind,
105                       self.conn.escape_string(func_name),
106                       self.conn.escape_string(argStrs),
107                       self.conn.escape_string(resultStr),
108                       self.conn.escape_string(msg)))
109
110     def query_big_obj(self, name):
111         query = "SELECT value FROM `" + self.tablename_bigobjs + "` WHERE name='%s';" % (self.conn.escape_string(name))
112         dictList = self.execute_dict(query)
113
114         if len(dictList)==0:
115             return None
116         else:
117             return dictList[0]["value"]
118
119     def query(self, beforeTime=None, afterTime=None, beforeId=None, afterId=None, kind=None, descending=False, limit=None):
120         query = "SELECT * FROM `" + self.tablename + "`"
121
122         where = []
123         if (kind!=None):
124             where.append("kind='" + self.conn.escape_string(kind) + "'")
125         if (beforeTime!=None):
126             where.append("timestamp<'" + self.conn.escape_string(str(beforeTime)) + "'")
127         if (afterTime!=None):
128             where.append("timestamp>'" + self.conn.escape_string(str(afterTime)) + "'")
129         if (beforeId!=None):
130             where.append("id<'" + self.conn.escape_string(str(beforeId)) + "'")
131         if (afterId!=None):
132             where.append("id>'" + self.conn.escape_string(str(afterId)) + "'")
133
134         if where != []:
135             query = query + " WHERE " + " AND ".join(where)
136
137         query = query + " ORDER BY id"
138         if (descending):
139             query = query + " DESC"
140
141         if (limit!=None):
142             query = query + " LIMIT " + str(limit)
143
144         dictList = self.execute_dict(query)
145
146         for dict in dictList:
147             dict['args'] = pickle.loads(dict['args'])
148             dict['result'] = pickle.loads(dict['result'])
149
150         return dictList
151