Jump to content

Opsview Windows Rest API Client


JRSmile
 Share

Recommended Posts

Hi there folks,

every two years or so i have a new script for you...

below you will find my approach to get some infos from the opsview.com rest api.

i might extend this to a full software but for now here is the basic authentication and information gathering of hashtags proove of concept.

Spoiler
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\..\..\Pictures\favicon.ico
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Comment=JR's Opsview Client
#AutoIt3Wrapper_Res_Description=JOC
#AutoIt3Wrapper_Res_Fileversion=0.1.0.0
#AutoIt3Wrapper_Res_LegalCopyright=JRSmile
#AutoIt3Wrapper_Res_Language=1031
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.14.1
    Author:         JRSmile

    Script Function:
    Opsview Windows Client

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include-once
#include "winhttp.au3"
#include "OO_JSON.au3"
Opt("MustDeclareVars", 1)

Global Const $STR_ENDISSTART = 0 ; End acts as next start when end = start
Global Const $STR_ENDNOTSTART = 1 ; End does not act as new start when end = start
Global Const $STR_REGEXPARRAYGLOBALMATCH = 3 ; Return array of global matches.



;Proxy Config
Global $aIEproxy = _WinHttpGetIEProxyConfigForCurrentUser()
Global $sProxy = $aIEproxy[2]

Global $aToken = ""
Global $sToken = ""
Global $oJSON = _OO_JSON_Init()
Global $obj = ""

Global $sUserName = "username"
Global $sPassword = "password"
Global $sDomain = "opsview.server.local"
Global $sPage = "rest/login"
Global $sAdditionalData = '{ username => "' & $sUserName & '", password => "' & $sPassword & '" }'
;~ Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1", $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $sProxy) ; with ie proxy settings
Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1") ; without ie proxy settings
Global $hConnect = _WinHttpConnect($hOpen, $sDomain)
Global $sReturned = _WinHttpSimpleSSLRequest($hConnect, "POST", $sPage, Default, $sAdditionalData, "Content-Type: text/x-data-dumper" & @CRLF)
_WinHttpCloseHandle($hConnect)
$aToken = _StringBetween($sReturned, "'", "'", $STR_ENDNOTSTART, True)
$sToken = $aToken[1]
$hConnect = _WinHttpConnect($hOpen, $sDomain)
$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", "rest/status/viewport/HASHTAG", Default, $sAdditionalData, "Content-Type: application/json" & @CRLF & "X-Opsview-Username: " & $sUserName & @CRLF & "X-Opsview-Token: " & $sToken & @CRLF)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
; See what's returned
Global $obj = $oJSON.parse($sReturned)

For $i = 0 To $obj.list.length - 1
    ConsoleWrite($obj.list.item($i).services.item(0).state & ": ")
    ConsoleWrite($obj.list.item($i).name & " --> ")
    ConsoleWrite($obj.list.item($i).services.item(0).output & @CRLF)
Next


Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $bCase = False)
    ; If starting from beginning of string
    $sStart = $sStart ? "\Q" & $sStart & "\E" : "\A"

    ; Set mode
    If $iMode <> $STR_ENDNOTSTART Then $iMode = $STR_ENDISSTART

    ; If ending at end of string
    If $iMode = $STR_ENDISSTART Then
        ; Use lookahead
        $sEnd = $sEnd ? "(?=\Q" & $sEnd & "\E)" : "\z"
    Else
        ; Capture end string
        $sEnd = $sEnd ? "\Q" & $sEnd & "\E" : "\z"
    EndIf

    ; Set correct case sensitivity
    If $bCase = Default Then
        $bCase = False
    EndIf

    Local $aReturn = StringRegExp($sString, "(?s" & (Not $bCase ? "i" : "") & ")" & $sStart & "(.*?)" & $sEnd, $STR_REGEXPARRAYGLOBALMATCH)
    If @error Then Return SetError(1, 0, 0)
    Return $aReturn
EndFunc   ;==>_StringBetween

 

you will need the winhttp.au3 and oo_json.au3 (use the forum search), additional thanks to trancexx for being awesome.

Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...