Merge "Fix for emitting 0s instead of nothing"
diff --git a/collectd/files/plugin/openstack_cinder.py b/collectd/files/plugin/openstack_cinder.py
index fe667a6..9f8e0e5 100644
--- a/collectd/files/plugin/openstack_cinder.py
+++ b/collectd/files/plugin/openstack_cinder.py
@@ -23,6 +23,13 @@
 
 PLUGIN_NAME = 'openstack_cinder'
 INTERVAL = openstack.INTERVAL
+volumes_statuses = ('creating', 'available', 'attaching',
+                    'in-use', 'deleting', 'error',
+                    'error-deleting', 'backing-up',
+                    'restoring-backup', 'error_restoring',
+                    'error_extending')
+snapshots_statuses = ('creating', 'available', 'deleting',
+                      'error', 'error_deleting')
 
 
 class CinderStatsPlugin(openstack.CollectdPlugin):
@@ -38,11 +45,18 @@
         self.interval = INTERVAL
         self.pagination_limit = 500
 
-    def itermetrics(self):
+    @staticmethod
+    def gen_metric(name, nb, state):
+        return {
+            'plugin_instance': name,
+            'values': nb,
+            'meta': {
+                'state': state,
+                'discard_hostname': True,
+            }
+        }
 
-        volumes_details = self.get_objects('cinderv2', 'volumes',
-                                           params={'all_tenants': 1},
-                                           detail=True)
+    def itermetrics(self):
 
         def groupby(d):
             return d.get('status', 'unknown').lower()
@@ -50,45 +64,44 @@
         def count_size_bytes(d):
             return d.get('size', 0) * 10 ** 9
 
-        status = self.count_objects_group_by(volumes_details,
-                                             group_by_func=groupby)
-        for s, nb in status.iteritems():
-            yield {
-                'plugin_instance': 'volumes',
-                'values': nb,
-                'meta': {'state': s, 'discard_hostname': True}
-            }
+        vols_details = self.get_objects('cinderv2', 'volumes',
+                                        params={'all_tenants': 1},
+                                        detail=True)
+        vols_status = self.count_objects_group_by(vols_details,
+                                                  group_by_func=groupby)
+        for status in volumes_statuses:
+            nb = vols_status.get(status, 0)
+            yield CinderStatsPlugin.gen_metric('volumes',
+                                               nb,
+                                               status)
 
-        sizes = self.count_objects_group_by(volumes_details,
-                                            group_by_func=groupby,
-                                            count_func=count_size_bytes)
-        for s, size in sizes.iteritems():
-            yield {
-                'plugin_instance': 'volumes_size',
-                'values': size,
-                'meta': {'state': s, 'discard_hostname': True}
-            }
+        vols_sizes = self.count_objects_group_by(vols_details,
+                                                 group_by_func=groupby,
+                                                 count_func=count_size_bytes)
+        for status in volumes_statuses:
+            nb = vols_sizes.get(status, 0)
+            yield CinderStatsPlugin.gen_metric('volumes_size',
+                                               nb,
+                                               status)
 
         snaps_details = self.get_objects('cinderv2', 'snapshots',
                                          params={'all_tenants': 1})
-        status_snaps = self.count_objects_group_by(snaps_details,
+        snaps_status = self.count_objects_group_by(snaps_details,
                                                    group_by_func=groupby)
-        for s, nb in status_snaps.iteritems():
-            yield {
-                'plugin_instance': 'snapshots',
-                'values': nb,
-                'meta': {'state': s, 'discard_hostname': True}
-            }
+        for status in snapshots_statuses:
+            nb = snaps_status.get(status, 0)
+            yield CinderStatsPlugin.gen_metric('snapshots',
+                                               nb,
+                                               status)
 
-        sizes = self.count_objects_group_by(snaps_details,
-                                            group_by_func=groupby,
-                                            count_func=count_size_bytes)
-        for n, size in sizes.iteritems():
-            yield {
-                'plugin_instance': 'snapshots_size',
-                'values': size,
-                'meta': {'state': s, 'discard_hostname': True}
-            }
+        snaps_sizes = self.count_objects_group_by(snaps_details,
+                                                  group_by_func=groupby,
+                                                  count_func=count_size_bytes)
+        for status in snapshots_statuses:
+            nb = snaps_sizes.get(status, 0)
+            yield CinderStatsPlugin.gen_metric('snapshots_size',
+                                               nb,
+                                               status)
 
 
 plugin = CinderStatsPlugin(collectd, PLUGIN_NAME, disable_check_metric=True)
@@ -105,6 +118,7 @@
 def read_callback():
     plugin.conditional_read_callback()
 
+
 if __name__ == '__main__':
     import time
     collectd.load_configuration(plugin)