@cryptotaxi247 / netdata-1 / commits / 5ee424509

preserve configs across installaitons

python mysql plugin Makefile with mysql.chart.py minor fixes in mysql.chart.py installation add connection timeout no need to execute query when chceking for active servers fix function failure message handle interval connection timeout directories should end with "/" timeout should be int type fix normalizing config_dir comment out mysql.conf updated readme to include mysql plugin remove pymysql

paulfantom committed Jun 13, 2016 at 23:37 UTC 5ee4245093f673985ef270974c08c5cf9b844dbc
8 files changed +585 -22
conf.d/Makefile.am
+2 -1
@@ -19,7 +19,8 @@ dist_nodeconfig_DATA = \
19
20 pythonconfigdir=$(configdir)/python.d
21 dist_pythonconfig_DATA = \
22 - python.d/example.conf
22 + python.d/example.conf \
23 + python.d/mysql.conf \
24 $(NULL)
25
26
conf.d/python.d.conf
+1
@@ -9,3 +9,4 @@
9 # By default python.d.plugin enables all modules stored in python.d
10 # Modules can be disabled with setting "module_name = no"
11 example=no
12 +mysql=no
conf.d/python.d/mysql.conf new
+18
@@ -0,0 +1,18 @@
1 +# Example configuration of mysql.chart.py
2 +# Indentation is important
3 +
4 +#update_every=5
5 +
6 +#config=[
7 +# {
8 +# 'name' : 'local',
9 +# 'user' : 'root',
10 +# 'password' : 'blablablabla',
11 +# 'socket' : '/var/run/mysqld/mysqld.sock'
12 +# },{
13 +# 'name' : 'remote',
14 +# 'user' : 'admin',
15 +# 'password' : 'bla',
16 +# 'host' : 'example.org',
17 +# 'port' : '3306'
18 +# }]
netdata-installer.sh
+9 -9
@@ -338,15 +338,15 @@ run make || exit 1
338
339 # backup user configurations
340 installer_backup_suffix="${PID}.${RANDOM}"
341 -for x in apps_groups.conf charts.d.conf
341 +for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
342 do
343 - if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}" ]
343 + if [ -f "${x}" ]
344 then
345 - cp -p "${NETDATA_PREFIX}/etc/netdata/${x}" "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
345 + cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
346
347 - elif [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
347 + elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
348 then
349 - rm -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
349 + rm -f "${x}.installer_backup.${installer_backup_suffix}"
350 fi
351 done
352
@@ -354,12 +354,12 @@ echo >&2 "Installing netdata ..."
354 run make install || exit 1
355
356 # restore user configurations
357 -for x in apps_groups.conf charts.d.conf
357 +for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
358 do
359 - if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
359 + if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
360 then
361 - cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" "${NETDATA_PREFIX}/etc/netdata/${x}"
362 - rm -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
361 + cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
362 + rm -f "${x}.installer_backup.${installer_backup_suffix}"
363 fi
364 done
365
plugins.d/python.d.plugin
+9 -10
@@ -27,12 +27,10 @@ class PythonCharts(object):
27 self.configs = modules_configs
28
29 # load modules
30 - self.modules = self._load_modules(modules_path,modules)
30 + modules = self._load_modules(modules_path,modules)
31
32 # check if loaded modules are on disabled modules list
33 - for mod in self.modules:
34 - if mod.__name__ in modules_disabled:
35 - self.modules.remove(mod)
33 + self.modules = [ m for m in modules if m.__name__ not in modules_disabled ]
34
35 # load configuration files
36 self._load_configs()
@@ -123,9 +121,9 @@ class PythonCharts(object):
121 "() function. Disabling it.")
122 elif reason[:7] == "failed ":
123 debug("chart '" +
126 - mod.__name__ +
127 - reason[3:] +
128 - "() function. reports failure.")
124 + mod.__name__ + "' " +
125 + reason[7:] +
126 + "() function reports failure.")
127 elif reason[:13] == "configuration":
128 debug(mod.__name__,
129 "configuration file '" +
@@ -272,6 +270,8 @@ def parse_cmdline(directory, *commands):
270 pass
271
272 debug("started from", commands[0], "with options:", *commands[1:])
273 + if len(mods) == 0 and DEBUG_FLAG is False:
274 + interval = None
275
276 return {'interval': interval,
277 'modules': mods}
@@ -292,9 +292,8 @@ def run():
292 # read configuration file
293 disabled = []
294 if config_dir[-1] != '/':
295 - configfile = config_dir + '/' + "python.d.conf"
296 - else:
297 - configfile = config_dir + "python.d.conf"
295 + config_dir += '/'
296 + configfile = config_dir + "python.d.conf"
297
298 try:
299 conf = read_config(configfile)
python.d/Makefile.am
+30
@@ -2,6 +2,7 @@ MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
2
3 dist_python_SCRIPTS = \
4 example.chart.py \
5 + mysql.chart.py \
6 $(NULL)
7
8 dist_python_DATA = \
@@ -10,4 +11,33 @@ dist_python_DATA = \
11
12 pythonmodulesdir=$(pythondir)/python_modules
13 dist_pythonmodules_DATA = \
14 + python_modules/__init__.py
15 + $(NULL)
16 +
17 +
18 +pythonmodulespymysqldir=$(pythondir)/python_modules/pymysql
19 +dist_pythonmodulespymysql_DATA = \
20 + python_modules/pymysql/charset.py \
21 + python_modules/pymysql/_compat.py \
22 + python_modules/pymysql/connections.py \
23 + python_modules/pymysql/converters.py \
24 + python_modules/pymysql/cursors.py \
25 + python_modules/pymysql/err.py \
26 + python_modules/pymysql/__init__.py \
27 + python_modules/pymysql/optionfile.py \
28 + python_modules/pymysql/_socketio.py \
29 + python_modules/pymysql/times.py \
30 + python_modules/pymysql/util.py \
31 + $(NULL)
32 +
33 +pythonmodulespymysqlconstantsdir=$(pythondir)/python_modules/pymysql/constants
34 +dist_pythonmodulespymysqlconstants_DATA = \
35 + python_modules/pymysql/constants/CLIENT.py \
36 + python_modules/pymysql/constants/COMMAND.py \
37 + python_modules/pymysql/constants/CR.py \
38 + python_modules/pymysql/constants/ER.py \
39 + python_modules/pymysql/constants/FIELD_TYPE.py \
40 + python_modules/pymysql/constants/FLAG.py \
41 + python_modules/pymysql/constants/__init__.py \
42 + python_modules/pymysql/constants/SERVER_STATUS.py \
43 $(NULL)
python.d/README.md
+83 -2
@@ -1,6 +1,87 @@
1 +Every plugin supports changing its data collection frequency by setting `update_every` variable in its configuration file.
2 +
3 The following python.d plugins are supported:
4
3 -# None
5 +# mysql
6 +
7 +The plugin will monitor one or more mysql servers
8 +
9 +It will produce the following charts (if data is available):
10 +
11 +1. **Bandwidth** in kbps
12 + * in
13 + * out
14 +
15 +2. **Queries** in queries/sec
16 + * queries
17 + * questions
18 + * slow queries
19 +
20 +3. **Operations** in operations/sec
21 + * opened tables
22 + * flush
23 + * commit
24 + * delete
25 + * prepare
26 + * read first
27 + * read key
28 + * read next
29 + * read prev
30 + * read random
31 + * read random next
32 + * rollback
33 + * save point
34 + * update
35 + * write
36 +
37 +4. **Table Locks** in locks/sec
38 + * immediate
39 + * waited
40 +
41 +5. **Select Issues** in issues/sec
42 + * full join
43 + * full range join
44 + * range
45 + * range check
46 + * scan
47 +
48 +6. **Sort Issues** in issues/sec
49 + * merge passes
50 + * range
51 + * scan
52 +
53 +### configuration
54 +
55 +You can provide, per server, the following:
56 +
57 +1. a name, anything you like, but keep it short
58 +2. username which have access to database (deafults to 'root')
59 +3. password (defaults to none)
60 +4. mysql socket (optional)
61 +5. mysql host (ip or hostname)
62 +3. mysql port (defaults to 3306)
63 +
64 +Here is an example for 2 servers updating data every 10 seconds
65 +
66 +```js
67 +update_every = 10
68 +
69 +config=[
70 + {
71 + 'name' : 'local',
72 + 'user' : 'root',
73 + 'password' : 'blablablabla',
74 + 'socket' : '/var/run/mysqld/mysqld.sock'
75 + },{
76 + 'name' : 'remote',
77 + 'user' : 'admin',
78 + 'password' : 'bla',
79 + 'host' : 'example.org',
80 + 'port' : '9000'
81 + }]
82 +```
83 +
84 +If no configuration is given, the plugin will attempt to connect to mysql server via unix socket at `/var/run/mysqld/mysqld.sock` without password and username `root`
85
5 -Currently we don't have any python plugins, but we are counting on you to add some new ones.
86 +---
87
python.d/mysql.chart.py new
+433
@@ -0,0 +1,433 @@
1 +#!/usr/bin/python3 -u
2 +
3 +try:
4 + import pymysql.cursors
5 + # https://github.com/PyMySQL/PyMySQL
6 +except ImportError:
7 + import sys
8 + sys.write.stderr("You need to install PyMySQL module to use mysql.chart.py plugin")
9 +
10 +config = [
11 + {
12 + 'name' : 'local',
13 + 'user' : 'root',
14 + 'password' : None,
15 + 'socket' : '/var/run/mysqld/mysqld.sock'
16 + }
17 +]
18 +
19 +update_every = 3
20 +priority = 90000
21 +
22 +#query = "SHOW GLOBAL STATUS WHERE value REGEX '^[0-9]'"
23 +QUERY = "SHOW GLOBAL STATUS"
24 +ORDER = ['net',
25 + 'queries',
26 + 'handlers',
27 + 'table_locks',
28 + 'join_issues',
29 + 'sort_issues',
30 + 'tmp',
31 + 'connections',
32 + 'binlog_cache',
33 + 'threads',
34 + 'thread_cache_misses',
35 + 'innodb_io',
36 + 'innodb_io_ops',
37 + 'innodb_io_pending_ops',
38 + 'innodb_log',
39 + 'innodb_os_log',
40 + 'innodb_os_log_io',
41 + 'innodb_cur_row_lock',
42 + 'innodb_rows',
43 + 'innodb_buffer_pool_pages',
44 + 'innodb_buffer_pool_bytes',
45 + 'innodb_buffer_pool_read_ahead',
46 + 'innodb_buffer_pool_reqs',
47 + 'innodb_buffer_pool_ops',
48 + 'qcache_ops',
49 + 'qcache',
50 + 'qcache_freemem',
51 + 'qcache_memblocks',
52 + 'key_blocks',
53 + 'key_requests',
54 + 'key_disk_ops',
55 + 'files',
56 + 'files_rate',
57 + 'binlog_stmt_cache',
58 + 'connection_errors']
59 +
60 +CHARTS = {
61 + 'net' : (
62 + "'' 'mysql Bandwidth' 'kilobits/s' bandwidth mysql.net area",
63 + (
64 + ("Bytes_received", "in incremental 8 1024"),
65 + ("Bytes_sent", "out incremental -8 1024")
66 + )),
67 + 'queries' : (
68 + "'' 'mysql Queries' 'queries/s' queries mysql.queries line",
69 + (
70 + ("Queries", "queries incremental 1 1"),
71 + ("Questions", "questions incremental 1 1"),
72 + ("Slow_queries", "slow_queries incremental -1 1")
73 + )),
74 + 'handlers' : (
75 + "'' 'mysql Handlers' 'handlers/s' handlers mysql.handlers line",
76 + (
77 + ("Handler_commit", "commit incremental 1 1"),
78 + ("Handler_delete", "delete incremental 1 1"),
79 + ("Handler_prepare", "prepare incremental 1 1"),
80 + ("Handler_read_first", "read_first incremental 1 1"),
81 + ("Handler_read_key", "read_key incremental 1 1"),
82 + ("Handler_read_next", "read_next incremental 1 1"),
83 + ("Handler_read_prev", "read_prev incremental 1 1"),
84 + ("Handler_read_rnd", "read_rnd incremental 1 1"),
85 + ("Handler_read_rnd_next", "read_rnd_next incremental 1 1"),
86 + ("Handler_rollback", "rollback incremental 1 1"),
87 + ("Handler_savepoint", "savepoint incremental 1 1"),
88 + ("Handler_savepoint_rollback", "savepoint_rollback incremental 1 1"),
89 + ("Handler_update", "update incremental 1 1"),
90 + ("Handler_write", "write incremental 1 1")
91 + )),
92 + 'table_locks' : (
93 + "'' 'mysql Tables Locks' 'locks/s' locks mysql.table_locks line",
94 + (
95 + ("Table_locks_immediate", "immediate incremental 1 1"),
96 + ("Table_locks_waited", "waited incremental -1 1")
97 + )),
98 + 'join_issues' : (
99 + "'' 'mysql Select Join Issues' 'joins/s' issues mysql.join_issues line",
100 + (
101 + ("Select_full_join", "full_join incremental 1 1"),
102 + ("Select_full_range_join", "full_range_join incremental 1 1"),
103 + ("Select_range", "range incremental 1 1"),
104 + ("Select_range_check", "range_check incremental 1 1"),
105 + ("Select_scan", "scan incremental 1 1"),
106 + )),
107 + 'sort_issues' : (
108 + "'' 'mysql Sort Issues' 'issues/s' issues mysql.sort.issues line",
109 + (
110 + ("Sort_merge_passes", "merge_passes incremental 1 1"),
111 + ("Sort_range", "range incremental 1 1"),
112 + ("Sort_scan", "scan incremental 1 1"),
113 + )),
114 + 'tmp' : (
115 + "'' 'mysql Tmp Operations' 'counter' temporaries mysql.tmp line",
116 + (
117 + ("Created_tmp_disk_tables", "disk_tables incremental 1 1"),
118 + ("Created_tmp_files", "files incremental 1 1"),
119 + ("Created_tmp_tables", "tables incremental 1 1"),
120 + )),
121 + 'connections' : (
122 + "'' 'mysql Connections' 'connections/s' connections mysql.connections line",
123 + (
124 + ("Connections", "all incremental 1 1"),
125 + ("Aborted_connects", "aborted incremental 1 1"),
126 + )),
127 + 'binlog_cache' : (
128 + "'' 'mysql Binlog Cache' 'transactions/s' binlog mysql.binlog_cache line",
129 + (
130 + ("Binlog_cache_disk_use", "disk incremental 1 1"),
131 + ("Binlog_cache_use", "all incremental 1 1"),
132 + )),
133 + 'threads' : (
134 + "'' 'mysql Threads' 'threads' threads mysql.threads line",
135 + (
136 + ("Threads_connected", "connected absolute 1 1"),
137 + ("Threads_created", "created incremental 1 1"),
138 + ("Threads_cached", "cached absolute -1 1"),
139 + ("Threads_running", "running absolute 1 1"),
140 + )),
141 + 'thread_cache_misses' : (
142 + "'' 'mysql Threads Cache Misses' 'misses' threads mysql.thread_cache_misses area",
143 + (
144 + ("Thread_cache_misses", "misses misses absolute 1 100"),
145 + )),
146 + 'innodb_io' : (
147 + "'' 'mysql InnoDB I/O Bandwidth' 'kilobytes/s' innodb mysql.innodb_io area",
148 + (
149 + ("Innodb_data_read", "read incremental 1 1024"),
150 + ("Innodb_data_written", "write incremental -1 1024"),
151 + )),
152 + 'innodb_io_ops' : (
153 + "'' 'mysql InnoDB I/O Operations' 'operations/s' innodb mysql.innodb_io_ops line",
154 + (
155 + ("Innodb_data_reads", "reads incremental 1 1"),
156 + ("Innodb_data_writes", "writes incremental -1 1"),
157 + ("Innodb_data_fsyncs", "fsyncs incremental 1 1"),
158 + )),
159 + 'innodb_io_pending_ops' : (
160 + "'' 'mysql InnoDB Pending I/O Operations' 'operations' innodb mysql.innodb_io_pending_ops line",
161 + (
162 + ("Innodb_data_pending_reads", "reads absolute 1 1"),
163 + ("Innodb_data_pending_writes", "writes absolute -1 1"),
164 + ("Innodb_data_pending_fsyncs", "fsyncs absolute 1 1"),
165 + )),
166 + 'innodb_log' : (
167 + "'' 'mysql InnoDB Log Operations' 'operations/s' innodb mysql.innodb_log line",
168 + (
169 + ("Innodb_log_waits", "waits incremental 1 1"),
170 + ("Innodb_log_write_requests", "write_requests incremental -1 1"),
171 + ("Innodb_log_writes", "incremental -1 1"),
172 + )),
173 + 'innodb_os_log' : (
174 + "'' 'mysql InnoDB OS Log Operations' 'operations' innodb mysql.innodb_os_log line",
175 + (
176 + ("Innodb_os_log_fsyncs", "fsyncs incremental 1 1"),
177 + ("Innodb_os_log_pending_fsyncs", "pending_fsyncs absolute 1 1"),
178 + ("Innodb_os_log_pending_writes", "pending_writes absolute -1 1"),
179 + )),
180 + 'innodb_os_log_io' : (
181 + "'' 'mysql InnoDB OS Log Bandwidth' 'kilobytes/s' innodb mysql.innodb_os_log_io area",
182 + (
183 + ("Innodb_os_log_written", "write incremental -1 1024"),
184 + )),
185 + 'innodb_cur_row_lock' : (
186 + "'' 'mysql InnoDB Current Row Locks' 'operations' innodb mysql.innodb_cur_row_lock area",
187 + (
188 + ("Innodb_row_lock_current_waits", "current_waits absolute 1 1"),
189 + )),
190 + 'innodb_rows' : (
191 + "'' 'mysql InnoDB Row Operations' 'operations/s' innodb mysql.innodb_rows area",
192 + (
193 + ("Innodb_rows_inserted", "read incremental 1 1"),
194 + ("Innodb_rows_read", "deleted incremental -1 1"),
195 + ("Innodb_rows_updated", "inserted incremental 1 1"),
196 + ("Innodb_rows_deleted", "updated incremental -1 1"),
197 + )),
198 + 'innodb_buffer_pool_pages' : (
199 + "'' 'mysql InnoDB Buffer Pool Pages' 'pages' innodb mysql.innodb_buffer_pool_pages line",
200 + (
201 + ("Innodb_buffer_pool_pages_data", "data absolute 1 1"),
202 + ("Innodb_buffer_pool_pages_dirty", "dirty absolute -1 1"),
203 + ("Innodb_buffer_pool_pages_free", "free absolute 1 1"),
204 + ("Innodb_buffer_pool_pages_flushed", "flushed incremental -1 1"),
205 + ("Innodb_buffer_pool_pages_misc", "misc absolute -1 1"),
206 + ("Innodb_buffer_pool_pages_total", "total absolute 1 1"),
207 + )),
208 + 'innodb_buffer_pool_bytes' : (
209 + "'' 'mysql InnoDB Buffer Pool Bytes' 'MB' innodb mysql.innodb_buffer_pool_bytes area",
210 + (
211 + ("Innodb_buffer_pool_bytes_data", "data absolute 1"),
212 + ("Innodb_buffer_pool_bytes_dirty", "dirty absolute -1"),
213 + )),
214 + 'innodb_buffer_pool_read_ahead' : (
215 + "'' 'mysql InnoDB Buffer Pool Read Ahead' 'operations/s' innodb mysql.innodb_buffer_pool_read_ahead area",
216 + (
217 + ("Innodb_buffer_pool_read_ahead", "all incremental 1 1"),
218 + ("Innodb_buffer_pool_read_ahead_evicted", "evicted incremental -1 1"),
219 + ("Innodb_buffer_pool_read_ahead_rnd", "random incremental 1 1"),
220 + )),
221 + 'innodb_buffer_pool_reqs' : (
222 + "'' 'mysql InnoDB Buffer Pool Requests' 'requests/s' innodb mysql.innodb_buffer_pool_reqs area",
223 + (
224 + ("Innodb_buffer_pool_read_requests", "reads incremental 1 1"),
225 + ("Innodb_buffer_pool_write_requests", "writes incremental -1 1"),
226 + )),
227 + 'innodb_buffer_pool_ops' : (
228 + "'' 'mysql InnoDB Buffer Pool Operations' 'operations/s' innodb mysql.innodb_buffer_pool_ops area",
229 + (
230 + ("Innodb_buffer_pool_reads", "'disk reads' incremental 1 1"),
231 + ("Innodb_buffer_pool_wait_free", "'wait free' incremental -1 1"),
232 + )),
233 + 'qcache_ops' : (
234 + "'' 'mysql QCache Operations' 'queries/s' qcache mysql.qcache_ops line",
235 + (
236 + ("Qcache_hits", "hits incremental 1 1"),
237 + ("Qcache_lowmem_prunes", "'lowmem prunes' incremental -1 1"),
238 + ("Qcache_inserts", "inserts incremental 1 1"),
239 + ("Qcache_not_cached", "'not cached' incremental -1 1"),
240 + )),
241 + 'qcache' : (
242 + "'' 'mysql QCache Queries in Cache' 'queries' qcache mysql.qcache line",
243 + (
244 + ("Qcache_queries_in_cache", "queries absolute 1 1"),
245 + )),
246 + 'qcache_freemem' : (
247 + "'' 'mysql QCache Free Memory' 'MB' qcache mysql.qcache_freemem area",
248 + (
249 + ("Qcache_free_memory", "free absolute 1"),
250 + )),
251 + 'qcache_memblocks' : (
252 + "'' 'mysql QCache Memory Blocks' 'blocks' qcache mysql.qcache_memblocks line",
253 + (
254 + ("Qcache_free_blocks", "free absolute 1"),
255 + ("Qcache_total_blocks", "total absolute 1 1"),
256 + )),
257 + 'key_blocks' : (
258 + "'' 'mysql MyISAM Key Cache Blocks' 'blocks' myisam mysql.key_blocks line",
259 + (
260 + ("Key_blocks_unused", "unused absolute 1 1"),
261 + ("Key_blocks_used", "used absolute -1 1"),
262 + ("Key_blocks_not_flushed", "'not flushed' absolute 1 1"),
263 + )),
264 + 'key_requests' : (
265 + "'' 'mysql MyISAM Key Cache Requests' 'requests/s' myisam mysql.key_requests area",
266 + (
267 + ("Key_read_requests", "reads incremental 1 1"),
268 + ("Key_write_requests", "writes incremental -1 1"),
269 + )),
270 + 'key_disk_ops' : (
271 + "'' 'mysql MyISAM Key Cache Disk Operations' 'operations/s' myisam mysql.key_disk_ops area",
272 + (
273 + ("Key_reads", "reads incremental 1 1"),
274 + ("Key_writes", "writes incremental -1 1"),
275 + )),
276 + 'files' : (
277 + "'' 'mysql Open Files' 'files' files mysql.files line",
278 + (
279 + ("Open_files", "files absolute 1 1"),
280 + )),
281 + 'files_rate' : (
282 + "'' 'mysql Opened Files Rate' 'files/s' files mysql.files_rate line",
283 + (
284 + ("Opened_files", "files incremental 1 1"),
285 + )),
286 + 'binlog_stmt_cache' : (
287 + "'' 'mysql Binlog Statement Cache' 'statements/s' binlog mysql.binlog_stmt_cache line",
288 + (
289 + ("Binlog_stmt_cache_disk_use", "disk incremental 1 1"),
290 + ("Binlog_stmt_cache_use", "all incremental 1 1"),
291 + )),
292 + 'connection_errors' : (
293 + "'' 'mysql Connection Errors' 'connections/s' connections mysql.connection_errors line",
294 + (
295 + ("Connection_errors_accept", "accept incremental 1 1"),
296 + ("Connection_errors_internal", "internal incremental 1 1"),
297 + ("Connection_errors_max_connections", "max incremental 1 1"),
298 + ("Connection_errors_peer_address", "peer_addr incremental 1 1"),
299 + ("Connection_errors_select", "select incremental 1 1"),
300 + ("Connection_errors_tcpwrap", "tcpwrap incremental 1 1")
301 + ))
302 +}
303 +mysql_def = {}
304 +valid = []
305 +connections = {}
306 +
307 +def get_data(config):
308 + global connections
309 + try:
310 + cnx = connections[config['name']]
311 + except KeyError:
312 + cnx = pymysql.connect(user=config['user'],
313 + password=config['password'],
314 + unix_socket=config['socket'],
315 + host=config['host'],
316 + port=config['port'],
317 + connect_timeout=int(update_every))
318 + connections[config['name']] = cnx
319 +
320 + try:
321 + with cnx.cursor() as cursor:
322 + cursor.execute(QUERY)
323 + raw_data = cursor.fetchall()
324 + except Exception:
325 + cnx.close()
326 + del connections[config['name']]
327 + return None
328 +
329 + return dict(raw_data)
330 +
331 +
332 +def check():
333 + # TODO what are the default credentials
334 + global valid, config
335 + if type(config) is str:
336 + from json import loads
337 + cfg = loads(config.replace("'",'"').replace('\n',' '))
338 + config = cfg
339 + for i in range(len(config)):
340 + if 'name' not in config[i]:
341 + config[i]['name'] = "mysql_srv_"+str(i)
342 + if 'user' not in config[i]:
343 + config[i]['user'] = 'root'
344 + if 'password' not in config[i]:
345 + config[i]['password'] = None
346 + if 'socket' not in config[i]:
347 + config[i]['socket'] = None
348 + if 'host' not in config[i]:
349 + config[i]['host'] = None
350 + if 'port' in config[i]:
351 + config[i]['port'] = int(config[i]['port'])
352 + else:
353 + config[i]['port'] = 3306
354 + if config[i]['host'] is None:
355 + config[i]['port'] = None
356 + if config[i]['host'] is None and config[i]['socket'] is None:
357 + remove.append(i)
358 +
359 + for srv in config:
360 + try:
361 + cnx = pymysql.connect(user=srv['user'],
362 + password=srv['password'],
363 + unix_socket=srv['socket'],
364 + host=srv['host'],
365 + port=srv['port'],
366 + connect_timeout=int(update_every))
367 + cnx.close()
368 + except:
369 + config.remove(srv)
370 +
371 + if len(config) == 0:
372 + return False
373 + return True
374 +
375 +
376 +def create():
377 + global config, mysql_def
378 + for name in ORDER:
379 + mysql_def[name] = []
380 + for line in CHARTS[name][1]:
381 + mysql_def[name].append(line[0])
382 +
383 + idx = 0
384 + for srv in config:
385 + data = get_data(srv)
386 + for name in ORDER:
387 + header = "CHART mysql_" + \
388 + str(srv['name']) + "." + \
389 + name + " " + \
390 + CHARTS[name][0] + " " + \
391 + str(priority + idx) + " " + \
392 + str(update_every)
393 + content = ""
394 + # check if server has this datapoint
395 + for line in CHARTS[name][1]:
396 + if line[0] in data:
397 + content += "DIMENSION " + line[0] + " " + line[1] + "\n"
398 + if len(content) > 0:
399 + print(header)
400 + print(content)
401 + idx += 1
402 +
403 + if idx == 0:
404 + return False
405 + return True
406 +
407 +
408 +def update(interval):
409 + global config
410 + for srv in config:
411 + data = get_data(srv)
412 + if data is None:
413 + config.remove(srv)
414 + # TODO notify user about problems with server
415 + continue
416 + try:
417 + data['Thread cache misses'] = int( int(data['Threads_created']) * 10000 / int(data['Connections']))
418 + except Exception:
419 + pass
420 + for chart, dimensions in mysql_def.items():
421 + header = "BEGIN mysql_" + str(srv['name']) + "." + chart + " " + str(interval) + '\n'
422 + lines = ""
423 + for d in dimensions:
424 + try:
425 + lines += "SET " + d + " = " + data[d] + '\n'
426 + except KeyError:
427 + pass
428 + if len(lines) > 0:
429 + print(header + lines + "END")
430 +
431 + if len(config) == 0:
432 + return False
433 + return True