@@ -0,0 +1,3740 @@
+# coding=utf-8%0A%0A%22%22%22%0AOpenvzCollector grabs stats from Openvz and submits them the Graphite%0A%0A#### Dependencies%0A%0A * /usr/sbin/vzlist%0A%0A%22%22%22%0A%0Aimport diamond.collector%0Aimport json%0Aimport subprocess%0A%0A%0Aclass OpenvzCollector(diamond.collector.Collector):%0A%0A    _FIELDS = (%0A        'laverage',%0A        'uptime'%0A    )%0A%0A    def get_default_config_help(self):%0A        config_help = super(OpenvzCollector, self).get_default_config_help()%0A        config_help.update(%7B%0A            'bin': 'The path to the vzlist',%0A            'keyname' : 'the name of key for concatenate value (default: hostname)',%0A        %7D)%0A        return config_help%0A%0A    def get_default_config(self):%0A        %22%22%22%0A        Returns the default collector settings%0A        %22%22%22%0A        config = super(OpenvzCollector, self).get_default_config()%0A        config.update(%7B%0A            'path': 'openvz',%0A            'bin': '/usr/sbin/vzlist',%0A            'keyname' : 'hostname'%0A        %7D)%0A        return config%0A%0A    def collect(self):%0A        data = %7B%7D%0A        output = self.poll()%0A%0A        instances_infos = json.loads(output)%0A%0A        if not instances_infos:%0A            return%0A%0A        for instance_values in instances_infos:%0A            serverkey = instance_values%5Bself.config%5B'keyname'%5D%5D.replace('.','_')%0A%0A            for keyvalue in instance_values:%0A                sfield = %5B'held', 'maxheld', 'usage'%5D%0A                # Get Array values%0A                if isinstance(instance_values%5Bkeyvalue%5D, dict):%0A                    for subkey in instance_values%5Bkeyvalue%5D:%0A                        stat_name = '%25s.%25s.%25s' %25 (%0A                            serverkey,%0A                            keyvalue,%0A                            subkey%0A                        )%0A                        if subkey in sfield:%0A                            try:%0A                                metric_value = float(instance_values%5Bkeyvalue%5D%5Bsubkey%5D)%0A                            except ValueError:%0A                                continue%0A%0A                            self.publish(%0A                                stat_name,%0A                                metric_value,%0A                                precision=5%0A                            )%0A                else:%0A                    # Get field value%0A                    if keyvalue in self._FIELDS:%0A                        # Get Load average values%0A                        if keyvalue == 'laverage':%0A                            submetric_name = %5B'01', '05', '15'%5D%0A                            for idx in range(0,3):%0A                                try:%0A                                    metric_value = float(instance_values%5Bkeyvalue%5D%5Bidx%5D)%0A                                except ValueError:%0A                                    continue%0A%0A                                stat_name = '%25s.%25s.%25s' %25 (serverkey, keyvalue, submetric_name%5Bidx%5D)%0A                                self.publish(%0A                                    stat_name,%0A                                    metric_value,%0A                                    precision=5%0A                                )%0A                        else:%0A                            # Field value%0A                            try:%0A                                metric_value = float(instance_values%5Bkeyvalue%5D)%0A                            except ValueError:%0A                                continue%0A%0A                            stat_name = '%25s.%25s' %25 (serverkey, keyvalue)%0A                            self.publish(stat_name, metric_value, precision=5)%0A%0A    def poll(self):%0A        try:%0A            command = %5Bself.config%5B'bin'%5D, '-j'%5D%0A%0A            output = subprocess.Popen(command,%0A                                      stdout=subprocess.PIPE).communicate()%5B0%5D%0A        except OSError:%0A            output = %22%22%0A%0A        return output%0A

Add OpenvzCollector