diff --git a/azure/monitoring_metric.go b/azure/monitoring_metric.go new file mode 100644 index 00000000..ceacc165 --- /dev/null +++ b/azure/monitoring_metric.go @@ -0,0 +1,164 @@ +package azure + +import ( + "context" + "strings" + "time" + + "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-04-01-preview/insights" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +type monitoringMetric struct { + // Resource Name + DimensionValue string + // MetadataValue represents a metric metadata value. + MetaData *insights.MetadataValue + // Metric the result data of a query. + Metric *insights.Metric + // The maximum metric value for the data point. + Maximum *float64 + // The minimum metric value for the data point. + Minimum *float64 + // The average of the metric values that correspond to the data point. + Average *float64 + // The number of metric values that contributed to the aggregate value of this data point. + SampleCount *float64 + // The sum of the metric values for the data point. + Sum *float64 + // The time stamp used for the data point. + TimeStamp string + // The units in which the metric value is reported. + Unit string +} + +//// TABLE DEFINITION + +func monitoringMetricColumns(columns []*plugin.Column) []*plugin.Column { + return append(columns, commonMonitoringMetricColumns()...) +} + +func commonMonitoringMetricColumns() []*plugin.Column { + return []*plugin.Column{ + { + Name: "maximum", + Description: "The maximum metric value for the data point.", + Type: proto.ColumnType_DOUBLE, + }, + { + Name: "minimum", + Description: "The minimum metric value for the data point.", + Type: proto.ColumnType_DOUBLE, + }, + { + Name: "average", + Description: "The average of the metric values that correspond to the data point.", + Type: proto.ColumnType_DOUBLE, + }, + { + Name: "sample_count", + Description: "The number of metric values that contributed to the aggregate value of this data point.", + Type: proto.ColumnType_DOUBLE, + }, + { + Name: "sum", + Description: "The sum of the metric values for the data point.", + Type: proto.ColumnType_DOUBLE, + }, + { + Name: "timestamp", + Description: "The time stamp used for the data point.", + Type: proto.ColumnType_TIMESTAMP, + Transform: transform.FromField("TimeStamp"), + }, + { + Name: "unit", + Description: "The units in which the metric value is reported.", + Type: proto.ColumnType_STRING, + }, + { + Name: "resource_group", + Description: ColumnDescriptionResourceGroup, + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(extractResourceGroupFromID), + }, + { + Name: "subscription_id", + Description: ColumnDescriptionSubscription, + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(idToSubscriptionID), + }, + } +} + +func getMonitoringIntervalForGranularity(granularity string) string { + switch strings.ToUpper(granularity) { + case "DAILY": + // 24 hours + return "PT24H" + case "HOURLY": + // 1 hour + return "PT1H" + } + // else 5 minutes + return "PT5M" +} + +func getMonitoringStartDateForGranularity(granularity string) string { + switch strings.ToUpper(granularity) { + case "DAILY": + // Last 1 year + return time.Now().UTC().AddDate(-1, 0, 0).Format(time.RFC3339) + case "HOURLY": + // Last 60 days + return time.Now().UTC().AddDate(0, 0, -60).Format(time.RFC3339) + } + // Last 5 days + return time.Now().UTC().AddDate(0, 0, -5).Format(time.RFC3339) +} + +func listAzureMonitorMetricStatistics(ctx context.Context, d *plugin.QueryData, granularity string, metricNameSpace string, metricNames string, dimensionValue string) (interface{}, error) { + session, err := GetNewSession(ctx, d, "MANAGEMENT") + if err != nil { + return nil, err + } + subscriptionID := session.SubscriptionID + + monitoringClient := insights.NewMetricsClient(subscriptionID) + monitoringClient.Authorizer = session.Authorizer + + // Define param values + interval := getMonitoringIntervalForGranularity(granularity) + aggregation := "average,count,maximum,minimum,total" + timeSpan := getMonitoringStartDateForGranularity(granularity) + "/" + time.Now().UTC().AddDate(0, 0, 1).Format(time.RFC3339) // Retrieve data within a year + orderBy := "timestamp" + top := int32(1000) // Maximum number of record fetch with given interval + filter := "" + + result, err := monitoringClient.List(ctx, dimensionValue, timeSpan, &interval, metricNames, aggregation, &top, orderBy, filter, insights.ResultTypeData, metricNameSpace) + if err != nil { + return nil, err + } + for _, metric := range *result.Value { + for _, timeseries := range *metric.Timeseries { + for _, data := range *timeseries.Data { + if data.Average != nil { + d.StreamListItem(ctx, &monitoringMetric{ + DimensionValue: dimensionValue, + TimeStamp: data.TimeStamp.Format(time.RFC3339), + Maximum: data.Maximum, + Minimum: data.Minimum, + Average: data.Average, + Sum: data.Total, + SampleCount: data.Count, + Unit: string(metric.Unit), + }) + } + } + } + } + + return nil, nil +} diff --git a/azure/plugin.go b/azure/plugin.go index 616776bc..46d773ef 100644 --- a/azure/plugin.go +++ b/azure/plugin.go @@ -22,67 +22,76 @@ func Plugin(ctx context.Context) *plugin.Plugin { Schema: ConfigSchema, }, TableMap: map[string]*plugin.Table{ - "azure_ad_group": tableAzureAdGroup(ctx), - "azure_ad_service_principal": tableAzureAdServicePrincipal(ctx), - "azure_ad_user": tableAzureAdUser(ctx), - "azure_api_management": tableAzureAPIManagement(ctx), - "azure_app_service_environment": tableAzureAppServiceEnvironment(ctx), - "azure_app_service_function_app": tableAzureAppServiceFunctionApp(ctx), - "azure_app_service_plan": tableAzureAppServicePlan(ctx), - "azure_app_service_web_app": tableAzureAppServiceWebApp(ctx), - "azure_application_security_group": tableAzureApplicationSecurityGroup(ctx), - "azure_compute_availability_set": tableAzureComputeAvailabilitySet(ctx), - "azure_compute_disk": tableAzureComputeDisk(ctx), - "azure_compute_disk_encryption_set": tableAzureComputeDiskEncryptionSet(ctx), - "azure_compute_image": tableAzureComputeImage(ctx), - "azure_compute_resource_sku": tableAzureResourceSku(ctx), - "azure_compute_snapshot": tableAzureComputeSnapshot(ctx), - "azure_compute_virtual_machine": tableAzureComputeVirtualMachine(ctx), - "azure_cosmosdb_account": tableAzureCosmosDBAccount(ctx), - "azure_cosmosdb_mongo_database": tableAzureCosmosDBMongoDatabase(ctx), - "azure_cosmosdb_sql_database": tableAzureCosmosDBSQLDatabase(ctx), - "azure_diagnostic_setting": tableAzureDiagnosticSetting(ctx), - "azure_express_route_circuit": tableAzureExpressRouteCircuit(ctx), - "azure_firewall": tableAzureFirewall(ctx), - "azure_key_vault": tableAzureKeyVault(ctx), - "azure_key_vault_key": tableAzureKeyVaultKey(ctx), - "azure_key_vault_secret": tableAzureKeyVaultSecret(ctx), - "azure_kubernetes_cluster": tableAzureKubernetesCluster(ctx), - "azure_location": tableAzureLocation(ctx), - "azure_log_alert": tableAzureLogAlert(ctx), - "azure_log_profile": tableAzureLogProfile(ctx), - "azure_management_lock": tableAzureManagementLock(ctx), - "azure_mysql_server": tableAzureMySQLServer(ctx), - "azure_network_interface": tableAzureNetworkInterface(ctx), - "azure_network_security_group": tableAzureNetworkSecurityGroup(ctx), - "azure_network_watcher": tableAzureNetworkWatcher(ctx), - "azure_network_watcher_flow_log": tableAzureNetworkWatcherFlowLog(ctx), - "azure_policy_assignment": tableAzurePolicyAssignment(ctx), - "azure_policy_definition": tableAzurePolicyDefinition(ctx), - "azure_postgresql_server": tableAzurePostgreSqlServer(ctx), - "azure_provider": tableAzureProvider(ctx), - "azure_public_ip": tableAzurePublicIP(ctx), - "azure_resource_group": tableAzureResourceGroup(ctx), - "azure_role_assignment": tableAzureIamRoleAssignment(ctx), - "azure_role_definition": tableAzureIamRoleDefinition(ctx), - "azure_route_table": tableAzureRouteTable(ctx), - "azure_security_center_auto_provisioning": tableAzureSecurityCenterAutoProvisioning(ctx), - "azure_security_center_contact": tableAzureSecurityCenterContact(ctx), - "azure_security_center_setting": tableAzureSecurityCenterSetting(ctx), - "azure_security_center_subscription_pricing": tableAzureSecurityCenterPricing(ctx), - "azure_sql_database": tableAzureSqlDatabase(ctx), - "azure_sql_server": tableAzureSQLServer(ctx), - "azure_storage_account": tableAzureStorageAccount(ctx), - "azure_storage_blob": tableAzureStorageBlob(ctx), - "azure_storage_blob_service": tableAzureStorageBlobService(ctx), - "azure_storage_container": tableAzureStorageContainer(ctx), - "azure_storage_queue": tableAzureStorageQueue(ctx), - "azure_storage_table": tableAzureStorageTable(ctx), - "azure_storage_table_service": tableAzureStorageTableService(ctx), - "azure_subnet": tableAzureSubnet(ctx), - "azure_subscription": tableAzureSubscription(ctx), - "azure_tenant": tableAzureTenant(ctx), - "azure_virtual_network": tableAzureVirtualNetwork(ctx), + "azure_ad_group": tableAzureAdGroup(ctx), + "azure_ad_service_principal": tableAzureAdServicePrincipal(ctx), + "azure_ad_user": tableAzureAdUser(ctx), + "azure_api_management": tableAzureAPIManagement(ctx), + "azure_app_service_environment": tableAzureAppServiceEnvironment(ctx), + "azure_app_service_function_app": tableAzureAppServiceFunctionApp(ctx), + "azure_app_service_plan": tableAzureAppServicePlan(ctx), + "azure_app_service_web_app": tableAzureAppServiceWebApp(ctx), + "azure_application_security_group": tableAzureApplicationSecurityGroup(ctx), + "azure_compute_availability_set": tableAzureComputeAvailabilitySet(ctx), + "azure_compute_disk": tableAzureComputeDisk(ctx), + "azure_compute_disk_encryption_set": tableAzureComputeDiskEncryptionSet(ctx), + "azure_compute_disk_metric_read_ops": tableComputeDisksReadOpsMetric(ctx), + "azure_compute_disk_metric_read_ops_hourly": tableComputeDisksReadOpsMetricHourly(ctx), + "azure_compute_disk_metric_read_ops_daily": tableComputeDisksReadOpsMetricDaily(ctx), + "azure_compute_disk_metric_write_ops": tableComputeDisksWriteOpsMetric(ctx), + "azure_compute_disk_metric_write_ops_daily": tableComputeDisksWriteOpsMetricDaily(ctx), + "azure_compute_disk_metric_write_ops_hourly": tableComputeDisksWriteOpsMetricHourly(ctx), + "azure_compute_image": tableAzureComputeImage(ctx), + "azure_compute_resource_sku": tableAzureResourceSku(ctx), + "azure_compute_snapshot": tableAzureComputeSnapshot(ctx), + "azure_compute_virtual_machine": tableAzureComputeVirtualMachine(ctx), + "azure_compute_virtual_machine_metric_cpu_utilization": tableComputeInstanceCpuUtilizationMetric(ctx), + "azure_compute_virtual_machine_metric_cpu_utilization_daily": tableComputeInstanceCpuUtilizationMetricDaily(ctx), + "azure_compute_virtual_machine_metric_cpu_utilization_hourly": tableComputeInstanceCpuUtilizationMetricHourly(ctx), + "azure_cosmosdb_account": tableAzureCosmosDBAccount(ctx), + "azure_cosmosdb_mongo_database": tableAzureCosmosDBMongoDatabase(ctx), + "azure_cosmosdb_sql_database": tableAzureCosmosDBSQLDatabase(ctx), + "azure_diagnostic_setting": tableAzureDiagnosticSetting(ctx), + "azure_express_route_circuit": tableAzureExpressRouteCircuit(ctx), + "azure_firewall": tableAzureFirewall(ctx), + "azure_key_vault": tableAzureKeyVault(ctx), + "azure_key_vault_key": tableAzureKeyVaultKey(ctx), + "azure_key_vault_secret": tableAzureKeyVaultSecret(ctx), + "azure_kubernetes_cluster": tableAzureKubernetesCluster(ctx), + "azure_location": tableAzureLocation(ctx), + "azure_log_alert": tableAzureLogAlert(ctx), + "azure_log_profile": tableAzureLogProfile(ctx), + "azure_management_lock": tableAzureManagementLock(ctx), + "azure_mysql_server": tableAzureMySQLServer(ctx), + "azure_network_interface": tableAzureNetworkInterface(ctx), + "azure_network_security_group": tableAzureNetworkSecurityGroup(ctx), + "azure_network_watcher": tableAzureNetworkWatcher(ctx), + "azure_network_watcher_flow_log": tableAzureNetworkWatcherFlowLog(ctx), + "azure_policy_assignment": tableAzurePolicyAssignment(ctx), + "azure_policy_definition": tableAzurePolicyDefinition(ctx), + "azure_postgresql_server": tableAzurePostgreSqlServer(ctx), + "azure_provider": tableAzureProvider(ctx), + "azure_public_ip": tableAzurePublicIP(ctx), + "azure_resource_group": tableAzureResourceGroup(ctx), + "azure_role_assignment": tableAzureIamRoleAssignment(ctx), + "azure_role_definition": tableAzureIamRoleDefinition(ctx), + "azure_route_table": tableAzureRouteTable(ctx), + "azure_security_center_auto_provisioning": tableAzureSecurityCenterAutoProvisioning(ctx), + "azure_security_center_contact": tableAzureSecurityCenterContact(ctx), + "azure_security_center_setting": tableAzureSecurityCenterSetting(ctx), + "azure_security_center_subscription_pricing": tableAzureSecurityCenterPricing(ctx), + "azure_sql_database": tableAzureSqlDatabase(ctx), + "azure_sql_server": tableAzureSQLServer(ctx), + "azure_storage_account": tableAzureStorageAccount(ctx), + "azure_storage_blob": tableAzureStorageBlob(ctx), + "azure_storage_blob_service": tableAzureStorageBlobService(ctx), + "azure_storage_container": tableAzureStorageContainer(ctx), + "azure_storage_queue": tableAzureStorageQueue(ctx), + "azure_storage_table": tableAzureStorageTable(ctx), + "azure_storage_table_service": tableAzureStorageTableService(ctx), + "azure_subnet": tableAzureSubnet(ctx), + "azure_subscription": tableAzureSubscription(ctx), + "azure_tenant": tableAzureTenant(ctx), + "azure_virtual_network": tableAzureVirtualNetwork(ctx), // "azure_storage_table": tableAzureStorageTable(ctx), }, } diff --git a/azure/table_azure_compute_disk_metric_read_ops.go b/azure/table_azure_compute_disk_metric_read_ops.go new file mode 100644 index 00000000..c49c9ebd --- /dev/null +++ b/azure/table_azure_compute_disk_metric_read_ops.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksReadOpsMetric(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_read_ops", + Description: "Azure Compute Disk Metrics - Read Ops", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricReadOps, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricReadOps(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "FIVE_MINUTES", "Microsoft.Compute/disks", "Composite Disk Read Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_disk_metric_read_ops_daily.go b/azure/table_azure_compute_disk_metric_read_ops_daily.go new file mode 100644 index 00000000..c97409b2 --- /dev/null +++ b/azure/table_azure_compute_disk_metric_read_ops_daily.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksReadOpsMetricDaily(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_read_ops_daily", + Description: "Azure Compute Disk Metrics - Read Ops (Daily)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricReadOpsDaily, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricReadOpsDaily(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "DAILY", "Microsoft.Compute/disks", "Composite Disk Read Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_disk_metric_read_ops_hourly.go b/azure/table_azure_compute_disk_metric_read_ops_hourly.go new file mode 100644 index 00000000..3a3a08f0 --- /dev/null +++ b/azure/table_azure_compute_disk_metric_read_ops_hourly.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksReadOpsMetricHourly(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_read_ops_hourly", + Description: "Azure Compute Disk Metrics - Read Ops (Hourly)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricReadOpsHourly, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricReadOpsHourly(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "HOURLY", "Microsoft.Compute/disks", "Composite Disk Read Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_disk_metric_write_ops.go b/azure/table_azure_compute_disk_metric_write_ops.go new file mode 100644 index 00000000..c2f7bf2b --- /dev/null +++ b/azure/table_azure_compute_disk_metric_write_ops.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksWriteOpsMetric(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_write_ops", + Description: "Azure Compute Disk Metrics - Write Ops", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricWriteOps, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricWriteOps(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "FIVE_MINUTES", "Microsoft.Compute/disks", "Composite Disk Write Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_disk_metric_write_ops_daily.go b/azure/table_azure_compute_disk_metric_write_ops_daily.go new file mode 100644 index 00000000..e35a873d --- /dev/null +++ b/azure/table_azure_compute_disk_metric_write_ops_daily.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksWriteOpsMetricDaily(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_write_ops_daily", + Description: "Azure Compute Disk Metrics - Write Ops (Daily)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricWriteOpsDaily, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricWriteOpsDaily(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "DAILY", "Microsoft.Compute/disks", "Composite Disk Write Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_disk_metric_write_ops_hourly.go b/azure/table_azure_compute_disk_metric_write_ops_hourly.go new file mode 100644 index 00000000..c92f1875 --- /dev/null +++ b/azure/table_azure_compute_disk_metric_write_ops_hourly.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeDisksWriteOpsMetricHourly(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_disk_metric_write_ops_hourly", + Description: "Azure Compute Disk Metrics - Write Ops (Hourly)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeDisks, + Hydrate: listComputeDiskMetricWriteOpsHourly, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the disk.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeDiskMetricWriteOpsHourly(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + diskInfo := h.Item.(compute.Disk) + + return listAzureMonitorMetricStatistics(ctx, d, "HOURLY", "Microsoft.Compute/disks", "Composite Disk Write Operations/sec", *diskInfo.ID) +} diff --git a/azure/table_azure_compute_virtual_machine_metric_cpu_utilization.go b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization.go new file mode 100644 index 00000000..c399016e --- /dev/null +++ b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeInstanceCpuUtilizationMetric(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_virtual_machine_metric_cpu_utilization", + Description: "Azure Compute Virtual Machine Metrics - CPU Utilization", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeVirtualMachines, + Hydrate: listComputeVirtualMachineMetricCpuUtilization, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the virtual machine.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeVirtualMachineMetricCpuUtilization(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + vmInfo := h.Item.(compute.VirtualMachine) + + return listAzureMonitorMetricStatistics(ctx, d, "FIVE_MINUTES", "Microsoft.Compute/virtualMachines", "Percentage CPU", *vmInfo.ID) +} diff --git a/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_daily.go b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_daily.go new file mode 100644 index 00000000..91610b21 --- /dev/null +++ b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_daily.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeInstanceCpuUtilizationMetricDaily(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_virtual_machine_metric_cpu_utilization_daily", + Description: "Azure Compute Virtual Machine Metrics - CPU Utilization (Daily)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeVirtualMachines, + Hydrate: listComputeVirtualMachineMetricCpuUtilizationDaily, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the virtual machine.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeVirtualMachineMetricCpuUtilizationDaily(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + vmInfo := h.Item.(compute.VirtualMachine) + + return listAzureMonitorMetricStatistics(ctx, d, "DAILY", "Microsoft.Compute/virtualMachines", "Percentage CPU", *vmInfo.ID) +} diff --git a/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_hourly.go b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_hourly.go new file mode 100644 index 00000000..ae856273 --- /dev/null +++ b/azure/table_azure_compute_virtual_machine_metric_cpu_utilization_hourly.go @@ -0,0 +1,39 @@ +package azure + +import ( + "context" + + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute" + "github.com/turbot/steampipe-plugin-sdk/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/plugin" + "github.com/turbot/steampipe-plugin-sdk/plugin/transform" +) + +//// TABLE DEFINITION + +func tableComputeInstanceCpuUtilizationMetricHourly(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "azure_compute_virtual_machine_metric_cpu_utilization_hourly", + Description: "Azure Compute Virtual Machine Metrics - CPU Utilization (Hourly)", + List: &plugin.ListConfig{ + ParentHydrate: listAzureComputeVirtualMachines, + Hydrate: listComputeVirtualMachineMetricCpuUtilizationHourly, + }, + Columns: monitoringMetricColumns([]*plugin.Column{ + { + Name: "name", + Description: "The name of the virtual machine.", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("DimensionValue").Transform(lastPathElement), + }, + }), + } +} + +//// LIST FUNCTION + +func listComputeVirtualMachineMetricCpuUtilizationHourly(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + vmInfo := h.Item.(compute.VirtualMachine) + + return listAzureMonitorMetricStatistics(ctx, d, "HOURLY", "Microsoft.Compute/virtualMachines", "Percentage CPU", *vmInfo.ID) +} diff --git a/azure/utils.go b/azure/utils.go index e4971ed1..c8ecc8c9 100644 --- a/azure/utils.go +++ b/azure/utils.go @@ -46,6 +46,19 @@ func extractResourceGroupFromID(ctx context.Context, d *transform.TransformData) return resourceGroup, nil } +func lastPathElement(_ context.Context, d *transform.TransformData) (interface{}, error) { + return getLastPathElement(types.SafeString(d.Value)), nil +} + +func getLastPathElement(path string) string { + if path == "" { + return "" + } + + pathItems := strings.Split(path, "/") + return pathItems[len(pathItems)-1] +} + func convertDateToTime(ctx context.Context, d *transform.TransformData) (interface{}, error) { if d.Value == nil { return nil, nil diff --git a/docs/tables/azure_compute_disk_metric_read_ops.md b/docs/tables/azure_compute_disk_metric_read_ops.md new file mode 100644 index 00000000..9153b2c6 --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_read_ops.md @@ -0,0 +1,41 @@ +# Table: azure_compute_disk_metric_read_ops + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_read_ops` table provides metric statistics at 5 minutes intervals for the most recent 5 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_read_ops +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_read_ops, + round(maximum::numeric,2) as max_read_ops, + round(average::numeric,2) as avg_read_ops, + sample_count +from + azure_compute_disk_metric_read_ops +where + average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_disk_metric_read_ops_daily.md b/docs/tables/azure_compute_disk_metric_read_ops_daily.md new file mode 100644 index 00000000..bb89e58d --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_read_ops_daily.md @@ -0,0 +1,41 @@ +# Table: azure_compute_disk_metric_read_ops_daily + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_read_ops_daily` table provides metric statistics at 24 hours intervals for the most recent 1 year. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_read_ops_daily +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_read_ops, + round(maximum::numeric,2) as max_read_ops, + round(average::numeric,2) as avg_read_ops, + sample_count +from + azure_compute_disk_metric_read_ops_daily +where + average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_disk_metric_read_ops_hourly.md b/docs/tables/azure_compute_disk_metric_read_ops_hourly.md new file mode 100644 index 00000000..9c4ebed6 --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_read_ops_hourly.md @@ -0,0 +1,41 @@ +# Table: azure_compute_disk_metric_read_ops_hourly + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_read_ops_hourly` table provides metric statistics at 1 hour intervals for the most recent 60 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_read_ops_hourly +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_read_ops, + round(maximum::numeric,2) as max_read_ops, + round(average::numeric,2) as avg_read_ops, + sample_count +from + azure_compute_disk_metric_read_ops_hourly +where + average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_disk_metric_write_ops.md b/docs/tables/azure_compute_disk_metric_write_ops.md new file mode 100644 index 00000000..c1d223d8 --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_write_ops.md @@ -0,0 +1,40 @@ +# Table: azure_compute_disk_metric_write_ops + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_write_ops` table provides metric statistics at 5 minutes intervals for the most recent 5 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_write_ops +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_write_ops, + round(maximum::numeric,2) as max_write_ops, + round(average::numeric,2) as avg_write_ops, + sample_count +from + azure_compute_disk_metric_write_ops +where average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_disk_metric_write_ops_daily.md b/docs/tables/azure_compute_disk_metric_write_ops_daily.md new file mode 100644 index 00000000..2de1b536 --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_write_ops_daily.md @@ -0,0 +1,41 @@ +# Table: azure_compute_disk_metric_write_ops_daily + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_write_ops_daily` table provides metric statistics at 24 hours intervals for the most recent 1 year. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_write_ops_daily +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_write_ops, + round(maximum::numeric,2) as max_write_ops, + round(average::numeric,2) as avg_write_ops, + sample_count +from + azure_compute_disk_metric_write_ops_daily +where + average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_disk_metric_write_ops_hourly.md b/docs/tables/azure_compute_disk_metric_write_ops_hourly.md new file mode 100644 index 00000000..d879792b --- /dev/null +++ b/docs/tables/azure_compute_disk_metric_write_ops_hourly.md @@ -0,0 +1,41 @@ +# Table: azure_compute_disk_metric_write_ops_hourly + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_disk_metric_write_ops_hourly` table provides metric statistics at 1 hour intervals for the most recent 60 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_disk_metric_write_ops_hourly +order by + name, + timestamp; +``` + +### Operations Over 10 Bytes average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_write_ops, + round(maximum::numeric,2) as max_write_ops, + round(average::numeric,2) as avg_write_ops, + sample_count +from + azure_compute_disk_metric_write_ops_hourly +where + average > 10 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization.md b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization.md new file mode 100644 index 00000000..d6fa0c7c --- /dev/null +++ b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization.md @@ -0,0 +1,40 @@ +# Table: azure_compute_virtual_machine_metric_cpu_utilization + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_virtual_machine_metric_cpu_utilization` table provides metric statistics at 5 minutes intervals for the most recent 5 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization +order by + name, + timestamp; +``` + +### CPU Over 80% average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_cpu, + round(maximum::numeric,2) as max_cpu, + round(average::numeric,2) as avg_cpu, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization +where average > 80 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_daily.md b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_daily.md new file mode 100644 index 00000000..2d4f1a9f --- /dev/null +++ b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_daily.md @@ -0,0 +1,41 @@ +# Table: azure_compute_virtual_machine_metric_cpu_utilization_daily + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_virtual_machine_metric_cpu_utilization_daily` table provides metric statistics at 24 hours intervals for the most recent 1 year. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization_daily +order by + name, + timestamp; +``` + +### CPU Over 80% average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_cpu, + round(maximum::numeric,2) as max_cpu, + round(average::numeric,2) as avg_cpu, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization_daily +where + average > 80 +order by + name, + timestamp; +``` diff --git a/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_hourly.md b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_hourly.md new file mode 100644 index 00000000..43d81a2a --- /dev/null +++ b/docs/tables/azure_compute_virtual_machine_metric_cpu_utilization_hourly.md @@ -0,0 +1,41 @@ +# Table: azure_compute_virtual_machine_metric_cpu_utilization_hourly + +GCP Monitoring metrics provide data about the performance of your systems. The `azure_compute_virtual_machine_metric_cpu_utilization_hourly` table provides metric statistics at 1 hour intervals for the most recent 60 days. + +## Examples + +### Basic info + +```sql +select + name, + timestamp, + minimum, + maximum, + average, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization_hourly +order by + name, + timestamp; +``` + +### CPU Over 80% average + +```sql +select + name, + timestamp, + round(minimum::numeric,2) as min_cpu, + round(maximum::numeric,2) as max_cpu, + round(average::numeric,2) as avg_cpu, + sample_count +from + azure_compute_virtual_machine_metric_cpu_utilization_hourly +where + average > 80 +order by + name, + timestamp; +``` diff --git a/go.mod b/go.mod index 77726b59..0348f3a6 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,12 @@ module github.com/turbot/steampipe-plugin-azure go 1.15 require ( - github.com/Azure/azure-sdk-for-go v53.0.0+incompatible + github.com/Azure/azure-sdk-for-go v55.4.0+incompatible github.com/Azure/azure-storage-blob-go v0.12.0 github.com/Azure/go-autorest/autorest v0.11.17 github.com/Azure/go-autorest/autorest/azure/auth v0.5.6 github.com/Azure/go-autorest/autorest/date v0.3.0 github.com/gofrs/uuid v4.0.0+incompatible // indirect - github.com/satori/go.uuid v1.2.0 // indirect github.com/tombuildsstuff/giovanni v0.15.1 github.com/turbot/go-kit v0.1.1 github.com/turbot/steampipe-plugin-sdk v0.2.7 diff --git a/go.sum b/go.sum index 300193d6..5c4b8349 100644 --- a/go.sum +++ b/go.sum @@ -3,11 +3,8 @@ github.com/Azure/azure-pipeline-go v0.2.3 h1:7U9HBg1JFK3jHl5qmo4CTZKFTVgMwdFHMVt github.com/Azure/azure-pipeline-go v0.2.3/go.mod h1:x841ezTBIMG6O3lAcl8ATHnsOPVl2bqk7S3ta6S6u4k= github.com/Azure/azure-sdk-for-go v45.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v47.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= -github.com/Azure/azure-sdk-for-go v50.1.0+incompatible h1:SUR6Y194mjyNkNbEzDHyYX8Butfa+Om9fcGSIy0ffhk= -github.com/Azure/azure-sdk-for-go v50.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= -github.com/Azure/azure-sdk-for-go v53.0.0+incompatible h1:DHeVnEdW3l7C/vXSdig9IJzoEpxgukIpYDfKBq6zNSI= -github.com/Azure/azure-sdk-for-go v53.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= -github.com/Azure/azure-sdk-for-go v53.1.0+incompatible h1:f2h0KLVGa3zIaMDMHBe5Lazc0FT5+L78z0B8K9PmDyg= +github.com/Azure/azure-sdk-for-go v55.4.0+incompatible h1:zn3+QwgCjRgIRgE0iWpF2LrFy5er/T0e5/EHrp1Zgak= +github.com/Azure/azure-sdk-for-go v55.4.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-storage-blob-go v0.12.0 h1:7bFXA1QB+lOK2/ASWHhp6/vnxjaeeZq6t8w1Jyp0Iaw= github.com/Azure/azure-storage-blob-go v0.12.0/go.mod h1:A0u4VjtpgZJ7Y7um/+ix2DHBuEKFC6sEIlj0xc13a4Q= github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= @@ -80,7 +77,6 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= -github.com/gofrs/uuid v1.2.0 h1:coDhrjgyJaglxSjxuJdqQSSdUpG3w6p1OwN2od6frBU= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -166,8 +162,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=