Skip to content

Commit

Permalink
Optimise - store server_os and server_platform in the database and up…
Browse files Browse the repository at this point in the history
…date only when logging on, not for every request.
  • Loading branch information
mark-unwin committed Sep 17, 2024
1 parent 6df8661 commit 07f1439
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
31 changes: 0 additions & 31 deletions app/Config/OpenAudit.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,6 @@ public function __construct()
unset($tz);
}

// get the server OS
$this->server_os = php_uname('s');

if ($this->server_os === 'Windows NT') {
$command = 'wmic os get name';
exec($command, $output);
if (!empty($output[1])) {
$os = explode('|', $output[1]);
$this->server_platform = $os[0];
}
} else if ($this->server_os === 'Darwin') {
$this->server_platform = 'MacOS';
$command = "sw_vers | grep \"ProductVersion:\" | cut -d: -f2 | xargs";
exec($command, $output);
if (!empty($output[0])) {
$this->server_platform .= ' ' . $output[0];
unset($output);
}
$command = "awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'macOS ' '{print \$NF}' | awk '{print substr(\$0, 0, length(\$0)-1)}'";
exec($command, $output);
if (!empty($output[0])) {
$this->server_platform .= ' ' . $output[0];
}
} else {
$command = 'cat /etc/os-release 2>/dev/null | grep -i ^PRETTY_NAME | cut -d= -f2 | cut -d\" -f2';
exec($command, $output);
if (!empty($output[0])) {
$this->server_platform = $output[0];
}
}

if ($this->internal_version < 20230615) {
# TODO - remove this and just set both to 0
$query = $db->query('SELECT count(*) as device_count FROM `system`');
Expand Down
38 changes: 38 additions & 0 deletions app/Controllers/Logon.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ public function createForm()
$db->query($sql, [$subnet]);
log_message('info', 'Default discovery subnet auto-populated with ' . $subnet . '.');
}

// get the server OS
$server_os = php_uname('s');

if ($server_os === 'Windows NT') {
$command = 'wmic os get name';
exec($command, $output);
if (!empty($output[1])) {
$os = explode('|', $output[1]);
$server_platform = $os[0];
}
} else if ($server_os === 'Darwin') {
$server_platform = 'MacOS';
$command = "sw_vers | grep \"ProductVersion:\" | cut -d: -f2 | xargs";
exec($command, $output);
if (!empty($output[0])) {
$server_platform .= ' ' . $output[0];
unset($output);
}
$command = "awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'macOS ' '{print \$NF}' | awk '{print substr(\$0, 0, length(\$0)-1)}'";
exec($command, $output);
if (!empty($output[0])) {
$server_platform .= ' ' . $output[0];
}
} else {
$command = 'cat /etc/os-release 2>/dev/null | grep -i ^PRETTY_NAME | cut -d= -f2 | cut -d\" -f2';
exec($command, $output);
if (!empty($output[0])) {
$server_platform = $output[0];
}
}
$sql = 'UPDATE configuration SET value = ? WHERE name = "server_os"';
$db->query($sql, [$server_os]);
log_message('info', 'Config auto-populated with ServerOS ' . $server_os . '.');
$sql = 'UPDATE configuration SET value = ? WHERE name = "server_platform"';
$db->query($sql, [$server_platform]);
log_message('info', 'Config auto-populated with ServerPlatform ' . $server_platform . '.');

$methods = array();
if ($db->tableExists('auth')) {
$authModel = model('AuthModel');
Expand Down
20 changes: 20 additions & 0 deletions app/Models/db_upgrades/db_5.5.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

$sql = "DELETE FROM configuration WHERE name = 'server_os'";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

$sql = "INSERT INTO `configuration` VALUES (NULL,'server_os','','text','n','system','2000-01-01 00:00:00','The OS Open-AudIT is running on (this server).')";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

$sql = "DELETE FROM configuration WHERE name = 'server_platform'";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

$sql = "INSERT INTO `configuration` VALUES (NULL,'server_platform','','text','n','system','2000-01-01 00:00:00','The OS Platform Open-AudIT is running on (this server).')";
$db->query($sql);
$output .= str_replace("\n", " ", (string)$db->getLastQuery()) . "\n\n";
log_message('info', (string)$db->getLastQuery());

// set our versions
$sql = "UPDATE `configuration` SET `value` = '20241012' WHERE `name` = 'internal_version'";
$db->query($sql);
Expand Down
2 changes: 2 additions & 0 deletions other/open-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,8 @@ INSERT INTO `configuration` VALUES (NULL,'feature_executables','n','bool','y','s
INSERT INTO `configuration` VALUES (NULL,'product','community','text','n','system','2000-01-01 00:00:00','Product type.');
INSERT INTO `configuration` VALUES (NULL,'license_footer','','text','n','system','2000-01-01 00:00:00','Footer text.');
INSERT INTO `configuration` VALUES (NULL,'license_limit','','number','n','system','2000-01-01 00:00:00','Licensed devices.');
INSERT INTO `configuration` VALUES (NULL,'server_os','','text','n','system','2000-01-01 00:00:00','The OS Open-AudIT is running on (this server).');
INSERT INTO `configuration` VALUES (NULL,'server_platform','','text','n','system','2000-01-01 00:00:00','The OS Platform Open-AudIT is running on (this server).');
/*!40000 ALTER TABLE `configuration` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down

0 comments on commit 07f1439

Please sign in to comment.