Skip to content

Commit af9aff4

Browse files
authored
Merge pull request #5722 from btriller/CFE-30
CFE-30: Add policy variable sys.cpusockets
2 parents c963304 + 8a3a7fe commit af9aff4

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

libenv/sysinfo.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <evalfunction.h>
5555
#include <json-utils.h>
5656
#include <unix.h> /* GetCurrentUserName() */
57+
#include <glob_lib.h>
5758

5859
#ifdef HAVE_ZONE_H
5960
# include <zone.h>
@@ -3059,6 +3060,52 @@ static void GetCPUInfo(EvalContext *ctx)
30593060
count = (int)sysconf(_SC_NPROCESSORS_ONLN);
30603061
#endif
30613062

3063+
#ifdef __linux__
3064+
int package_id = 0, max_package_id = 0;
3065+
char buffer[CF_SMALLBUF] = "";
3066+
StringSet *package_id_files = GlobFileList("/sys/devices/system/cpu/cpu*/topology/physical_package_id");
3067+
StringSetIterator it = StringSetIteratorInit(package_id_files);
3068+
const char *file = NULL;
3069+
3070+
while ((file = StringSetIteratorNext(&it)))
3071+
{
3072+
int f = open(file, O_RDONLY);
3073+
if (f == -1)
3074+
{
3075+
continue;
3076+
}
3077+
ssize_t n_read = FullRead(f, buffer, sizeof(buffer));
3078+
if (n_read < 1)
3079+
{
3080+
continue;
3081+
}
3082+
if (sscanf(buffer, "%d", &package_id) == 1)
3083+
{
3084+
if (package_id > max_package_id)
3085+
{
3086+
max_package_id = package_id;
3087+
}
3088+
}
3089+
}
3090+
max_package_id++;
3091+
StringSetDestroy(package_id_files);
3092+
3093+
if (max_package_id == 1)
3094+
{
3095+
NDEBUG_UNUSED int ret = snprintf(buffer, CF_SMALLBUF, "%d_cpusocket", max_package_id);
3096+
assert(ret >= 0 && ret < CF_SMALLBUF);
3097+
}
3098+
else
3099+
{
3100+
NDEBUG_UNUSED int ret = snprintf(buffer, CF_SMALLBUF, "%d_cpusockets", max_package_id);
3101+
assert(ret >= 0 && ret < CF_SMALLBUF);
3102+
}
3103+
EvalContextClassPutHard(ctx, buffer, "source=agent,derived-from=sys.cpusockets");
3104+
NDEBUG_UNUSED int ret = snprintf(buffer, CF_SMALLBUF, "%d", max_package_id);
3105+
assert(ret >= 0 && ret < CF_SMALLBUF);
3106+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cpusockets", buffer, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=CPU sockets");
3107+
#endif
3108+
30623109
#if defined(HAVE_SYS_SYSCTL_H) && defined(HW_NCPU)
30633110
// BSD-derived platforms
30643111
int mib[2] = { CTL_HW, HW_NCPU };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#######################################################
2+
#
3+
# Test that sys.cpusockets variable exists on Linux
4+
#
5+
#######################################################
6+
7+
body common control
8+
{
9+
inputs => { "../../default.cf.sub" };
10+
bundlesequence => { default("$(this.promise_filename)") };
11+
version => "1.0";
12+
}
13+
14+
#######################################################
15+
16+
bundle agent test
17+
{
18+
meta:
19+
"description" -> { "CFE-30" }
20+
string => "Test that sys.cpusockets variable exists on Linux";
21+
22+
"test_soft_fail"
23+
string => "!linux",
24+
meta => { "CFE-30" };
25+
}
26+
27+
#######################################################
28+
29+
bundle agent check
30+
{
31+
classes:
32+
"ok"
33+
expression => isvariable("sys.cpusockets");
34+
35+
methods:
36+
"any"
37+
usebundle => dcs_passif("ok", "$(this.promise_filename)"),
38+
inherit => "true";
39+
40+
reports:
41+
"I have $(sys.cpusockets) CPU socket(s)";
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#######################################################
2+
#
3+
# Test that n_cpusocket(s) class exists on Linux
4+
#
5+
#######################################################
6+
7+
body common control
8+
{
9+
inputs => { "../../default.cf.sub" };
10+
bundlesequence => { default("$(this.promise_filename)") };
11+
version => "1.0";
12+
}
13+
14+
#######################################################
15+
16+
bundle agent test
17+
{
18+
meta:
19+
"description" -> { "CFE-30" }
20+
string => "Test that n_cpusocket(s) class exists on Linux";
21+
22+
"test_soft_fail"
23+
string => "!linux",
24+
meta => { "CFE-30" };
25+
26+
vars:
27+
"num_matching"
28+
int => countclassesmatching("[0-9]+_cpusockets?");
29+
}
30+
31+
#######################################################
32+
33+
bundle agent check
34+
{
35+
classes:
36+
"ok"
37+
expression => eval("$(test.num_matching) == 1", "class", "infix");
38+
39+
methods:
40+
"any"
41+
usebundle => dcs_passif("ok", "$(this.promise_filename)"),
42+
inherit => "true";
43+
}

0 commit comments

Comments
 (0)