Skip to content

Commit d90b18b

Browse files
committed
Use --format switch to create different inventories for glpi and ocs
If no format is specified, use a common denominator (less tags so no one complains)
1 parent 1d1647b commit d90b18b

File tree

2 files changed

+89
-82
lines changed

2 files changed

+89
-82
lines changed

Inventory.cpp

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@ bool
9494
Inventory::Build(bool noSoftware)
9595
{
9696
Logger::Log(LOG_INFO, "Building inventory...");
97+
std::string inventoryFormat = Configuration::Get()->KeyValue("format");
98+
if (inventoryFormat.empty()) {
99+
Logger::Log(LOG_WARNING, "No inventory format specified. Using lowest common denominator.");
100+
}
97101
try {
98-
// TODO: For glpi only
99-
tinyxml2::XMLElement* versionClient = fDocument->NewElement("VERSIONCLIENT");
100-
versionClient->LinkEndChild(fDocument->NewText("1.9.9"));
101-
fContent->LinkEndChild(versionClient);
102+
if (inventoryFormat == "FORMAT_GLPI") {
103+
tinyxml2::XMLElement* versionClient = fDocument->NewElement("VERSIONCLIENT");
104+
versionClient->LinkEndChild(fDocument->NewText("1.9.9"));
105+
fContent->LinkEndChild(versionClient);
106+
}
102107
_AddAccountInfo();
103108
_AddBIOSInfo();
104109
_AddOperatingSystemInfo();
@@ -544,19 +549,75 @@ Inventory::_AddCPUsInfo()
544549
tinyxml2::XMLElement* cpu = fDocument->NewElement("CPUS");
545550
fContent->LinkEndChild(cpu);
546551

547-
#if 0
548-
// OCSInventoryFormatOCS always reports "CPU Enabled" here
549-
// TODO: GLPI doesn't really like serial number and rejects the inventory
550-
tinyxml2::XMLElement* serial = fDocument->NewElement("SERIALNUMBER");
551-
std::string cpuSerial = cpuInfo.fields["serial"];
552-
if (cpuSerial.empty())
553-
cpuSerial = "CPU Enabled";
554-
if (cpuSerial == "None")
555-
cpuSerial = "";
556-
serial->LinkEndChild(
557-
fDocument->NewText(cpuSerial.c_str()));
558-
cpu->LinkEndChild(serial);
559-
#endif
552+
// OCS Inventory
553+
if (Configuration::Get()->KeyValue("format") == "FORMAT_OCS") {
554+
// The official OCSInventory linux Agent always reports "CPU Enabled" here
555+
tinyxml2::XMLElement* serial = fDocument->NewElement("SERIALNUMBER");
556+
std::string cpuSerial = cpuInfo.fields["serial"];
557+
if (cpuSerial.empty())
558+
cpuSerial = "CPU Enabled";
559+
if (cpuSerial == "None")
560+
cpuSerial = "";
561+
serial->LinkEndChild(
562+
fDocument->NewText(cpuSerial.c_str()));
563+
cpu->LinkEndChild(serial);
564+
565+
tinyxml2::XMLElement* currentSpeed = fDocument->NewElement("CURRENT_SPEED");
566+
currentSpeed->LinkEndChild(
567+
fDocument->NewText(cpuInfo.fields["current_speed"].c_str()));
568+
cpu->LinkEndChild(currentSpeed);
569+
570+
tinyxml2::XMLElement* cores = fDocument->NewElement("CORES");
571+
cores->LinkEndChild(
572+
fDocument->NewText(cpuInfo.fields["cores"].c_str()));
573+
cpu->LinkEndChild(cores);
574+
tinyxml2::XMLElement* arch = fDocument->NewElement("CPUARCH");
575+
arch->LinkEndChild(
576+
fDocument->NewText(gComponents["OS"].fields["architecture"].c_str()));
577+
cpu->LinkEndChild(arch);
578+
tinyxml2::XMLElement* dataWidth = fDocument->NewElement("DATA_WIDTH");
579+
std::string dataWidthString = gComponents["CPU"].fields["width"];
580+
if (dataWidthString.empty()) {
581+
// TODO: This is not completely correct:
582+
// We could have a 64 bit capable CPU on a 32 bit OS.
583+
if (gComponents["OS"].fields["architecture"] == "x86_64")
584+
dataWidthString = "64";
585+
else
586+
dataWidthString = "32";
587+
}
588+
dataWidth->LinkEndChild(
589+
fDocument->NewText(dataWidthString.c_str()));
590+
cpu->LinkEndChild(dataWidth);
591+
// Not a copy/paste error: the fields are the same
592+
593+
tinyxml2::XMLElement* currentAddressWidth = fDocument->NewElement("CURRENT_ADDRESS_WIDTH");
594+
currentAddressWidth->LinkEndChild(
595+
fDocument->NewText(dataWidthString.c_str()));
596+
cpu->LinkEndChild(currentAddressWidth);
597+
598+
tinyxml2::XMLElement* cacheSize = fDocument->NewElement("L2CACHESIZE");
599+
cacheSize->LinkEndChild(
600+
fDocument->NewText(cpuInfo.fields["cache_size"].c_str()));
601+
cpu->LinkEndChild(cacheSize);
602+
603+
tinyxml2::XMLElement* logicalCpu = fDocument->NewElement("LOGICAL_CPUS");
604+
logicalCpu->LinkEndChild(
605+
fDocument->NewText(cpuInfo.fields["logical_cpus"].c_str()));
606+
cpu->LinkEndChild(logicalCpu);
607+
608+
tinyxml2::XMLElement* voltage = fDocument->NewElement("VOLTAGE");
609+
voltage->LinkEndChild(
610+
fDocument->NewText(cpuInfo.fields["voltage"].c_str()));
611+
cpu->LinkEndChild(voltage);
612+
613+
} else if (Configuration::Get()->KeyValue("format") == "FORMAT_GLPI") {
614+
// GLPI
615+
tinyxml2::XMLElement* cores = fDocument->NewElement("CORE");
616+
cores->LinkEndChild(
617+
fDocument->NewText(cpuInfo.fields["cores"].c_str()));
618+
cpu->LinkEndChild(cores);
619+
}
620+
560621
tinyxml2::XMLElement* manufacturer = fDocument->NewElement("MANUFACTURER");
561622
cpu->LinkEndChild(manufacturer);
562623
manufacturer->LinkEndChild(
@@ -566,12 +627,7 @@ Inventory::_AddCPUsInfo()
566627
speed->LinkEndChild(
567628
fDocument->NewText(cpuInfo.fields["speed"].c_str()));
568629
cpu->LinkEndChild(speed);
569-
#if 0
570-
tinyxml2::XMLElement* currentSpeed = fDocument->NewElement("CURRENT_SPEED");
571-
currentSpeed->LinkEndChild(
572-
fDocument->NewText(cpuInfo.fields["current_speed"].c_str()));
573-
cpu->LinkEndChild(currentSpeed);
574-
#endif
630+
575631
tinyxml2::XMLElement* model = fDocument->NewElement("TYPE");
576632
model->LinkEndChild(
577633
fDocument->NewText(cpuInfo.fields["type"].c_str()));
@@ -582,59 +638,6 @@ Inventory::_AddCPUsInfo()
582638
name->LinkEndChild(
583639
fDocument->NewText(cpuInfo.fields["type"].c_str()));
584640
cpu->LinkEndChild(name);
585-
#if 0
586-
// TODO: GLPI wants "CORE" here
587-
tinyxml2::XMLElement* cores = fDocument->NewElement("CORES");
588-
cores->LinkEndChild(
589-
fDocument->NewText(cpuInfo.fields["cores"].c_str()));
590-
cpu->LinkEndChild(cores);
591-
#endif
592-
#if 0
593-
tinyxml2::XMLElement* arch = fDocument->NewElement("CPUARCH");
594-
arch->LinkEndChild(
595-
fDocument->NewText(gComponents["OS"].fields["architecture"].c_str()));
596-
cpu->LinkEndChild(arch);
597-
#endif
598-
#if 0
599-
tinyxml2::XMLElement* dataWidth = fDocument->NewElement("DATA_WIDTH");
600-
std::string dataWidthString = gComponents["CPU"].fields["width"];
601-
if (dataWidthString.empty()) {
602-
// TODO: This is not completely correct:
603-
// We could have a 64 bit capable CPU on a 32 bit OS.
604-
if (gComponents["OS"].fields["architecture"] == "x86_64")
605-
dataWidthString = "64";
606-
else
607-
dataWidthString = "32";
608-
}
609-
dataWidth->LinkEndChild(
610-
fDocument->NewText(dataWidthString.c_str()));
611-
cpu->LinkEndChild(dataWidth);
612-
// Not a copy/paste error: the fields are the same
613-
614-
tinyxml2::XMLElement* currentAddressWidth = fDocument->NewElement("CURRENT_ADDRESS_WIDTH");
615-
currentAddressWidth->LinkEndChild(
616-
fDocument->NewText(dataWidthString.c_str()));
617-
cpu->LinkEndChild(currentAddressWidth);
618-
#endif
619-
#if 0
620-
tinyxml2::XMLElement* cacheSize = fDocument->NewElement("L2CACHESIZE");
621-
cacheSize->LinkEndChild(
622-
fDocument->NewText(cpuInfo.fields["cache_size"].c_str()));
623-
cpu->LinkEndChild(cacheSize);
624-
#endif
625-
#if 0
626-
tinyxml2::XMLElement* logicalCpu = fDocument->NewElement("LOGICAL_CPUS");
627-
logicalCpu->LinkEndChild(
628-
fDocument->NewText(cpuInfo.fields["logical_cpus"].c_str()));
629-
cpu->LinkEndChild(logicalCpu);
630-
#endif
631-
#if 0
632-
tinyxml2::XMLElement* voltage = fDocument->NewElement("VOLTAGE");
633-
voltage->LinkEndChild(
634-
fDocument->NewText(cpuInfo.fields["voltage"].c_str()));
635-
cpu->LinkEndChild(voltage);
636-
#endif
637-
638641

639642
}
640643
Logger::Log(LOG_DEBUG, "\tAdded CPUs Info!");

main.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ PrintHelpAndExit()
5252
std::cout << "Usage:" << std::endl;
5353
std::cout << " -h, --help Print usage" << std::endl;
5454
std::cout << " -c, --conf <config_file> Specify configuration file" << std::endl;
55-
std::cout << " -s, --server <server> Specify OCSInventory server url" << std::endl;
55+
std::cout << " -s, --server <server> Specify OCSInventory/GLPI server url" << std::endl;
5656
std::cout << " If the server needs authentication, use the standard syntax <user>:<password>@<host>" << std::endl;
57-
std::cout << " --format <format> Specify the inventory format: FORMAT_OCS or FORMAT_JSON" << std::endl;
58-
std::cout << " -l, --local <folder> Don't send inventory, instead save a local copy in the specified file or folder" << std::endl;
59-
std::cout << " --stdout Don't send inventory, print it to stdout" << std::endl;
57+
std::cout << " --format <format> Specify the inventory format: FORMAT_OCS or FORMAT_GLPI" << std::endl;
58+
std::cout << " -l, --local <folder> Save a local inventory in the specified file or folder" << std::endl;
59+
std::cout << " --stdout Print inventory to stdout" << std::endl;
6060
std::cout << std::endl;
6161
std::cout << " -t, --tag <TAG> Specify tag. Will be ignored by server if a value already exists" << std::endl;
6262
std::cout << " --nosoftware Do not retrieve installed software" << std::endl;
@@ -158,8 +158,12 @@ HandleArgs(int argc, char **argv)
158158
std::string optName = sLongOptions[optIndex].name;
159159
if (optName == "nosoftware")
160160
config->SetVolatileKeyValue("nosoftware", "true");
161-
else if (optName == "format")
162-
config->SetVolatileKeyValue("format", optarg);
161+
else if (optName == "format") {
162+
if (::strcasecmp(optarg, "FORMAT_OCS") == 0 ||
163+
::strcasecmp(optarg, "FORMAT_GLPI") == 0) {
164+
config->SetVolatileKeyValue("format", optarg);
165+
}
166+
}
163167
else if (optName == "stdout" && !daemonize)
164168
config->SetVolatileKeyValue("stdout", "true");
165169
else if (optName == "use-current-time-in-device-ID")

0 commit comments

Comments
 (0)