The NetEye 4.9 release introduced a brand new module called Shutdown Manager. My aim in this blog is not to explain how the Shutdown Manager works, but instead to introduce the logic behind it in order to better explain when you should use it, and how you can deliver a configuration file during Icingaweb2 Director deployment.
The Shutdown Manager module is used for shutting down hosts following a pre-determined order and using a dedicated command. The host is linked to its “Shutdown Command” using a custom variable in the Director module. During Director deployment, a dedicated service with an individualized command is created for each host belonging to the Shutdown Manager (i.e., with a linked Shutdown Command).
To be able to shut down a host, we must ensure that the service and its related command will not change over time. In other words, that you should not be able to update its name or configuration, or accidentally delete them. In addition, we have to deploy them in the zone where the host is defined. In a distributed environment, it’s essential that Director be able to ship some elements of the configuration to specific zones and other elements to a global zone.
What follows is a step-by-step guide for delivering just such a configuration.
ShipConfigFilesHook
class. Remember to then add the hook to the run.php
file of your module.
<?php namespace Icinga\Module\Shutdownmanager\Director; use Icinga\Module\Director\Hook\ShipConfigFilesHook; class ShipShutdownServiceConfigFile extends ShipConfigFilesHook { }
command
line such as:
object CheckCommand "shutdown-command" { import "plugin-check-command" command = [ "" ] }Here’s a service that should run on the Icinga master node:
object Service "neteye-shutdown-manager-enabled" { check_command = "shutdown-command" enable_active_checks = false enable_passive_checks = true host_name = "' . $hostname . '" }And finally, a service that runs on agents. This means that the service should contain the host endpoint:
object Service "neteye-shutdown-manager-enabled" { check_command = "shutdown-command" enable_active_checks = false enable_passive_checks = true host_name = "' . $hostname . '" command_endpoint = host_name }Regardless of whether it’s a local service or a remote “Run on Agent” service, the
host_name
parameter will be dynamically set by the fetchFiles()
function below.
shutdownmanager
, add the related service writing the service definition to a dedicated file. We want to add a single file for each zone that contains all the service definitions. Director will then create a subdirectory for each Zone it wants to deploy a configuration to. Each one of these files should be saved in the correct zone. Every command is rendered to the global zone by default. For this reason, the command definition should be saved in the director-global
directory.
public function fetchFiles() { $files = []; // get all the Shutdownmanager hosts $hostsWithCommand = $this->getAllHostsWithShutdownCommand(); // create command file in director-global zone $commandZonePath = 'zones.d/director-global/shutdown-manager-command.conf'; $files[$commandZonePath] = $this->renderShutdownCheckCommandDefinition(); /** @var IcingaHost $host */ foreach ($hostsWithCommand as $host) { // set the correct zone path based on host zone $zonePath = sprintf('zones.d/%s/shutdown-manager-services.conf', $host->getRenderingZone()); // in the case of first service set $files to empty string $files = $this->initializeZonePath($zonePath, $files); $hostVars = (array) $host->getVars(); /** @var ShutdownCommandModel $command */ $command = $this->getCommandById($hostVars['shutdown_command']); if ($command->getRunOnAgent() === 1) { $files[$zonePath] .= $this->renderServiceForRunOnAgentHost($host->getObjectName()); } else { $files[$zonePath] .= $this->renderServiceForNotRunOnAgentHost($host->getObjectName()); } } return $files; }