metrics.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/fatih/color"
  11. dto "github.com/prometheus/client_model/go"
  12. "github.com/prometheus/prom2json"
  13. log "github.com/sirupsen/logrus"
  14. "github.com/spf13/cobra"
  15. "gopkg.in/yaml.v2"
  16. "github.com/crowdsecurity/go-cs-lib/pkg/trace"
  17. )
  18. // FormatPrometheusMetrics is a complete rip from prom2json
  19. func FormatPrometheusMetrics(out io.Writer, url string, formatType string) error {
  20. mfChan := make(chan *dto.MetricFamily, 1024)
  21. errChan := make(chan error, 1)
  22. // Start with the DefaultTransport for sane defaults.
  23. transport := http.DefaultTransport.(*http.Transport).Clone()
  24. // Conservatively disable HTTP keep-alives as this program will only
  25. // ever need a single HTTP request.
  26. transport.DisableKeepAlives = true
  27. // Timeout early if the server doesn't even return the headers.
  28. transport.ResponseHeaderTimeout = time.Minute
  29. go func() {
  30. defer trace.CatchPanic("crowdsec/ShowPrometheus")
  31. err := prom2json.FetchMetricFamilies(url, mfChan, transport)
  32. if err != nil {
  33. errChan <- fmt.Errorf("failed to fetch prometheus metrics: %w", err)
  34. return
  35. }
  36. errChan <- nil
  37. }()
  38. result := []*prom2json.Family{}
  39. for mf := range mfChan {
  40. result = append(result, prom2json.NewFamily(mf))
  41. }
  42. if err := <-errChan; err != nil {
  43. return err
  44. }
  45. log.Debugf("Finished reading prometheus output, %d entries", len(result))
  46. /*walk*/
  47. lapi_decisions_stats := map[string]struct {
  48. NonEmpty int
  49. Empty int
  50. }{}
  51. acquis_stats := map[string]map[string]int{}
  52. parsers_stats := map[string]map[string]int{}
  53. buckets_stats := map[string]map[string]int{}
  54. lapi_stats := map[string]map[string]int{}
  55. lapi_machine_stats := map[string]map[string]map[string]int{}
  56. lapi_bouncer_stats := map[string]map[string]map[string]int{}
  57. decisions_stats := map[string]map[string]map[string]int{}
  58. alerts_stats := map[string]int{}
  59. stash_stats := map[string]struct {
  60. Type string
  61. Count int
  62. }{}
  63. for idx, fam := range result {
  64. if !strings.HasPrefix(fam.Name, "cs_") {
  65. continue
  66. }
  67. log.Tracef("round %d", idx)
  68. for _, m := range fam.Metrics {
  69. metric, ok := m.(prom2json.Metric)
  70. if !ok {
  71. log.Debugf("failed to convert metric to prom2json.Metric")
  72. continue
  73. }
  74. name, ok := metric.Labels["name"]
  75. if !ok {
  76. log.Debugf("no name in Metric %v", metric.Labels)
  77. }
  78. source, ok := metric.Labels["source"]
  79. if !ok {
  80. log.Debugf("no source in Metric %v for %s", metric.Labels, fam.Name)
  81. } else {
  82. if srctype, ok := metric.Labels["type"]; ok {
  83. source = srctype + ":" + source
  84. }
  85. }
  86. value := m.(prom2json.Metric).Value
  87. machine := metric.Labels["machine"]
  88. bouncer := metric.Labels["bouncer"]
  89. route := metric.Labels["route"]
  90. method := metric.Labels["method"]
  91. reason := metric.Labels["reason"]
  92. origin := metric.Labels["origin"]
  93. action := metric.Labels["action"]
  94. mtype := metric.Labels["type"]
  95. fval, err := strconv.ParseFloat(value, 32)
  96. if err != nil {
  97. log.Errorf("Unexpected int value %s : %s", value, err)
  98. }
  99. ival := int(fval)
  100. switch fam.Name {
  101. /*buckets*/
  102. case "cs_bucket_created_total":
  103. if _, ok := buckets_stats[name]; !ok {
  104. buckets_stats[name] = make(map[string]int)
  105. }
  106. buckets_stats[name]["instantiation"] += ival
  107. case "cs_buckets":
  108. if _, ok := buckets_stats[name]; !ok {
  109. buckets_stats[name] = make(map[string]int)
  110. }
  111. buckets_stats[name]["curr_count"] += ival
  112. case "cs_bucket_overflowed_total":
  113. if _, ok := buckets_stats[name]; !ok {
  114. buckets_stats[name] = make(map[string]int)
  115. }
  116. buckets_stats[name]["overflow"] += ival
  117. case "cs_bucket_poured_total":
  118. if _, ok := buckets_stats[name]; !ok {
  119. buckets_stats[name] = make(map[string]int)
  120. }
  121. if _, ok := acquis_stats[source]; !ok {
  122. acquis_stats[source] = make(map[string]int)
  123. }
  124. buckets_stats[name]["pour"] += ival
  125. acquis_stats[source]["pour"] += ival
  126. case "cs_bucket_underflowed_total":
  127. if _, ok := buckets_stats[name]; !ok {
  128. buckets_stats[name] = make(map[string]int)
  129. }
  130. buckets_stats[name]["underflow"] += ival
  131. /*acquis*/
  132. case "cs_parser_hits_total":
  133. if _, ok := acquis_stats[source]; !ok {
  134. acquis_stats[source] = make(map[string]int)
  135. }
  136. acquis_stats[source]["reads"] += ival
  137. case "cs_parser_hits_ok_total":
  138. if _, ok := acquis_stats[source]; !ok {
  139. acquis_stats[source] = make(map[string]int)
  140. }
  141. acquis_stats[source]["parsed"] += ival
  142. case "cs_parser_hits_ko_total":
  143. if _, ok := acquis_stats[source]; !ok {
  144. acquis_stats[source] = make(map[string]int)
  145. }
  146. acquis_stats[source]["unparsed"] += ival
  147. case "cs_node_hits_total":
  148. if _, ok := parsers_stats[name]; !ok {
  149. parsers_stats[name] = make(map[string]int)
  150. }
  151. parsers_stats[name]["hits"] += ival
  152. case "cs_node_hits_ok_total":
  153. if _, ok := parsers_stats[name]; !ok {
  154. parsers_stats[name] = make(map[string]int)
  155. }
  156. parsers_stats[name]["parsed"] += ival
  157. case "cs_node_hits_ko_total":
  158. if _, ok := parsers_stats[name]; !ok {
  159. parsers_stats[name] = make(map[string]int)
  160. }
  161. parsers_stats[name]["unparsed"] += ival
  162. case "cs_lapi_route_requests_total":
  163. if _, ok := lapi_stats[route]; !ok {
  164. lapi_stats[route] = make(map[string]int)
  165. }
  166. lapi_stats[route][method] += ival
  167. case "cs_lapi_machine_requests_total":
  168. if _, ok := lapi_machine_stats[machine]; !ok {
  169. lapi_machine_stats[machine] = make(map[string]map[string]int)
  170. }
  171. if _, ok := lapi_machine_stats[machine][route]; !ok {
  172. lapi_machine_stats[machine][route] = make(map[string]int)
  173. }
  174. lapi_machine_stats[machine][route][method] += ival
  175. case "cs_lapi_bouncer_requests_total":
  176. if _, ok := lapi_bouncer_stats[bouncer]; !ok {
  177. lapi_bouncer_stats[bouncer] = make(map[string]map[string]int)
  178. }
  179. if _, ok := lapi_bouncer_stats[bouncer][route]; !ok {
  180. lapi_bouncer_stats[bouncer][route] = make(map[string]int)
  181. }
  182. lapi_bouncer_stats[bouncer][route][method] += ival
  183. case "cs_lapi_decisions_ko_total", "cs_lapi_decisions_ok_total":
  184. if _, ok := lapi_decisions_stats[bouncer]; !ok {
  185. lapi_decisions_stats[bouncer] = struct {
  186. NonEmpty int
  187. Empty int
  188. }{}
  189. }
  190. x := lapi_decisions_stats[bouncer]
  191. if fam.Name == "cs_lapi_decisions_ko_total" {
  192. x.Empty += ival
  193. } else if fam.Name == "cs_lapi_decisions_ok_total" {
  194. x.NonEmpty += ival
  195. }
  196. lapi_decisions_stats[bouncer] = x
  197. case "cs_active_decisions":
  198. if _, ok := decisions_stats[reason]; !ok {
  199. decisions_stats[reason] = make(map[string]map[string]int)
  200. }
  201. if _, ok := decisions_stats[reason][origin]; !ok {
  202. decisions_stats[reason][origin] = make(map[string]int)
  203. }
  204. decisions_stats[reason][origin][action] += ival
  205. case "cs_alerts":
  206. /*if _, ok := alerts_stats[scenario]; !ok {
  207. alerts_stats[scenario] = make(map[string]int)
  208. }*/
  209. alerts_stats[reason] += ival
  210. case "cs_cache_size":
  211. stash_stats[name] = struct {
  212. Type string
  213. Count int
  214. }{Type: mtype, Count: ival}
  215. default:
  216. continue
  217. }
  218. }
  219. }
  220. if formatType == "human" {
  221. acquisStatsTable(out, acquis_stats)
  222. bucketStatsTable(out, buckets_stats)
  223. parserStatsTable(out, parsers_stats)
  224. lapiStatsTable(out, lapi_stats)
  225. lapiMachineStatsTable(out, lapi_machine_stats)
  226. lapiBouncerStatsTable(out, lapi_bouncer_stats)
  227. lapiDecisionStatsTable(out, lapi_decisions_stats)
  228. decisionStatsTable(out, decisions_stats)
  229. alertStatsTable(out, alerts_stats)
  230. stashStatsTable(out, stash_stats)
  231. } else if formatType == "json" {
  232. for _, val := range []interface{}{acquis_stats, parsers_stats, buckets_stats, lapi_stats, lapi_bouncer_stats, lapi_machine_stats, lapi_decisions_stats, decisions_stats, alerts_stats, stash_stats} {
  233. x, err := json.MarshalIndent(val, "", " ")
  234. if err != nil {
  235. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  236. }
  237. out.Write(x)
  238. }
  239. return nil
  240. } else if formatType == "raw" {
  241. for _, val := range []interface{}{acquis_stats, parsers_stats, buckets_stats, lapi_stats, lapi_bouncer_stats, lapi_machine_stats, lapi_decisions_stats, decisions_stats, alerts_stats, stash_stats} {
  242. x, err := yaml.Marshal(val)
  243. if err != nil {
  244. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  245. }
  246. out.Write(x)
  247. }
  248. return nil
  249. }
  250. return nil
  251. }
  252. var noUnit bool
  253. func runMetrics(cmd *cobra.Command, args []string) error {
  254. if err := csConfig.LoadPrometheus(); err != nil {
  255. return fmt.Errorf("failed to load prometheus config: %w", err)
  256. }
  257. if csConfig.Prometheus == nil {
  258. return fmt.Errorf("prometheus section missing, can't show metrics")
  259. }
  260. if !csConfig.Prometheus.Enabled {
  261. return fmt.Errorf("prometheus is not enabled, can't show metrics")
  262. }
  263. if prometheusURL == "" {
  264. prometheusURL = csConfig.Cscli.PrometheusUrl
  265. }
  266. if prometheusURL == "" {
  267. return fmt.Errorf("no prometheus url, please specify in %s or via -u", *csConfig.FilePath)
  268. }
  269. err := FormatPrometheusMetrics(color.Output, prometheusURL+"/metrics", csConfig.Cscli.Output)
  270. if err != nil {
  271. return fmt.Errorf("could not fetch prometheus metrics: %w", err)
  272. }
  273. return nil
  274. }
  275. func NewMetricsCmd() *cobra.Command {
  276. cmdMetrics := &cobra.Command{
  277. Use: "metrics",
  278. Short: "Display crowdsec prometheus metrics.",
  279. Long: `Fetch metrics from the prometheus server and display them in a human-friendly way`,
  280. Args: cobra.ExactArgs(0),
  281. DisableAutoGenTag: true,
  282. RunE: runMetrics,
  283. }
  284. cmdMetrics.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
  285. cmdMetrics.PersistentFlags().BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")
  286. return cmdMetrics
  287. }