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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\Objects\Extension\Arguments;
use Zend_Db_Select as DbSelect;
class IcingaCommand extends IcingaObject implements ObjectWithArguments, ExportInterface
{
use Arguments;
protected $table = 'icinga_command';
protected $type = 'CheckCommand';
protected $uuidColumn = 'uuid';
protected $defaultProperties = [
'id' => null,
'uuid' => null,
'object_name' => null,
'object_type' => null,
'disabled' => 'n',
'methods_execute' => null,
'command' => null,
'timeout' => null,
'zone_id' => null,
'is_string' => null,
];
protected $booleans = [
'is_string' => 'is_string',
];
protected $supportsCustomVars = true;
protected $supportsFields = true;
protected $supportsImports = true;
protected $supportedInLegacy = true;
protected $intervalProperties = [
'timeout' => 'timeout',
];
protected $relations = [
'zone' => 'IcingaZone',
];
protected static $pluginDir;
protected $hiddenExecuteTemplates = [
'PluginCheck' => 'plugin-check-command',
'PluginNotification' => 'plugin-notification-command',
'PluginEvent' => 'plugin-event-command',
// Special, internal:
'IcingaCheck' => 'icinga-check-command',
'ClusterCheck' => 'cluster-check-command',
'ClusterZoneCheck' => 'plugin-check-command',
'IdoCheck' => 'ido-check-command',
'RandomCheck' => 'random-check-command',
];
/**
* Render the 'medhods_execute' property as 'execute'
*
* Execute is a reserved word in SQL, column name was prefixed
*
* Avoid complaints for method names with underscore:
* @codingStandardsIgnoreStart
*
* @return string
*/
protected function renderMethods_execute()
{
// @codingStandardsIgnoreEnd
return '';
}
protected function renderObjectHeader()
{
if ($execute = $this->get('methods_execute')) {
$itlImport = sprintf(
' import "%s"' . "\n",
$this->hiddenExecuteTemplates[$execute]
);
} else {
$itlImport = '';
}
$execute = $this->getSingleResolvedProperty('methods_execute');
if ($execute === 'PluginNotification') {
return $this->renderObjectHeaderWithType('NotificationCommand') . $itlImport;
} elseif ($execute === 'PluginEvent') {
return $this->renderObjectHeaderWithType('EventCommand') . $itlImport;
} else {
return parent::renderObjectHeader() . $itlImport;
}
}
/**
* @param $type
* @return string
*/
protected function renderObjectHeaderWithType($type)
{
return sprintf(
"%s %s %s {\n",
$this->getObjectTypeName(),
$type,
c::renderString($this->getObjectName())
);
}
public function mungeCommand($value)
{
if (is_array($value)) {
$value = implode(' ', $value);
} elseif (is_object($value)) {
// { type => Function } -> really??
return null;
// return $value;
}
if (self::$pluginDir !== null) {
if (($pos = strpos($value, self::$pluginDir)) === 0) {
$value = substr($value, strlen(self::$pluginDir) + 1);
}
}
return $value;
}
public function getNextSkippableKeyName()
{
$key = $this->makeSkipKey();
$cnt = 1;
while (isset($this->arguments()->$key)) {
$cnt++;
$key = $this->makeSkipKey($cnt);
}
return $key;
}
protected function makeSkipKey($num = null)
{
if ($num === null) {
return '(no key)';
}
return sprintf('(no key.%d)', $num);
}
protected function prefersGlobalZone()
{
return true;
}
/**
* @return string
* @throws \Zend_Db_Select_Exception
*/
public function countDirectUses()
{
$db = $this->getDb();
$id = (int) $this->get('id');
$qh = $db->select()->from(
array('h' => 'icinga_host'),
array('cnt' => 'COUNT(*)')
)->where('h.check_command_id = ?', $id)
->orWhere('h.event_command_id = ?', $id);
$qs = $db->select()->from(
array('s' => 'icinga_service'),
array('cnt' => 'COUNT(*)')
)->where('s.check_command_id = ?', $id)
->orWhere('s.event_command_id = ?', $id);
$qn = $db->select()->from(
array('n' => 'icinga_notification'),
array('cnt' => 'COUNT(*)')
)->where('n.command_id = ?', $id);
$query = $db->select()->union(
[$qh, $qs, $qn],
DbSelect::SQL_UNION_ALL
);
return $db->fetchOne($db->select()->from(
['all_cnts' => $query],
['cnt' => 'SUM(cnt)']
));
}
/**
* @return bool
* @throws \Zend_Db_Select_Exception
*/
public function isInUse()
{
return $this->countDirectUses() > 0;
}
public function getUniqueIdentifier()
{
return $this->getObjectName();
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
$props = (array) $this->toPlainObject();
if (isset($props['arguments'])) {
foreach ($props['arguments'] as $key => $argument) {
if (property_exists($argument, 'command_id')) {
unset($props['arguments'][$key]->command_id);
}
}
}
$props['fields'] = $this->loadFieldReferences();
ksort($props);
return (object) $props;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaCommand
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
$name = $properties['object_name'];
$key = $name;
if ($replace && static::exists($key, $db)) {
$object = static::load($key, $db);
} elseif (static::exists($key, $db)) {
throw new DuplicateKeyException(
'Command "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return array
*/
protected function loadFieldReferences()
{
$db = $this->getDb();
$res = $db->fetchAll(
$db->select()->from([
'cf' => 'icinga_command_field'
], [
'cf.datafield_id',
'cf.is_required',
'cf.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = cf.datafield_id', [])
->where('command_id = ?', $this->get('id'))
->order('varname ASC')
);
if (empty($res)) {
return [];
} else {
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}
protected function renderCommand()
{
$command = $this->get('command');
$prefix = '';
if (preg_match('~^([A-Z][A-Za-z0-9_]+\s\+\s)(.+?)$~', $command, $m)) {
$prefix = $m[1];
$command = $m[2];
} elseif (! $this->isAbsolutePath($command)) {
$prefix = 'PluginDir + ';
$command = '/' . $command;
}
$inherited = $this->getInheritedProperties();
if ($this->get('is_string') === 'y' || ($this->get('is_string') === null
&& property_exists($inherited, 'is_string') && $inherited->is_string === 'y')) {
return c::renderKeyValue('command', $prefix . c::renderString($command));
} else {
$parts = preg_split('/\s+/', $command, -1, PREG_SPLIT_NO_EMPTY);
array_unshift($parts, c::alreadyRendered($prefix . c::renderString(array_shift($parts))));
return c::renderKeyValue('command', c::renderArray($parts));
}
}
/**
* @codingStandardsIgnoreStart
*/
protected function renderIs_string()
{
// @codingStandardsIgnoreEnd
return '';
}
protected function isAbsolutePath($path)
{
return $path[0] === '/'
|| $path[0] === '\\'
|| preg_match('/^[A-Za-z]:\\\/', substr($path, 0, 3))
|| preg_match('/^%[A-Z][A-Za-z0-9\(\)-]*%/', $path);
}
public static function setPluginDir($pluginDir)
{
self::$pluginDir = $pluginDir;
}
public function getLegacyObjectType()
{
// there is only one type of command in Icinga 1.x
return 'command';
}
protected function renderLegacyCommand()
{
$command = $this->get('command');
if (preg_match('~^(\$USER\d+\$/?)(.+)$~', $command)) {
// should be fine, since the user decided to use a macro
} elseif (! $this->isAbsolutePath($command)) {
$command = '$USER1$/'.$command;
}
return c1::renderKeyValue(
$this->getLegacyObjectType().'_line',
c1::renderString($command)
);
}
}
|