Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.66 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Generic Functions for parsing Json to psobjects because convertfrom-json has a 2meg limit. If anyone knows a way around this problem get in touch with me.
  4. .EXAMPLE
  5. none don't bother with this function unless you really need it.
  6.  
  7. #>
  8. # Loading dependencies for this json parser. The lib that's in play is System.Web.Extensions.dll so if you don't have it on your system you need to add it.
  9. # TODO: package the lib with the module for now I have it already.
  10.  
  11. $assemblyload = [System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
  12. $global:javaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
  13. $global:javaScriptSerializer.MaxJsonLength = [System.Int32]::MaxValue
  14. $global:javaScriptSerializer.RecursionLimit = 99
  15. function ParseItem($jsonItem) {
  16. if($jsonItem.PSObject.TypeNames -match "Array") {
  17. return ParseJsonArray($jsonItem)
  18. }
  19. elseif($jsonItem.PSObject.TypeNames -match "Dictionary") {
  20. return ParseJsonObject([HashTable]$jsonItem)
  21. }
  22. else {
  23. return $jsonItem
  24. }
  25. }
  26. <#
  27. .SYNOPSIS
  28. Generic Functions for parsing Json to psobjects because convertfrom-json has a 2meg limit. If anyone knows a way around this problem get in touch with me.
  29. .EXAMPLE
  30. none don't bother with this function unless you really need it.
  31.  
  32. #>
  33. function ParseJsonObject($jsonObj) {
  34. $result = New-Object -TypeName PSCustomObject
  35. foreach ($key in $jsonObj.Keys) {
  36. $item = $jsonObj[$key]
  37. if ($item) {
  38. $parsedItem = ParseItem $item
  39. } else {
  40. $parsedItem = $null
  41. }
  42. $result | Add-Member -MemberType NoteProperty -Name $key -Value $parsedItem
  43. }
  44. return $result
  45. }
  46. <#
  47. .SYNOPSIS
  48. Generic Functions for parsing Json to psobjects because convertfrom-json has a 2meg limit. If anyone knows a way around this problem get in touch with me.
  49. .EXAMPLE
  50. none don't bother with this function unless you really need it.
  51.  
  52. #>
  53. function ParseJsonArray($jsonArray) {
  54. $result = @()
  55. $jsonArray | ForEach-Object {
  56. $result += , (ParseItem $_)
  57. }
  58. return $result
  59. }
  60. <#
  61. .SYNOPSIS
  62. Generic Functions for parsing Json to psobjects because convertfrom-json has a 2meg limit. If anyone knows a way around this problem get in touch with me.
  63. .EXAMPLE
  64. none don't bother with this function unless you really need it.
  65.  
  66. #>
  67. function ParseJsonString($json) {
  68. $config = $javaScriptSerializer.DeserializeObject($json)
  69. return ParseJsonObject($config)
  70. }
  71.  
  72. <#
  73. .SYNOPSIS
  74. This cmdlet returns a json to use with the zabbix api
  75. .EXAMPLE
  76. JsonConstructor -method "user.login" -params @{user=$zabbixApiUser;password=$zabbixApiPasswd}
  77.  
  78. #>
  79. function JsonConstructor ($params,$token,$method)
  80. {
  81. if ($method -eq 'user.login') #Total Hack because you can't give the auth value in an api call with zabbix 2.4 for a login.
  82. {
  83. $objJson = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
  84. Add-Member -PassThru NoteProperty method $method |
  85. Add-Member -PassThru NoteProperty params $params |
  86. Add-Member -PassThru NoteProperty id '1'
  87. )
  88. return $objJson | ConvertTo-Json -Depth 3
  89. }
  90. else
  91. {
  92. $objJson = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
  93. Add-Member -PassThru NoteProperty method $method |
  94. Add-Member -PassThru NoteProperty params $params |
  95. Add-Member -PassThru NoteProperty id '2' |
  96. Add-Member -PassThru NoteProperty auth $token
  97. )
  98. return $objJson | ConvertTo-Json -Depth 3
  99. }
  100. }
  101. <#
  102. .SYNOPSIS
  103. Generic WebRequest function accepts json and zabbix host as a parameter
  104. .EXAMPLE
  105. none
  106.  
  107. #>
  108.  
  109. function WebRequest ($jsonCmd,$zabbixHost) # genereic webrequest method had to do it this way because of SSL errors.
  110. {
  111. #// use this code when you have SSL warnings.
  112. add-type @"
  113. using System.Net;
  114. using System.Security.Cryptography.X509Certificates;
  115. public class TrustAllCertsPolicy : ICertificatePolicy {
  116. public bool CheckValidationResult(
  117. ServicePoint srvPoint, X509Certificate certifi$toecate,
  118. WebRequest request, int certificateProblem) {
  119. return true;
  120. }
  121. }
  122. "@
  123. [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
  124. $request = Invoke-WebRequest -Uri $zabbixHost -Method POST -Body $jsonCmd -ContentType "application/json"
  125. $response = ParseJsonString $request.content
  126. return $response
  127.  
  128. }
  129.  
  130. <#
  131. .SYNOPSIS
  132. This cmdlet returns a valid auth token works with the api version 2.0 >
  133. .EXAMPLE
  134. get-zabbixToken -zabbixApiUser Admin -zabbixApiPasswd Zabbix -zabbixApiURL https://zabbix.mydomain.com.
  135.  
  136. #>
  137.  
  138. Function Get-ZabbixToken ([string]$zabbixApiURL,[string]$zabbixApiUser,[string]$zabbixApiPasswd)
  139. {
  140. $method = "user.login"
  141. $params = @{user=$zabbixApiUser;password=$zabbixApiPasswd}
  142. $objAuth = JsonConstructor -method $method -params $params
  143. $result = webRequest $objAuth $zabbixApiURL
  144. return $result.result
  145. }
  146.  
  147. <#
  148. .SYNOPSIS
  149. This Cmdlet returns information about a host in zabbix however you can supply one host or an array of hosts. It equally returns the interfaceID used when creating items on a given host
  150. .EXAMPLE
  151. Get-ZabbixHost -hosts mypc.mydomain.com -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com.
  152.  
  153. #>
  154.  
  155. function Get-ZabbixHosts ($hosts,$token,$zabbixApiURL)
  156. {
  157. $method = "host.get"
  158. $params = @{output = "extend";filter = @{host=@($hosts)};selectInterfaces = "extend";selectItems = "extend";selectTriggers = "extend"; selectParentTemplates = "extend"}
  159. $objHost = JsonConstructor -method $method -params $params -token $token
  160. $result = webRequest $objHost $zabbixApiURL
  161. return $result.result
  162. }
  163.  
  164. <#
  165. .SYNOPSIS
  166. Not Implemented Yet
  167. .EXAMPLE
  168. N/A
  169.  
  170. #>
  171.  
  172. function Get-ZabbixGraph()
  173. {
  174. }
  175.  
  176. <#
  177. .SYNOPSIS
  178. This Cmdlet returns all the items attached to a host in zabbix you can supply an array of host ID's
  179. .EXAMPLE
  180. Get-ZabbixItem -hostids 100112424 -zabbixApiURL https://zabbix.mydomain.com -token ab5bf1ea28f1ec58f6fbe33a75c1f98c
  181.  
  182. #>
  183.  
  184. function Get-ZabbixItem ($hostids,$zabbixApiURL,$token)
  185. {
  186. $method = "item.get"
  187. $params = @{output = "extend";hostids = @($hostids);sortfiled = "name"; selectItemDiscovery = "extend" }
  188. $objItem = JsonConstructor -method $method -params $params -token $token
  189. $result = webRequest $objItem $zabbixApiURL
  190. return $result.result
  191. }
  192.  
  193. <#
  194. .SYNOPSIS
  195. This Cmdlet returns information about a Template it can be an array of templates
  196. .EXAMPLE
  197. Get-ZabbixTemplate -TemplateNames MyCustomTemplate -zabbixApiUrl https://my.zabbixinstance.com -token ab5bf1ea28f1ec58f6fbe33a75c1f98c
  198.  
  199. #>
  200.  
  201. function Get-ZabbixTemplate ($TemPlateNames,$zabbixApiURL,$token)
  202. {
  203. $method = "template.get"
  204. $params = @{output = "extend";selectItems = "extend";selectHosts = "extend";selectTriggers = "extend";selectGroups = "extend" ;filter = @{host = @($TemPlateNames)}}
  205. $objTemplate = JSONConstructor -method $method -params $params -token $token
  206. $result = webRequest $objTemplate $zabbixApiURL
  207. return $result.result
  208. }
  209.  
  210. <#
  211. .SYNOPSIS
  212. This Cmdlet Is used to create a new blank template and return it's ID you can also link it to hosts if you like while creating it.
  213. .EXAMPLE
  214. Get-ZabbixHost Set-ZabbixTemplate -TemplateName MyTestTemplate -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://my.zabbixinstance.com -description MyCustomDescription -groupids someGroupID
  215.  
  216. #>
  217.  
  218. function Set-ZabbixTemplate ($TemplateName,$token,$zabbixApiURL,$description,$groupids)
  219. {
  220. $method = "template.create"
  221. $params = @{host = $TemplateName; description = $description ;groups = @{groupid = $groupids} }
  222. $objTemplate = JsonConstructor -method $method -params $params -token $token
  223. $result = webRequest $objTemplate $zabbixApiURL
  224. return $result.result
  225. }
  226.  
  227.  
  228.  
  229. <#
  230. .SYNOPSIS
  231. This Cmdlet returns information about a Trigger attached to a host/hosts.
  232. .EXAMPLE
  233. Get-ZabbixHost -hosts mypc.mydomain.com -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com.
  234.  
  235. #>
  236.  
  237. function Get-ZabbixTrigger ($hostids,$zabbixApiURL,$token)
  238. {
  239. $method = "trigger.get"
  240. $params = @{output = "extend"; ;hostids = @($hostids);expandExpression=@{};selectItems = "extend"}
  241. $objTrigger = JsonConstructor -method $method -params $params -token $token
  242. $result = webRequest $objTrigger $zabbixApiURL
  243. return $result.result
  244. }
  245.  
  246. <#
  247. .SYNOPSIS
  248. This Cmdlet returns information about a host in zabbix however you can supply one host or an array of hosts.
  249. .EXAMPLE
  250. Get-ZabbixHost -hosts mypc.mydomain.com -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com.
  251.  
  252. #>
  253.  
  254. function Set-ZabbixHost()
  255. {
  256. }
  257.  
  258. <#
  259. .SYNOPSIS
  260. This Cmdlet is used to create an Item in zabbix applicationIDs is optional visit https://www.zabbix.com/documentation/2.4/manual/api/reference/item/object to get more information
  261. .EXAMPLE
  262. Set-ZabbixItem -itemName MyItem -key 'key[info]' -hostid 1000001 -type 10 -valuetype 4 -interfaceid 100002 -applicationIDs 609 -delay 30 -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com.
  263.  
  264. #>
  265.  
  266. function Set-ZabbixItem ($itemName,$key,$hostid,$type,$valuetype,$interfaceid,$applicationIDs,$delay,$token,$zabbixApiURL)
  267. {
  268. $method = "item.create"
  269. $params = @{name = $itemName ; key_ = $key ; hostid = $hostid ; type = $type ; value_type = $valuetype ; interfaceid = $interfaceid ; application = @($applicationIDs) ; delay = $delay }
  270. $objItem = JsonConstructor -method $method -params $params -token $token
  271. $result = webRequest $objItem $zabbixApiURL
  272. return $result.result
  273. }
  274.  
  275. <#
  276. .SYNOPSIS
  277. This Cmdlet is used to create a trigger. Normally in json you would escape " with \ but in this case the constructor handles this for you so just specify the expression between '' and you will not have any issues.
  278. .EXAMPLE
  279. Set-ZabbixTrigger -triggerName MyTriggerName -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com. -triggerExpression '{host:key.function()}<0'
  280.  
  281. #>
  282.  
  283. function Set-ZabbixTrigger($triggerName,$token,$zabbixApiURL,$triggerExpression,[int]$triggerPriority)
  284. {
  285. $method = "trigger.create"
  286. $params = @{description = $triggerName ; expression = $triggerExpression; priority = $triggerPriority }
  287. $objTrigger = JsonConstructor -method $method -params $params -token $token
  288. $result = webRequest $objTrigger $zabbixApiURL
  289. return $result.result
  290. }
  291.  
  292. <#
  293. .SYNOPSIS
  294. This Cmdlet is used to create an Item in zabbix applicationIDs is optional visit https://www.zabbix.com/documentation/2.4/manual/api/reference/item/object to get more information
  295. .EXAMPLE
  296. Set-ZabbixItem -itemName MyItem -key 'key[info]' -hostid 1000001 -type 10 -valuetype 4 -interfaceid 100002 -applicationIDs 609 -delay 30 -token ab5bf1ea28f1ec58f6fbe33a75c1f98c -zabbixApiURL https://zabbix.mydomain.com.
  297.  
  298. #>
  299.  
  300. function Disable-ZabbixItem ($itemid,$token,$zabbixApiURL)
  301. {
  302. $method = "item.update"
  303. $params = @{itemid = $itemid; status = 1 }
  304. $objItem = JsonConstructor -method $method -params $params -token $token
  305. $result = webRequest $objItem $zabbixApiURL
  306. return $result.result
  307. }
  308.  
  309. function Set-ZabbixItemType ($itemid,$token,$zabbixApiURL,$itemType)
  310. {
  311. $method = "item.update"
  312. $params = @{itemid = $itemid; type = $itemType }
  313. $objItem = JsonConstructor -method $method -params $params -token $token
  314. $result = webRequest $objItem $zabbixApiURL
  315. return $result.result
  316. }
  317. function Set-ZabbixGraph ($graphName,$graphWidth,$graphHeight,$graphItems)
  318. {
  319. $method = "graph.create"
  320. $params = @{ name = $graphName; width = $graphWidth ; height = $graphHeight ; gitems = @(@{itemid = $itemId }) }
  321. $objItem = JsonConstructor -method $method -params $params -token $token
  322. $result = webRequest $objItem $zabbixApiURL
  323. return $result.result
  324. }
  325.  
  326. function Get-ZabbixUser ($userId,$token,$zabbixApiURL)
  327. {
  328. $method = "user.get"
  329. $params = @{ userids= "$userId";output= "extend" }
  330. $objItem = JsonConstructor -method $method -params $params -token $token
  331. write-output $objItem
  332. $result = webRequest $objItem $zabbixApiURL
  333. return $result.result
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement