|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Reporter; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use Tvi\MonitorBundle\Check\CheckAbstract; |
|
17
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
18
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
|
19
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @author Kevin Bond <[email protected]>, Vladimir Turnaev <[email protected]> |
|
|
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
|
|
24
|
|
|
class Console extends ReporterAbstract |
|
25
|
|
|
{ |
|
26
|
|
|
private const STATUS_TAG_SUCCESS = '<fg=green;options=bold>'; |
|
27
|
|
|
private const STATUS_TAG_WARNING = '<fg=yellow;options=bold>'; |
|
28
|
|
|
private const STATUS_TAG_SKIP = '<fg=blue;options=bold>'; |
|
29
|
|
|
private const STATUS_TAG_FAILURE = '<fg=red;options=bold>'; |
|
30
|
|
|
private const STATUS_TAG_UNKNOWN = '<fg=white;options=bold>'; |
|
31
|
|
|
|
|
32
|
|
|
private const IMPORTANCE_TAG_EMERGENCY = '<fg=red;options=bold>'; |
|
33
|
|
|
private const IMPORTANCE_TAG_WARNING = '<fg=yellow;options=bold>'; |
|
34
|
|
|
private const IMPORTANCE_TAG_NOTE = '<fg=green;options=bold>'; |
|
35
|
|
|
private const IMPORTANCE_TAG_INFO = '<fg=blue;options=bold>'; |
|
36
|
|
|
private const IMPORTANCE_TAG_DEFAULT = '<fg=default>'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
|
|
|
|
|
39
|
|
|
* @var OutputInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $output; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
|
|
|
|
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $withData; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
|
|
|
|
|
49
|
|
|
* @param OutputInterface $output |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
100 |
|
public function __construct(OutputInterface $output = null, $withData = false) |
|
52
|
|
|
{ |
|
53
|
100 |
|
if (null === $output) { |
|
54
|
100 |
|
$output = new ConsoleOutput(); |
|
55
|
|
|
} |
|
56
|
100 |
|
$this->output = $output; |
|
57
|
|
|
|
|
58
|
100 |
|
$this->withData = $withData; |
|
59
|
100 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
|
|
|
|
|
64
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
|
65
|
|
|
{ |
|
66
|
|
|
list($status, $code) = $this->getStatusByResul($result); |
|
67
|
|
|
|
|
68
|
|
|
$statusTag = static::tagByCode($code); |
|
69
|
|
|
|
|
70
|
|
|
$groupTag = $check->getGroup(); |
|
|
|
|
|
|
71
|
|
|
$tags = $check->getTags(); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
if ($tags) { |
|
74
|
|
|
$tags = implode(', ', $tags); |
|
75
|
|
|
$tags = "[$tags]"; |
|
76
|
|
|
$groupTag .= ' / '.$tags; |
|
77
|
|
|
} else { |
|
78
|
|
|
$tags = null; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$message = $result->getMessage(); |
|
82
|
|
|
if ($message) { |
|
83
|
|
|
$message = ", {$message}"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$labelTag = static::tagByImportance($check->getImportance()); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$this->output->writeln(sprintf('%s%-30s</> %-40s %s%-10s</> %s%s', |
|
|
|
|
|
|
89
|
|
|
$labelTag, $check->getId(), $groupTag, $statusTag, $status, $check->getLabel(), $message)); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
if ($this->withData) { |
|
92
|
|
|
$dataOut = $this->getDataOut($result); |
|
93
|
|
|
if ($dataOut) { |
|
94
|
|
|
$this->output->writeln(sprintf('%-71s < %s >', ' ', $dataOut)); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
|
|
|
|
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
|
|
|
|
|
102
|
|
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
|
103
|
|
|
{ |
|
104
|
|
|
parent::onStart($checks, $runnerConfig); |
|
105
|
|
|
|
|
106
|
|
|
$this->output->writeln(sprintf('<fg=white;options=bold>%-30s %-40s %-10s %s, %s</>', 'Check', 'Group / Tag(s)', 'Status', 'Info', 'Message')); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
|
|
|
|
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
|
|
|
|
|
112
|
|
|
public function onFinish(ResultsCollection $results) |
|
113
|
|
|
{ |
|
114
|
|
|
parent::onFinish($results); |
|
115
|
|
|
|
|
116
|
|
|
$this->output->writeln('---------------------------------'); |
|
117
|
|
|
$this->output->writeln(sprintf('%s: %s', 'total', $this->getTotalCount())); |
|
118
|
|
|
$this->output->writeln(''); |
|
119
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SUCCESS, 'SUCCESSES', $this->getSuccessCount())); |
|
120
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_WARNING, 'WARNINGS', $this->getWarningCount())); |
|
121
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SKIP, 'SKIP', $this->getSkipCount())); |
|
122
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_FAILURE, 'FAILURES', $this->getFailureCount())); |
|
123
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_UNKNOWN, 'UNKNOWNS', $this->getUnknownCount())); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public static function tagByCode($code): string |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$tags = [ |
|
129
|
|
|
static::STATUS_CODE_SUCCESS => static::STATUS_TAG_SUCCESS, |
|
130
|
|
|
static::STATUS_CODE_WARNING => static::STATUS_TAG_WARNING, |
|
131
|
|
|
static::STATUS_CODE_SKIP => static::STATUS_TAG_SKIP, |
|
132
|
|
|
static::STATUS_CODE_FAILURE => static::STATUS_TAG_FAILURE, |
|
133
|
|
|
'default' => static::STATUS_TAG_UNKNOWN, |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
return $tags[$code] ?? $tags['default']; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function tagByImportance($importance): string |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$tags = [ |
|
142
|
|
|
CheckAbstract::IMPORTANCE_EMERGENCY => static::IMPORTANCE_TAG_EMERGENCY, |
|
143
|
|
|
CheckAbstract::IMPORTANCE_WARNING => static::IMPORTANCE_TAG_WARNING, |
|
144
|
|
|
CheckAbstract::IMPORTANCE_NOTE => static::IMPORTANCE_TAG_NOTE, |
|
145
|
|
|
CheckAbstract::IMPORTANCE_INFO => static::IMPORTANCE_TAG_INFO, |
|
146
|
|
|
'default' => static::IMPORTANCE_TAG_DEFAULT, |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
return $tags[$importance] ?? $tags['default']; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
|
|
|
|
|
154
|
|
|
* @return null|string |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getDataOut(ResultInterface $result) |
|
157
|
|
|
{ |
|
158
|
|
|
$dataOut = null; |
|
159
|
|
|
$message = $result->getMessage(); |
|
160
|
|
|
if ($message) { |
|
161
|
|
|
$data = $result->getData(); |
|
162
|
|
|
if (null !== $data) { |
|
163
|
|
|
$dataOut = json_encode($data); |
|
164
|
|
|
|
|
165
|
|
|
if (\mb_strlen($dataOut) > 100) { |
|
166
|
|
|
$dataOut .= "\n"; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $dataOut; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|