Returns metrics stat file contents. root: a cgroups root (a path as a string) container: an object with string attribute id stat: a string filename Returns contents of / / with newlines replaced with spaces. Returns None on errors.
(root, container, stat)
| 147 | |
| 148 | @staticmethod |
| 149 | def _metrics_from_stat_file(root, container, stat): |
| 150 | """Returns metrics stat file contents. |
| 151 | |
| 152 | root: a cgroups root (a path as a string) |
| 153 | container: an object with string attribute id |
| 154 | stat: a string filename |
| 155 | |
| 156 | Returns contents of <root>/<container.id>/<stat> |
| 157 | with newlines replaced with spaces. |
| 158 | Returns None on errors. |
| 159 | """ |
| 160 | dirname = os.path.join(root, "docker", container.id) |
| 161 | if not os.path.isdir(dirname): |
| 162 | # Container may no longer exist. |
| 163 | return None |
| 164 | try: |
| 165 | statcontents = open(os.path.join(dirname, stat)).read() |
| 166 | return statcontents.replace("\n", " ").strip() |
| 167 | except IOError as e: |
| 168 | # Ignore errors; cgroup can disappear on us. |
| 169 | logging.warning("Ignoring exception reading cgroup. " + |
| 170 | "This can happen if container just exited. " + str(e)) |
| 171 | return None |
| 172 | |
| 173 | def _monitor(self): |
| 174 | """Monitors CPU usage of containers. |