This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbpoller.js
69 lines (61 loc) · 2.16 KB
/
dbpoller.js
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
58
59
60
61
62
63
64
65
66
67
68
69
var mysql = require('mysql');
var config = require('./config');
var sets = config.sets;
// Get inspection data from a MySQL connection and add to data[hostname]
function getmysqlstatus(conn, data) {
var hostname = conn.config.connectionConfig.host;
conn.query('show global variables', function(err, rows, fields) {
setTimeout(getmysqlstatus, 5000, conn, data);
if (err) {
console.log("Database error with " + hostname + ": " + err);
return;
}
if (hostname in data) {
data[hostname + '_prev'] = data[hostname];
}
var d = data[hostname] = {};
for (var i=0; i<rows.length; i++) {
d[rows[i].Variable_name] = rows[i].Value;
}
conn.query('show global status', function(err, rows, fields) {
if (err) {
console.log("Database error with " + hostname + ": " + err);
return;
}
for (var i=0; i<rows.length; i++) {
d[rows[i].Variable_name] = rows[i].Value;
}
// Calculate queries per second
if (hostname + '_prev' in data &&
'Questions' in data[hostname + '_prev']) {
d['Questions_pr_second'] =
(d['Questions'] - data[hostname + '_prev']['Questions']) / 5;
}
if ('Threads_running' in d && d['Threads_running'] > config.maxthreads) {
logpl(conn, "somefile");
}
// Add the sets
for (var set in sets) {
d[set] = {}
for(var i=0;i<sets[set].length;i++) {
if (sets[set][i] in d) {
d[set][sets[set][i]] = d[sets[set][i]];
}
}
}
});
});
}
function logpl(conn, filename) {
conn.query("show full processlist", function(err, rows, fields) {
console.log(rows);
});
};
function connect(dbserver) {
var conn = mysql.createPool({host:dbserver, user:config.dbuser, password:config.dbpass});
conn.on('error', function(err) { console.log(err); });
console.log("Created connection to " + dbserver + " as user " + config.dbuser);
return conn;
}
exports.getmysqlstatus = getmysqlstatus;
exports.connect = connect;