4

http://azure.microsoft.com/en-us/documentation/articles/app-insights-monitor-performance-live-website-now/

我有几个站点托管在使用云服务 Webroles 部署的 azure 上的 VM 上。

有没有人看过从代码或命令行安装状态监视器的例子?目标是在不登录远程桌面的情况下将 IIS 站点自动添加到 Azure App Insight,如上面的链接所示。

4

2 回答 2

4

我们也遇到了这个问题并编写了一个脚本,该脚本应该插入到现有的安装 VS Online App Insights 的方法中

if(${env:InstalledStatusMonitor} -eq 1)
{
    Write-Host "Status monitor has already been installed on this machine by this script" -ForegroundColor Green
}

Write-Host "Using chocolatey to install the Web Platform Installer ..."  -ForegroundColor Green
iex ((new-object net.webclient).downloadstring('https://chocolatey.org/install.ps1'))
cinst webpi -y
Stop-Process -Name WebPlatformInstaller

Write-Host "Using Web Platform Installer to install Status Monitor ..." -ForegroundColor Green
&"$env:ProgramFiles\Microsoft\Web Platform Installer\WebpiCmd.exe" /Install /AcceptEula /Products:ApplicationInsightsStatusMonitor

Write-Host "Adding app pool account to the 'Performance Monitor Users' local group" -ForegroundColor Green
$group = "Performance Monitor Users"
$user = "Network Service"
$computer = $(Get-WmiObject Win32_Computersystem).name
$de = [ADSI]"WinNT://$computer/$group,group" 
$de.psbase.Invoke("Add",([ADSI]"WinNT://$user").path)
Write-Host "Waiting 30 seconds for Status Monitor to finish its install ..." -ForegroundColor Green
Start-Sleep -Seconds 30

Write-Host "Stop-Starting services to enable tracing..." -ForegroundColor Green
# For some reason, even though Status Monitor calls "iisreset.exe /restart"
# calling it here leaves IIS and website stopped.
&iisreset.exe /restart

Write-Host "waiting a few seconds..." -ForegroundColor Yellow
Start-Sleep -Seconds 2
Write-Host "starting..." -ForegroundColor Yellow

Start-Service -Name W3SVC
Get-WebApplication | Select ApplicationPool -Unique | %{ Start-WebAppPool $_.applicationPool }
Get-Website | Start-Website
Write-Host "started" -ForegroundColor Yellow

Write-Host "Cleaning up running applications" -ForegroundColor Green
Stop-Process -Name Microsoft.Diagnostics.Agent.StatusMonitor


Write-Host "Setting environment variable to indicate status monitor has been installed" -ForegroundColor Green
[Environment]::SetEnvironmentVariable("InstalledStatusMonitor", "1", "Machine")

Write-Host "Installation complete" -ForegroundColor Green

我在这里写了一篇关于它的博客文章,所以如果我们遇到问题并且我忘记更新这篇文章,你应该能够在那里看到更新。

更新 1

感谢 pksorensen 的直接链接,看起来这是 Web 平台安装程序从那里下载包的地方。我在这方面做了更多工作,现在有一个完全自动化的流程在工作。我在这里有一个示例存储库,其中包含一个工作项目。. 此提交应描述您需要对自己的 Web 项目执行哪些操作以使状态监视器在 Web 角色上工作。

Sergey Kanzhelev 有一个帖子,表明也应该可以对工人角色执行此操作。

以下是所需的各个步骤。如果我有机会研究它,我将尝试提供有关非网络角色的更新:

1.服务定义.csdef

将以下 Startup 条目添加到您的 Web 部署项目中,它将在创建或部署角色时运行。

<ServiceDefinition>
  <WebRole>

    ...

    <Startup>
      <Task commandLine="Role_Start\Bootstrap.bat" executionContext="elevated" taskType="simple">
        <Environment>
          <Variable name="EMULATED">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
          </Variable>
        </Environment>
      </Task>
    </Startup>
  </WebRole>
</ServiceDefinition>

2.WebProject/Role_Start/Bootstrap.bat

这是启动任务调用的批处理文件。它必须放在您的网络项目中。将属性设置为始终将文件复制到输出目录。

