Repository: DataDog/dd-agent

Issue:
[mysql] float division by zero
mysql monitoring problem in case of `performance_schema=off` in `[mysqld]` settings

```
    mysql
    -----
      - instance #0 [ERROR]: 'float division by zero'
      - Collected 0 metrics, 0 events & 3 service checks
```

instance options is

```
    options:
        replication: false
        galera_cluster: false
        extra_performance_metrics: false
```

Both agent and mysqld running on same `CoreOS` server (official `mysql:5.7` used)
(on neighbor server same set, but without `performance_schema=off` working fine)



Discussion:
Hi @TrurlMcByte 
Thank you for reporting this issue. I think I see where this is coming from, but to confirm could you please send a flare our way? It contains more detailed information from the agent and will help us troubleshoot more efficiently.
Instructions to send a flare can be found [here](https://help.datadoghq.com/hc/en-us/articles/204991415-Send-logs-and-configs-to-Datadog-via-flare-command).

Thanks again

internal case id is 63386
mysqld config: 

```
[mysqld]
# /etc/my.cnf:
innodb_file_per_table=1
innodb_buffer_pool_size=5M
innodb_log_buffer_size=256K
query_cache_size=0
#max_connections=10
key_buffer_size=8
thread_cache_size=0
host_cache_size=0
innodb_ft_cache_size=1600000
innodb_ft_total_cache_size=32000000

# per thread or per operation settings
thread_stack=131072
sort_buffer_size=32K
read_buffer_size=8200
read_rnd_buffer_size=8200
max_heap_table_size=16K
tmp_table_size=1K
bulk_insert_buffer_size=0
join_buffer_size=128
net_buffer_length=1K
innodb_sort_buffer_size=64K

skip-log-bin
#settings that relate to the binary log (if enabled)
binlog_cache_size=4K
binlog_stmt_cache_size=4K

#largest impact (50% memory)
performance_schema=off
#
#log = /var/lib/mysql/mysqld.log
#log-error = /var/lib/mysql/mysqld.error.log
#
character-set-server=utf8
collation-server=utf8_unicode_ci
sql-mode="TRADITIONAL"
default-storage-engine="MyISAM"
#
```

docker image mysql:5.7

Thank you for sending that over.

There are 2 problems here:
- the first one is we don't check the value of `key_buffer_size` [here](https://github.com/DataDog/dd-agent/blob/5.8.5/checks.d/mysql.py#L479)
- the second one is that the unit is missing in your config file for `key_buffer_size` so mysql doesn't understand it and uses 0 instead (thus the value we collect, then try to divide with)

We'll work on a fix for the first one, I leave the second one to you ;)

Let me know if you need any more help here.


Resolving patch:
diff --git a/checks.d/mysql.py b/checks.d/mysql.py
--- a/checks.d/mysql.py
+++ b/checks.d/mysql.py
@@ -490,13 +490,15 @@ def _collect_metrics(self, host, db, tags, options, queries):
         results['Key_buffer_size'] = key_buffer_size
 
         try:
-            key_cache_utilization = 1 - ((key_blocks_unused * key_cache_block_size) / key_buffer_size)
+            # can be null if the unit is missing in the user config (4 instead of 4G for eg.)
+            if key_buffer_size != 0:
+                key_cache_utilization = 1 - ((key_blocks_unused * key_cache_block_size) / key_buffer_size)
+                results['Key_cache_utilization'] = key_cache_utilization
 
             results['Key_buffer_bytes_used'] = self._collect_scalar(
                 'Key_blocks_used', results) * key_cache_block_size
             results['Key_buffer_bytes_unflushed'] = self._collect_scalar(
                 'Key_blocks_not_flushed', results) * key_cache_block_size
-            results['Key_cache_utilization'] = key_cache_utilization
         except TypeError as e:
             self.log.error("Not all Key metrics are available, unable to compute: {0}".format(e))
 


Test patch: