gen_telegraf_conf.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "strings"
  17. "text/template"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/osprofile"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. )
  23. func GetValFromMap(valMap map[string]string, key string) string {
  24. return valMap[key]
  25. }
  26. // copy from: https://github.com/yunionio/ansible-telegraf/blob/master/templates/telegraf.conf.j2
  27. const TELEGRAF_CONF_TEMPLETE = `### MANAGED BY ansible-telegraf ANSIBLE ROLE ###
  28. [global_tags]
  29. {{ range $value := .telegraf_global_tags }}
  30. {{ GetValFromMap $value "tag_name" }} = "{{ GetValFromMap $value "tag_value" }}"
  31. {{- end }}
  32. # Configuration for telegraf agent
  33. [agent]
  34. interval = "60s"
  35. debug = false
  36. hostname = ""
  37. round_interval = true
  38. flush_interval = "60s"
  39. flush_jitter = "0s"
  40. collection_jitter = "0s"
  41. metric_batch_size = 1000
  42. metric_buffer_limit = 10000
  43. quiet = false
  44. logfile = "{{ .telegraf_agent_logfile }}"
  45. logfile_rotation_max_size = "10MB"
  46. logfile_rotation_max_archives = 1
  47. omit_hostname = true
  48. ###############################################################################
  49. # OUTPUTS #
  50. ###############################################################################
  51. [[outputs.influxdb]]
  52. urls = ["{{ .influxdb_url }}"]
  53. database = "{{ .influxdb_name }}"
  54. insecure_skip_verify = true
  55. ###############################################################################
  56. # INPUTS #
  57. ###############################################################################`
  58. const TELEGRAF_INPUT_LINUX = `
  59. [[inputs.cpu]]
  60. name_prefix = "agent_"
  61. percpu = true
  62. totalcpu = true
  63. collect_cpu_time = false
  64. report_active = true
  65. [[inputs.disk]]
  66. name_prefix = "agent_"
  67. ignore_fs = ["tmpfs", "devtmpfs", "overlay", "squashfs", "iso9660"]
  68. [[inputs.diskio]]
  69. name_prefix = "agent_"
  70. skip_serial_number = false
  71. [[inputs.kernel]]
  72. name_prefix = "agent_"
  73. [[inputs.kernel_vmstat]]
  74. name_prefix = "agent_"
  75. [[inputs.mem]]
  76. name_prefix = "agent_"
  77. [[inputs.processes]]
  78. name_prefix = "agent_"
  79. [[inputs.swap]]
  80. name_prefix = "agent_"
  81. [[inputs.system]]
  82. name_prefix = "agent_"
  83. [[inputs.net]]
  84. name_prefix = "agent_"
  85. [[inputs.netstat]]
  86. name_prefix = "agent_"
  87. [[inputs.nstat]]
  88. name_prefix = "agent_"
  89. [[inputs.internal]]
  90. name_prefix = "agent_"
  91. collect_memstats = false
  92. [[inputs.nvidia_smi]]
  93. name_prefix = "agent_"
  94. `
  95. const TELEGRAF_INPUT_WINDOWS = `
  96. [[inputs.cpu]]
  97. name_prefix = "agent_"
  98. percpu = true
  99. totalcpu = true
  100. collect_cpu_time = false
  101. report_active = true
  102. [[inputs.disk]]
  103. name_prefix = "agent_"
  104. ignore_fs = ["tmpfs", "devtmpfs", "overlay", "squashfs", "iso9660"]
  105. [[inputs.diskio]]
  106. name_prefix = "agent_"
  107. skip_serial_number = false
  108. [[inputs.mem]]
  109. name_prefix = "agent_"
  110. [[inputs.processes]]
  111. name_prefix = "agent_"
  112. [[inputs.swap]]
  113. name_prefix = "agent_"
  114. [[inputs.system]]
  115. name_prefix = "agent_"
  116. [[inputs.net]]
  117. name_prefix = "agent_"
  118. [[inputs.netstat]]
  119. name_prefix = "agent_"
  120. [[inputs.nstat]]
  121. name_prefix = "agent_"
  122. [[inputs.internal]]
  123. name_prefix = "agent_"
  124. collect_memstats = false
  125. [[inputs.nvidia_smi]]
  126. name_prefix = "agent_"
  127. `
  128. const TELEGRAF_INPUT_BAREMETAL = `
  129. [[inputs.cpu]]
  130. name_prefix = "agent_"
  131. percpu = true
  132. totalcpu = true
  133. collect_cpu_time = false
  134. report_active = true
  135. [[inputs.disk]]
  136. name_prefix = "agent_"
  137. ignore_fs = ["tmpfs", "devtmpfs", "overlay", "squashfs", "iso9660"]
  138. [[inputs.diskio]]
  139. name_prefix = "agent_"
  140. skip_serial_number = false
  141. [[inputs.sensors]]
  142. name_prefix = "agent_"
  143. [[inputs.smart]]
  144. name_prefix = "agent_"
  145. use_sudo = true
  146. [[inputs.mem]]
  147. name_prefix = "agent_"
  148. [[inputs.processes]]
  149. name_prefix = "agent_"
  150. [[inputs.swap]]
  151. name_prefix = "agent_"
  152. [[inputs.system]]
  153. name_prefix = "agent_"
  154. [[inputs.net]]
  155. name_prefix = "agent_"
  156. [[inputs.netstat]]
  157. name_prefix = "agent_"
  158. [[inputs.nstat]]
  159. name_prefix = "agent_"
  160. [[inputs.internal]]
  161. name_prefix = "agent_"
  162. collect_memstats = false
  163. [[inputs.nvidia_smi]]
  164. name_prefix = "agent_"
  165. `
  166. var temp *template.Template
  167. func init() {
  168. var err error
  169. temp, err = template.New("").Funcs(template.FuncMap{
  170. "GetValFromMap": GetValFromMap,
  171. }).Parse(TELEGRAF_CONF_TEMPLETE)
  172. if err != nil {
  173. log.Fatalf("parse telegraf template: %s", err)
  174. }
  175. }
  176. func getTelegrafInputs(hypervisor, osType string) string {
  177. if hypervisor == api.HYPERVISOR_BAREMETAL {
  178. return TELEGRAF_INPUT_BAREMETAL
  179. } else {
  180. if osType == osprofile.OS_TYPE_WINDOWS {
  181. return TELEGRAF_INPUT_WINDOWS
  182. } else {
  183. return TELEGRAF_INPUT_LINUX
  184. }
  185. }
  186. }
  187. func GenerateTelegrafConf(
  188. serverDetails *api.ServerDetails, influxdbUrl, osType, hypervisor string,
  189. ) (string, error) {
  190. telegrafArgs := GetLocalArgs(serverDetails, influxdbUrl)
  191. if osType == osprofile.OS_TYPE_WINDOWS {
  192. telegrafArgs["telegraf_agent_logfile"] = "/Program Files/Telegraf/telegraf.log"
  193. } else {
  194. telegrafArgs["telegraf_agent_logfile"] = "/var/log/telegraf.log"
  195. }
  196. strBuild := strings.Builder{}
  197. log.Infof("telegraf args %v", telegrafArgs)
  198. err := temp.Execute(&strBuild, telegrafArgs)
  199. if err != nil {
  200. return "", errors.Wrap(err, "build telegraf config")
  201. }
  202. return strBuild.String() + getTelegrafInputs(hypervisor, osType), nil
  203. }