summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/TransportStep.php
blob: d138eb40787babd4b62bd049474d43aa857fa12f (plain)
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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring;

use Exception;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Setup\Step;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;

class TransportStep extends Step
{
    protected $data;

    protected $error;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function apply()
    {
        $transportConfig = $this->data['transportConfig'];
        $transportName = $transportConfig['name'];
        unset($transportConfig['name']);

        try {
            Config::fromArray(array($transportName => $transportConfig))
                ->setConfigFile(Config::resolvePath('modules/monitoring/commandtransports.ini'))
                ->saveIni();
        } catch (Exception $e) {
            $this->error = $e;
            return false;
        }

        $this->error = false;
        return true;
    }

    public function getSummary()
    {
        switch ($this->data['transportConfig']['transport']) {
            case 'local':
                $details = '<p>' . sprintf(
                    mt(
                        'monitoring',
                        'Icinga Web 2 will use the named pipe located at "%s"'
                            . ' to send commands to your monitoring instance.'
                    ),
                    $this->data['transportConfig']['path']
                ) . '</p>';
                break;
            case 'remote':
                $details = '<p>'
                    . sprintf(
                        mt(
                            'monitoring',
                            'Icinga Web 2 will use the named pipe located on a remote machine at "%s" to send commands'
                                . ' to your monitoring instance by using the connection details listed below:'
                        ),
                        $this->data['transportConfig']['path']
                    )
                    . '</p>'
                    . '<table>'
                    . '<tbody>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Remote Host') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['host'] . '</td>'
                    . '</tr>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Remote SSH Port') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['port'] . '</td>'
                    . '</tr>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Remote SSH User') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['user'] . '</td>'
                    . '</tr>'
                    . '</tbody>'
                    . '</table>';
                break;
            case 'api':
                $details = '<p>'
                    . mt(
                        'monitoring',
                        'Icinga Web 2 will use the Icinga 2 API to send commands'
                            . ' to your monitoring instance by using the connection details listed below:'
                    )
                    . '</p>'
                    . '<table>'
                    . '<tbody>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Host') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['host'] . '</td>'
                    . '</tr>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Port') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['port'] . '</td>'
                    . '</tr>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Username') . '</strong></td>'
                    . '<td>' . $this->data['transportConfig']['username'] . '</td>'
                    . '</tr>'
                    . '<tr>'
                    . '<td><strong>' . mt('monitoring', 'Password') . '</strong></td>'
                    . '<td>' . str_repeat('*', strlen($this->data['transportConfig']['password'])) . '</td>'
                    . '</tr>'
                    . '</tbody>'
                    . '</table>';
                break;
            default:
                throw new ProgrammingError(
                    'Unknown command transport type: %s',
                    $this->data['transportConfig']['transport']
                );
        }

        return '<h2>' . mt('monitoring', 'Command Transport', 'setup.page.title') . '</h2>'
            . '<div class="topic">' . $details . '</div>';
    }

    public function getReport()
    {
        if ($this->error === false) {
            return array(sprintf(
                mt('monitoring', 'Command transport configuration has been successfully created: %s'),
                Config::resolvePath('modules/monitoring/commandtransports.ini')
            ));
        } elseif ($this->error !== null) {
            return array(
                sprintf(
                    mt(
                        'monitoring',
                        'Command transport configuration could not be written to: %s. An error occured:'
                    ),
                    Config::resolvePath('modules/monitoring/commandtransports.ini')
                ),
                sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->error))
            );
        }
    }
}