Theo dõi trạng thái thông báo trong PRTG (Hoạt động / Tạm dừng / Đã xóa)

Nếu bạn muốn theo dõi trạng thái của thông báo trong PRTG (Setup | Account Settings | Notifications), thì hãy sử dụng tập lệnh PowerShell (.ps1) sau đây. Nội dung script:

  • Truy vấn các thiết lập của một thông báo nhất định
  • Kiểm tra qua RegEx nếu Trạng thái được đặt thành Hoạt động hoặc Tạm dừng
  • Tìm kiếm ID thông báo và hiển thị thông báo lỗi tương ứng nếu không thể tìm thấy ID (ví dụ: nếu thông báo đã bị xóa)

Yêu cầu

Một người dùng cục bộ có quyền truy cập vào các thông báo.
Đặt Chính sách thực thi của PowerShell thành ít nhất là RemoteSign.

Cách sử dụng Script

  • Lưu script thành tệp .ps1 \Custom Sensors\EXEXML\ thư mục con của thư mục chương trình PRTG trên máy thăm dò từ nơi bạn muốn kiểm tra điểm truy cập.
#    ____  ____  ____________
#   / __ \/ __ \/_  __/ ____/
#  / /_/ / /_/ / / / / / __  
# / ____/ _, _/ / / / /_/ /  
#/_/   /_/ |_| /_/  \____/                         
#    NETWORK MONITOR
#-------------------
#(c) 2018 Dariusz Gorka, Paessler AG
#
# This script checks if a certain notification within PRTG is paused,active or deleted.
#

# Parameter "-username" - Username from within PRTG (User that can access Notification-Settings)
# Parameter "-passhash" - The Passhash of the User ("Show Passhash" in the User Settings Page)
# Parameter "-prtghost" - The URL for PRTG !!! Please without an ending "/" !!!
# Parameter "-notifid" - The ID of the notification - displayed in the URL of the Notification
# copy paste for the Parameters field in PRTG:
# -username "MyUser" -passhash "12345678910" -prtghost "http(s)://PRTGServer" -notifid "300"
#

param(
    [string] $username = "prtgadmin",
    [string] $passhash = "1278894095",
    [string] $prtghost = "https://server",
    [String] $notifid = "4085"
)

# Check URL
$prtgserverlength = $prtghost.Length - 1
$prtgserverlastch = $prtghost.Substring($prtgserverlength)
if ($prtgserverlastch -eq "/"){
$prtghost = $prtghost.Substring(0,$prtghost.Length - 1)
} 

# Check connectivity
$conerror = 0
try{
        $conurl = [String]::Format("{0}/api/status.json?username={1}&passhash={2}",$prtghost,$username,$passhash)
        Invoke-WebRequest -Uri $conurl -UseBasicParsing | Out-Null
    } catch {
        $conerror = 1
}


if ($conerror -eq 0) {

    # Check if Notification is there
    $notification_site_url = ([String]::Format("{0}/controls/editnotification.htm?id={1}&username={2}&passhash={3}",$prtghost,$notifid,$username,$passhash))
    $notification_site = Invoke-WebRequest -uri $notification_site_url -UseBasicParsing

    $notif_id_found = $notification_site -match '<div\s+class="errormsg">'

    if (!$notif_id_found) {

    $notif_result = $notification_site -match 'name="active_"\s+value="1"\s+checked\s+id="active1"'

    if ($notif_result) {
        $notification_site -match '(name="name_"\s+id="name_"\s+autocomplete="off"\s+value=")(.*)("\s+><\/div>)' | Out-Null
        $notification_name = $Matches[2]
        
        Write-Host @"
        <PRTG>
            <result>
                <channel>Notification</channel>
                <value>1</value>
                <showChart>1</showChart>
                <showTable>1</showTable>
                <Float>1</Float>
                <unit>CustomUnit</unit>
                <customunit>Active</customunit>
                <mode>Absolute</mode>
                <LimitMode>1</LimitMode>
                <LimitMinError>1</LimitMinError>
            </result>
            <text>
                The Notification '$($notification_name)' is active!
            </text>
        </PRTG>
"@

    } else {
        $notification_site -match '(name="name_"\s+id="name_"\s+autocomplete="off"\s+value=")(.*)("\s+><\/div>)' | Out-Null
        $notification_name = $Matches[2]

        Write-Host @"
        <PRTG>
            <result>
                <channel>Notification</channel>
                <value>0</value>
                <showChart>1</showChart>
                <showTable>1</showTable>
                <Float>1</Float>
                <unit>CustomUnit</unit>
                <customunit>Active</customunit>
                <mode>Absolute</mode>
                <LimitMode>1</LimitMode>
                <LimitMinError>1</LimitMinError>
            </result>
            <text>
                The Notification '$($notification_name)' is paused!
            </text>
        </PRTG>
"@
    }
} else {
    # Notification not found

    Write-Host @"
    <PRTG>
        <error>1</error>
        <text>
            The Notification-ID '$($notifid)' was not found! Maybe it is deleted?
        </text>
    </PRTG>
"@
}
} else {
    # Connection Error

    Write-Host @"
    <PRTG>
        <error>1</error>
        <text>
            Something went wrong with the connection! Please check the parameters!
        </text>
    </PRTG>
"@
}
  • Thêm 1 EXE/Script Advanced sensor.
  • Trong mục cài đặt cảm biến, chọn tập lệnh trên từ danh sách thả xuống và cung cấp các thông số sau trong Parameters:

-username "<USER>" -passhash "<PASSHASH>" -prtghost "<PRTGServer>" -notifid "<ID>"

  • Adjust the <USER><PASSHASH><PRTGServer> and <ID> parameters like in the examples below, depending on your scenario:
    • <USER>: prtg-api
    • <PASSHASH>: 12345678910 (You can find the passhash under Setup | Account Settings | My Account, section User Account)
    • <PRTGServer>https://myprtg.com
    • <ID>: 300
  • Click Continue để lưu cài đặt.

Thông báo PRTG

Với các bước trên bạn đã hoàn thành việc thiết lập theo dõi trạng thái thông báo trong PRTG.

Liên hệ Pacisoft nếu bạn có bất kì thắc mắc nào về PRTG.

Xem thêm: