File: IcingaApiCommandDispatcher.php

package info (click to toggle)
icinga 1.0.2-2%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 33,964 kB
  • ctags: 13,294
  • sloc: xml: 154,821; ansic: 99,198; sh: 14,761; sql: 5,852; php: 5,126; perl: 2,838; makefile: 1,268
file content (116 lines) | stat: -rw-r--r-- 3,099 bytes parent folder | download | duplicates (2)
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
<?php

/**
 * 
 * @author Christian Doebler <christian doebler@netways.de>
 *
 */
class IcingaApiCommandDispatcher
	extends IcingaApiCommand {

	/*
	 * VARIABLES
	 */

	private $interface = false;
	private $settings = false;
	private $commands = false;

	private $sendObject = false;

	/*
	 * METHODS
	 */

	/**
	 * clears the command pool
	 * @param	void
	 * @return	void
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function clearCommands () {
		$this->commands = false;
	}

	/**
	 * sets the interface to be used for sending commands
	 * @param	string			$interface				command interface
	 * @param	array			$config					interface config
	 * @return	IcingaApiCommandDispatcher				instance of IcingaApiCommandDispatcher object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function setInterface($interface, array $config = array()) {
		$this->interface = $interface;
		$this->settings = $config;
		return $this;
	}

	/**
	 * sets the command(s) which should be sent
	 * @param	array			$command				icinga command(s)
	 * @return	IcingaApiCommandDispatcher				instance of IcingaApiCommandDispatcher object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function setCommand ($command) {
		if (!is_array($this->commands)) {
			$this->commands = array();
		}
		if (!is_array($command)) {
			$command = array($command);
		}
		foreach ($command as $currentCommand) {
			$commandLine = $currentCommand->getCommandLine();
			if ($commandLine !== false) {
				array_push($this->commands, $commandLine);
			} else {
				$this->commands = false;
				throw new IcingaApiCommandDispatcherException('setCommand(): Incomplete command definition!');
				break;
			}
		}
		return $this;
	}

	/**
	 * sends command(s) by using the defined interface
	 * @param	void
	 * @return	boolean									true on success otherwise false
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function send () {
		$sendOk = false;
		if ($this->interface !== false && $this->settings !== false && $this->commands !== false) {
			$class = 'IcingaApiCommandSend' . $this->interface;
			$this->sendObject = new $class;
			$this->sendObject->setConfig($this->settings);
			$this->sendObject->setCommand($this->commands);
			$this->sendObject->send();
		} else {
			
			throw new IcingaApiCommandDispatcherException('send(): interface, settings or command(s) invalid!');
		}
		return $sendOk;
	}

	/**
	 * returns the call stack for further processing
	 * @param	void
	 * @return	array									call stack of sent commands; if send() has not been called yet, return value will be boolean false
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function getCallStack () {
		$returnData = false;
		if ($this->sendObject !== false) {
			$returnData = $this->sendObject->getCallStack();
		} else {
			throw new IcingaApiCommandDispatcherException('getCallStack(): Call Object not set yet!');
		}
		return $returnData;
	}

}

// class exceptions
class IcingaApiCommandDispatcherException extends IcingaApiCommand {}

?>