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
|
|
|
const STATUS_TAG_SUCCESS = '<fg=green;options=bold>'; |
27
|
|
|
const STATUS_TAG_WARNING = '<fg=yellow;options=bold>'; |
28
|
|
|
const STATUS_TAG_SKIP = '<fg=blue;options=bold>'; |
29
|
|
|
const STATUS_TAG_FAILURE = '<fg=red;options=bold>'; |
30
|
|
|
const STATUS_TAG_UNKNOWN = '<fg=white;options=bold>'; |
31
|
|
|
|
32
|
|
|
const IMPORTANCE_TAG_EMERGENCY = '<fg=red;options=bold>'; |
33
|
|
|
const IMPORTANCE_TAG_WARNING = '<fg=yellow;options=bold>'; |
34
|
|
|
const IMPORTANCE_TAG_NOTE = '<fg=green;options=bold>'; |
35
|
|
|
const IMPORTANCE_TAG_INFO = '<fg=blue;options=bold>'; |
36
|
|
|
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
|
99 |
|
public function __construct(OutputInterface $output = null, $withData = false) |
52
|
|
|
{ |
53
|
99 |
|
if (null === $output) { |
54
|
99 |
|
$output = new ConsoleOutput(); |
55
|
|
|
} |
56
|
99 |
|
$this->output = $output; |
57
|
|
|
|
58
|
99 |
|
$this->withData = $withData; |
59
|
99 |
|
} |
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%-60s</> %-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
|
|
|
/** |
|
|
|
|
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
|
|
|
|
103
|
|
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
104
|
|
|
{ |
105
|
|
|
parent::onStart($checks, $runnerConfig); |
106
|
|
|
|
107
|
|
|
$this->output->writeln(sprintf('<fg=white;options=bold>%-60s %-40s %-10s %s, %s</>', 'Check', 'Group / Tag(s)', 'Status', 'Info', 'Message')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
public function onFinish(ResultsCollection $results) |
114
|
|
|
{ |
115
|
|
|
parent::onFinish($results); |
116
|
|
|
|
117
|
|
|
$this->output->writeln('---------------------------------'); |
118
|
|
|
$this->output->writeln(sprintf('%s: %s', 'total', $this->getTotalCount())); |
119
|
|
|
$this->output->writeln(''); |
120
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SUCCESS, 'SUCCESSES', $this->getSuccessCount())); |
121
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_WARNING, 'WARNINGS', $this->getWarningCount())); |
122
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SKIP, 'SKIP', $this->getSkipCount())); |
123
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_FAILURE, 'FAILURES', $this->getFailureCount())); |
124
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_UNKNOWN, 'UNKNOWNS', $this->getUnknownCount())); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function tagByCode($code): string |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$tags = [ |
130
|
|
|
static::STATUS_CODE_SUCCESS => static::STATUS_TAG_SUCCESS, |
131
|
|
|
static::STATUS_CODE_WARNING => static::STATUS_TAG_WARNING, |
132
|
|
|
static::STATUS_CODE_SKIP => static::STATUS_TAG_SKIP, |
133
|
|
|
static::STATUS_CODE_FAILURE => static::STATUS_TAG_FAILURE, |
134
|
|
|
'default' => static::STATUS_TAG_UNKNOWN, |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
return $tags[$code] ?? $tags['default']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function tagByImportance($importance): string |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$tags = [ |
143
|
|
|
CheckAbstract::IMPORTANCE_EMERGENCY => static::IMPORTANCE_TAG_EMERGENCY, |
144
|
|
|
CheckAbstract::IMPORTANCE_WARNING => static::IMPORTANCE_TAG_WARNING, |
145
|
|
|
CheckAbstract::IMPORTANCE_NOTE => static::IMPORTANCE_TAG_NOTE, |
146
|
|
|
CheckAbstract::IMPORTANCE_INFO => static::IMPORTANCE_TAG_INFO, |
147
|
|
|
'default' => static::IMPORTANCE_TAG_DEFAULT, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
return $tags[$importance] ?? $tags['default']; |
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
|
|
|
|