influxdb_url.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 utils
  15. import (
  16. "context"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/sets"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. ansible_api "yunion.io/x/onecloud/pkg/apis/ansible"
  22. comapi "yunion.io/x/onecloud/pkg/apis/compute"
  23. )
  24. type sProxyEndpoint struct {
  25. Id string
  26. Address string
  27. }
  28. var ErrCannotReachInfluxbd = errors.Error("no suitable network to reach influxdb")
  29. func GetLocalArgs(serverDetails *comapi.ServerDetails, influxdbUrl string) map[string]interface{} {
  30. info := sServerInfo{}
  31. info.serverDetails = serverDetails
  32. info.ServerId = serverDetails.Id
  33. networkIds := sets.NewString()
  34. for _, nic := range serverDetails.Nics {
  35. networkIds.Insert(nic.NetworkId)
  36. info.VpcId = nic.VpcId
  37. }
  38. info.NetworkIds = networkIds.UnsortedList()
  39. return getArgs(&info, influxdbUrl)
  40. }
  41. func GetArgs(ctx context.Context, serverId, proxyEndpointId string, others interface{}) (map[string]interface{}, error) {
  42. host, ok := others.(*ansible_api.AnsibleHost)
  43. if !ok {
  44. return nil, errors.Error("unknown others, want *AnsibleHost")
  45. }
  46. info, err := GetServerInfo(ctx, serverId)
  47. if err != nil {
  48. return nil, errors.Wrapf(err, "unable to get serverInfo of server %s", serverId)
  49. }
  50. monitorUrl := info.serverDetails.MonitorUrl
  51. log.Infof("TSDB monitor Url: %s", monitorUrl)
  52. foundSvc := false
  53. errs := []error{}
  54. for _, svcName := range []string{apis.SERVICE_TYPE_INFLUXDB, apis.SERVICE_TYPE_VICTORIA_METRICS} {
  55. if tsdbUrl, err := FindValidServiceUrl(ctx, Service{svcName, monitorUrl}, proxyEndpointId, info, host); err != nil {
  56. errs = append(errs, errors.Wrapf(err, "unable to convertInfluxdbUrl %s", monitorUrl))
  57. } else {
  58. monitorUrl = tsdbUrl
  59. foundSvc = true
  60. break
  61. }
  62. }
  63. if !foundSvc {
  64. return nil, errors.Wrapf(errors.NewAggregate(errs), "convert TSDB service URL")
  65. }
  66. if len(monitorUrl) == 0 {
  67. return nil, errors.Wrap(ErrCannotReachInfluxbd, "please create usable Proxy Endpoint for server and try again")
  68. }
  69. return getArgs(&info, monitorUrl), nil
  70. }
  71. func getArgs(info *sServerInfo, influxdbUrl string) map[string]interface{} {
  72. tags := map[string]string{
  73. "host": info.serverDetails.Host,
  74. "host_id": info.serverDetails.HostId,
  75. "host_ip": info.serverDetails.HostAccessIp,
  76. "vm_id": info.serverDetails.Id,
  77. "vm_ip": info.serverDetails.IPs,
  78. "vm_name": info.serverDetails.Name,
  79. "zone": info.serverDetails.Zone,
  80. "zone_id": info.serverDetails.ZoneId,
  81. "zone_ext_id": info.serverDetails.ZoneExtId,
  82. "os_type": info.serverDetails.OsType,
  83. "status": info.serverDetails.Status,
  84. "cloudregion": info.serverDetails.Cloudregion,
  85. "cloudregion_id": info.serverDetails.CloudregionId,
  86. "region_ext_id": info.serverDetails.RegionExtId,
  87. "tenant": info.serverDetails.Project,
  88. "tenant_id": info.serverDetails.ProjectId,
  89. "brand": info.serverDetails.Brand,
  90. "scaling_group_id": info.serverDetails.ScalingGroupId,
  91. "domain_id": info.serverDetails.DomainId,
  92. "project_domain": info.serverDetails.ProjectDomain,
  93. }
  94. telegrafTags := make([]map[string]string, 0, len(tags))
  95. for name, value := range tags {
  96. telegrafTags = append(telegrafTags, map[string]string{
  97. "tag_name": name,
  98. "tag_value": value,
  99. })
  100. }
  101. ret := map[string]interface{}{
  102. "influxdb_url": influxdbUrl,
  103. "influxdb_name": "telegraf",
  104. "telegraf_global_tags": telegrafTags,
  105. }
  106. if info.serverDetails.Hypervisor == comapi.HYPERVISOR_BAREMETAL {
  107. ret["server_type"] = "baremetal"
  108. }
  109. return ret
  110. }