fluentbit.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 system_service
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/log"
  20. )
  21. type SFluentbit struct {
  22. *SBaseSystemService
  23. }
  24. func NewFluentbitService() *SFluentbit {
  25. return &SFluentbit{NewBaseSystemService("fluentbit", nil)}
  26. }
  27. func (s *SFluentbit) GetConfig(kwargs map[string]interface{}) string {
  28. // 写到这
  29. conf := ""
  30. conf += "[SERVICE]\n"
  31. conf += " Flush 5\n"
  32. conf += " Daemon Off\n"
  33. conf += " Log_Level info\n"
  34. conf += " Parsers_File parsers.conf\n"
  35. conf += " Plugins_File plugins.conf\n"
  36. conf += " HTTP_Server Off\n"
  37. conf += "\n"
  38. conf += "[INPUT]\n"
  39. conf += " Name systemd\n"
  40. unitsInf := kwargs["units"]
  41. units, _ := unitsInf.([]string)
  42. for _, u := range units {
  43. conf += fmt.Sprintf(" Systemd_Filter _SYSTEMD_UNIT=%s.service\n", u)
  44. }
  45. conf += " Tag host.*\n"
  46. conf += "\n"
  47. ielUrl := kwargs["elasticsearch"]
  48. mesUrl, _ := ielUrl.(map[string]string)
  49. sesUrl := mesUrl["url"]
  50. esurl, err := url.Parse(sesUrl)
  51. if err != nil {
  52. log.Errorln(err)
  53. return ""
  54. }
  55. esHostname := strings.Split(esurl.Host, ":")[0]
  56. conf += "[OUTPUT]\n"
  57. conf += " Name es\n"
  58. conf += " Match *\n"
  59. conf += fmt.Sprintf(" Host %s\n", esHostname)
  60. conf += " Port 9200\n"
  61. conf += " Logstash_Format on\n"
  62. conf += " Retry_Limit False\n"
  63. conf += " Type flb_type\n"
  64. conf += " Time_Key @timestamp\n"
  65. conf += " Logstash_Prefix onecloud\n"
  66. return conf
  67. }
  68. func (s *SFluentbit) GetConfigFile() string {
  69. return "/etc/fluent-bit/fluent-bit.conf"
  70. }
  71. func (s *SFluentbit) Reload(kwargs map[string]interface{}) error {
  72. return s.reload(s.GetConfig(kwargs), s.GetConfigFile())
  73. }
  74. func (s *SFluentbit) BgReload(kwargs map[string]interface{}) {
  75. go s.reload(s.GetConfig(kwargs), s.GetConfigFile())
  76. }
  77. func (s *SFluentbit) BgReloadConf(kwargs map[string]interface{}) {
  78. go s.reloadConf(s.GetConfig(kwargs), s.GetConfigFile())
  79. }