-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
187 lines (159 loc) · 6.07 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// log the error as soon as possible
process.on('uncaughtException', function(err) {
console.error('msg %s, name %s, stack->\n%s', err.message, err.name, err.stack || 'NONE');
process.exit(-1);
});
var _format = require('util').format;
var _mysql = require('mysql');
var _os = require('os');
var _param = require('./param.json');
var _tools = require('graphdat-plugin-tools');
var _connection; // the mysql connection
var _pollInterval; // the interval to poll the metrics
var _previous = {}; // remember the previous poll data so we can provide proper counts
var _source; // the source of the metrics
// ================
// HELPER FUNCTIONS
// ================
// get the natural difference between a and b
function diff(a, b)
{
if (a == null || b == null)
return 0;
else
return Math.max(a - b, 0);
}
// get the natural sum of the passed in values
function sum()
{
var s = 0;
for (var i = 0; i < arguments.length; i++)
s += (arguments[i] == null || isNaN(arguments[i])) ? 0 : arguments[i];
return Math.max(s, 0);
}
function parse(x)
{
var y = parseFloat(x, 10);
return (isNaN(y) ? 0 : y);
}
function closeAndExit(err)
{
process.removeAllListeners('SIGINT');
process.removeAllListeners('SIGTERM');
if (_connection)
_connection.end(function(err1) { process.exit((err || err1) ? -1 : 0); });
else
process.exit((err) ? -1 : 0);
}
function handleError(err)
{
console.error(err);
closeAndExit(err);
}
process.on('SIGINT', closeAndExit);
process.on('SIGTERM', closeAndExit);
// ==========
// VALIDATION
// ==========
// Check that we have all of the SQL creds
if (!_param.hostname && !_param.socketPath)
return handleError('To get statistics from MySQL, either a Socket Path or hostname is required');
if (!_param.username)
return handleError('To get statistics from MySQL, a username is required');
if (!_param.password)
return handleError('To get statistics from MySQL, a password is required');
// If we do not have a port, use the default
_param.port = _param.port || 3306;
// If you do not have a poll intevarl, use 1000 as the default
_pollInterval = _param.pollInterval || 1000;
// If we do not have a source, we prefix everything with the servers hostname
_source = (_param.source || _os.hostname()).trim();
// mysql config
var _mysqlConfig = {
host : _param.hostname,
port : _param.port,
socketPath : _param.socketPath,
user : _param.username,
password : _param.password
};
// ===============
// LET GET STARTED
// ===============
var connectAttempt = 0;
function mysqlConnect()
{
// init the connection
_connection = _mysql.createConnection(_mysqlConfig);
_connection.connect(function(err)
{
// this is the first attempt to connect, you probably entered in the wrong details
// so exit early and let you update them
if (err && ++connectAttempt === 1)
{
return handleError(_format('Could not connect to the database\nmsg %s, name %s, stack->\n%s', err.message, err.name, err.stack || 'NONE'));
}
else if (err)
{
console.error('Could not connect to the database:', err);
setTimeout(mysqlConnect, 2000);
}
});
_connection.on('error', function(err)
{
console.error('Error connecting to the database:', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST')
{
// try again, mysql was probably restarted
mysqlConnect();
}
else
{
handleError(err);
}
});
}
mysqlConnect();
function poll()
{
_connection.query('SHOW GLOBAL STATUS;', function(err1, status, _)
{
if (err1)
return handleError(err1);
_connection.query('SHOW GLOBAL VARIABLES;', function(err2, vars, _)
{
if (err2)
return handleError(err2);
var adjusted = {};
var current = {};
var rows = status.concat(vars);
rows.forEach(function(row)
{
if (!row || (!('Variable_name' in row)) || (!('Value' in row)))
return;
var c = parse(row.Value);
current[row.Variable_name] = parse(c);
adjusted[row.Variable_name] = diff(c, _previous[row.Variable_name]);
});
// QUERY CACHE
var qcacheMemory = parse(diff(current.query_cache_size, current.Qcache_free_memory) / current.query_cache_size);
var qcacheHits = parse(adjusted.Qcache_hits / (adjusted.Com_select + adjusted.Qcache_hits));
console.log('MYSQL_CONNECTIONS %d %s', adjusted.Connections, _source);
console.log('MYSQL_ABORTED_CONNECTIONS %d %s', sum(adjusted.Aborted_connects, adjusted.Aborted_clients), _source);
console.log('MYSQL_BYTES_IN %d %s', adjusted.Bytes_received, _source);
console.log('MYSQL_BYTES_OUT %d %s', adjusted.Bytes_sent, _source);
console.log('MYSQL_SLOW_QUERIES %d %s', adjusted.Slow_queries, _source);
console.log('MYSQL_ROW_MODIFICATIONS %d %s', sum(adjusted.Handler_write, adjusted.Handler_update, adjusted.Handler_delete), _source);
console.log('MYSQL_ROW_READS %d %s', sum(adjusted.Handler_read_first, adjusted.Handler_read_key, adjusted.Handler_read_next,adjusted.Handler_read_prev, adjusted.Handler_read_rnd, adjusted.Handler_read_rnd_next), _source);
console.log('MYSQL_TABLE_LOCKS %d %s', adjusted.Table_locks_immediate, _source);
console.log('MYSQL_TABLE_LOCKS_WAIT %d %s', adjusted.Table_locks_waited, _source);
console.log('MYSQL_COMMITS %d %s', adjusted.Handler_commit, _source);
console.log('MYSQL_ROLLBACKS %d %s', adjusted.Handler_rollback, _source);
console.log('MYSQL_QCACHE_MEMORY %d %s', qcacheMemory, _source);
console.log('MYSQL_QCACHE_HITS %d %s', qcacheHits, _source);
console.log('MYSQL_QCACHE_PRUNES %d %s', adjusted.Qcache_lowmem_prunes, _source);
_previous = current;
setTimeout(poll, _pollInterval);
});
});
}
poll();