:: The basis for this script is described here
:: http://blogs.msdn.com/b/visualstudioalm/archive/2014/04/16/new-agent-for-application-insights-available.aspx
:: The scripts can be downloaded directly from
:: http://go.microsoft.com/fwlink/?LinkID=329971

@echo off

:: Do not attempt to install the agent on an emulated environment
if "%EMULATED%"=="true" goto :EndOfScript

:: Set appropriate execution policy on the host machine
set ExecutionPolicyLevel=RemoteSigned
for /F "usebackq" %%i in (`powershell -noprofile -command "Get-ExecutionPolicy"`) do (
    set ExecutionPolicy=%%i
    if /I "%%i"=="Unrestricted" goto :AllIsWell
    if /I "%%i"=="RemoteSigned" goto :AllIsWell
    Powershell.exe -NoProfile -Command "Set-ExecutionPolicy RemoteSigned" < NUL >> NUL 2>> NUL
)

:AllIsWell

Powershell.exe -NoProfile -Command "& '%~dp0InstallStatusMonitor.ps1'" < NUL  >> NUL 2>> NUL
echo "done" >"%ROLEROOT%\startup.task.done.sem"

:EndOfScript

exit 0

3.WebProject/Role_Start/InstallStatusMonitor.ps1

与 Bootstrap.bat 文件一样,设置属性以将此文件也复制到输出目录。这是之前发布的脚本的更新版本

# The basis for this script is described here
# http://blogs.msdn.com/b/visualstudioalm/archive/2014/04/16/new-agent-for-application-insights-available.aspx
# The scripts can be downloaded directly from
# http://go.microsoft.com/fwlink/?LinkID=329971

#Constants
$downloadUrl = "https://go.microsoft.com/fwlink/?LinkID=512247&clcid=0x409"

# Variables
$rootDir = Split-Path $MyInvocation.MyCommand.Path
$downloadPath = Join-Path $rootDir "ApplicationInsightsAgent.msi"

# Functions
# Infrastructure functions
function TryV1
{
    param
    (
        [ScriptBlock] $Command = $(throw "The parameter -Command is required."),
        [ScriptBlock] $Catch   = { throw $_ },
        [ScriptBlock] $Finally = { }
    )

    & {
        $local:ErrorActionPreference = "SilentlyContinue"

        trap
        {
            trap
            {
                & {
                    trap { throw $_ }
                    & $Finally
                }

                throw $_
            }

            $_ | & { & $Catch }
        }

        & $Command
    }

    & {
        trap { throw $_ }
        & $Finally
    }
}

function Retry
{
    param (
        [ScriptBlock] $RetryCommand
    )

    for ($attempts=0; $attempts -lt 5; $attempts++)
    {        
        TryV1 {

            & $RetryCommand
            break

        } -Catch {

            if($attempts -lt 4)
            {
                Log-Message "Attempt:$attempts Exception Occured. Sleeping and Retrying..."
                Log-Message $_
                Log-Message $_.InvocationInfo.PositionMessage
                Start-Sleep -Seconds 1
            }
            else
            {
                throw $_
            }
        }
    }

}

function Log-Message
{
    param(
        [string] $message
    )

    $logString = ("{0}: {1}" -f (Get-Date), $message)
    $unifiedStartupInfoLogPath = Join-Path $rootDir "ApmAgentInstall.log"
    Add-Content $unifiedStartupInfoLogPath $logString
    Write-Host $logString -ForegroundColor Green
}

function Log-Error
{
    param(
        [string] $message
    )
    $logString =  ("{0}: {1}" -f (Get-Date), $message)
    $unifiedStartupErrorLogPath = Join-Path $rootDir "ApmAgentInstallError.log"
    Add-Content $unifiedStartupInfoLogPath $logString
    Write-Host $logString -ForegroundColor Red
}

# Functions
# Operations functions
function Get-AppInsightsInstallationStatus(){

    if(${env:InstalledStatusMonitor} -eq 1)
    {
        return $true
    }
    else
    {
        return $false
    }
}

function Download-StatusMonitor
{
    Retry {

        $wc = New-Object System.Net.WebClient
        $wc.DownloadFile($downloadUrl, $downloadPath)
    }
}

function Install-StatusMonitor(){
    $logPath = Join-Path $rootDir "StatusMonitorInstall.log"
    &$downloadPath /quiet /passive /log $logPath
    Log-Message "Waiting 30 seconds for Status Monitor to finish its install ..."
    Start-Sleep -Seconds 30
}

