loganalytics.go 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "time"
  19. "yunion.io/x/pkg/util/httputils"
  20. )
  21. type SLoganalyticsWorkspace struct {
  22. Id string `json:"id"`
  23. Location string `json:"location"`
  24. Name string `json:"name"`
  25. SLoganalyticsWorkspaceProperties SLoganalyticsWorkspaceProperties `json:"properties"`
  26. Type string `json:"type"`
  27. }
  28. type SLoganalyticsWorkspaceProperties struct {
  29. CreatedDate string `json:"createdDate"`
  30. CustomerId string `json:"customerId"`
  31. SLoganalyticsWorkspacePropertiesFeatures SLoganalyticsWorkspacePropertiesFeatures `json:"features"`
  32. ModifiedDate string `json:"modifiedDate"`
  33. ProvisioningState string `json:"provisioningState"`
  34. PublicNetworkAccessForIngestion string `json:"publicNetworkAccessForIngestion"`
  35. PublicNetworkAccessForQuery string `json:"publicNetworkAccessForQuery"`
  36. RetentionInDays int `json:"retentionInDays"`
  37. SLoganalyticsWorkspacePropertiesSku SLoganalyticsWorkspacePropertiesSku `json:"sku"`
  38. Source string `json:"source"`
  39. SLoganalyticsWorkspacePropertiesWorkspaceCapping SLoganalyticsWorkspacePropertiesWorkspaceCapping `json:"workspaceCapping"`
  40. }
  41. type SLoganalyticsWorkspacePropertiesWorkspaceCapping struct {
  42. DailyQuotaGb int `json:"dailyQuotaGb"`
  43. DataIngestionStatus string `json:"dataIngestionStatus"`
  44. QuotaNextResetTime string `json:"quotaNextResetTime"`
  45. }
  46. type SLoganalyticsWorkspacePropertiesSku struct {
  47. LastSkuUpdate string `json:"lastSkuUpdate"`
  48. Name string `json:"name"`
  49. }
  50. type SLoganalyticsWorkspacePropertiesFeatures struct {
  51. EnableLogAccessUsingOnlyResourcePermissions bool `json:"enableLogAccessUsingOnlyResourcePermissions"`
  52. Legacy int `json:"legacy"`
  53. SearchVersion int `json:"searchVersion"`
  54. }
  55. func (self *SAzureClient) GetLoganalyticsWorkspaces() ([]SLoganalyticsWorkspace, error) {
  56. if len(self.workspaces) > 0 {
  57. return self.workspaces, nil
  58. }
  59. self.workspaces = []SLoganalyticsWorkspace{}
  60. return self.workspaces, self.list("Microsoft.OperationalInsights/workspaces", url.Values{}, &self.workspaces)
  61. }
  62. type WorkspaceData struct {
  63. Name string
  64. Columns []struct {
  65. Name string
  66. Type string
  67. }
  68. Rows [][]string
  69. }
  70. func (self *SAzureClient) GetInstanceDiskUsage(workspace string, instanceId string, start, end time.Time) ([]WorkspaceData, error) {
  71. params := url.Values{}
  72. params.Set("timespan", "P1D")
  73. 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))
  74. params.Set("query", query)
  75. resp, err := self.ljsonRequest(string(httputils.GET), fmt.Sprintf("v1/workspaces/%s/query", workspace), nil, params)
  76. if err != nil {
  77. return nil, err
  78. }
  79. ret := []WorkspaceData{}
  80. return ret, resp.Unmarshal(&ret, "tables")
  81. }