-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPSZabbix.psm1
2151 lines (1782 loc) · 66.5 KB
/
PSZabbix.psm1
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$ErrorActionPreference = "Stop"
$latestSession = $null
################################################################################
## INTERNAL HELPERS
################################################################################
function New-JsonrpcRequest($method, $params, $auth = $null)
{
if ($params.output -eq $null -and $method -like "*.get")
{
$params["output"] = "extend"
}
return ConvertTo-Json @{
jsonrpc = "2.0"
method = $method
params = $params
id = 1
auth = $auth
} -Depth 20
}
function Get-ApiVersion($Session)
{
$r = Invoke-RestMethod -Uri $session.Uri -Method Post -ContentType "application/json" -Body (new-JsonrpcRequest "apiinfo.version" @{})
$r.result
}
function New-ApiSession
{
<#
.SYNOPSIS
Create a new authenticated session which can be used to call the Zabbix REST API.
.DESCRIPTION
It must be called all other functions. It returns the actual session object, but usually
this object is not needed as the module caches and reuses the latest successful session.
The validity of the credentials is checked and an error is thrown if not.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The session object.
.EXAMPLE
PS> New-ZbxApiSession "http://myserver/zabbix/api_jsonrpc.php" (Get-Credentials MyAdminLogin)
Name Value
---- -----
Auth 2cce0ad0fac0a5da348fdb70ae9b233b
Uri http://myserver/zabbix/api_jsonrpc.php
WARNING : Connected to Zabbix version 3.2.1
#>
param(
# The Zabbix REST endpoint. It should be like "http://myserver/zabbix/api_jsonrpc.php".
[uri] $ApiUri,
# The credentials used to authenticate. Use Get-Credential to create this object.
[PSCredential]$auth,
# If this switch is used, the information message "connected to..." will not be displayed.
[switch]$Silent
)
$r = Invoke-RestMethod -Uri $ApiUri -Method Post -ContentType "application/json" -Body (new-JsonrpcRequest "user.login" @{user = $auth.UserName; password = $auth.GetNetworkCredential().Password})
if ($r -eq $null -or $r.result -eq $null -or [string]::IsNullOrWhiteSpace($r.result))
{
Write-Error -Message "Session could not be opened"
}
$script:latestSession = @{Uri = $ApiUri; Auth = $r.result}
$script:latestSession
$ver = Get-ApiVersion -Session $script:latestSession
$vers = $ver.split(".")
if ( ($vers[0] -lt 2) -or ($vers[0] -eq 2 -and $vers[1] -lt 4))
{
Write-Warning "PSZabbix has not been tested with this version of Zabbix ${ver}. Tested version are >= 2.4. It should still work but be warned."
}
if (-not $Silent)
{
Write-Warning "Connected to Zabbix version ${ver}"
}
}
function Invoke-ZabbixApi($session, $method, $parameters = @{})
{
if ($session -eq $null) { $session = $latestSession }
if ($session -eq $null)
{
Write-Error -Message "No session is opened. Call New-ZabbixApiSession before or pass a previously retrieved session object as a parameter."
return
}
$r = Invoke-RestMethod -Uri $session.Uri -Method Post -ContentType "application/json" -Body (new-JsonrpcRequest $method $parameters $session.Auth)
if ($r.error -ne $null)
{
Write-Error -Message "$($r.error.message) $($r.error.data)" -ErrorId $r.error.code
}
else
{
return $r.result
}
}
################################################################################
## HOSTS
################################################################################
Add-Type -TypeDefinition @"
public enum ZbxStatus
{
Enabled = 0,
Disabled = 1
}
"@
function Get-Host
{
<#
.SYNOPSIS
Retrieve and filter hosts.
.DESCRIPTION
Query all hosts (not templates) with basic filters, or get all hosts.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The ZabbixHost objects corresponding to the filter.
.EXAMPLE
PS> Get-ZbxHost
hostid host name status
------ ---- ---- ------
10084 Zabbix server Zabbix server Enabled
10105 Agent Mongo 1 Agent Mongo 1 Enabled
.EXAMPLE
PS> Get-ZbxHost -Id 10084
hostid host name status
------ ---- ---- ------
10084 Zabbix server Zabbix server Enabled
.EXAMPLE
PS> Get-ZbxHost "Agent*"
hostid host name status
------ ---- ---- ------
10105 Agent Mongo 1 Agent Mongo 1 Enabled
10106 Agent Mongo 2 Agent Mongo 2 Enabled
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$False)][Alias("HostId")]
# Only retrieve the items with the given ID(s).
[int[]] $Id,
[Parameter(Mandatory=$False)]
# Only retrieve items which belong to the given group(s).
[int[]] $HostGroupId,
[Parameter(Mandatory=$False, Position=0)][Alias("HostName")]
# Filter by hostname. Accepts wildcard.
[string] $Name
)
$prms = @{search= @{}; searchWildcardsEnabled = 1; selectInterfaces = @("interfaceid", "ip", "dns"); selectParentTemplates = 1}
if ($Id.Length -gt 0) {$prms["hostids"] = $Id}
if ($HostGroupId.Length -gt 0) {$prms["groupids"] = $GroupId}
if ($Name -ne $null) {$prms["search"]["name"] = $Name}
Invoke-ZabbixApi $session "host.get" $prms |% {$_.status = [ZbxStatus]$_.status; $_.hostid = [int]$_.hostid; $_.PSTypeNames.Insert(0,"ZabbixHost"); $_}
}
function New-Host
{
<#
.SYNOPSIS
Create a new host.
.DESCRIPTION
Create a new host.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The ZabbixHost object created.
.EXAMPLE
PS> New-ZbxHost -Name "mynewhostname$(Get-Random)" -HostGroupId 2 -TemplateId 10108 -Dns localhost
hostid host name status
------ ---- ---- ------
10084 mynewhostname321 mynewhostname Enabled
.NOTES
Contrary to other New-* functions inside this module, this method does not take pipe input.
This is inconsistent and needs to be changed.
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[parameter(Mandatory=$true)][Alias("HostName")]
# The name of the new host (not the visible name)
[string] $Name,
[parameter(Mandatory=$false)][Alias("DisplayName")]
# The name as displayed in the interface. Defaults to Name.
[string] $VisibleName,
[parameter(Mandatory=$false)]
# A description of the new host.
[string] $Description = $null,
[parameter(Mandatory=$true, ParameterSetName="Ids")]
# The groups the new host should belong to.
[int[]] $HostGroupId,
[parameter(Mandatory=$true, ParameterSetName="Objects")]
# The groups the new host should belong to.
[PSCustomObject[]] $HostGroup,
[parameter(Mandatory=$true, ParameterSetName="Ids")]
# The templates the new host should belong to.
[int[]] $TemplateId,
[parameter(Mandatory=$true, ParameterSetName="Objects")]
# The templates the new host should belong to.
[PSCustomObject[]] $Template,
[parameter(Mandatory=$false)]
# An optional map of inventory properties
$Inventory = @{},
[parameter(Mandatory=$true)]
# The DNS or IP address to use to contact the host
[string] $Dns,
[parameter(Mandatory=$false)]
# The port to use to use to contact the host. Default is 10050.
[int] $Port = 10050,
[parameter(Mandatory=$false)]
# Should the newly created host be enabled? Default is true.
[ZbxStatus] $Status = [ZbxStatus]::Enabled,
[parameter(Mandatory=$false)]
# The ID of the proxy to use. Default is no proxy.
[int] $ProxyId
)
$isIp = 0
try { [ipaddress]$Dns; $isIp = 1} catch {}
if ($Hostgroupid -ne $null)
{
$HostGroup = @()
$HostGroupId |% { $HostGroup += @{"groupid" = $_} }
}
if ($TemplateId -ne $null)
{
$Template = @()
$TemplateId |% { $Template += @{"templateid" = $_} }
}
$prms = @{
host = $Name
name = if ([string]::IsNullOrWhiteSpace($VisibleName)) { $null } else { $VisibleName }
description = $Description
interfaces = @( @{
type = 1
main = 1
useip = $isIp
dns = if ($isIp -eq 1) { "" } else { $Dns }
ip = if ($isIp -eq 0) { "" } else { $Dns }
port = $Port
})
groups = $HostGroup
templates = $Template
inventory_mode = 0
inventory = $Inventory
status = [int]$Status
proxy_hostid = if ($ProxyId -eq $null) { "" } else { $ProxyId }
}
$r = Invoke-ZabbixApi $session "host.create" $prms
Get-Host -session $s -Id $r.hostids
}
function Remove-Host
{
<#
.SYNOPSIS
Remove one or more hosts from Zabbix.
.DESCRIPTION
Removal is immediate.
.INPUTS
This function accepts ZabbixHost objects or host IDs from the pipe. Equivalent to using -HostId parameter.
.OUTPUTS
The ID of the removed objects.
.EXAMPLE
Remove all hosts
PS> Get-ZbxHost | Remove-ZbxHost
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)][ValidateNotNullOrEmpty()][Alias("Id")]
# The ID of one or more hosts to remove. You can also pipe a ZabbixHost object or any object with a hostid or id property.
[int[]]$HostId
)
begin
{
$prms = @()
}
process
{
$prms += $HostId
}
end
{
if ($prms.Count -eq 0) { return }
Invoke-ZabbixApi $session "host.delete" $prms | select -ExpandProperty hostids
}
}
function Enable-Host
{
<#
.SYNOPSIS
Enable one or more hosts from Zabbix.
.DESCRIPTION
Simple change of the status of the host. Idempotent.
.INPUTS
This function accepts ZabbixHost objects or host IDs from the pipe. Equivalent to using -HostId parameter.
.OUTPUTS
The ID of the changed objects.
.EXAMPLE
Enable all hosts
PS> Get-ZbxHost | Enable-ZbxHost
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=0)][Alias("Id", "Host")]
# The ID of one or more hosts to enable. You can also pipe a ZabbixHost object or any object with a hostid or id property.
[int[]]$HostId
)
begin
{
$ids = @()
}
Process
{
$HostId |% {$ids += @{hostid = $_}}
}
end
{
if ($ids.Count -eq 0) { return }
Invoke-ZabbixApi $session "host.massupdate" @{hosts=$ids; status=0} | select -ExpandProperty hostids
}
}
function Disable-Host
{
<#
.SYNOPSIS
Disable one or more hosts from Zabbix.
.DESCRIPTION
Simple change of the status of the host. Idempotent.
.INPUTS
This function accepts ZabbixHost objects or host IDs from the pipe. Equivalent to using -HostId parameter.
.OUTPUTS
The ID of the changed objects.
.EXAMPLE
Disable all hosts
PS> Get-ZbxHost | Disable-ZbxHost
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=0)][Alias("Id", "Host")]
# The ID of one or more hosts to disable. You can also pipe a ZabbixHost object or any object with a hostid or id property.
[int[]]$HostId
)
begin
{
$ids = @()
}
Process
{
$HostId |% {$ids += @{hostid = $_}}
}
end
{
if ($ids.Count -eq 0) { return }
Invoke-ZabbixApi $session "host.massupdate" @{hosts=$ids; status=1} | select -ExpandProperty hostids
}
}
function Add-HostGroupMembership
{
<#
.SYNOPSIS
Make a host (or multiple hosts) member of one or more host groups.
.DESCRIPTION
This is additional: existing membership to other groups are not changed.
.INPUTS
This function accepts ZabbixHost objects from the pipe. Equivalent to using -Host parameter.
.OUTPUTS
The ID of the changed objects.
.EXAMPLE
PS> Get-ZbxHost | Add-ZbxHostGroupMembership (Get-ZbxGroup group1),(Get-ZbxGroup group2)
10084
10085
Add two groups to all hosts
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=1)][ValidateScript({ $_.PSObject.TypeNames[0] -eq 'ZabbixHost'})][ValidateNotNullOrEmpty()]
# The host or hostid to add to the hostgroup.
[PSCustomObject[]]$Host,
[Parameter(Mandatory=$true, Position=0)][ValidateNotNullOrEmpty()]
# The Host is added to this list of one or more hostgroups.
[PSCustomObject[]]$HostGroup
)
begin
{
$grpids = @($HostGroup |% {@{groupid = $_.groupid}} )
$prms = @{hosts = @(); groups = $grpids}
}
process
{
$prms["hosts"] += $Host.hostid
}
end
{
Invoke-ZabbixApi $session "host.massadd" $prms | select -ExpandProperty hostids
}
}
function Remove-HostGroupMembership
{
<#
.SYNOPSIS
Remove a host (or multiple hosts) as a member of one or more host groups.
.DESCRIPTION
This is additional: existing membership to other groups are not changed.
.INPUTS
This function accepts ZabbixHost objects from the pipe. Equivalent to using -Host parameter.
.OUTPUTS
The ID of the changed objects.
.EXAMPLE
PS> Get-ZbxHost | Remove-ZbxHostGroupMembership (Get-ZbxGroup group1),(Get-ZbxGroup group2)
10084
10085
Make sure no host is member of two specified groups.
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=1)][ValidateScript({ $_.PSObject.TypeNames[0] -eq 'ZabbixHost'})][ValidateNotNullOrEmpty()]
# The host or hostid to remove from the hostgroup(s).
[PSCustomObject[]]$Host,
[Parameter(Mandatory=$true, Position=0)][ValidateNotNullOrEmpty()][ValidateScript({ $_.PSObject.TypeNames[0] -eq 'ZabbixGroup'})]
# The Host is removed from this list of one or more hostgroups.
[PSCustomObject[]]$HostGroup
)
begin
{
$grpids = @($HostGroup |% {$_.groupid} )
$prms = @{hostids = @(); groupids = $grpids}
}
process
{
$prms["hostids"] += $Host.hostid
}
end
{
Invoke-ZabbixApi $session "host.massremove" $prms | select -ExpandProperty hostids
}
}
function Update-Host
{
<#
.SYNOPSIS
Update the attributes of one or more existing Zabbix Hosts.
.DESCRIPTION
Please note that this method actually updates all attributes of the host, even if they were not changed.
This is first and foremost made to update direct attributes. To update linked linked objects like templates or interfaces, it is often more practical to use the dedicated cmdlets.
.INPUTS
This function accepts ZabbixHost objects, on pipe or as argument.
.OUTPUTS
The ID of the changed objects.
.EXAMPLE
Sets a new description for all hosts.
PS> Get-ZbxHost |% { $_.name = "toto"; $_ } | Update-ZbxHost
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[ValidateScript({ $_.PSObject.TypeNames[0] -eq 'ZabbixHost'})][ValidateNotNullOrEmpty()]
# One or more hosts to update.
[PSObject[]]$Host
)
begin
{
$Hosts = @()
}
process
{
$Hosts += $Host
}
end
{
if ($Hosts.Count -eq 0) { return }
Invoke-ZabbixApi $session "host.update" $Hosts | select -ExpandProperty hostids
}
}
################################################################################
## TEMPLATES
################################################################################
function Get-Template
{
<#
.SYNOPSIS
Retrieve and filter templates.
.DESCRIPTION
Query all templates (not hosts) with basic filters, or get all templates.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The ZabbixTemplate objects corresponding to the filter.
.EXAMPLE
PS> Get-ZbxHost
templateid name description
---------- ---- -----------
10001 Template OS Linux
10047 Template App Zabbix Server
10048 Template App Zabbix Proxy
10050 Template App Zabbix Agent
.EXAMPLE
PS> Get-ZbxHost -Id 10001
templateid name description
---------- ---- -----------
10001 Template OS Linux
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$False)][Alias("TemplateId")]
# Only retrieve the template with the given ID.
[int[]] $Id,
[Parameter(Mandatory=$False)]
# Only retrieve remplates which belong to the given group(s).
[int[]] $GroupId,
[Parameter(Mandatory=$False)]
# Only retrieve templates which are linked to the given hosts
[int[]] $HostId,
[Parameter(Mandatory=$False)]
# Only retrieve templates which are children of the given parent template(s)
[int[]] $ParentId,
[Parameter(Mandatory=$False, Position=0)][Alias("TemplateName")]
# Filter by name. Accepts wildcard.
[string] $Name
)
$prms = @{search= @{}; searchWildcardsEnabled=1}
if ($Id.Length -gt 0) {$prms["templateids"] = $Id}
if ($GroupId.Length -gt 0) {$prms["groupids"] = $GroupId}
if ($HostId.Length -gt 0) {$prms["hostids"] = $HostId}
if ($Name -ne $null) {$prms["search"]["name"] = $Name}
Invoke-ZabbixApi $session "template.get" $prms |% {$_.templateid = [int]$_.templateid; $_.PSTypeNames.Insert(0,"ZabbixTemplate"); $_}
}
function Remove-Template
{
<#
.SYNOPSIS
Remove one or more templates from Zabbix.
.DESCRIPTION
Removal is immediate.
.INPUTS
This function accepts ZabbixTemplate objects or template IDs from the pipe. Equivalent to using -TemplateId parameter.
.OUTPUTS
The ID of the removed objects.
.EXAMPLE
Remove all templates
PS> Get-ZbxTemplate | Remove-ZbxTemplate
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)][ValidateNotNullOrEmpty()][Alias("Template")]
# The templates to remove. Either template objects (with a templateid property) or directly IDs.
[int[]]$TemplateId
)
begin
{
$prms = @()
}
process
{
$prms += $TemplateId
}
end
{
if ($prms.Count -eq 0) { return }
Invoke-ZabbixApi $session "template.delete" $prms | select -ExpandProperty templateids
}
}
################################################################################
## HOST GROUPS
################################################################################
#region host groups
function Get-HostGroup
{
<#
.SYNOPSIS
Retrieve and filter host groups.
.DESCRIPTION
Query all host groups with basic filters, or get all host groups.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The ZabbixHostGroup objects corresponding to the filter.
.EXAMPLE
PS> Get-ZbxHostGroup "Linux*"
groupid internal flags name
------- -------- ----- ----
1 0 0 Linux Group 1
2 0 0 Linux Group 2
.EXAMPLE
PS> Get-ZbxHostGroup
groupid internal flags name
------- -------- ----- ----
1 0 0 Templates
2 0 0 Linux servers
4 0 0 Zabbix servers
.EXAMPLE
PS> Get-ZbxHostGroup -Id 1
groupid internal flags name
------- -------- ----- ----
1 0 0 Templates
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$False)]
# Only retrieve the groups with the given ID(s)
[int[]] $Id,
[Parameter(Mandatory=$False)]
# Only retrieve the groups which contain the given host(s)
[int[]] $HostId,
[Parameter(Mandatory=$False, Position=0)][Alias("GroupName")]
# Filter by name. Accepts wildcard.
[string] $Name
)
$prms = @{search= @{}; searchWildcardsEnabled = 1; selectHosts = 1}
if ($HostId.Length -gt 0) {$prms["hostids"] = $HostId}
if ($Id.Length -gt 0) {$prms["groupids"] = $Id}
if ($Name -ne $null) {$prms["search"]["name"] = $Name}
Invoke-ZabbixApi $session "hostgroup.get" $prms |% {$_.groupid = [int]$_.groupid; $_.PSTypeNames.Insert(0,"ZabbixGroup"); $_}
}
function New-HostGroup
{
<#
.SYNOPSIS
Create a new host group.
.DESCRIPTION
Create a new host group.
.INPUTS
This function accepts a ZabbixHostGroup as pipe input, or any object which properties map the function parameters.
.OUTPUTS
The ZabbixHostGroup object created.
.EXAMPLE
PS> New-ZbxHostGroup "newgroupname1","newgroupname2"
groupid internal flags name
------- -------- ----- ----
13 0 0 newgroupname1
14 0 0 newgroupname2
.EXAMPLE
PS> "newgroupname1","newgroupname2" | New-ZbxHostGroup
groupid internal flags name
------- -------- ----- ----
13 0 0 newgroupname1
14 0 0 newgroupname2
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)][ValidateNotNullOrEmpty()][Alias("HostGroupName")]
# The name of the new group (one or more separated by commas)
[string[]] $Name
)
begin
{
$prms = @()
}
process
{
$Name |% { $prms += @{name = $_} }
}
end
{
if ($prms.Count -eq 0) { return }
$r = Invoke-ZabbixApi $session "hostgroup.create" $prms
Get-HostGroup -Session $s -Id $r.groupids
}
}
function Remove-HostGroup
{
<#
.SYNOPSIS
Remove one or more host groups from Zabbix.
.DESCRIPTION
Removal is immediate.
.INPUTS
This function accepts ZabbixHostGroup objects or host group IDs from the pipe. Equivalent to using -HostGroupId parameter.
.OUTPUTS
The ID of the removed objects.
.EXAMPLE
Remove all groups
PS> Get-ZbxHostGroup | Remove-ZbxHostGroup
10084
10085
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)][ValidateNotNullOrEmpty()][Alias("Id", "GroupId")]
# ID of one or more groups to remove. You can also pipe in objects with an "ID" of "groupid" property.
[int[]]$HostGroupId
)
begin
{
$prms = @()
}
process
{
$prms += $HostGroupId
}
end
{
if ($prms.Count -eq 0) { return }
Invoke-ZabbixApi $session "hostgroup.delete" $prms | select -ExpandProperty groupids
}
}
#endregion
################################################################################
## USER GROUPS
################################################################################
Add-Type -TypeDefinition @"
public enum ZbxGuiAccess
{
WithDefaultAuthenticationMethod = 0,
WithInternalAuthentication = 1,
Disabled = 2
}
"@
function Get-UserGroup
{
<#
.SYNOPSIS
Retrieve and filter user groups.
.DESCRIPTION
Query all user groups with basic filters, or get all user groups.
.INPUTS
This function does not take pipe input.
.OUTPUTS
The ZabbixUserGroup objects corresponding to the filter.
.EXAMPLE
PS> Get-ZbxUserGroup "*admin*"
usrgrpid count name
-------- ----- ----
7 1 Zabbix administrators
.EXAMPLE
PS> Get-ZbxUserGroup
usrgrpid count name
-------- ----- ----
7 1 Zabbix administrators
8 4 Guests
9 0 Disabled
.EXAMPLE
PS> Get-ZbxUserGroup -Id 7
usrgrpid count name
-------- ----- ----
7 1 Zabbix administrators
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,
[Parameter(Mandatory=$False)][Alias("UserGroupId", "UsrGrpId")]
# Only retrieve the usergroup with the given ID
[int[]] $Id,
[Parameter(Mandatory=$False)]
# Only retrieve groups which contain the given users
[int[]] $UserId,
[Parameter(Mandatory=$False, Position=0)][Alias("UserGroupName")]
# Filter by name. Accepts wildcard.
[string] $Name
)
$prms = @{searchWildcardsEnabled=1; selectUsers= 1; selectRights = 1; search= @{}}
if ($Id.Length -gt 0) {$prms["usrgrpids"] = $Id}
if ($UserId.Length -gt 0) {$prms["userids"] = $UserId}
if ($Name -ne $null) {$prms["search"]["name"] = $Name}
Invoke-ZabbixApi $session "usergroup.get" $prms |% {$_.usrgrpid = [int]$_.usrgrpid; $_.users_status = [ZbxStatus]$_.users_status; $_.debug_mode = [ZbxStatus]$_.debug_mode; $_.PSTypeNames.Insert(0,"ZabbixUserGroup"); $_}
}
function New-UserGroup
{
<#
.SYNOPSIS
Create a new user group.
.DESCRIPTION
Create a new user group.
.INPUTS
This function accepts a ZabbixUserGroup as pipe input, or any object which properties map the function parameters.
.OUTPUTS
The ZabbixUserGroup object created.
.EXAMPLE
PS> New-ZbxUserGroup "newgroupname1","newgroupname2"
usrgrpid count name
-------- ----- ----
7 1 newgroupname1
8 1 newgroupname2
.EXAMPLE
PS> "newgroupname1","newgroupname2" | New-ZbxUserGroup
usrgrpid count name
-------- ----- ----
7 1 newgroupname1
8 1 newgroupname2
#>
param
(
[Parameter(Mandatory=$False)]
# A valid Zabbix API session retrieved with New-ZbxApiSession. If not given, the latest opened session will be used, which should be enough in most cases.
[Hashtable] $Session,