1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?php
/**
*
* @author Christian Doebler <christian doebler@netways.de>
*
*/
class IcingaApiCommand
extends IcingaApi {
/*
* VARIABLES
*/
private $config = array (
'command_id' => false,
'command' => false,
'fields' => false,
'target' => false,
);
private $commands = false;
private $commandLine = false;
/*
* METHODS
*/
/**
* class constructor
* @param void
* @return IcingaApiCommand instance of IcingaApiCommand object
* @author Christian Doebler <christian.doebler@netways.de>
*/
public function __construct () {
$this->loadCommands();
}
/**
* loads command definitions
* @return void
* @author Christian Doebler <christian.doebler@netways.de>
*/
private function loadCommands () {
if ($this->commands === false) {
$this->commands = IcingaApiCommandCollection::getInstance();
} else {
throw new IcingaApiCommandException('Could not load command definitions!');
}
}
/**
* sets an icinga command
* @param mixed $command command id (integer) or command name (string)
* @return IcingaApiCommand the current object
* @author Christian Doebler <christian.doebler@netways.de>
*/
public function setCommand ($command = false) {
if ($command !== false) {
$commandId = false;
$commandName = false;
if (is_int($command)) {
$commandName = $this->commands->getCommandNameById($command);
if ($commandName !== false) {
$commandId = $command;
}
} else {
$commandId = $this->commands->getCommandIdByName($command);
if ($commandId !== false) {
$commandName = $command;
}
}
if ($commandId !== false && $commandName !== false) {
$commandFields = $this->commands->getCommandFields($commandName);
$this->config['command_id'] = $commandId;
$this->config['command'] = $commandName;
$this->config['fields'] = $commandFields;
} else {
$this->config['command_id'] = false;
$this->config['command'] = false;
$this->config['fields'] = false;
}
} else {
throw new IcingaApiCommandException('setCommand(): Invalid command!');
}
$this->createCommandLine();
return $this;
}
/**
* checks target (command parameter)
* @param string $target target
* @return boolean true if target is valid otherwise false
* @author Christian Doebler <christian.doebler@netways.de>
*/
private function checkTarget ($target = false) {
$targetOk = false;
if (
$target == self::COMMAND_INSTANCE ||
$target == self::COMMAND_HOSTGROUP ||
$target == self::COMMAND_SERVICEGROUP ||
$target == self::COMMAND_HOST ||
$target == self::COMMAND_SERVICE ||
$target == self::COMMAND_ID ||
$target == self::COMMAND_AUTHOR ||
$target == self::COMMAND_COMMENT ||
$target == self::COMMAND_STARTTIME ||
$target == self::COMMAND_ENDTIME ||
$target == self::COMMAND_STICKY ||
$target == self::COMMAND_PERSISTENT ||
$target == self::COMMAND_NOTIFY ||
$target == self::COMMAND_RETURN_CODE ||
$target == self::COMMAND_CHECKTIME ||
$target == self::COMMAND_FIXED ||
$target == self::COMMAND_OUTPUT ||
$target == self::COMMAND_PERFDATA ||
$target == self::COMMAND_DURATION ||
$target == self::COMMAND_DATA
) {
$targetOk = true;
}
return $targetOk;
}
/**
* sets command target (command parameter)
* @param string $key command key
* @param mixed $value command value
* @return IcingaApiCommand instance of IcingaApiCommand object
* @author Christian Doebler <christian.doebler@netways.de>
*/
public function setTarget ($key = false, $value = false) {
if (!empty($key) && $value !== false) {
if ($this->config['target'] === false) {
$this->target = array();
}
$this->config['target'][$key] = $value;
} else {
throw new IcingaApiCommandException('setTarget(): Invalid key or value!');
}
$this->createCommandLine();
return $this;
}
/**
* checks fields (command parameter values)
* @param void
* @return boolean true if fields are ok otherwise false
* @author Christian Doebler <christian.doebler@netways.de>
*/
public function checkFields () {
$fieldsOk = true;
if (is_array($this->config['fields']) && is_array($this->config['target'])) {
foreach ($this->config['fields'] as $currentField) {
if (!array_key_exists($currentField, $this->config['target'])) {
$fieldsOk = false;
break;
}
}
}
return $fieldsOk;
}
/**
* generated the command line which will be sent to icinga
* @param void
* @return void
* @author Christian Doebler <christian.doebler@netways.de>
*/
private function createCommandLine () {
if (
$this->config['command_id'] !== false &&
$this->config['command'] !== false &&
$this->checkFields()
) {
$template = '[%s] %s' . str_repeat(';%s', count($this->config['fields']));
$variables = array(time(), $this->config['command']);
foreach ($this->config['fields'] as $currentField) {
array_push($variables, $this->config['target'][$currentField]);
}
$this->commandLine = vsprintf($template, $variables);
}
}
/**
* returns the command line which will be sent to icinga
* @param void
* @return string command line
* @author Christian Doebler <christian.doebler@netways.de>
*/
public function getCommandLine () {
return $this->commandLine;
}
}
// class execeptions
class IcingaApiCommandException extends IcingaApiException {};
?>
|