File: IcingaApiCommandSendSsh.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 (148 lines) | stat: -rw-r--r-- 3,499 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
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
<?php

/**
 * 
 * @author Christian Doebler <christian doebler@netways.de>
 *
 */
class IcingaApiCommandSendSsh
	extends IcingaApiCommandSend
	implements IcingaApiCommandInterface {

	/*
	 * VARIABLES
	 */

	protected $config = array (
		'ssh_bin'		=> '/usr/bin/ssh',
		'ssh_user'		=> 'icinga',
		'ssh_host'		=> 'localhost',
		'ssh_port'		=> 22,
		'ssh_timeout'	=> 20,
		'ssh_pipe'		=> '/usr/local/icinga/var/rw/icinga.cmd',
	);
	protected $commands = false;

	private $callStack = array();

	/*
	 * CONSTANTS
	 */

	const SSH_CALL_TEMPLATE = '%s -p %d -oConnectTimeout=%d %s@%s \'echo "%s" > %s\'';
	const SSH_DIR = '/tmp';

	/*
	 * METHODS
	 */

	/**
	 * (non-PHPdoc)
	 * @see objects/command/IcingaApiCommandInterface#checkConfig()
	 */
	public function checkConfig (array $config) {
		$configOk = true;
		foreach ($config as $key => $value) {
			if (!array_key_exists($key, $config)) {
				$configOk = false;
				throw new IcingaApiCommandSendSshException('checkConfig(): Invalid key "' . $key . '"!');
			}
		}
		return $configOk;
	}

	/**
	 * (non-PHPdoc)
	 * @see objects/command/IcingaApiCommandInterface#setConfig()
	 */
	public function setConfig (array $config) {
		if ($this->checkConfig($config)) {
			foreach ($config as $key => $value) {
				$this->config[$key] = $config[$key];
			}
		}
		return $this;
	}

	/**
	 * (non-PHPdoc)
	 * @see objects/command/IcingaApiCommandInterface#send()
	 */
	public function send () {
		$success = false;
		if ($this->commands !== false) {
			foreach ($this->commands as $command) {
				$sshCall = $this->getSshCall($command);
				if (!$this->executeCall($sshCall)) {
					throw new IcingaApiCommandSendSshException('send(): command exeution failed!');
				}
			}
		} else {
			throw new IcingaApiCommandSendSshException('send(): Config or command(s) missing!');
		}
		return $success;
	}

	/**
	 * generates the ssh-command line
	 * @param	string		$command				icinga command to be sent
	 * @return	string								ssh-command line
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	private function getSshCall ($command) {
		$sshCall = sprintf(
			self::SSH_CALL_TEMPLATE,
			$this->config['ssh_bin'],
			$this->config['ssh_port'],
			$this->config['ssh_timeout'],
			$this->config['ssh_user'],
			$this->config['ssh_host'],
			str_replace('"', '\\"', $command),
			$this->config['ssh_pipe']
		);
		return $sshCall;
	}

	/**
	 * executes the current ssh-command line
	 * @param	string		$call					ssh-command line
	 * @return	boolean								true on success, false on error
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	private function executeCall ($call) {
		$executionOk = false;
		$env = Array ('PATH' => '');
		$pipes = false;
		$desc = Array (
			array ('pipe', 'r'), // STDIN
			array ('pipe', 'w'), // STDOUT
			array ('pipe', 'w'), // STDERR
		);
		$proc = proc_open($call, $desc, $pipes, self::SSH_DIR);
		if (is_resource($proc)) {
			$aux = stream_get_contents($pipes[0]);
			$aux = stream_get_contents($pipes[1]);
			$stderr = stream_get_contents($pipes[2]);
			$exit = (int)proc_close($proc);
			if ($exit == 0) {
				$executionOk = true;
			}
		}
		array_push($this->callStack, array($call => $executionOk));
		return $executionOk;
	}

	/**
	 * (non-PHPdoc)
	 * @see objects/command/IcingaApiCommandInterface#getCallStack()
	 */
	public function getCallStack () {
		return $this->callStack;
	}

}

// class exceptions
class IcingaApiCommandSendSshException extends IcingaApiCommandSendException {}

?>