summaryrefslogtreecommitdiffstats
path: root/doc/60-CLI.md
blob: 5d35244605e0ba476da4bbfeb27cd06f7e5c5fd9 (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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<a id="CLI"></a>Director CLI
============================

Large parts of the Director's functionality are also available on your CLI.


Manage Objects
--------------

Use `icingacli director <type> <action>` show, create modify or delete
Icinga objects of a specific type:

| Action       | Description                           |
|--------------|---------------------------------------|
| `create`     | Create a new object                   |
| `delete`     | Delete a specific object              |
| `exists`     | Whether a specific object exists      |
| `set`        | Modify an existing objects properties |
| `show`       | Show a specific object                |


Currently the following object types are available on CLI:

* command
* endpoint
* host
* hostgroup
* notification
* service
* timeperiod
* user
* usergroup
* zone


### Create a new object

Use this command to create a new Icinga object


#### Usage

`icingacli director <type> create [<name>] [options]`


#### Options

| Option            | Description                                           |
|-------------------|-------------------------------------------------------|
| `--<key> <value>` | Provide all properties as single command line options |
| `--json`          | Otherwise provide all options as a JSON string        |


#### Examples

To create a new host you can provide all of its properties as command line
parameters:

```shell
icingacli director host create localhost \
    --imports generic-host \
    --address 127.0.0.1 \
    --vars.location 'My datacenter'
```

It would say:

    Host 'localhost' has been created

Providing structured data could become tricky that way. Therefore you are also
allowed to provide JSON formatted properties:

```shell
icingacli director host create localhost \
    --json '{ "address": "127.0.0.1", "vars": { "test": [ "one", "two" ] } }'
```

Passing JSON via STDIN is also possible:

```shell
icingacli director host create localhost --json < my-host.json
```


### Delete a specific object

Use this command to delete a single Icinga object. Just run

    icingacli director <type> delete <name>

That's it. To delete the host created before, this would read

    icingacli director host delete localhost

It will tell you whether your command succeeded:

    Host 'localhost' has been deleted


### Whether a specific object exists

Use this command to find out whether a single Icinga object exists. Just
run:

    icingacli director <type> exists <name>

So if you run...

    icingacli director host exists localhost

...it will either tell you ...

    Host 'localhost' exists

...or:
 
    Host 'localhost' does not exist

When executed from custom scripts you could also just check the exit code,
`0` means that the object exists, `1` that it doesn't.


### Modify an existing objects properties

Use this command to modify specific properties of an existing Icinga object.


#### Usage

    icingacli director <type> set <name> [options]


#### Options

| Option                     | Description                                           |
|----------------------------|-------------------------------------------------------|
| `--<key> <value>`          | Provide all properties as single command line options |
| `--append-<key> <value>`   | Appends to array values, like `imports`,              |
|                            | `groups` or `vars.system_owners`                      |
| `--remove-<key> [<value>]` | Remove a specific property, eventually only           |
|                            | when matching `value`. In case the property is an     |
|                            | array it will remove just `value` when given          |
| `--json`                   | Otherwise provide all options as a JSON string        |
| `--replace`                | Replace all object properties with the given ones     |
| `--auto-create`            | Create the object in case it does not exist           |
| `--allow-overrides`        | Set variable overrides for virtual Services           |


#### Examples

```shell
icingacli director host set localhost \
    --address 127.0.0.2 \
    --vars.location 'Somewhere else'
```

It will either tell you

    Host 'localhost' has been modified

or, when for example issued immediately a second time:

    Host 'localhost' has not been modified

Like create, this also allows you to provide JSON-formatted properties:

```shell
icingacli director host set localhost --json '{ "address": "127.0.0.2" }'
```

This command will fail in case the specified object does not exist. This is
when the `--auto-create` parameter comes in handy. Command output will tell
you whether an object has either been created or (not) modified.

With `set` you only set the specified properties and do not touch the other
ones. You could also want to completely override an object, purging all other
eventually existing and unspecified parameters. Please use `--replace` if this
is the desired behaviour.


### Show a specific object

Use this command to show single objects rendered as Icinga 2 config or
in JSON format.


#### Usage

`icingacli director <type> show <name> [options]`


#### Options

| Option            | Description                                          |
|-------------------|------------------------------------------------------|
| `--resolved`      | Resolve all inherited properties and show a flat     |
|                   | object                                               |
| `--json`          | Use JSON format                                      |
| `--no-pretty`     | JSON is pretty-printed per default (for PHP >= 5.4)  |
|                   | Use this flag to enforce unformatted JSON            |
| `--no-defaults`   | Per default JSON output skips null or default values |
|                   | With this flag you will get all properties           |
| `--with-services` | For hosts only, also shows attached services         |

### Clone an existing object

Use this command to clone a specific object.

#### Usage

`icingacli director <type> clone <name> --from <original> [options]`

#### Options

| Option              | Description                                         |
|---------------------|-----------------------------------------------------|
| `--from <original>` | The name of the object you want to clone            |
| `--<key> <value>`   | Override specific properties while cloning          |
| `--replace`         | In case an object <name> already exists replace it  |
|                     | with the clone                                      |
| `--flat`            | Do no keep inherited properties but create a flat   |
|                     | object with all resolved/inherited properties       |

#### Examples

```shell
icingacli director host clone localhost2 --from localhost
```

```shell
icingacli director host clone localhost3 --from localhost --address 127.0.0.3
```


### Other interesting tasks


#### Rename objects

There is no rename command, but a simple `set` can easily accomplish this task:

    icingacli director host set localhost --object_name localhost2

Please note that it is usually absolutely no problem to rename objects with
the Director. Even renaming something essential as a template like the famous
`generic-host` will not cause any trouble. At least not unless you have other
components outside your Director depending on that template.


#### Disable an object

Objects can be disabled. That way they will still exist in your Director DB,
but they will not be part of your next deployment. Toggling the `disabled`
property is all you need:

    icingacli director host set localhost --disabled

Valid values for booleans are `y`, `n`, `1` and `0`. So to re-enable an object
you could use:

    icingacli director host set localhost --disabled n


#### Working with booleans

As we learned before, `y`, `n`, `1` and `0` are valid values for booleans. But
custom variables have no data type. And even if there is such, you could always
want to change or override this from CLI. So you usually need to provide booleans
in JSON format in case you need them in a custom variable.

There is however one exception from this rule. CLI parameters without a given
value are handled as boolean flags by the Icinga Web 2 CLI. That explains why
the example disabling an object worked without passing `y` or `1`. You could
use this also to set a custom variable to boolean `true`:

    icingacli director host set localhost --vars.some_boolean

Want to change it to false? No chance this way, you need to pass JSON:

    icingacli director host set localhost --json '{ "vars.some_boolean": false }'

This example shows the dot-notation to set a specific custom variable. If we
have had used `{ "vars": { "some_boolean": false } }`, all other custom vars
on this object would have been removed.


#### Change object types

The Icinga Director distincts between the following object types:

| Type              | Description                                                 |
|-------------------|-------------------------------------------------------------|
| `object`          | The default object type. A host, a command and similar      |
| `template`        | An Icinga template                                          |
| `apply`           | An apply rule. This allows for assign rules                 |
| `external_object` | An external object. Can be referenced and used, will not be |
|                   | deployed                                                    |

Example for creating a host template:

```sh
icingacli director host create 'Some template' \
    --object_type template \
    --check_command hostalive
```

Please take a lot of care when modifying object types, you should not do so for
a good reason. The CLI allows you to issue operations that are not allowed in the
web frontend. Do not use this unless you really understand its implications. And
remember, with great power comes great responsibility.


Import/Export Director Objects
------------------------------

Some objects are not directly related to Icinga Objects but used by the Director
to manage them. To make it easier for administrators to for example pre-fill an
empty Director Instance with Import Sources and Sync Rules, related import/export
commands come in handy.

Use `icingacli director export <type> [options]` to export objects of a specific
type:

| Type                  | Description                                     |
|-----------------------|-------------------------------------------------|
| `datafields`          | Export all DataField definitions                |
| `datalists`           | Export all DataList definitions                 |
| `hosttemplatechoices` | Export all IcingaTemplateChoiceHost definitions |
| `importsources`       | Export all ImportSource definitions             |
| `jobs`                | Export all Job definitions                      |
| `syncrules`           | Export all SyncRule definitions                 |

#### Options

| Option        | Description                                          |
|---------------|------------------------------------------------------|
| `--no-pretty` | JSON is pretty-printed per default. Use this flag to |
|               | enforce unformatted JSON                             |

Use `icingacli director import <type> < exported.json` to import objects of a
specific type:

| Type                  | Description                                     |
|-----------------------|-------------------------------------------------|
| `importsources`       | Import ImportSource definitions from STDIN      |
| `syncrules`           | Import SyncRule definitions from STDIN          |


This feature is available since v1.5.0.


Director Configuration Basket
-----------------------------

A basket contains a set of Director Configuration objects (like Templates,
Commands, Import/Sync definitions - but not single Hosts or Services). This
CLI command allows you to integrate them into your very own workflows

## Available Actions

| Action     | Description                                       |
|------------|---------------------------------------------------|
| `dump`     | JSON-dump for objects related to the given Basket |
| `list`     | List configured Baskets                           |
| `restore`  | Restore a Basket from JSON dump provided on STDIN |
| `snapshot` | Take a snapshot for the given Basket              |

### Options

| Option   | Description                                          |
|----------|------------------------------------------------------|
| `--name` | `dump` and `snapshot` require a specific object name |

Use `icingacli director basket restore < exported-basket.json` to restore objects
from a specific basket. Take a snapshot or a backup first to be on the safe side.

This feature is available since v1.6.0.


Health Check Plugin
-------------------

You can use the Director CLI as an Icinga CheckPlugin and monitor your Director
Health. This will run all or just one of the following test suites:

| Name         | Description                                                       |
|--------------|-------------------------------------------------------------------|
| `config`     | Configuration, Schema, Migrations                                 |
| `sync`       | All configured Sync Rules (pending changes are not a problem)     |
| `import`     | All configured Import Sources (pending changes are not a problem) |
| `jobs`       | All configured Jobs (ignores disabled ones)                       |
| `deployment` | Deployment Endpoint, last deployment outcome                      |

#### Usage

`icingacli director health check [options]`

#### Options

| Option           | Description                           |
|------------------|---------------------------------------|
| `--check <name>` | Run only a specific test suite        |
| `--<db> <name>`  | Use a specific Icinga Web DB resource |

#### Examples

```shell
icingacli director health check
```

Example for running a check only for the configuration:

```shell
icingacli director health check --check config
```

Sample output:

```
Director configuration: 5 tests OK
[OK] Database resource 'Director DB' has been specified'
[OK] Make sure the DB schema exists
[OK] There are no pending schema migrations
[OK] Deployment endpoint is 'icinga.example.com'
[OK] There is a single un-deployed change
```


Kickstart and schema handling
-----------------------------

The `kickstart` and the `migration` command are handled in the [automation section](03-Automation.md),
so they are skipped here.


Configuration handling
----------------------

### Render your configuration

The Director distincts between rendering and deploying your configuration.
Rendering means that Icinga 2 config will be pre-rendered and stored to the
Director DB. Nothing bad happens if you decide to render the current config
thousands of times in a loop. In case a config with the same checksum already
exists, it will store - nothing.

You can trigger config rendering by running

```shell
icingacli director config render
```

In case a new config has been created, it will tell you so:
```
New config with checksum b330febd0820493fb12921ad8f5ea42102a5c871 has been generated
```

Run it once again, and you'll see that the output changes:
```
Config with checksum b330febd0820493fb12921ad8f5ea42102a5c871 already exists
```


### Config deployment

#### Usage

`icingacli director config deploy [options]`

#### Options

| Option                     | Description                                                      |
|----------------------------|------------------------------------------------------------------|
| `--checksum <checksum>`    | Optionally deploy a specific configuration                       |
| `--force`                  | Force a deployment, even when the configuration hasn't changed   |
| `--wait <seconds>`         | Optionally wait until Icinga completed it's restart              |
| `--grace-period <seconds>` | Do not deploy if a deployment took place less than <seconds> ago |

#### Examples

You do not need to explicitly render your config before deploying it to your
Icinga 2 master node. Just trigger a deployment, it will re-render the current
config:

```shell
icingacli director config deploy 
```

The output tells you which config has been shipped:

```
Config 'b330febd0820493fb12921ad8f5ea42102a5c871' has been deployed
```

Director tries to avoid needless deployments, so in case you immediately deploy
again, the output changes:
```
Config matches active stage, nothing to do
```

You can override this by adding the `--force` parameter. It will then tell you:

```
Config matches active stage, deploying anyway
```

In case you want to do not want `deploy` to waste time to re-render your
config or in case you decide to re-deploy a specific, eventually older config
version the `deploy` command allows you to provide a specific checksum:

```shell
icingacli director config deploy --checksum b330febd0820493fb12921ad8f5ea42102a5c871
```

When using `icingacli` deployments in an automated way, and want to avoid fast
consecutive deployments, you can provide a grace period:

```shell
icingacli director config deploy --grace-period 300
```

### Deployments status
In case you want to fetch the information about the deployments status, 
you can call the following CLI command:
```shell
icingacli director config deploymentstatus
```
```json
{
    "active_configuration": {
        "stage_name": "5c65cae0-4f1b-47b4-a890-766c82681622",
        "config": "617b9cbad9e141cfc3f4cb636ec684bd60073be1",
        "activity": "4f7bc6600dd50a989f22f82d3513e561ef333363"
    }
}
```
In case there is no active stage name related to the Director, active_configuration 
is set to null.

Another possibility is to pass a list of checksums to fetch the status of 
specific deployments and (activity log) activities.
Following, you can see an example of how to do it:
```shell
icingacli director config deploymentstatus \
    --configs 617b9cbad9e141cfc3f4cb636ec684bd60073be1 \
    --activities 4f7bc6600dd50a989f22f82d3513e561ef333363
```
```json
{
    "active_configuration": {
        "stage_name": "5c65cae0-4f1b-47b4-a890-766c82681622",
        "config": "617b9cbad9e141cfc3f4cb636ec684bd60073be1",
        "activity": "4f7bc6600dd50a989f22f82d3513e561ef333363"
    },
    "configs": {
        "617b9cbad9e141cfc3f4cb636ec684bd60073be1": "active"
    },
    "activities": {
        "4f7bc6600dd50a989f22f82d3513e561ef333363": "active"
    }
}
```

You can also decide to access directly to a value inside the result JSON by 
using the `--key` param:
```shell
icingacli director config deploymentstatus \
    --configs 617b9cbad9e141cfc3f4cb636ec684bd60073be1 \
    --activities 4f7bc6600dd50a989f22f82d3513e561ef333363 \
    --key active_configuration.config
```
```
617b9cbad9e141cfc3f4cb636ec684bd60073be1
```



### Cronjob usage

You could decide to pre-render your config in the background quite often. As of
this writing this has one nice advantage. It allows the GUI to find out whether
a bunch of changes still results into the very same config. 
only one 


Run sync and import jobs
------------------------

### Import Sources

#### List available Import Sources

This shows a table with your defined Import Sources, their IDs and
current state. As triggering Imports requires an ID, this is where you
can look up the desired ID.

`icingacli director importsource list`

#### Check a given Import Source for changes

This command fetches data from the given Import Source and compares it
to the most recently imported data.

`icingacli director importsource check --id <id>`

##### Options

| Option        | Description                                             |
|---------------|---------------------------------------------------------|
| `--id <id>`   | An Import Source ID. Use the list command to figure out |
| `--benchmark` | Show timing and memory usage details                    |

#### Fetch data from a given Import Source

This command fetches data from the given Import Source and outputs them
as plain JSON

`icingacli director importsource fetch --id <id>`

##### Options

| Option        | Description                                             |
|---------------|---------------------------------------------------------|
| `--id <id>`   | An Import Source ID. Use the list command to figure out |
| `--benchmark` | Show timing and memory usage details                    |

#### Trigger an Import Run for a given Import Source

This command fetches data from the given Import Source and stores it to
the Director DB, so that the next related Sync Rule run can work with
fresh data. In case data didn't change, nothing is going to be stored.

`icingacli director importsource run --id <id>`

##### Options

| Option        | Description                                             |
|---------------|---------------------------------------------------------|
| `--id <id>`   | An Import Source ID. Use the list command to figure out |
| `--benchmark` | Show timing and memory usage details                    |

### Sync Rules

#### List defined Sync Rules

This shows a table with your defined Sync Rules, their IDs and current
state. As triggering a Sync requires an ID, this is where you can look
up the desired ID.

`icingacli director syncrule list`

#### Check a given Sync Rule for changes

This command runs a complete Sync in memory but doesn't persist eventual
changes.

`icingacli director syncrule check --id <id>`

##### Options

| Option        | Description                                        |
|---------------|----------------------------------------------------|
| `--id <id>`   | A Sync Rule ID. Use the list command to figure out |
| `--benchmark` | Show timing and memory usage details               |

#### Trigger a Sync Run for a given Sync Rule

This command builds new objects according your Sync Rule, compares them
with existing ones and persists eventual changes.

`icingacli director syncrule run --id <id>`

##### Options

| Option        | Description                                        |
|---------------|----------------------------------------------------|
| `--id <id>`   | A Sync Rule ID. Use the list command to figure out |
| `--benchmark` | Show timing and memory usage details               |


Database housekeeping
---------------------

Your database may grow over time and ask for various housekeeping tasks. You
can usually store a lot of data in your Director DB before you would even
notice a performance impact. 

Still, we started to prepare some tasks that assist with removing useless
garbage from your DB. You can show available tasks with:

    icingacli director housekeeping tasks

The output might look as follows:

```
 Housekeeping task (name)                                  | Count
-----------------------------------------------------------|-------
 Undeployed configurations (oldUndeployedConfigs)          |     3
 Unused rendered files (unusedFiles)                       |     0
 Unlinked imported row sets (unlinkedImportedRowSets)      |     0
 Unlinked imported rows (unlinkedImportedRows)             |     0
 Unlinked imported properties (unlinkedImportedProperties) |     0
```

You could run a specific task with

    icingacli director housekeeping run <taskName>

...like in:

    icingacli director housekeeping run unlinkedImportedRows

Or you could also run all of them, that's the preferred way of doing this:

    icingacli director housekeeping run ALL

Please note that some tasks once issued create work for other tasks, as
lost imported rows might appear once you remove lost row sets. So `ALL`
is usually the best choice as it runs all of them in the best order.