首页 > 其他分享 >【Azure Policy】分享Policy实现对Azure Activity Log导出到Log A workspace中

【Azure Policy】分享Policy实现对Azure Activity Log导出到Log A workspace中

时间:2024-09-05 20:02:42浏览次数:12  
标签:category Log parameters enabled Azure logsEnabled Policy

问题描述

使用Azure Policy服务,对公司内部全部的订阅下的Activity Log,都需要配置导出到Log A Workspace中。

以下Policy规则可以实现此目的。

 

Policy内容说明

在Policy Rule部分中,选择资源的类型为 "Microsoft.Resources/subscriptions", 效果使用 DeployIfNotExists (如果不存在,则通过修复任务进行修正。

在 existenceCondition 条件中,如果当前订阅已经启用了 diagnostic setting并且输出日志到同一个Log A workspace,表示满足Policy要求,不需要进行修正。

在 deployment 中,使用了 ARM 模板, 为订阅添加Diagnostic Setting并且所有的日志Category均启用。

因 deployment 操作,会修改诊断日志配置(属于Monitor服务)以及Log A Workspace,所以需要为这个ARM Deployment操作给与两个contributor权限, 即 roleDefinitionIds 中的内容。

使用三个输入参数 logAnalytics, effect, logsEnabled 作为Policy的判断条件。

Policy示例

{
  "mode": "All",
  "policyRule": {
    "if": {
      "field": "type",
      "equals": "Microsoft.Resources/subscriptions"
    },
    "then": {
      "effect": "[parameters('effect')]",
      "details": {
        "type": "Microsoft.Insights/diagnosticSettings",
        "deploymentScope": "subscription",
        "existenceScope": "subscription",
        "existenceCondition": {
          "allOf": [
            {
              "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
              "equals": "[parameters('logsEnabled')]"
            },
            {
              "field": "Microsoft.Insights/diagnosticSettings/workspaceId",
              "equals": "[parameters('logAnalytics')]"
            }
          ]
        },
        "deployment": {
          "location": "chinaeast2",
          "properties": {
            "mode": "incremental",
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "logAnalytics": {
                  "type": "string"
                },
                "logsEnabled": {
                  "type": "string"
                }
              },
              "variables": {},
              "resources": [
                {
                  "name": "policy-enabled-ActivityLogs-to-logA",
                  "type": "Microsoft.Insights/diagnosticSettings",
                  "apiVersion": "2017-05-01-preview",
                  "location": "Global",
                  "properties": {
                    "workspaceId": "[parameters('logAnalytics')]",
                    "logs": [
                      {
                        "category": "Administrative",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "Security",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "ServiceHealth",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "Alert",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "Recommendation",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "Policy",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "Autoscale",
                        "enabled": "[parameters('logsEnabled')]"
                      },
                      {
                        "category": "ResourceHealth",
                        "enabled": "[parameters('logsEnabled')]"
                      }
                    ]
                  }
                }
              ],
              "outputs": {}
            },
            "parameters": {
              "logAnalytics": {
                "value": "[parameters('logAnalytics')]"
              },
              "logsEnabled": {
                "value": "[parameters('logsEnabled')]"
              }
            }
          }
        },
        "roleDefinitionIds": [
          "/providers/microsoft.authorization/roleDefinitions/749f88d5-cbae-40b8-bcfc-e573ddc772fa",
          "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
        ]
      }
    }
  },
  "parameters": {
    "logAnalytics": {
      "type": "String",
      "metadata": {
        "displayName": "Primary Log Analytics workspace",
        "description": "If this workspace is outside of the scope of the assignment you must manually grant 'Log Analytics Contributor' permissions (or similar) to the policy assignment's principal ID.",
        "strongType": "omsWorkspace",
        "assignPermissions": true
      }
    },
    "effect": {
      "type": "String",
      "metadata": {
        "displayName": "Effect",
        "description": "Enable or disable the execution of the policy"
      },
      "allowedValues": [
        "DeployIfNotExists",
        "Disabled"
      ],
      "defaultValue": "DeployIfNotExists"
    },
    "logsEnabled": {
      "type": "String",
      "metadata": {
        "displayName": "Enable logs",
        "description": "Whether to enable logs stream to the Log Analytics workspace - True or False"
      },
      "allowedValues": [
        "True",
        "False"
      ],
      "defaultValue": "True"
    }
  }
}

 

