Record OS version

This commit is contained in:
Peter J. Holzer 2023-02-03 13:27:25 +01:00
parent cfc514a3eb
commit 62b22d7516
1 changed files with 43 additions and 0 deletions

43
clients/record_os_version Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/python3
import re
import subprocess
import time
import ltsdb_record
p = subprocess.run(["/usr/bin/lsb_release", "-ir"], capture_output=True, text=True)
for ln in p.stdout.split("\n")[:-1]:
m = re.match(r"(.*?)\s*:\s+(.*)", ln)
if m:
if m.group(1) == "Distributor ID":
distributor = m.group(2).lower()
elif m.group(1) == "Release":
release = m.group(2)
if distributor == "ubuntu":
# special rule for ubuntu. The format is year.month, so we convert the
# months into fractional years
m = re.match(r"(\d+)\.(\d+)", release)
release = int(m.group(1)) + (int(m.group(2)) - 1) / 12
else:
# for everybody else we assume its a fp number
release = float(release)
report0 = []
report0.append({ "measure": "os_version_" + distributor, "unit": "version", "value": release})
now = time.time()
report = [
{
"description": {
"hostname": ltsdb_record.node,
"measure": r["measure"],
"unit": r["unit"]
},
"data": [
[now, r["value"]]
]
}
for r in report0
]
success = ltsdb_record.record_observations(report)
exit(1 - success)