如果您为 Windows Server 等操作系统自带许可,则根据合同义务,您可能需要监控和报告使用这些许可的虚拟机实例、物理服务器和物理 CPU 核心的数量。
本文介绍了如何使用开源工具许可跟踪器监控您使用的虚拟机实例、物理服务器和物理 CPU 核心的数量,以及如何使用 Looker Studio 直观呈现结果。
许可证跟踪器工具的工作原理是分析 Compute Engine 审核日志来确定每个虚拟机实例的展示位置。展示位置描述了虚拟机实例在特定物理服务器上运行的时间段。每次将虚拟机从一台物理服务器迁移到另一台物理服务器时,都会标记一个展示位置结束并启动另一个展示位置。
例如,假设一个虚拟机实例启动,然后在几个月后再次停止。在其运行时中的某个时刻,虚拟机自动从 Server 1
迁移到 Server 2
,然后再次迁移回 Server 1
。此历史记录对应于 3 次放置:
首次运行证书跟踪器工具时,它会分析过去 90 天的 Compute Engine 使用情况,并将其结果写入 BigQuery。在后续运行中,该工具会分析上次运行和当天之间的增量,并相应地更新 BigQuery 数据集。
许可跟踪器工具取代了 IAP Desktop 提供的使用情况报告功能。
费用
本指南使用 Google Cloud的收费组件,包括:
您可使用价格计算器根据您的预计使用情况来估算费用。
部署许可跟踪器工具
本部分介绍了如何将 Cloud Run 和 Cloud Scheduler 设置为每天自动运行一次许可跟踪器工具。
下图说明了本文中所述的解决方案:
- 许可跟踪器工具部署在 Cloud Run 上,并配置为分析一个或多个项目的日志。
- Cloud Scheduler 每天触发一次 Cloud Run 作业,从而使许可跟踪器工具更新 BigQuery 数据集。
- Looker 数据洞察信息中心会直观呈现 BigQuery 中的数据并将其提供给用户。
创建项目
如需设置 Cloud Run 和 Cloud Scheduler,请创建一个新项目:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, Cloud Logging, Resource Manager, Cloud Build, Cloud Scheduler, and Cloud Run APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, Cloud Logging, Resource Manager, Cloud Build, Cloud Scheduler, and Cloud Run APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. 在 Google Cloud 控制台中,点击激活 Cloud Shell
按钮,以打开 Cloud Shell。
设置一个环境变量,以包含您的项目 ID:
gcloud config set project PROJECT_ID
将 PROJECT_ID 替换为您的项目 ID。
设置要部署到的 Cloud Run 区域:
gcloud config set run/region REGION
将 REGION 替换为支持 Cloud Run 和 Cloud Scheduler 的区域。
为该工具创建服务账号:
SERVICE_ACCOUNT=$(gcloud iam service-accounts create license-tracker \ --display-name "License Tracker" \ --format "value(email)")
允许服务创建和访问 BigQuery 数据集,并在同一项目中启动 Cloud Run 作业:
gcloud projects add-iam-policy-binding $(gcloud config get-value core/project) \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/bigquery.admin" gcloud projects add-iam-policy-binding $(gcloud config get-value core/project) \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/run.invoker"
允许 Cloud Build 执行 Cloud Run 部署并管理 Cloud Scheduler 作业:
PROJECT_NUMBER=$(gcloud projects describe $(gcloud config get-value core/project) --format='value(projectNumber)') \ gcloud iam service-accounts add-iam-policy-binding $SERVICE_ACCOUNT \ --member "serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \ --role "roles/iam.serviceAccountUser" gcloud projects add-iam-policy-binding $(gcloud config get-value core/project) \ --member "serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \ --role "roles/run.developer" gcloud projects add-iam-policy-binding $(gcloud config get-value core/project) \ --member "serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \ --role "roles/cloudscheduler.admin"
克隆 GitHub 代码库并切换到
latest
分支:git clone https://github.com/GoogleCloudPlatform/gce-license-tracker.git cd gce-license-tracker git checkout latest
将构建提交到 Cloud Build:
gcloud builds submit . --substitutions=_REGION=$(gcloud config get-value run/region)
该构建会将许可跟踪器应用部署到 Cloud Run,并将 Cloud Scheduler 配置为每天触发一次作业。
构建大约需要 3 分钟才能完成。
- Compute Engine API 已启用。
- 项目授予 Compute Viewer (
roles/compute.viewer
) 和 Logs Viewer (roles/logging.viewer
) 对该工具服务账号的访问权限。 在 Google Cloud 控制台中,进入 Cloud Run > 作业。
选择 license-tracker 作业以打开作业详细信息。
点击执行。
根据您选择的项目的数量和大小,初始分析可能需要几个小时才能完成。
作业完成后,您的项目将包含一个名为
license_usage
的 BigQuery 数据集。(可选)在 Cloud Logging 中查看日志:
复制信息中心的数据源:
对于每个数据源,请执行以下操作:
- 点击上面的链接以打开相应数据源。
- 点击制作此数据源的副本 (Make a copy of this data source)。
- 点击复制数据源。
- 如果您被提升为将信息中心连接到 BigQuery,请点击授权。
- (可选)点击标头并重命名数据源。
- 在结算项目列表中,选择用于部署许可跟踪器工具的项目。
- 点击重新连接。
- 在应用连接更改 (Apply connection changes) 对话框中,选择应用。
复制信息中心:
打开示例信息中心。
信息中心未连接到数据源,因此不会显示任何数据。
点击 … > 制作副本
在复制此报告对话框中,选择数据源的副本:
点击复制报告。
信息中心现在会显示 BigQuery 数据集中的数据。
- 共享信息中心并授予用户查看报告的权限。
- 向用户授予 BigQuery Data Viewer (
roles/bigquery.dataViewer
) 和 BigQuery Job User (roles/bigquery.jobUser
) 角色。您可以在数据集或项目级层授予这些角色。 在 Google Cloud 控制台中,点击激活 Cloud Shell
按钮,以打开 Cloud Shell。
设置一个环境变量,以包含您的项目 ID:
gcloud config set project PROJECT_ID
将 PROJECT_ID 替换为包含现有部署的项目的 ID。
选择现有部署的 Cloud Run 区域:
gcloud config set run/region REGION
克隆 GitHub 代码库并切换到
latest
分支:git clone https://github.com/GoogleCloudPlatform/gce-license-tracker.git cd gce-license-tracker git checkout latest
将构建提交到 Cloud Build:
gcloud builds submit . --substitutions=_REGION=$(gcloud config get-value run/region)
构建会更新现有部署,大约需要 3 分钟才能完成。
部署应用
现在,将许可跟踪器工具部署到 Cloud Run:
选择要分析的项目
许可跟踪器工具会分析为其应用以下所有条件的所有 Google Cloud 项目:
如需在分析中包含项目、文件夹或整个组织,请执行以下操作:
项目
SCOPE_ID=RESOURCE_PROJECT_ID SCOPE_TYPE=projects gcloud projects add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/compute.viewer" \ --condition None gcloud projects add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/logging.viewer" \ --condition None
将 RESOURCE_PROJECT_ID 替换为您希望许可跟踪器工具分析的 Google Cloud项目的 ID。
文件夹
SCOPE_ID=RESOURCE_FOLDER_ID SCOPE_TYPE=folders gcloud resource-manager folders add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/compute.viewer" \ --condition None gcloud resource-manager folders add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/logging.viewer" \ --condition None
将 RESOURCE_FOLDER_ID 替换为许可跟踪器工具要分析的项目所在文件夹的 ID。
组织
SCOPE_ID=ORGANIZATION_ID SCOPE_TYPE=organizations gcloud organizations add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/compute.viewer" \ --condition None gcloud organizations add-iam-policy-binding $SCOPE_ID \ --member "serviceAccount:$SERVICE_ACCOUNT" \ --role "roles/logging.viewer" \ --condition None
将 ORGANIZATION_ID 替换为您的组织的 ID。
开始初始分析
您现在可以开始初始分析:
创建信息中心
现在,您可以通过创建示例信息中心的副本来创建 Looker 数据洞察信息中心:
授予其他用户对信息中心的访问权限
信息中心的数据源配置为使用查看者的凭据访问 BigQuery。如需向其他用户授予对信息中心的访问权限,您必须:
自定义信息中心
您可以使用 Looker 数据洞察报告编辑器修改现有图表或添加其他图表,从而自定义信息中心。
示例信息中心中的所有图表均基于 BigQuery 数据集中的 placements
视图。此视图包含所有项目的所有放置并使用以下架构:
列 | 数据类型 | 说明 |
---|---|---|
instance_id | INTEGER | 实例 ID |
instance_name | STRING | 实例名称 |
instance_zone | STRING | 实例的可用区 ID |
instance_project_id | STRING | 实例的项目 ID |
tenancy | STRING | 如果是在单租户节点上运行,则为 S ;否则为 F |
node_type | STRING | 单租户节点类型 |
node_project_id | STRING | 节点的项目 ID;如果是共享单租户节点,则此值可能与 instance_project_id 不同 |
server_id | STRING | 物理服务器的唯一 ID |
operating_system_family | STRING | WIN 、LINUX 或 null (如果无法识别) |
许可 | STRING | 映像使用的许可字符串 |
license_type | STRING | BYOL 、SPLA 或 null (如果无法识别) |
machine_type | STRING | 实例的机器类型 |
memory_mb | INTEGER | 分配给实例的 RAM 容量(以 MB 为单位) |
vcpu_count | INTEGER | 分配给实例的 vCPU 数量 |
vcpu_min_allocated | INTEGER | 分配给实例的 vCPU 数下限;如果您在单租户虚拟机上过度使用 CPU,则此值可能与 vcpu_count 不同 |
maintenance_policy | STRING | 实例的维护政策 |
start_date | TIMESTAMP | 展示位置开始 |
end_date | TIMESTAMP | 放置结束 |
您可以在自定义现有图表或向信息中心添加自己的图表时使用 placement
视图。
升级许可跟踪器工具
本部分介绍如何升级现有的许可跟踪器工具部署,以使用新版应用或使用其他配置。