标签:category,Log,parameters,enabled,Azure,logsEnabled,Policy
From: https://www.cnblogs.com/lulight/p/18399171

相关文章

  • AI Logo制作工具网站——LogoAI.ai
    AILogo制作工具,可通过输入文本生成Logo。可自定义颜色、字体、布局等,生成的Logo可以无水印下载。功能介绍:免费每日使用配额:每天可免费使用AI生成Logo。高级自定义选项:支持对Logo的颜色、字体、布局和图标进行自定义设计,以适应不同需求。无水印下载:生成的Logo无水印,可以直接......
  • java.lang.ClassNotFoundException: net.logstash.logback.appender.LogstashTcpSocke
    出现以下错误,是因为项目缺少依赖:atorg.springframework.boot.logging.logback.LogbackLoggingSystem.reportConfigurationErrorsIfNecessary(LogbackLoggingSystem.java:189)atorg.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(Log......
  • Loki ERROR log告警
    实现loki采集error日志,出现error日志,告警。1.修改Loki配置文件在loki-local-config.yml中添加Alertmanager的配置,包括告警规则存储路径、Alertmanager的URL等。例如:ruler:enable_api:trueenable_alertmanager_v2:truealertmanager_refresh_interval:3m0salertmanager_......
  • Anylogic(2)——导出Java程序bat无法运行(Windows)
    1.Anylogic打包以后,双击bat,无论如何都无法运行。找了很多资料,最后得出可能是Java版本问题,因为最初安装anylogic的版本是8.9,Java安装得是1.8.x版本。改为安装Java9,但是依旧报错,也不知道具体,经痛苦多番挣扎,有2种情况。(报错如下)。 两种情况:①路径问题,因为某些时候Java......
  • 【Azure Policy】使用deployIfNotExists 把 Azure Activity logs 导出保存在Storage A
    问题描述使用AzurePolicy,对订阅下的全部ActivityLog配置DiagnosticSetting,要求:在Subscription或ManagementGroup级别,针对未启用ActivityLog功能的订阅,启用ActivityLog功能;对已经启用了Activitylog功能的订阅,使用该Policy纠正并统一其参数配置;所收集到的AzureActivityLog存......
  • log4net
    建表CREATETABLE[dbo].[LogDetails]([LogID]intNOTNULLIDENTITY(1,1),[LogDate]datetimeNOTNULL,[LogThread]nvarchar(100)NOTNULL,[LogLevel]nvarchar(200)NOTNULL,[LogLogger]nvarchar(500)NOTNULL,[LogMessage]nvarchar(3000)NOTNULL,[Lo......
  • 【Azure Policy】使用deployIfNotExists 把 Azure Activity logs 导出保存在Storage A
    问题描述使用AzurePolicy,对订阅下的全部ActivityLog配置DiagnosticSetting,要求:在Subscription或ManagementGroup级别,针对未启用ActivityLog功能的订阅,启用ActivityLog功能;对已经启用了Activitylog功能的订阅,使用该Policy纠正并统一其参数配置;所收集到的AzureActivity......
  • 【Azure Redis】Redis-CLI连接Redis 6380端口始终遇见 I/O Error
    问题描述使用Redis-cli连接Redis服务,因为工具无法直接支持TLS6380端口连接,所以需要使用stunnel配置TLS/SSL服务。根据文章(LinuxVM使用6380端口(SSL方式)连接AzureRedis(redis-cli&stunnel):https://www.cnblogs.com/lulight/p/14188279.html),配置stunnel后,始终......
  • mac 上golang编译 安卓系统的so 错误 'android/log.h' file not found
    lib.gopackagemainimport"C"//exportSpeedTestfuncSpeedTest(config*C.char){ configContent:=C.GoString(config) run(configContent)}funcmain(){}需要安装NDK,用Androidstudio安装,在SDKManeger的SDKTool里选择安装NDK(sidebyside),成功后一般在......
  • docker安装logstash7
     一、创建网络dockernetworkcreate-dbridgeelastic#和elasticsearch使用同一个网络 二、拉取logstash镜像dockerpulllogstash:7.1.1 三、创建logstash容器dockerrun-it\--namelogstash\-p9600:9600\-p5044:5044\--netelast......