(self, metric_type, field_metric_map, query, db, tags)
Query status and get a dictionary back.
        Extract each field out of the dictionary
        and stuff it in the corresponding metric.

        query: show status...
        field_metric_map: {"Seconds_behind_master": "mysqlSecondsBehindMaster"}
Query status and get a dictionary back.
        Extract each field out of the dictionary
        and stuff it in the corresponding metric.
Query status and get a dictionary back . Extract each field out of the dictionary and stuff it in the corresponding metric .
def _collect_dict(self, metric_type, field_metric_map, query, db, tags):
        """
        Query status and get a dictionary back.
        Extract each field out of the dictionary
        and stuff it in the corresponding metric.

        query: show status...
        field_metric_map: {"Seconds_behind_master": "mysqlSecondsBehindMaster"}
        """
        try:
            with closing(db.cursor()) as cursor:
                cursor.execute(query)
                result = cursor.fetchone()
                if result is not None:
                    for field, metric in list(iteritems(field_metric_map)):
                        # Find the column name in the cursor description to identify the column index
                        # http://www.python.org/dev/peps/pep-0249/
                        # cursor.description is a tuple of (column_name, ..., ...)
                        try:
                            col_idx = [d[0].lower() for d in cursor.description].index(field.lower())
                            self.log.debug("Collecting metric: %s", metric)
                            if result[col_idx] is not None:
                                self.log.debug("Collecting done, value %s", result[col_idx])
                                if metric_type == GAUGE:
                                    self.gauge(
                                        metric, float(result[col_idx]), tags=tags, hostname=self.resolved_hostname
                                    )
                                elif metric_type == RATE:
                                    self.rate(
                                        metric, float(result[col_idx]), tags=tags, hostname=self.resolved_hostname
                                    )
                                else:
                                    self.gauge(
                                        metric, float(result[col_idx]), tags=tags, hostname=self.resolved_hostname
                                    )
                            else:
                                self.log.debug("Received value is None for index %d", col_idx)
                        except ValueError:
                            self.log.exception("Cannot find %s in the columns %s", field, cursor.description)
        except Exception:
            self.warning("Error while running %s\n%s", query, traceback.format_exc())
            self.log.exception("Error while running %s", query)
def _collect_dict ( self , metric_type , field_metric_map , query , db , tags ) : try : with closing ( db . cursor ( ) ) as cursor : cursor . execute ( query ) result = cursor . fetchone ( ) if result is not None : for field , metric in list ( iteritems ( field_metric_map ) ) : # Find the column name in the cursor description to identify the column index # http://www.python.org/dev/peps/pep-0249/ # cursor.description is a tuple of (column_name, ..., ...) try : col_idx = [ d [ 0 ] . lower ( ) for d in cursor . description ] . index ( field . lower ( ) ) self . log . debug ( "Collecting metric: %s" , metric ) if result [ col_idx ] is not None : self . log . debug ( "Collecting done, value %s" , result [ col_idx ] ) if metric_type == GAUGE : self . gauge ( metric , float ( result [ col_idx ] ) , tags = tags , hostname = self . resolved_hostname ) elif metric_type == RATE : self . rate ( metric , float ( result [ col_idx ] ) , tags = tags , hostname = self . resolved_hostname ) else : self . gauge ( metric , float ( result [ col_idx ] ) , tags = tags , hostname = self . resolved_hostname ) else : self . log . debug ( "Received value is None for index %d" , col_idx ) except ValueError : self . log . exception ( "Cannot find %s in the columns %s" , field , cursor . description ) except Exception : self . warning ( "Error while running %s\n%s" , query , traceback . format_exc ( ) ) self . log . exception ( "Error while running %s" , query )