-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectorMysql.py
More file actions
executable file
·57 lines (43 loc) · 1.73 KB
/
collectorMysql.py
File metadata and controls
executable file
·57 lines (43 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/opt/bin/python
import ConfigParser
import MySQLdb
import datetime
config = ConfigParser.RawConfigParser()
config.read('config/collector.cfg')
username = config.get('mysql', 'username')
password = config.get('mysql', 'password')
host = config.get('mysql', 'host')
dbName = config.get('mysql', 'dbName')
db = {}
def connectToDatasource(connectionId):
global db
if connectionId not in db.keys():
db[connectionId] = MySQLdb.connect(user=username, passwd=password, host=host, db=dbName)
print "connecting to db, connection id:{0}".format(connectionId)
try:
db[connectionId].query("SET AUTOCOMMIT=1")
except MySQLdb.ProgrammingError:
print "error connecting to the database"
pass
def writeToDatasource(connectionId, temp=0, date=datetime.datetime.now(), sensorName='unknown'):
connectToDatasource(connectionId)
try:
db[connectionId].query(
"INSERT INTO logs (value, datetime, fk_sensor) VALUES ({0}, '{1}', '{2}')"
.format(temp, date, sensorName))
except MySQLdb.ProgrammingError:
print "error in query with parameters: {0} {1} {2} ".format(temp, date, sensorName)
pass
def writeToControlDatasource(connectionId, value=0, date=datetime.datetime.now(),):
connectToDatasource(connectionId)
try:
db[connectionId].query(
"INSERT INTO logs_control (status, datetime) VALUES ({0}, '{1}')"
.format(value, date))
except MySQLdb.ProgrammingError:
print "error in query with parameters: {0} {1} ".format(value, date)
pass
def close(connectionId):
global db
db[connectionId].close()
del db[connectionId]