Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Handle multiple SCCM configuration
- Add `collection` scope
- PHPUnit test suite for XML generation and query building logic

### Fixed

- Reduce MSSQL connections from N*8 per device to 2 per config by sharing a single connection across all per-device data fetching

## [2.5.1] - 2025-10-07

Expand Down
110 changes: 23 additions & 87 deletions inc/sccm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,8 @@ public function getDevices(int $config_id, $where = 0, $limit = 99999999): void
$sccm_db->disconnect();
}

public function getDatas(int $config_id, $type, $deviceid, $limit = 99999999): array
public function getDatas(PluginSccmSccmdb $sccm_db, $type, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

if ($type == 'processors') {
$fields = ['Manufacturer00', 'Name00', 'NormSpeed00', 'AddressWidth00', 'CPUKey00', 'NumberOfCores00', 'NumberOfLogicalProcessors00'];
$table = 'Processor_DATA';
Expand All @@ -130,20 +122,11 @@ public function getDatas(int $config_id, $type, $deviceid, $limit = 99999999): a
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getNetwork(int $config_id, $deviceid, $limit = 99999999): array
public function getNetwork(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "SELECT NeDa.IPAddress00 as \"ND-IpAddress\",
NeDa.MACAddress00 as \"ND-MacAddress\",
NeDa.IPSubnet00 as \"ND-IpSubnet\",
Expand All @@ -166,20 +149,11 @@ public function getNetwork(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getSoftware(int $config_id, $deviceid, $limit = 99999999): array
public function getSoftware(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "SELECT ArPd_64.DisplayName0 as \"ArPd-DisplayName\",
ArPd_64.InstallDate0 as \"ArPd-InstallDate\",
ArPd_64.Version0 as \"ArPd-Version\",
Expand Down Expand Up @@ -207,20 +181,11 @@ public function getSoftware(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getMemories(int $config_id, $deviceid, $limit = 99999999): array
public function getMemories(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "SELECT
Capacity0 as \"Mem-Capacity\",
Caption0 as \"Mem-Caption\",
Expand All @@ -246,20 +211,11 @@ public function getMemories(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getVideos(int $config_id, $deviceid, $limit = 99999999): array
public function getVideos(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "
SELECT
VideoProcessor0 as \"Vid-Chipset\",
Expand All @@ -281,20 +237,11 @@ public function getVideos(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getSounds(int $config_id, $deviceid, $limit = 99999999): array
public function getSounds(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "
SELECT distinct
Description0 as \"Snd-Description\",
Expand All @@ -312,20 +259,11 @@ public function getSounds(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getStorages(int $config_id, $deviceid, $limit = 99999999): array
public function getStorages(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "
SELECT
md.SystemName00,
Expand All @@ -352,20 +290,11 @@ public function getStorages(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

public function getMedias(int $config_id, $deviceid, $limit = 99999999): array
public function getMedias(PluginSccmSccmdb $sccm_db, $deviceid, $limit = 99999999): array
{
$sccm_db = new PluginSccmSccmdb();
$res = $sccm_db->connect($config_id);
if (!$res) {
throw new BadRequestHttpException(
__s('Cannot connect to SCCM database', 'sccm'),
);
}

$query = "
SELECT distinct
Description0 as \"Med-Description\",
Expand All @@ -386,7 +315,6 @@ public function getMedias(int $config_id, $deviceid, $limit = 99999999): array
$i++;
}

$sccm_db->disconnect();
return $data;
}

Expand Down Expand Up @@ -475,6 +403,13 @@ public static function executeCollect($task): int
true,
);

$sccm_db = new PluginSccmSccmdb();
if (!$sccm_db->connect($config_id)) {
throw new RuntimeException(
sprintf('[Config %s] Cannot connect to SCCM database', $config_id),
);
}

foreach ($PluginSccmSccm->devices as $device_values) {
$PluginSccmSccmxml = new PluginSccmSccmxml($device_values);

Expand All @@ -483,14 +418,14 @@ public static function executeCollect($task): int
$PluginSccmSccmxml->setHardware();
$PluginSccmSccmxml->setOS();
$PluginSccmSccmxml->setBios();
$PluginSccmSccmxml->setProcessors($config_id);
$PluginSccmSccmxml->setSoftwares($config_id);
$PluginSccmSccmxml->setMemories($config_id);
$PluginSccmSccmxml->setVideos($config_id);
$PluginSccmSccmxml->setSounds($config_id);
$PluginSccmSccmxml->setProcessors($sccm_db);
$PluginSccmSccmxml->setSoftwares($sccm_db);
$PluginSccmSccmxml->setMemories($sccm_db);
$PluginSccmSccmxml->setVideos($sccm_db);
$PluginSccmSccmxml->setSounds($sccm_db);
$PluginSccmSccmxml->setUsers();
$PluginSccmSccmxml->setNetworks($config_id);
$PluginSccmSccmxml->setStorages($config_id);
$PluginSccmSccmxml->setNetworks($sccm_db);
$PluginSccmSccmxml->setStorages($sccm_db);

$SXML = $PluginSccmSccmxml->sxml;
$SXML->asXML($REP_XML . $PluginSccmSccmxml->device_id . ".ocs");
Expand All @@ -499,6 +434,7 @@ public static function executeCollect($task): int
$task->addVolume(1);
}

$sccm_db->disconnect();
Toolbox::logInFile('sccm', "[Config {$config_id}] Collect completed\n", true);
$retcode = 1;
} catch (Throwable $e) {
Expand Down
53 changes: 26 additions & 27 deletions inc/sccmxml.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

use function Safe\preg_match;
use function Safe\preg_match_all;
use function Safe\preg_replace;

class PluginSccmSccmxml
{
Expand Down Expand Up @@ -161,14 +160,14 @@ public function setBios(): void
$BIOS->addChild('SKUNUMBER', $this->data['PBD-Version']);
}

public function setProcessors(int $config_id): void
public function setProcessors(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$cpukeys = [];

$CONTENT = $this->sxml->CONTENT[0];
$i = 0;
foreach ($sccm->getDatas($config_id, 'processors', $this->device_id) as $value) {
foreach ($sccm->getDatas($sccm_db, 'processors', $this->device_id) as $value) {
if (!in_array($value['CPUKey00'], $cpukeys)) {
$CONTENT->addChild('CPUS');
$CPUS = $this->sxml->CONTENT[0]->CPUS[$i];
Expand All @@ -186,24 +185,24 @@ public function setProcessors(int $config_id): void
}
}

public function setSoftwares(int $config_id): void
public function setSoftwares(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$antivirus = [];
$inject_antivirus = false;
$CONTENT = $this->sxml->CONTENT[0];
$i = 0;

foreach ($sccm->getSoftware($config_id, $this->device_id) as $value) {
foreach ($sccm->getSoftware($sccm_db, $this->device_id) as $value) {
$CONTENT->addChild('SOFTWARES');
$SOFTWARES = $this->sxml->CONTENT[0]->SOFTWARES[$i];

if (isset($value['ArPd-DisplayName']) && preg_match("#&#", $value['ArPd-DisplayName'])) {
$value['ArPd-DisplayName'] = preg_replace("#&#", "&", $value['ArPd-DisplayName']);
if (isset($value['ArPd-DisplayName'])) {
$value['ArPd-DisplayName'] = str_replace('&', '&', $value['ArPd-DisplayName']);
}

if (isset($value['ArPd-Publisher']) && preg_match("#&#", $value['ArPd-Publisher'])) {
$value['ArPd-Publisher'] = preg_replace("#&#", "&", $value['ArPd-Publisher']);
if (isset($value['ArPd-Publisher'])) {
$value['ArPd-Publisher'] = str_replace('&', '&', $value['ArPd-Publisher']);
}

$SOFTWARES->addChild('NAME', $value['ArPd-DisplayName'] ?: NOT_AVAILABLE);
Expand Down Expand Up @@ -236,13 +235,13 @@ public function setSoftwares(int $config_id): void
}
}

public function setMemories(int $config_id): void
public function setMemories(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$CONTENT = $this->sxml->CONTENT[0];
$i = 0;

foreach ($sccm->getMemories($config_id, $this->device_id) as $value) {
foreach ($sccm->getMemories($sccm_db, $this->device_id) as $value) {
$CONTENT->addChild('MEMORIES');
$MEMORIES = $this->sxml->CONTENT[0]->MEMORIES[$i];

Expand All @@ -262,13 +261,13 @@ public function setMemories(int $config_id): void
}
}

public function setVideos(int $config_id): void
public function setVideos(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$CONTENT = $this->sxml->CONTENT[0];
$i = 0;

foreach ($sccm->getVideos($config_id, $this->device_id) as $value) {
foreach ($sccm->getVideos($sccm_db, $this->device_id) as $value) {
$CONTENT->addChild('VIDEOS');
$VIDEOS = $this->sxml->CONTENT[0]->VIDEOS[$i];

Expand All @@ -282,13 +281,13 @@ public function setVideos(int $config_id): void
}
}

public function setSounds(int $config_id): void
public function setSounds(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$CONTENT = $this->sxml->CONTENT[0];
$i = 0;

foreach ($sccm->getSounds($config_id, $this->device_id) as $value) {
foreach ($sccm->getSounds($sccm_db, $this->device_id) as $value) {
$CONTENT->addChild('SOUNDS');
$SOUNDS = $this->sxml->CONTENT[0]->SOUNDS[$i];

Expand Down Expand Up @@ -345,12 +344,12 @@ public function determineNetworkType(string $network_description): string
return "ethernet";
}

public function setNetworks(int $config_id): void
public function setNetworks(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$CONTENT = $this->sxml->CONTENT[0];

$networks = $sccm->getNetwork($config_id, $this->device_id);
$networks = $sccm->getNetwork($sccm_db, $this->device_id);

if ($networks !== []) {
$i = 0;
Expand Down Expand Up @@ -380,13 +379,13 @@ public function setNetworks(int $config_id): void
}
}

public function setStorages(int $config_id): void
public function setStorages(PluginSccmSccmdb $sccm_db, ?PluginSccmSccm $sccm = null): void
{
$sccm = new PluginSccmSccm();
$sccm ??= new PluginSccmSccm();
$CONTENT = $this->sxml->CONTENT[0];
$i = 0;

foreach ($sccm->getStorages($config_id, $this->device_id) as $value) {
foreach ($sccm->getStorages($sccm_db, $this->device_id) as $value) {
$value['gld-TotalSize'] = intval($value['gld-TotalSize']) * 1024;
$value['gld-FreeSpace'] = intval($value['gld-FreeSpace']) * 1024;

Expand All @@ -403,7 +402,7 @@ public function setStorages(int $config_id): void
}

$i = 0;
foreach ($sccm->getMedias($config_id, $this->device_id) as $value) {
foreach ($sccm->getMedias($sccm_db, $this->device_id) as $value) {
$CONTENT->addChild('STORAGES');
$STORAGES = $this->sxml->CONTENT[0]->STORAGES[$i];
$STORAGES->addChild('DESCRIPTION', $value['Med-Description']);
Expand Down
Loading