yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/loganalytics.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"time"
    21  
    22  	"yunion.io/x/onecloud/pkg/util/httputils"
    23  )
    24  
    25  type SLoganalyticsWorkspace struct {
    26  	Id                               string                           `json:"id"`
    27  	Location                         string                           `json:"location"`
    28  	Name                             string                           `json:"name"`
    29  	SLoganalyticsWorkspaceProperties SLoganalyticsWorkspaceProperties `json:"properties"`
    30  	Type                             string                           `json:"type"`
    31  }
    32  
    33  type SLoganalyticsWorkspaceProperties struct {
    34  	CreatedDate                                      string                                           `json:"createdDate"`
    35  	CustomerId                                       string                                           `json:"customerId"`
    36  	SLoganalyticsWorkspacePropertiesFeatures         SLoganalyticsWorkspacePropertiesFeatures         `json:"features"`
    37  	ModifiedDate                                     string                                           `json:"modifiedDate"`
    38  	ProvisioningState                                string                                           `json:"provisioningState"`
    39  	PublicNetworkAccessForIngestion                  string                                           `json:"publicNetworkAccessForIngestion"`
    40  	PublicNetworkAccessForQuery                      string                                           `json:"publicNetworkAccessForQuery"`
    41  	RetentionInDays                                  int                                              `json:"retentionInDays"`
    42  	SLoganalyticsWorkspacePropertiesSku              SLoganalyticsWorkspacePropertiesSku              `json:"sku"`
    43  	Source                                           string                                           `json:"source"`
    44  	SLoganalyticsWorkspacePropertiesWorkspaceCapping SLoganalyticsWorkspacePropertiesWorkspaceCapping `json:"workspaceCapping"`
    45  }
    46  
    47  type SLoganalyticsWorkspacePropertiesWorkspaceCapping struct {
    48  	DailyQuotaGb        int    `json:"dailyQuotaGb"`
    49  	DataIngestionStatus string `json:"dataIngestionStatus"`
    50  	QuotaNextResetTime  string `json:"quotaNextResetTime"`
    51  }
    52  
    53  type SLoganalyticsWorkspacePropertiesSku struct {
    54  	LastSkuUpdate string `json:"lastSkuUpdate"`
    55  	Name          string `json:"name"`
    56  }
    57  
    58  type SLoganalyticsWorkspacePropertiesFeatures struct {
    59  	EnableLogAccessUsingOnlyResourcePermissions bool `json:"enableLogAccessUsingOnlyResourcePermissions"`
    60  	Legacy                                      int  `json:"legacy"`
    61  	SearchVersion                               int  `json:"searchVersion"`
    62  }
    63  
    64  func (self *SAzureClient) GetLoganalyticsWorkspaces() ([]SLoganalyticsWorkspace, error) {
    65  	if len(self.workspaces) > 0 {
    66  		return self.workspaces, nil
    67  	}
    68  	self.workspaces = []SLoganalyticsWorkspace{}
    69  	return self.workspaces, self.list("Microsoft.OperationalInsights/workspaces", url.Values{}, &self.workspaces)
    70  }
    71  
    72  type WorkspaceData struct {
    73  	Name    string
    74  	Columns []struct {
    75  		Name string
    76  		Type string
    77  	}
    78  	Rows [][]string
    79  }
    80  
    81  func (self *SAzureClient) GetInstanceDiskUsage(workspace string, instanceId string, start, end time.Time) ([]WorkspaceData, error) {
    82  	params := url.Values{}
    83  	params.Set("timespan", "P1D")
    84  	query := fmt.Sprintf(`Perf | where ObjectName == "LogicalDisk" or ObjectName == "Logical Disk" | where _ResourceId == "%s" | where InstanceName != "_Total" | where CounterName == "%% Free Space" | where TimeGenerated between(datetime("%s") .. datetime("%s")) |  project TimeGenerated, InstanceName, CounterValue, Computer, _ResourceId`, instanceId, start.Format(time.RFC3339), end.Format(time.RFC3339))
    85  	params.Set("query", query)
    86  	resp, err := self.ljsonRequest(string(httputils.GET), fmt.Sprintf("v1/workspaces/%s/query", workspace), nil, params)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	ret := []WorkspaceData{}
    91  	return ret, resp.Unmarshal(&ret, "tables")
    92  }