Skip to content

Commit

Permalink
Add ability to report the number of logical processors (#271)
Browse files Browse the repository at this point in the history
* Add logical processors to custom field

* Updated the update_results function to append any custom fields rather than overwriting them

* fix: flake to load all dependencies as outlined in contributing.md

* fix: bug in cpu_count where it may only retrieve sockets

* chore: Rename custom field to be consistent with the api

---------

Co-authored-by: samdesmondking <[email protected]>
  • Loading branch information
justinbarclay and SamDesmondKing authored Jul 10, 2024
1 parent b9c394b commit 6ad4d40
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions unix/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
postVenvCreation = ''
unset SOURCE_DATE_EPOCH
pip install -r ./requirements.txt
pip install -r dev-requirements.txt
pip install -e .
'';

# Now we can execute any commands within the virtual environment.
Expand Down
27 changes: 19 additions & 8 deletions unix/src/machine_stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ def storage_used_gb(facts):


def cpu_count(facts):
"""Return the number of CPUs"""
return max(
[
int(facts.get("ansible_processor_count", 0)),
int(facts.get("ansible_processor_vcpus", 0)),
]
)
"""Return the number of CPU cores"""
return max(int(facts.get("ansible_processor_count", 0)),
int(facts.get("ansible_processor_cores", 0)))

def cpu_logical_processors(facts):
"""Return the number of CPU logical processors."""
return int(facts.get("ansible_processor_vcpus", 0))


def cpu_name(proc):
Expand Down Expand Up @@ -200,8 +200,16 @@ def update_results(self, host, data: dict):

if host not in self._total_results:
self._total_results[host] = data
else:
return

# Ensure we append any custom fields, rather than overwriting them
if 'custom_fields' in data and 'custom_fields' in self._total_results[host]:
combined_custom_fields = {**self._total_results[host]['custom_fields'], **data['custom_fields']}
data['custom_fields'].update(combined_custom_fields)
self._total_results[host].update(data)
return

self._total_results[host].update(data)

def v2_runner_on_ok(self, result):
self._plugins.ok_callback(self, result)
Expand All @@ -222,6 +230,9 @@ def v2_runner_on_ok(self, result):
"storage_allocated_gb": storage_allocated_gb(facts),
"storage_used_gb": storage_used_gb(facts),
"cpu_count": cpu_count(facts),
"custom_fields": {
"cpu_logical_processors": cpu_logical_processors(facts)
},
"operating_system": facts["ansible_distribution"],
"operating_system_version": facts["ansible_distribution_version"],
"cpu_name": cpu_name(facts["ansible_processor"]),
Expand Down
3 changes: 2 additions & 1 deletion windows/server_stats.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ $ServerStats = {
CPU_Description = $cpu.Description
CPU_Manufacturer = $cpu.Manufacturer
CPU_L2CacheSize = $cpu.L2CacheSize
CPU_L3CacheSize = $cpu.L3CacheSize
CPU_L3CacheSize = $cpu.L3CacheSize
cpu_logical_processors = $cpu.NumberOfLogicalProcessors
CPU_SocketDesignation = $cpu.SocketDesignation
TotalVisible_Memory_GB = $OSTotalVisibleMemory
TotalVirtual_Memory_GB = $OSTotalVirtualMemory
Expand Down

0 comments on commit 6ad4d40

Please sign in to comment.