-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzabbixPoshAPI.psm1
More file actions
1609 lines (1474 loc) · 54.7 KB
/
zabbixPoshAPI.psm1
File metadata and controls
1609 lines (1474 loc) · 54.7 KB
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
function getJSON($url = $ApiURL, $object) {
try {
$bytes = [System.Text.Encoding]::ASCII.GetBytes($object)
$web = [System.Net.WebRequest]::Create($url)
$web.Method = “POST”
$web.ContentLength = $bytes.Length
$web.ContentType = “application/json”
$stream = $web.GetRequestStream()
$stream.Write($bytes,0,$bytes.Length)
$stream.close()
$reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
return $reader.ReadToEnd()| ConvertFrom-Json
$reader.Close()
} catch {
$err_msg = "ERROR: [getJSON] Unable to process request. Make sure API server is available. Exception details:`nException: $($_.Exception)`nLine: $($_.InvocationInfo.ScriptLineNumber)`nOffset: $($_.InvocationInfo.OffsetInLine)"
#sendMail $errors_emails "[$server] Error from $scriptname" $err_msg
write-host ($err_msg) -ForeGroundColor Red; Break
}
}
function evaluate-JSON {
<#
.Synopsis
Send a JSON object to a Web server and evaluates the response
.Example
evaluate-JSON -jsonApiUrl "http://mywebservice/api" -jsonObj $objHost -errorMsg "[get-ZabbixHost] : Unable to retrieve hosts, aborting." -noDataMsg "[get-ZabbixHost] : No host matching the description"
.Parameter jsonApiUrl
URL of the JSON web service
.Parameter jsonObj
JSON request object that will be sent to Web Server
.Parameter errorMsg
Message to be displayed in -verbose mode if an error occured, should allow to easily determine the function and the problem
.Parameter noDataMsg
Message to be displayed in -verbose mode if the request is correct but did not return data
.Notes
NAME: evaluate-JSON
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True,
HelpMessage="URL of the JSON web service")]
[string]$jsonApiUrl
,
[Parameter(Mandatory=$True,
HelpMessage="JSON object to evaluate")]
[object]$jsonObj
,
[Parameter(Mandatory=$False,
HelpMessage="Message to display is error is encountered")]
[String]$errorMsg
,
[Parameter(Mandatory=$False,
HelpMessage="Message to display is no data is returned")]
[String]$noDataMsg
)
Process {
$getJSON = getJSON $jsonApiURL $jsonObj
if ($getJSON.error){
$error =$getJSON.error.data
Write-Verbose "$errorMsg. Error message is $error "
return $False
}
elseif ($getJSON.result.count -eq 0) {
Write-Verbose "$noDataMsg "
return $false
}
else {
return $getJSON
}
}
}
function connect-Zabbix {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$True
,HelpMessage="Zabbix credentials, must have API and dashboard access")]
[PSCredential]$zabbixCredential
,
[Parameter(Mandatory=$True
,HelpMessage="Zabbix API URL, usually http://zabbix-server-name/zabbix/api_jsonrpc.php")]
[string]$zabbixApiURL
)
Process {
$zabbixApiPasswd = $zabbixCredential.getNetworkCredential().password
$zabbixApiUser = $zabbixCredential.userName
$global:session = get-ZabbixSession $zabbixApiUser $zabbixApiPasswd $zabbixApiURL
if (!$session.result) {
Write-verbose "[connect-Zabbix] : Unable to connect to Zabbix, aborting"
Remove-Variable session -Scope global
return $false
}
else {
Write-verbose "[connect-Zabbix] : Connection to Zabbix is successfull"
$zabbixAPIVersion = get-zabbixAPIInfo -zabbixApiURL $zabbixApiURL
$session | Add-Member -MemberType NoteProperty -Name "zabbixAPIVersion" -Value $zabbixAPIVersion.result
$session | Add-Member -MemberType NoteProperty -Name "zabbixApiURL" -Value $zabbixApiURL
return $session
}
}
}
function get-zabbixAPIInfo {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$True
,HelpMessage="Zabbix API URL, usually http://zabbix-server-name/zabbix/api_jsonrpc.php")]
[string]$zabbixApiURL
)
Process {
#construct the JSON object
$objAPIInfo = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'apiinfo.version' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#return $objGraphItem
#make the request and evaluate it
evaluate-JSON -jsonApiUrl $zabbixApiURL -jsonObj $objAPIInfo -errorMsg "[get-zabbixAPIInfo] : Unable to retrieve API info, aborting." -noDataMsg "[get-zabbixAPIInfo] : No result returned"
}
}
#Function to setup session and return.
function get-ZabbixSession($zabbixApiUser, $zabbixApiPasswd, $zabbixApiURL) {
#Create authentication JSON object using ConvertTo-JSON
$objAuth = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method ‘user.login’ |
Add-Member -PassThru NoteProperty params @{user=$zabbixApiUser;password=$zabbixApiPasswd} |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
return getJSON $zabbixApiURL $objAuth
}
function get-ZabbixUser {
<#
.Synopsis
This function retrieves all users or a single user, depending if the -username parameter is used
.Example
get-ZabbixUser
get-ZabbixUser -userName smorand
.Parameter userName
provide a users alias to limit the scope of the search, not mandatory
.Notes
NAME: get-ZabbixUser
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False
,HelpMessage="Provide a user alias to limit the scope of the search")]
[String]$userName
)
Process {
if ($userName) {
$filter= @{"alias" = "$userName"}
$objUser = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'user.get' |
Add-Member -PassThru NoteProperty params @{output="extend";filter=$filter} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
else {
$objUser = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'user.get' |
Add-Member -PassThru NoteProperty params @{output="extend"} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
return getJSON $session.zabbixApiURL $objUser
}
}
function get-ZabbixHost {
<#
.Synopsis
Retrieves Zabbix hosts based on several search options
.Example
get-ZabbixHost
get-ZabbixHost -hostName "myhost"
get-ZabbixHost -hostGroupName "mygroup"
get-ZabbixHost -hostPattern "bdd"
get-ZabbixHost -hostGroupName "linuxgroup" -hostPattern "zabbixserver"
.Parameter hostName
provide a host name to limit the scope of the search, not mandatory
.Parameter hostGroupName
provide a host group name pattern to limit the scope of the search to that group, not compatible with -hostname
.Parameter hostPattern
Limit the scope of the search to hosts with names that match the pattern, not compatible with -hostname, not mandatory
.Parameter short
Returns only host ids
.Parameter sort
sort by host name
.Parameter selectGroup
Return the host groups that the host belongs to in the groups property
.Notes
NAME: get-ZabbixHost
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="hostname",
Mandatory=$False,
HelpMessage="Provide a host name to limit the scope of the search")]
[String]$hostName
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$False,
HelpMessage="provide a host name pattern to limit the scope of the search to that pattern.Could be a list sperated by , not compatible with -hostName")]
[String] $hostPattern
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$False,
HelpMessage="provide a host group name pattern to limit the scope of the search to that group, only compatible with host pattern")]
[String] $hostGroupName
,
[Parameter(Mandatory=$False,
HelpMessage="Return the host groups that the host belongs to in the groups property, not mandatory")]
[Switch] $selectGroups
,
[Parameter(Mandatory=$False,
HelpMessage="short version, returns only hostids. Usefull because JSON input is limited to 2MB in size... :( , not mandatory")]
[Switch] $short
,
[Parameter(Mandatory=$False,
HelpMessage="sort by host name, not mandatory")]
[Switch] $sort
)
Process {
#construct the params
$params=@{}
#construct the "groupids param"
if ($hostGroupName) {
#get the group name id and check that there is only one! (i dont know how to use several ids yet...)
$groupid=get-ZabbixHostGroup -hostGroupName $hostGroupName
if ($groupid) {
#if only one group is returned we're good
if($groupid.result.count -eq 1) {
$params.add("groupids",$groupid.result.groupid)
}
#if more than 1, we can't use that
else {
Write-verbose "[get-ZabbixHost] : More than one group returned, aborting. Number of groups returned : $groupsids.result.count"
return $false
}
}
else {
return $false
}
}
#construct the "search description" param
if ($hostPattern) {
$search=@{}
$search.add("host", $hostPattern)
$params.add("search",$search)
}
#construct the "filter host" param
if ($hostName) {
$filter=@{}
$filter= @{"host" = "$hostName"}
$params.add("filter",$filter)
}
#finish the params
if ($short) {$params.add("output", "shorten")}
else {$params.add("output", "extend") }
if ($sort) {$params.add("sortfield","host") }
if ($selectGroups) {$params.add("select_groups","refer")}
#construct the JSON object
$objHost = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'host.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#return $objHost
#make the request and evaluate it
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objHost -errorMsg "[get-ZabbixHost] : Unable to retrieve hosts, aborting." -noDataMsg "[get-ZabbixHost] : No host matching the description"
}
}
function get-ZabbixHostGroup {
<#
.Synopsis
This function retrieves all host groups or a single host group, depending if the -hostGroupName parameter is used
.Example
get-ZabbixHostGroup
get-ZabbixHostGroup -hostGroupName smorand
.Parameter hostGroupName
provide a host group name to limit the scope of the search, not mandatory
.Notes
NAME: get-ZabbixHostGroup
AUTHOR: Simon Morand (MBVSI)
#>
Param(
[Parameter(Mandatory=$False
,HelpMessage="Provide a host group name to limit the scope of the search")]
[String]$hostGroupName
)
Process {
if ($hostGroupName) {
$filter= @{"name" = "$hostGroupName"}
$objHostGroup = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'hostgroup.get' |
Add-Member -PassThru NoteProperty params @{output="extend";filter=$filter} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
else {
$objHostGroup = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'hostgroup.get' |
Add-Member -PassThru NoteProperty params @{output="extend"} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
$getJSON = getJSON $session.zabbixApiURL $objHostGroup
if (!$getJSON.result){
Write-Verbose "[get-ZabbixHostGroup] : Unable to retrieve host group $hostgroupname, aborting"
return $false
}
else {return $getJSON }
}
}
#### Here are a few made by JP #####
function Export-ZabbixHost {
<#
.Synopsis
This function export host XML configuration
.Example
export-ZabbixHost
export-ZabbixHost -hostID 10161
.Parameter hostID
ID number of host, can be retreived by Get-ZabbixHost -HostName command
.Notes
NAME: Export-ZabbixHost
AUTHOR: JanPaul Klompmaker
#>
Param(
[Parameter(Mandatory=$True
,HelpMessage="Provide a host id")]
[String]$hostID
,
[Parameter(Mandatory=$False
,HelpMessage="Output file")]
[String]$output
)
Process {
if ($hostID) {
$filter= @{"hosts" = $hostID}
$objHostID = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'configuration.export' |
Add-Member -PassThru NoteProperty params @{format="xml";options=$filter} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '1') | ConvertTo-Json
}
$export = evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objHostID -errorMsg "[get-ZabbixHostbyID] : Unable to retrieve host group $objHostID, aborting." -noDataMsg "[get-ZabbixHostbyID] : No host group matching the description"
if ($output) {
$output = [xml]$export.result
$output.zabbix_export | Out-File $output # doesnt work yet?!?!?!?!
} else {
[xml]$export.result
}
}
}
function get-ZabbixTemplate {
<#
.Synopsis
This function retrieves all templates or a singletemplate, depending if the -templateName parameter is used
.Example
get-ZabbixTemplate
get-ZabbixTemplate -templateName Linux-Server
.Parameter templateName
provide a host group name to limit the scope of the search, not mandatory
.Notes
NAME: get-ZabbixTemplate
AUTHOR: Simon Morand (MBVSI)
#>
Param(
[Parameter(Mandatory=$False
,HelpMessage="Provide a host group name to limit the scope of the search")]
[String]$templateName
)
Process {
if ($templateName) {
$filter= @{"host" = "$templateName"}
$objTemplate = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'template.get' |
Add-Member -PassThru NoteProperty params @{output="extend";filter=$filter} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
else {
$objTemplate = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'template.get' |
Add-Member -PassThru NoteProperty params @{output="extend"} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
$getJSON = getJSON $session.zabbixApiURL $objTemplate
if (!$getJSON.result){
Write-Verbose "[get-ZabbixTemplate] : Unable to retrieve host group $templateName, aborting"
return $false
}
else {return $getJSON }
}
}
function create-ZabbixHost {
<#
.Synopsis
This function creates a host in Zabbix
.Example
create-ZabbixHost -hostName myhost -hostFQDN myhost.lan -hostGroupName Linux-Group -templateName Template-Linux -proxyName myproxy
.Parameter hostName
Name of the host to be created, mandatory
.Parameter hostFQDN
FQDN of the host to be created, mandatory
.Parameter hostGroupName
provide a host group name to limit the scope of the search, mandatory
.Parameter templateName
provide a host group name to limit the scope of the search, mandatory
.Parameter proxyName
Proxy that will monitor the host, mandatory
.Notes
NAME: create-ZabbixHost
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$true,
HelpMessage="Name of the host to be created")]
[String] $hostName
,
[Parameter(Mandatory=$true,
HelpMessage="FQDN of the host to be created")]
[String] $hostFQDN
,
[Parameter(Mandatory=$true,
HelpMessage="Host Group in which to place the host")]
[String] $hostGroupName
,
[Parameter(Mandatory=$true,
HelpMessage="Template to link the host with")]
[String] $templateName
,
[Parameter(Mandatory=$true,
HelpMessage="Proxy that will monitor the host")]
[String] $proxyName
)
Process
{
#retrieve proxy info
$proxy = get-ZabbixProxy -proxyName $proxyName
if (!$proxy) {
Write-InRed "Unable to retrieve proxy ID, aborting"
return $false
}
$proxyID = $proxy.result.proxyid
$proxy = @{"proxyid" = "$proxyID"}
#retrieve template info
$template = get-ZabbixTemplate -templateName $templateName
if (!$template) {
Write-InRed "Unable to retrieve template ID, aborting"
return $false
}
$templateID = $template.result.hostid
$templates = @{"templateid" = "$templateID"}
#retrieve host group info
$hostgroup = get-ZabbixHostGroup -hostGroupName $hostGroupName
if (!$hostgroup) {
Write-InRed "Unable to retrieve host group ID, aborting"
return $false
}
$hostGroupID = $hostgroup.result.groupid
$groups = @{"groupid" = "$hostGroupID"}
#create the json object
$objHost = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'host.create' |
Add-Member -PassThru NoteProperty params @{host=“$hostName”;dns="$hostFQDN";groups=$groups;templates=$templates;proxy_hostid=$proxyID} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#make the request and evaluate it
$getJSON = getJSON $session.zabbixApiURL $objHost
if (!$getJSON.result){
$Error =$getJSON.error.data
Write-InRed "Unable to create host $hostName, aborting. Error message is : $error"
return $false
}
else {
Write-Verbose "[create-ZabbixHost] : Host $hostName created successfully!"
return $true
}
}
}
function get-ZabbixProxy {
<#
.Synopsis
This function retrieves all proxy or a single one, depending if the -proxyName parameter is used
.Example
get-ZabbixProxy
get-ZabbixProxy -proxyName zabprox10
.Parameter proxyName
provide a proxy name to limit the scope of the search, not mandatory
.Notes
NAME: get-ZabbixProxy
AUTHOR: Simon Morand (MBVSI)
#>
Param(
[Parameter(Mandatory=$False
,HelpMessage="Provide a host proxy name to limit the scope of the search")]
[String]$proxyName
)
Process {
if ($proxyName) {
$search= @{"host" = "$proxyName"}
$objProxy = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'proxy.get' |
Add-Member -PassThru NoteProperty params @{output="extend";search=$search} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
else {
$objProxy = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'template.get' |
Add-Member -PassThru NoteProperty params @{output="extend"} |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
}
$getJSON = getJSON $session.zabbixApiURL $objProxy
if (!$getJSON.result){
Write-Verbose "[get-ZabbixProxy] : Unable to retrieve host group $proxyName, aborting"
return $false
}
else {return $getJSON }
}
}
function get-ZabbixItem {
<#
.Synopsis
Retrieves items, you can search for host/hostgroup and item description
.Example
get-ZabbixItem
get-zabbixItem -short
get-zabbixItem -itemDescription "CPU Load" -hostName "host1.lan"
get-zabbixItem -itemDesciption "CPU Load" -hostGroupName "Group 1"
get-zabbixItem -hostGroupName "Group 1" -hostPattern "apache"
.Parameter itemDescription
provide an item description pattern to limit the scope of the search, not mandatory
.Parameter hostName
provide a host name to limit the scope of the search, not compatible with -hostGroupName and -hostPattern, not mandatory
.Parameter hostID
provide a host id to limit the scope of the search, not compatible with -hostGroupName nor -hostName, not mandatory
.Parameter hostGroupName
provide a host group name to limit the scope of the search, not compatible with -hostName, not mandatory
.Parameter hostPattern
provide a host name pattern to limit the scope of the search to that patter, not compatible with -hostName, not mandatory
.Parameter short
short version, returns only itemids. Usefull because JSON input is limited to 2MB in size... :(
.Notes
NAME: get-ZabbixItem
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False,
HelpMessage="provide an item description pattern to limit the scope of the search")]
[String] $itemDescription
,
[Parameter(ParameterSetName="hostname",
Mandatory=$False,
HelpMessage="provide a host name to limit the scope of the search, not compatible with -hostGroupName and -hostPattern")]
[String] $hostName
,
[Parameter(ParameterSetName="hostid",
Mandatory=$False,
HelpMessage="provide a host id to limit the scope of the search, not compatible with -hostGroupName nor -hostName")]
[String] $hostId
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$False,
HelpMessage="provide a host group name to limit the scope of the search, not compatible with -hostName")]
[String] $hostGroupName
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$False,
HelpMessage="provide a host name pattern to limit the scope of the search to that patter, not compatible with -hostName")]
[String] $hostPattern
,
[Parameter(Mandatory=$False,
HelpMessage="short version, returns only itemids. Usefull because JSON input is limited to 2MB in size... :( ")]
[Switch] $short
)
Process{
#construct the params
$params=@{}
#contruct the "hostids" param.
#Fist : let's check if we have some parameters that imply a second request
#Because "search group and filter on host name" technique doesnt seem to work, we provide a list of host ids
if ( ($hostGroupName) -and ($hostPattern) ) {
$hosts=get-ZabbixHost -hostGroupName $hostGroupName -hostPattern $hostPattern -short
$query=1
}
elseif ($hostGroupName) {
$hosts=get-ZabbixHost -hostGroupName $hostGroupName -short
$query=1
}
elseif ($hostPattern) {
$hosts=get-ZabbixHost -hostPattern $hostPattern -short
$query=1
}
#if any host filtering is asked and a proper list of host has been returned by get-zabbixhost, format the "hostids" param
if ( ($query -eq 1) -and ($hosts) ) {
$hostids=@()
foreach ($hostid in $hosts.result)
{
$hostids += $hostid.hostid
}
$params.add("hostids",$hostids)
}
#Then let's look for some parameters that do not imply a second request (-hostId or -hostName)
if ($hostId) {
$params.add("hostids",$hostId)
}
elseif ($hostName) {$params.add("host",$hostName)}
#construct the "search description" param
if ($itemDescription) {
$search=@{}
$search.add("description", $itemDescription)
$params.add("search",$search)
}
#finish the params
if ($short) {$params.add("output", "shorten")}
else {$params.add("output", "extend") }
#construct the JSON object
$objitem = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'item.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#return $objitem
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objitem -errorMsg "[get-ZabbixItem] : Unable to retrieve items, aborting"
#>
}
}
function create-zabbixGraph {
<#
.Synopsis
Create a graph in Zabbix with items matching a description, with hosts from a host group , optionally you can specify a host pattern. Very specific I know..;but it does the trick for me
.Example
create-zabbixGraph -graphName "Linux servers CPU Load" -itemDescription "CPU Load" -hostGroupName "Linux servers"
create-zabbixGraph -graphName "Apache servers CPU Load" -itemDescription "CPU Load" -hostGroupName "Liunx servers" -hostPattern "Apache"
.Parameter graphName
provide a name for the Grpah, mandatory
.Parameter hostGroupName
provide a host group name to limit the scope of the search
.Parameter itemDescription
provide an item description pattern to limit the scope of the search, mandatory
.Parameter hostPattern
provide a host name pattern to limit the scope of the search to that pattern
.Notes
NAME: create-ZabbixGraph
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True,
HelpMessage="provide a name for the Grpah")]
[String] $graphName
,
[Parameter(Mandatory=$True,
HelpMessage="provide an item description pattern to limit the scope of the search")]
[String] $itemDescription
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$True,
HelpMessage="provide a host group name to limit the scope of the search")]
[String] $hostGroupName
,
[Parameter(ParameterSetName="hostgroup",
Mandatory=$False,
HelpMessage="provide a host name pattern to limit the scope of the search to that pattern")]
[String] $hostPattern
)
Process {
#create a list of colours to be used in the graph
$colours=@("009900", "000099", "666666", "990000", "009999", "990099", "00EE00",
"3333FF", "FF3333", "EE00EE", "FFFF33", "CCCCCC", "000066", "C04000", "800000",
"191970", "3EB489", "FFDB58", "000080", "CC7722","808000", "FF7F00", "002147",
"AEC6CF", "836953", "CFCFC4", "77DD77", "F49AC2","FFB347", "FFD1DC", "B39EB5",
"FF6961", "CB99C9", "FDFD96", "FFE5B4", "D1E231","8E4585", "FF5A36", "701C1C",
"FF7518", "69359C", "E30B5D", "826644", "FF0000","414833", "65000B", "002366",
"E0115F", "B7410E", "FF6700", "F4C430", "FF8C69","C2B280", "967117", "ECD540",
"082567" )
#SO, in the end we want a graph with a list of items sorted by host name...no easy way to do this...
#1st, get a sorted list of hosts
if ($hostPattern) {
$hosts = get-ZabbixHost -hostGroupName $hostGroupName -hostPattern $hostPattern -short -sort
}
else {
$hosts = get-ZabbixHost -hostGroupName $hostGroupName -short -sort
}
if (!$hosts) {
Write-Error "No hosts returned, aborting"
return $false
}
#then loop through the hosts, get the item(s) and add them to the list
$params=@{}
$gitems=@()
$c=0 #index for the $colours array
foreach ($h in $hosts.result) {
$items = get-ZabbixItem -hostId $h.hostid -itemDescription $itemDescription -short
if ($items.result) {
foreach ($itemid in $items.result) {
$gitem=@{}
$gitem.add("itemid", $itemid.itemid)
$gitem.add("color", $colours[$c])
$gitem.add("yaxisside", "0")
$gitems += $gitem
$c += 1
}
}
else {Write-debug "[create-zabbixGraph] : no items matching $itemdescription, continuing"}
}
$params.add("gitems", $gitems)
$params.add("name", $graphName)
$params.add("width", "900")
$params.add("height", "200")
#construct the JSON object
$objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graph.create' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json -depth 4
#return $objgraph
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objgraph -errorMsg "[create-zabbixgraph] : Unable to create graph, aborting"
}
}
function get-ZabbixGraphItem {
<#
.Synopsis
Retrieves Zabbix graph items
.Example
get-zabbixGraphItem -graphID 123456
get-zabbixGraphItem -graphID 123456 -short -sort
.Parameter graphId
Provide a single graph id to limit the scope of the search, mandatory
.Parameter short
Returns only item ids, not mandatory
.Parameter sort
sort by host name, not mandatory
.Notes
NAME: get-ZabbixGraphItem
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="graphid",
Mandatory=$True,
HelpMessage="provide a single graph id to limit the scope of the search, mandatory")]
[String]$graphId
,
[Parameter(Mandatory=$False,
HelpMessage="short version, returns only item ids. Usefull because JSON input is limited to 2MB in size... :( ")]
[Switch] $short
,
[Parameter(Mandatory=$False,
HelpMessage="sort by host name")]
[Switch] $sort
)
Process {
#construct the params
$params=@{}
$params.add("graphids", $graphId)
if ($short) {$params.add("output", "shorten")}
else {$params.add("output", "extend") }
if ($sort) {$params.add("sortfield","gitemid") }
#construct the JSON object
$objGraphItem = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graphitem.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#make the request and evaluate it
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objGraphItem -errorMsg "[get-zabbixGraphItem] : Unable to retrieve hosts, aborting." -noDataMsg "[get-zabbixGraphItem
] : No host matching the description"
}
}
function get-ZabbixGraph {
<#
.Synopsis
Retrieves Zabbix graphs, deprecated, you should use get-zabbixGrpahByHost, get-ZabbixGraphByID, get-ZabbixGraphByGroup instead
.Example
get-zabbixGraph -graphID 123456
get-zabbixGraph -hostID 123456
get-zabbixGraph -hostName "myhost.lan"
get-zabbixGrpah -graphName "CPU Load" -hostName "apache01.lan"
.Parameter graphId
provide a single graph id to limit the scope of the search, not compatible with -hostId nor -hostName
.Parameter graphName
provide a single graph name to limit the scope of the search, not mandatory
.Parameter hostID
provide a single host id to limit the scope of the search, not compatible with -graphId nor -hostName
.Parameter hostName
provide a single host name to limit the scope of the search, not compatible with -hostId nor -graphId
.Parameter short
Returns only item ids
.Notes
NAME: get-ZabbixGraph
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="retrieveFromGraphId",
Mandatory=$False,
HelpMessage="provide a single graph id to limit the scope of the search, not compatible with -hostId nor -hostName")]
[String]$graphId
,
[Parameter(ParameterSetName="filterByHostId",
Mandatory=$False,
HelpMessage="provide a single host id to limit the scope of the search, not compatible with -graphId nor -hostName")]
[String]$hostId
,
[Parameter(ParameterSetName="filterByHostName",
Mandatory=$False,
HelpMessage="provide a single host name to limit the scope of the search, not compatible with -hostId nor -graphId")]
[String]$hostName
,
[Parameter(Mandatory=$False,
HelpMessage="provide a single graph name to limit the scope of the search, not mandatory")]
[String]$graphName
,
[Parameter(Mandatory=$False,
HelpMessage="short version, returns only item ids. Usefull because JSON input is limited to 2MB in size... :( ")]
[Switch] $short
)
Process {
#construct the params
$params=@{}
if ($grapId) { $params.add("graphids", $graphId) }
elseif ($groupId) { $params.add("groupids", $groupId) }
elseif ($hostId) { $params.add("hostids", $hostId) }
elseif ($hostName) {
$h=get-ZabbixHost -hostName $hostName
if ($h) {
$params.add("hostids",$h.result.hostid)
}
else { Write-Verbose "[get-ZabbixGraph] : no host name matching, ignoring this parameter" }
}
elseif ($graphName) {
$filter=@{}
$filter= @{"name" = "$graphName"}
$params.add("filter",$filter)
}
if ($short) {$params.add("output", "shorten")}
else {$params.add("output", "extend") }
#construct the JSON object
$objGraphItem = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graph.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#return $objGraphItem
#make the request and evaluate it
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objGraphItem -errorMsg "[get-zabbixGraph] : Unable to retrieve graph, aborting." -noDataMsg "[get-zabbixGraph] : No graph matching the description"
}
}
function get-ZabbixGraphByID {
<#
.Synopsis
Retrieves a Zabbix graph from it's ID
.Example
get-zabbixGrpahByID -graphID 123456
.Parameter graphID
provide a single graph id to limit the scope of the search, mandatory
.Notes
NAME: get-ZabbixGraphByID
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="hostId",
Mandatory=$True,
HelpMessage="provide a single graph id to limit the scope of the search, mandatory")]
[String]$graphId
)
Process {
#construct the params
$params=@{}
$params.add("graphids", $graphId)
$params.add("output", "extend")
#construct the JSON object
$objGraphItem = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'graph.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
#return $objGraphItem
#make the request and evaluate it
evaluate-JSON -jsonApiUrl $session.zabbixApiURL -jsonObj $objGraphItem -errorMsg "[get-zabbixGraphByID] : Unable to retrieve graph, aborting." -noDataMsg "[get-zabbixGraph] : No graph matching the description"
}
}
function get-ZabbixGraphByHost {
<#
.Synopsis
Retrieves Zabbix graphs for a specific host
.Example
get-zabbixGraphByHost -hostID 123456
get-zabbixGraphByHost -hostID 123456 -graphDescription "CPU"
get-zabbixGraphByHost -hostName "myhost.lan"
get-zabbixGraphByHost -hostName "myhost.lan" -graphDescription "CPU"
get-zabbixGraphByHost -hostName "myhost.lan" -short
.Parameter hostID
provide a single host id to limit the scope of the search, not compatible with -hostName, mandatory
.Parameter hostName
provide a single host name to limit the scope of the search, not compatible with -hostId, mandatory
.Parameter graphDescription
provide a single graph name to limit the scope of the search. If the description has no match, Zabbix API will ignore it and return all graphs! not mandatory
.Parameter short
Returns only item ids
.Notes
NAME: get-ZabbixGraphByHost
AUTHOR: Simon Morand (MBVSI)
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="hostId",
Mandatory=$True,
HelpMessage="provide a single host id to limit the scope of the search, not compatible with -hostName")]
[String]$hostId
,
[Parameter(ParameterSetName="hostName",
Mandatory=$True,
HelpMessage="provide a single host name to limit the scope of the search, not compatible with -hostId")]
[String]$hostName
,
[Parameter(Mandatory=$False,
HelpMessage="provide a single graph name to limit the scope of the search, not mandatory")]
[String]$graphDescription
,
[Parameter(Mandatory=$False,
HelpMessage="short version, returns only item ids. Usefull because JSON input is limited to 2MB in size... :( ")]
[Switch] $short
)
Process {
#construct the params
$params=@{}
#depending on the parameter set we might need an additional request to get the host id from the name
if ($hostId)
{ $params.add("hostids", $hostId) }