Browse Source

mongodb: various KeyError fixes (#5931)

* ensure that `locks` key in server_status dict before in

* ensure that `mapped` in server_status['mem'] + minor
Ilya Mashchenko 5 years ago
parent
commit
647cc8851d
1 changed files with 26 additions and 22 deletions
  1. 26 22
      collectors/python.d.plugin/mongodb/mongodb.chart.py

+ 26 - 22
collectors/python.d.plugin/mongodb/mongodb.chart.py

@@ -469,7 +469,8 @@ class Service(SimpleService):
             self.metrics_to_collect.extend(COMMANDS)
         if 'wiredTiger' in server_status:
             self.metrics_to_collect.extend(WIREDTIGER)
-        if 'Collection' in server_status['locks']:
+        has_locks = 'locks' in server_status
+        if has_locks and 'Collection' in server_status['locks']:
             self.metrics_to_collect.extend(LOCKS)
 
     def create_charts_(self, server_status):
@@ -496,13 +497,14 @@ class Service(SimpleService):
             self.order.remove('command_total_rate')
             self.order.remove('command_failed_rate')
 
-        if 'Collection' not in server_status['locks']:
+        has_no_locks = 'locks' not in server_status
+        if has_no_locks or 'Collection' not in server_status['locks']:
             self.order.remove('locks_collection')
             self.order.remove('locks_database')
             self.order.remove('locks_global')
             self.order.remove('locks_metadata')
 
-        if 'oplog' not in server_status['locks']:
+        if has_no_locks or 'oplog' not in server_status['locks']:
             self.order.remove('locks_oplog')
 
         for dbase in self.databases:
@@ -632,7 +634,7 @@ class Service(SimpleService):
         if not raw_data:
             return None
 
-        to_netdata = dict()
+        data = dict()
         serverStatus = raw_data['serverStatus']
         dbStats = raw_data.get('dbStats')
         replSetGetStatus = raw_data.get('replSetGetStatus')
@@ -649,23 +651,22 @@ class Service(SimpleService):
                     break
 
             if not isinstance(value, dict) and key:
-                to_netdata[new_name or key] = value if not func else func(value)
+                data[new_name or key] = value if not func else func(value)
 
-        to_netdata['nonmapped'] = to_netdata['virtual'] - serverStatus['mem'].get('mappedWithJournal',
-                                                                                  to_netdata['mapped'])
-        if to_netdata.get('maximum bytes configured'):
-            maximum = to_netdata['maximum bytes configured']
-            to_netdata['wiredTiger_percent_clean'] = int(to_netdata['bytes currently in the cache']
-                                                         * 100 / maximum * 1000)
-            to_netdata['wiredTiger_percent_dirty'] = int(to_netdata['tracked dirty bytes in the cache']
-                                                         * 100 / maximum * 1000)
+        if 'mapped' in serverStatus['mem']:
+            data['nonmapped'] = data['virtual'] - serverStatus['mem'].get('mappedWithJournal', data['mapped'])
+
+        if data.get('maximum bytes configured'):
+            maximum = data['maximum bytes configured']
+            data['wiredTiger_percent_clean'] = int(data['bytes currently in the cache'] * 100 / maximum * 1000)
+            data['wiredTiger_percent_dirty'] = int(data['tracked dirty bytes in the cache'] * 100 / maximum * 1000)
 
         # dbStats
         if dbStats:
             for dbase in dbStats:
                 for metric in DBSTATS:
                     key = '_'.join([dbase, metric])
-                    to_netdata[key] = dbStats[dbase][metric]
+                    data[key] = dbStats[dbase][metric]
 
         # replSetGetStatus
         if replSetGetStatus:
@@ -676,30 +677,33 @@ class Service(SimpleService):
             for member in members:
                 if not member.get('self'):
                     other_hosts.append(member)
+
                 # Replica set time diff between current time and time when last entry from the oplog was applied
                 if member.get('optimeDate', unix_epoch) != unix_epoch:
                     member_optimedate = member['name'] + '_optimedate'
-                    to_netdata.update({member_optimedate: int(delta_calculation(delta=utc_now - member['optimeDate'],
-                                                                                multiplier=1000))})
+                    delta = utc_now - member['optimeDate']
+                    data[member_optimedate] = int(delta_calculation(delta=delta, multiplier=1000))
+
                 # Replica set members state
                 member_state = member['name'] + '_state'
                 for elem in REPL_SET_STATES:
                     state = elem[0]
-                    to_netdata.update({'_'.join([member_state, state]): 0})
-                to_netdata.update({'_'.join([member_state, str(member['state'])]): member['state']})
+                    data.update({'_'.join([member_state, state]): 0})
+                data.update({'_'.join([member_state, str(member['state'])]): member['state']})
+
             # Heartbeat lag calculation
             for other in other_hosts:
                 if other['lastHeartbeatRecv'] != unix_epoch:
                     node = other['name'] + '_heartbeat_lag'
-                    to_netdata[node] = int(delta_calculation(delta=utc_now - other['lastHeartbeatRecv'],
-                                                             multiplier=1000))
+                    delta = utc_now - other['lastHeartbeatRecv']
+                    data[node] = int(delta_calculation(delta=delta, multiplier=1000))
 
         if getReplicationInfo:
             first_event = getReplicationInfo['ASCENDING']['ts'].as_datetime()
             last_event = getReplicationInfo['DESCENDING']['ts'].as_datetime()
-            to_netdata['timeDiff'] = int(delta_calculation(delta=last_event - first_event, multiplier=1000))
+            data['timeDiff'] = int(delta_calculation(delta=last_event - first_event, multiplier=1000))
 
-        return to_netdata
+        return data
 
     def _create_connection(self):
         conn_vars = {'host': self.host, 'port': self.port}