function Grant-LoggingPermissionToAppPool(){
    $groupName = "Performance Monitor Users"
    $user = "Network Service"
    $group = [ADSI]"WinNT://./$groupName,group"
    if(($group.PSBase.Invoke('Members') | %{$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}) -contains $user)
    {
        Log-Message "'$user' is already a member of '$groupName', don't need to do anything"
        return
    }    
    else
    {
        Log-Message "'$user' is now a member of '$groupName'"
        $group.Add("WinNT://$user")
    }
}

function Restart-IISOnAzureWebRole(){
    # For some reason, even though Status Monitor calls "iisreset.exe /restart"
    # calling it here leaves IIS and website stopped.
    &iisreset.exe /restart

    Log-Message "waiting a few seconds..."
    Start-Sleep -Seconds 2
    Log-Message "starting..."

    Start-Service -Name W3SVC
    Get-WebApplication | Select ApplicationPool -Unique | %{ Start-WebAppPool $_.applicationPool }
    Get-Website | Start-Website
    Log-Message "started"
}

# Main body
Log-Message "Starting Status Monitor installation"

Log-Message "Downloading component..."
Download-StatusMonitor

Log-Message "Installing component..."
Install-StatusMonitor

Log-Message "Adding app pool account to the 'Performance Monitor Users' local group"
Grant-LoggingPermissionToAppPool

Log-Message "Stop-Starting services to enable tracing..."
Restart-IISOnAzureWebRole

Log-Message "Completed installation successfully"

更新 2

如果您需要添加自定义属性以进行跟踪,例如能够根据角色名称或角色实例区分依赖项,您需要在文档中描述之前进入 Application Insights 管道。

假设您希望能够过滤角色名称和角色实例 ID,您将创建一个自定义上下文初始化程序。

using System.Text.RegularExpressions;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Utilities
{
    public class AppInsightsCurrentRoleIdAsTagInitializer : Microsoft.ApplicationInsights.Extensibility.IContextInitializer
    {
        public void Initialize(TelemetryContext context)
        {
            context.Properties["Greenfinch - RoleName"] = RoleEnvironment.CurrentRoleInstance.Role.Name;
            context.Properties["Greenfinch - RoleInstanceId"] = InstanceId;
        }

        private string InstanceId
        {
            get
            {
                var instanceId = Regex.Match(RoleEnvironment.CurrentRoleInstance.Id, "\\d+$", RegexOptions.Compiled).Value;
                return string.IsNullOrWhiteSpace(instanceId)
                    ? "unable to get instance id"
                    : instanceId;
            }
        }
    }
}

但不是将其插入代码中,而是将其添加到 ApplicationInsights.config 文件中:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">

  ...

  <ContextInitializers>

    ...

    <Add Type="Utilities.AppInsightsCurrentRoleIdAsTagInitializer, Utilities" />
  </ContextInitializers>

  ...

</ApplicationInsights>
于 2015-03-05T10:03:10.667 回答
0

在我的 IAAS 模型中,我引用了这些脚本来设置它,在安装和重新启动 IIS 之前,它还使用直接 msi URL 而不是 Web 平台。

我为此使用了 PowerShell DSC 并进行了某种蓝绿色 IIS 重置,因此不会出现中断,因此服务器可以配置状态监视器作为初始配置的一部分,并且应用程序代码可以驱动遥测。

Package AppInsights
     {                             

        Ensure      = "Present"  
        Path        = "E:\dsc\ApplicationInsightsAgent.msi" # copied at some vm location
        Name        = "Application Insights Status Monitor"
        ProductID   =  "CBF2C62C-9537-4D8E-9754-92E54A0822D4"                       
    }

如果您的 web.config 没有检测密钥,您可以在安装后使用以下命令在 DSC 中启用您的网站。

Import-Module 'C:\Program Files\Microsoft Application Insights\Status Monitor\PowerShell\Microsoft.Diagnostics.Agent.StatusMonitor.PowerShell.dll'
Start-ApplicationInsightsMonitoring -Name 'your web site' -InstrumentationKey '<KeyHere>'
于 2018-05-09T23:36:15.197 回答