Page MenuHomeDevCentral

D3492.diff
No OneTemporary

D3492.diff

diff --git a/roles/devserver/userland-software/files/ccache-metrics.py b/roles/devserver/userland-software/files/ccache-metrics.py
new file mode 100755
--- /dev/null
+++ b/roles/devserver/userland-software/files/ccache-metrics.py
@@ -0,0 +1,151 @@
+#!/usr/bin/env python3
+
+# -------------------------------------------------------------
+# OpenMetrics statistics for ccache
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Nasqueron
+# Description: Convert ccache -s output into OpenMetrics format
+# License: BSD-2-Clause
+# Source file: roles/devserver/userland-software/files/ccache-metrics.py
+# -------------------------------------------------------------
+#
+# <auto-generated>
+# This file is managed by our rOPS SaltStack repository.
+#
+# Changes to this file may cause incorrect behavior
+# and will be lost if the state is redeployed.
+# </auto-generated>
+
+
+from datetime import datetime
+import re
+import subprocess
+import time
+
+
+CCACHE_DATE_FORMAT = "%a %b %d %H:%M:%S %Y"
+PREFIX = "ccache"
+
+
+# -------------------------------------------------------------
+# OpenMetrics
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def format_metric(key, value, prefix):
+ metric_key = f"{prefix}_{key}"
+
+ if key.endswith("_directory") or key.endswith("_config"):
+ if not key.endswith("_info"):
+ metric_key += "_info"
+
+ metric_key += "{path=\"" + value + "\"}"
+ type = "info"
+ value = 1
+ elif key.endswith("_rate") or key.endswith("_size") or key == "files_in_cache":
+ type = "gauge"
+ else:
+ if not key.endswith("_total"):
+ metric_key += "_total"
+ type = "counter"
+
+ return f"# TYPE {prefix}_{key} {type}\n{metric_key} {value}"
+
+
+def format_openmetrics(stats, prefix):
+ return [format_metric(key, value, prefix) for key, value in stats.items()]
+
+
+# -------------------------------------------------------------
+# ccache
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def query_ccache_stats():
+ raw_stats = call_ccache_stats().strip().split("\n")
+
+ stats = [parse_ccache_stat_line(line) for line in raw_stats]
+
+ return dict(stats)
+
+
+def call_ccache_stats():
+ process = subprocess.run(["ccache", "-s"], capture_output=True)
+ return process.stdout.decode()
+
+
+def format_key(key):
+ replace_map = {" ": "_", "(": "", ")": "", "/": "_"}
+
+ for k, v in replace_map.items():
+ key = key.replace(k, v)
+
+ return key
+
+
+def parse_date(value):
+ """Parse a ccache date in UTC into a UNIX timestamp."""
+ dt = datetime.strptime(value, CCACHE_DATE_FORMAT)
+
+ return int(time.mktime(dt.timetuple()))
+
+
+def parse_size(value):
+ """Parse a ccache disk size (kB, MB, GB) into bytes."""
+ tokens = value.split(" ")
+ size = tokens[0]
+ unit = tokens[1]
+
+ if unit == "kB":
+ return float(size) * 1024
+ elif unit == "MB":
+ return float(size) * 1024 * 1024
+ elif unit == "GB":
+ return float(size) * 1024 * 1024 * 1024
+
+ raise ValueError(value)
+
+
+def parse_value(key, value):
+ # Date
+ if key == "stats_updated":
+ return parse_date(value)
+
+ # Percent
+ if value.endswith("%"):
+ token = value.split(" ")[0]
+ return float(token) / 100
+
+ # Disk size
+ if key.endswith("_size"):
+ return parse_size(value)
+
+ return value
+
+
+def parse_ccache_stat_line(line):
+ tokens = re.sub(r" {3,}", "\t", line).split("\t")
+
+ key = format_key(tokens[0])
+ value = parse_value(key, tokens[-1])
+
+ return key, value
+
+
+# -------------------------------------------------------------
+# Application entry point
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def run():
+ ccache_stats = query_ccache_stats()
+ ccache_metrics = format_openmetrics(ccache_stats, PREFIX)
+
+ for metric in ccache_metrics:
+ print(metric)
+
+ print("# EOF")
+
+
+if __name__ == "__main__":
+ run()
diff --git a/roles/devserver/userland-software/misc.sls b/roles/devserver/userland-software/misc.sls
--- a/roles/devserver/userland-software/misc.sls
+++ b/roles/devserver/userland-software/misc.sls
@@ -86,6 +86,11 @@
file.managed:
- source: salt://roles/devserver/userland-software/files/ccache.conf
+{{ dirs.bin }}/ccache-metrics:
+ file.managed:
+ - source: salt://roles/devserver/userland-software/files/ccache-metrics.py
+ - mode: 755
+
/etc/make.conf:
file.managed:
- source: salt://roles/devserver/userland-software/files/make.conf

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 22, 16:50 (18 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2256321
Default Alt Text
D3492.diff (4 KB)

Event Timeline