]> git.ipfire.org Git - collecty.git/commitdiff
processors: Collect usage for all individual cores
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 21 Sep 2020 09:50:03 +0000 (09:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 21 Sep 2020 09:50:03 +0000 (09:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/plugins/base.py
src/collecty/plugins/processor.py

index 8f9cb7893ed6e3f617ee3123768d01fc365e8550..6045a4545f365125b79b9aa8b1e3b026609f81a5 100644 (file)
@@ -497,6 +497,22 @@ class Object(object):
                except ValueError:
                        return None
 
+       def read_proc_stat(self):
+               """
+                       Reads /proc/stat and returns it as a dictionary
+               """
+               ret = {}
+
+               with open("/proc/stat") as f:
+                       for line in f:
+                               # Split the key from the rest of the line
+                               key, line = line.split(" ", 1)
+
+                               # Remove any line breaks
+                               ret[key] = line.rstrip()
+
+               return ret
+
 
 class GraphTemplate(object):
        # A unique name to identify this graph template.
index 11b3aa8550a92e1471dae6ef249060a693a7c408..4629743e0b1b9b05d92929a9a748262dd74b2122 100644 (file)
@@ -19,6 +19,8 @@
 #                                                                             #
 ###############################################################################
 
+import multiprocessing
+
 from . import base
 
 from ..colours import *
@@ -99,34 +101,38 @@ class ProcessorObject(base.Object):
                "DS:wait:DERIVE:0:U",
                "DS:irq:DERIVE:0:U",
                "DS:sirq:DERIVE:0:U",
+               "DS:steal:DERIVE:0:U",
+               "DS:guest:DERIVE:0:U",
        ]
 
+       def init(self, cpu_id=None):
+               self.cpu_id = cpu_id
+
        @property
        def id(self):
+               if self.cpu_id is not None:
+                       return "%s" % self.cpu_id
+
                return "default"
 
        def collect(self):
                """
                        Reads the CPU usage.
                """
-               with open("/proc/stat") as f:
-                       for line in f:
-                               if not line.startswith("cpu"):
-                                       continue
-
-                               columns = line.split()
-                               if len(columns) < 8:
-                                       continue
-
-                               return (
-                                       columns[1], # user
-                                       columns[2], # nice
-                                       columns[3], # sys
-                                       columns[4], # idle
-                                       columns[5], # wait
-                                       columns[6], # irq
-                                       columns[7], # sirq
-                               )
+               stat = self.read_proc_stat()
+
+               if self.cpu_id is None:
+                       values = stat.get("cpu")
+               else:
+                       values = stat.get("cpu%s" % self.cpu_id)
+
+               # Convert values into a list
+               values = values.split()
+
+               if not len(values) == 10:
+                       raise ValueError("Received unexpected output from /proc/stat: %s" % values)
+
+               return values
 
 
 class ProcessorPlugin(base.Plugin):
@@ -138,3 +144,7 @@ class ProcessorPlugin(base.Plugin):
        @property
        def objects(self):
                yield ProcessorObject(self)
+
+               num = multiprocessing.cpu_count()
+               for i in range(num):
+                       yield ProcessorObject(self, cpu_id=i)