sdk.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 Xiaomi, Inc.
  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 sdk
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. cmodel "github.com/open-falcon/falcon-plus/common/model"
  19. "github.com/open-falcon/falcon-plus/common/sdk/requests"
  20. "github.com/open-falcon/falcon-plus/modules/aggregator/g"
  21. f "github.com/open-falcon/falcon-plus/modules/api/app/model/falcon_portal"
  22. "github.com/toolkits/net/httplib"
  23. "time"
  24. )
  25. func HostnamesByID(group_id int64) ([]string, error) {
  26. uri := fmt.Sprintf("%s/api/v1/hostgroup/%d", g.Config().Api.PlusApi, group_id)
  27. req, err := requests.CurlPlus(uri, "GET", "aggregator", g.Config().Api.PlusApiToken,
  28. map[string]string{}, map[string]string{})
  29. if err != nil {
  30. return []string{}, err
  31. }
  32. type RESP struct {
  33. HostGroup f.HostGroup `json:"hostgroup"`
  34. Hosts []f.Host `json:"hosts"`
  35. }
  36. resp := &RESP{}
  37. err = req.ToJson(&resp)
  38. if err != nil {
  39. return []string{}, err
  40. }
  41. hosts := []string{}
  42. nowTs := time.Now().Unix()
  43. for _, x := range resp.Hosts {
  44. if x.MaintainBegin <= nowTs && nowTs <= x.MaintainEnd {
  45. continue
  46. }
  47. hosts = append(hosts, x.Hostname)
  48. }
  49. return hosts, nil
  50. }
  51. func QueryLastPoints(endpoints, counters []string) (resp []*cmodel.GraphLastResp, err error) {
  52. cfg := g.Config()
  53. uri := fmt.Sprintf("%s/api/v1/graph/lastpoint", cfg.Api.PlusApi)
  54. var req *httplib.BeegoHttpRequest
  55. headers := map[string]string{"Content-type": "application/json"}
  56. req, err = requests.CurlPlus(uri, "POST", "aggregator", cfg.Api.PlusApiToken,
  57. headers, map[string]string{})
  58. if err != nil {
  59. return
  60. }
  61. req.SetTimeout(time.Duration(cfg.Api.ConnectTimeout)*time.Millisecond,
  62. time.Duration(cfg.Api.RequestTimeout)*time.Millisecond)
  63. body := []*cmodel.GraphLastParam{}
  64. for _, e := range endpoints {
  65. for _, c := range counters {
  66. body = append(body, &cmodel.GraphLastParam{e, c})
  67. }
  68. }
  69. b, err := json.Marshal(body)
  70. if err != nil {
  71. return
  72. }
  73. req.Body(b)
  74. err = req.ToJson(&resp)
  75. if err != nil {
  76. return
  77. }
  78. return resp, nil
  79. }