-
Notifications
You must be signed in to change notification settings - Fork 580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/execute shutdown command #7709
Closed
lestat80
wants to merge
3
commits into
Icinga:master
from
WuerthPhoenix:feature/execute-shutdown-command
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "icinga/commandutils.hpp" | ||
#include "icinga/host.hpp" | ||
#include "icinga/checkcommand.hpp" | ||
#include "remote/apilistener.hpp" | ||
#include "remote/httputility.hpp" | ||
|
||
using namespace icinga; | ||
|
||
|
||
void CommandUtils::ExecuteCommandLocally(const Checkable::Ptr& checkable, const CheckCommand::Ptr& command, const Dictionary::Ptr& params) | ||
{ | ||
|
||
Dictionary::Ptr macros; | ||
if (!params->Contains("macros")) | ||
macros = new Dictionary(); | ||
else | ||
macros = params->Get("macros"); | ||
|
||
Array::Ptr command_line_tmp = command->GetCommandLine(); | ||
Value shutdown_command_value = params->Get("shutdown_command"); | ||
Array::Ptr full_command; | ||
if (shutdown_command_value.IsObjectType<Array>()){ | ||
full_command = shutdown_command_value; | ||
} else{ | ||
Log(LogNotice, "CommandUtils") | ||
<< "Shutdown command must be of type Array."; | ||
return; | ||
} | ||
|
||
if(full_command->GetLength() == 0){ | ||
Log(LogNotice, "CommandUtils") | ||
<< "Shutdown command must not be empty."; | ||
return; | ||
} | ||
CheckResult::Ptr cr = new CheckResult(); | ||
command->SetCommandLine(full_command); | ||
command->Execute(checkable, cr, macros, true); | ||
command->SetCommandLine(command_line_tmp); | ||
} | ||
|
||
|
||
void CommandUtils::SendCommandMessageToEndpoints(const Endpoint::Ptr& endpoint, const ApiListener::Ptr& listener, const Dictionary::Ptr& message) | ||
{ | ||
|
||
Zone::Ptr local_zone = Zone::GetLocalZone(); | ||
Endpoint::Ptr local_endpoint = Endpoint::GetLocalEndpoint(); | ||
|
||
std::set<Endpoint::Ptr> target_endpoints; | ||
for (const Zone::Ptr& zone : ConfigType::GetObjectsByType<Zone>()) { | ||
if (zone->GetParent() == local_zone) { | ||
std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints(); | ||
target_endpoints.insert(endpoints.begin(), endpoints.end()); | ||
} | ||
} | ||
|
||
for (const Endpoint::Ptr& e : target_endpoints) { | ||
if (e == local_endpoint || ! e->GetConnected()) | ||
continue; | ||
if (e == endpoint) { | ||
Log(LogNotice, "CommandUtils") << "Sending message to target endpoint '" << e->GetName() << "'."; | ||
listener->SyncSendMessage(e, message); | ||
return; | ||
} | ||
} | ||
|
||
for (const Endpoint::Ptr& e : target_endpoints) { | ||
if (e == local_endpoint || ! e->GetConnected()) | ||
continue; | ||
Log(LogNotice, "CommandUtils") << "Broadcasting message to connected endpoint '" << e->GetName() << "'."; | ||
listener->SyncSendMessage(e, message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef COMMANDUTILS_H | ||
#define COMMANDUTILS_H | ||
|
||
#include "remote/apilistener.hpp" | ||
#include "icinga/checkcommand.hpp" | ||
#include "icinga/host.hpp" | ||
|
||
namespace icinga | ||
{ | ||
|
||
class CommandUtils | ||
{ | ||
public: | ||
static void ExecuteCommandLocally(const Checkable::Ptr& checkable, const CheckCommand::Ptr& command, const Dictionary::Ptr& params); | ||
static void SendCommandMessageToEndpoints(const Endpoint::Ptr& endpoint, const ApiListener::Ptr& listener, const Dictionary::Ptr& message); | ||
|
||
private: | ||
CommandUtils(); | ||
}; | ||
|
||
} | ||
|
||
#endif /* COMMANDUTILS_H */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value is retrieved in various places, but it never gets set. Is that a new command object attribute or is the PR incomplete?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint is called by passing a parameter called shutdown_command, which represents a valid shutdown command for the target host. An example follows:
curl -s -k -u root:admin -H 'X-HTTP-Method-Override: POST' -H 'Accept: application/json' https://localhost:5665/v1/actions/shutdown-host -d '{ "type": "Host", "filter": "host.name==\"<hostname>\"", "shutdown_command": ["systemctl", "poweroff"]}'