1
0
mirror of https://github.com/znilnet/ZabbixEasyTool.git synced 2024-12-21 16:37:54 +01:00

API-Login Test

This commit is contained in:
Bernhard Linz 2017-09-17 21:12:14 +02:00
parent a8d4129744
commit 910e625cc3

View File

@ -266,7 +266,12 @@ EndFunc
; #############################################################################################################################################################
; #############################################################################################################################################################
Func _CheckMaintenanceStatus()
MsgBox(64, "Check Maintenance Status", "...")
;~ MsgBox(64, "Check Maintenance Status", "...")
Local $__sToken = _zbx_Login( GUICtrlRead($FormSetupAPIInputURL), $FormSetupAPIInputUsername, $FormSetupAPIInputPassword)
If $__sToken = "" Then
GUICtrlSetBkColor($FormMainLabelStatus, 0xFF0000)
GUICtrlSetData($FormMainLabelStatus, "API Login Failure")
EndIf
EndFunc
; #############################################################################################################################################################
@ -491,6 +496,269 @@ Func _zetRegWrite($__sRegValueName, $__sRegValue, $__bNoRead = False)
EndIf
EndFunc
; #############################################################################################################################################################
Func _zbx_Login( $__zbxURL, $__zbxUser, $__zbxPassword)
Local $__zbxJSON = '{"params":{"password":"' & $__zbxPassword & '","user":"' & $__zbxUser & '","userData":true},"jsonrpc":"2.0","method":"user.login","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxSessionId = ""
$__oHTTP.Open("POST", $__zbxURL , False)
; ignore all SSL errors
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
;$__oHTTP.Option(4) = 0x3300
; Unknown certification authority (CA) or untrusted root 0x0100
; Wrong usage 0x0200
; Invalid common name (CN) 0x1000
; Invalid date or certificate expired 0x2000
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbx_Login", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "sessionid") > 0 Then
$__zbxSessionId = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxSessionId
EndFunc
; #############################################################################################################################################################
Func _zbx_Logout( $__zbxURL, $__zbxSessionId)
Local $__zbxJSON = '{"jsonrpc": "2.0","method": "user.logout","params": [],"id": 42,"auth": "' & $__zbxSessionId & '"}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxResult = 0
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbx_Logout", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "result") > 0 Then
$__zbxResult = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxResult
EndFunc
; #############################################################################################################################################################
Func _zbx_HostGetId($__zbxURL, $__zbxSessionId, $__zbxHostname)
Local $__zbxJSON = '{"params":{"filter":{"name":"' & $__zbxHostname & '"},"output":["hostid"]},"jsonrpc": "2.0","method": "host.get","auth": "' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxHostId = 0
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
;~ MsgBox(0, "_zbx_HostGetId", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "hostid") > 0 Then
$__zbxHostId = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxHostId
EndFunc
; #############################################################################################################################################################
Func _zbx_HostGetMaintenanceIDs($__zbxURL, $__zbxSessionId, $__zbxHostId, $__zbxFilterByName = "")
Local $__zbxJSON = '{"params":{"output":"extend","selectHosts":"refer","selectGroups": "refer","hostids": "' & $__zbxHostId & '"},"jsonrpc": "2.0","method": "maintenance.get","auth": "' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__a_zbxHostMaintenanceId[1] = [ 0 ]
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
;~ MsgBox(0, "_zbx_HostGetMaintenanceIDs", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "maintenanceid") > 0 Then
If $__zbxFilterByName <> "" Then
For $j = 1 To 12 Step 1
If StringInStr($__atemp[$i + $j], "ZabbixEasy") > 0 Then
$__a_zbxHostMaintenanceId[0] = $__a_zbxHostMaintenanceId[0] + 1
ReDim $__a_zbxHostMaintenanceId[ $__a_zbxHostMaintenanceId[0] + 1 ]
$__a_zbxHostMaintenanceId[ $__a_zbxHostMaintenanceId[0] ] = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
$i = $i + 1
ExitLoop
EndIf
Next
Else
$__a_zbxHostMaintenanceId[0] = $__a_zbxHostMaintenanceId[0] + 1
ReDim $__a_zbxHostMaintenanceId[ $__a_zbxHostMaintenanceId[0] + 1 ]
$__a_zbxHostMaintenanceId[ $__a_zbxHostMaintenanceId[0] ] = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
$i = $i + 1
EndIf
EndIf
Next
EndIf
Return $__a_zbxHostMaintenanceId
EndFunc
; #############################################################################################################################################################
Func _zbx_HostAddMaintenance($__zbxURL, $__zbxSessionId, $__zbxHostId, $__zbxName, $__zbxDescription, $__zbxDurationTime = 3600)
Local $__StartTimeStructUTC = _Date_Time_GetSystemTime()
Local $__StartTimeUTC = _DateDiff('s', "1970/01/01 00:00:00", _Date_Time_SystemTimeToDateTimeStr($__StartTimeStructUTC, 1))
Local $__EndTimeUTC = $__StartTimeUTC + $__zbxDurationTime
Local $__zbxJSON = '{"jsonrpc":"2.0","method":"maintenance.create","params":[{"groupids":[],"hostids":["' & $__zbxHostId & '"],' & _
'"name":" ' & $__zbxName & '",' & _
'"maintenance_type":"0","description":" ' & $__zbxDescription & '","active_since":"' & $__StartTimeUTC & '","active_till":"' & $__EndTimeUTC & '",' & _
'"timeperiods":[{"timeperiod_type":0,"start_date":"' & $__StartTimeUTC & '","period":' & $__EndTimeUTC & '}]}],"auth":"' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxMaintenanceId = 0
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
;~ MsgBox(0, "_zbx_HostAddMaintenance", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "maintenanceid") > 0 Then
$__zbxMaintenanceId = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxMaintenanceId
EndFunc
; #############################################################################################################################################################
Func _zbxHostRemoveMaintenance($__zbxURL, $__zbxSessionId, $__zbxHostId, $__zbxMaintenanceId)
Local $__zbxJSON = '{"jsonrpc":"2.0","method":"maintenance.delete","params":["' & $__zbxMaintenanceId & '"],"auth":"' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxResult = 0
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbxHostRemoveMaintenance", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "maintenanceid") > 0 Then
$__zbxResult = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxResult
EndFunc
; #############################################################################################################################################################
Func _zbxHostTriggerGet($__zbxURL, $__zbxSessionId, $__zbxHostId)
Local $__zbxJSON = '{"jsonrpc": "2.0","method": "trigger.get","params":{"output":["triggerid","description","priority"],' & _
'"filter":{"value":1,"hostid":"' & $__zbxHostId & '"},"sortfield":"priority","sortorder":"DESC"},"auth": "' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__a_zbxHostTriggers[1][4] ; [0][0] = Anzahl
; [x][1] = triggerid
; [x][2] = description
; [x][3] = priority
$__a_zbxHostTriggers[0][0] = 0
$__a_zbxHostTriggers[0][1] = "triggerid"
$__a_zbxHostTriggers[0][2] = "description"
$__a_zbxHostTriggers[0][3] = "priority"
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbxHostTriggerGet", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "triggerid") > 0 Then
$__a_zbxHostTriggers[0][0] = $__a_zbxHostTriggers[0][0] + 1
ReDim $__a_zbxHostTriggers[ $__a_zbxHostTriggers[0][0] + 1 ][4]
$__a_zbxHostTriggers[ $__a_zbxHostTriggers[0][0] ][1] = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
For $j = $i To $__atemp[0] Step 1
If StringInStr($__atemp[$j], "description") > 0 Then
$__a_zbxHostTriggers[ $__a_zbxHostTriggers[0][0] ][2] = StringTrimRight(StringTrimLeft($__atemp[$j + 1],1),1)
EndIf
If StringInStr($__atemp[$j], "priority") > 0 Then
$__a_zbxHostTriggers[ $__a_zbxHostTriggers[0][0] ][3] = StringRegExpReplace($__atemp[$j + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Next
EndIf
Return $__a_zbxHostTriggers
EndFunc
; #############################################################################################################################################################
Func _zbxTriggerEventGet($__zbxURL, $__zbxSessionId, $__zbxTriggerId)
Local $__zbxJSON = '{"jsonrpc": "2.0","method": "event.get","params":{"output":"extend","select_acknowledges":"extend","objectids":"' & $__zbxTriggerId & _
'","sortfield":["clock","eventid"],"sortorder": "DESC","filter":{"value":1}},"auth":"' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__a_zbxTriggerEvents[1] = [ 0 ]
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbxHostEventGet", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], '"eventid"') > 0 Then
$__a_zbxTriggerEvents[0] = $__a_zbxTriggerEvents[0] + 1
ReDim $__a_zbxTriggerEvents[ $__a_zbxTriggerEvents[0] + 1 ]
$__a_zbxTriggerEvents[ $__a_zbxTriggerEvents[0] ] = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
$i = $i + 1
EndIf
Next
EndIf
Return $__a_zbxTriggerEvents
EndFunc
; #############################################################################################################################################################
Func _zbxEventAcknowledge($__zbxURL, $__zbxSessionId, $__zbxEvendId, $__zbxMessage = "ZabbixEasy")
Local $__zbxJSON = '{"jsonrpc":"2.0","method":"event.acknowledge","params":{"eventids": "' & $__zbxEvendId & '","message":"' & $__zbxMessage & '","action":0},"auth": "' & $__zbxSessionId & '","id":42}'
Local $__oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Local $__zbxResult = 0
$__oHTTP.Open("POST", $__zbxURL , False)
$__oHTTP.Option("WinHttpRequestOption_SslErrorIgnoreFlags") = 0x3300
$__oHTTP.SetRequestHeader("Content-Type", "application/json-rpc")
$__oHTTP.Send($__zbxJSON)
Local $__oReceived = $__oHTTP.ResponseText
Local $__oStatusCode = $__oHTTP.Status
MsgBox(0, "_zbxHostRemoveMaintenance", StringReplace($__oReceived,",", "," & @CRLF) & @CRLF & @CRLF & "Status Code: " & $__oStatusCode)
If $__oStatusCode = 200 Then
Local $__atemp = StringSplit($__oReceived, ",:", 0)
For $i = 1 To $__atemp[0] Step 1
If StringInStr($__atemp[$i], "eventids") > 0 Then
$__zbxResult = StringRegExpReplace($__atemp[$i + 1], '[^A-Za-z0-9_().+\%\-\s]+', "")
ExitLoop
EndIf
Next
EndIf
Return $__zbxResult
EndFunc
#EndRegion Funktionen