metrics.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.v3"
  16. "github.com/crowdsecurity/go-cs-lib/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. appsec_engine_stats := map[string]map[string]int{}
  59. appsec_rule_stats := map[string]map[string]map[string]int{}
  60. alerts_stats := map[string]int{}
  61. stash_stats := map[string]struct {
  62. Type string
  63. Count int
  64. }{}
  65. for idx, fam := range result {
  66. if !strings.HasPrefix(fam.Name, "cs_") {
  67. continue
  68. }
  69. log.Tracef("round %d", idx)
  70. for _, m := range fam.Metrics {
  71. metric, ok := m.(prom2json.Metric)
  72. if !ok {
  73. log.Debugf("failed to convert metric to prom2json.Metric")
  74. continue
  75. }
  76. name, ok := metric.Labels["name"]
  77. if !ok {
  78. log.Debugf("no name in Metric %v", metric.Labels)
  79. }
  80. source, ok := metric.Labels["source"]
  81. if !ok {
  82. log.Debugf("no source in Metric %v for %s", metric.Labels, fam.Name)
  83. } else {
  84. if srctype, ok := metric.Labels["type"]; ok {
  85. source = srctype + ":" + source
  86. }
  87. }
  88. value := m.(prom2json.Metric).Value
  89. machine := metric.Labels["machine"]
  90. bouncer := metric.Labels["bouncer"]
  91. route := metric.Labels["route"]
  92. method := metric.Labels["method"]
  93. reason := metric.Labels["reason"]
  94. origin := metric.Labels["origin"]
  95. action := metric.Labels["action"]
  96. mtype := metric.Labels["type"]
  97. fval, err := strconv.ParseFloat(value, 32)
  98. if err != nil {
  99. log.Errorf("Unexpected int value %s : %s", value, err)
  100. }
  101. ival := int(fval)
  102. switch fam.Name {
  103. /*buckets*/
  104. case "cs_bucket_created_total":
  105. if _, ok := buckets_stats[name]; !ok {
  106. buckets_stats[name] = make(map[string]int)
  107. }
  108. buckets_stats[name]["instantiation"] += ival
  109. case "cs_buckets":
  110. if _, ok := buckets_stats[name]; !ok {
  111. buckets_stats[name] = make(map[string]int)
  112. }
  113. buckets_stats[name]["curr_count"] += ival
  114. case "cs_bucket_overflowed_total":
  115. if _, ok := buckets_stats[name]; !ok {
  116. buckets_stats[name] = make(map[string]int)
  117. }
  118. buckets_stats[name]["overflow"] += ival
  119. case "cs_bucket_poured_total":
  120. if _, ok := buckets_stats[name]; !ok {
  121. buckets_stats[name] = make(map[string]int)
  122. }
  123. if _, ok := acquis_stats[source]; !ok {
  124. acquis_stats[source] = make(map[string]int)
  125. }
  126. buckets_stats[name]["pour"] += ival
  127. acquis_stats[source]["pour"] += ival
  128. case "cs_bucket_underflowed_total":
  129. if _, ok := buckets_stats[name]; !ok {
  130. buckets_stats[name] = make(map[string]int)
  131. }
  132. buckets_stats[name]["underflow"] += ival
  133. /*acquis*/
  134. case "cs_parser_hits_total":
  135. if _, ok := acquis_stats[source]; !ok {
  136. acquis_stats[source] = make(map[string]int)
  137. }
  138. acquis_stats[source]["reads"] += ival
  139. case "cs_parser_hits_ok_total":
  140. if _, ok := acquis_stats[source]; !ok {
  141. acquis_stats[source] = make(map[string]int)
  142. }
  143. acquis_stats[source]["parsed"] += ival
  144. case "cs_parser_hits_ko_total":
  145. if _, ok := acquis_stats[source]; !ok {
  146. acquis_stats[source] = make(map[string]int)
  147. }
  148. acquis_stats[source]["unparsed"] += ival
  149. case "cs_node_hits_total":
  150. if _, ok := parsers_stats[name]; !ok {
  151. parsers_stats[name] = make(map[string]int)
  152. }
  153. parsers_stats[name]["hits"] += ival
  154. case "cs_node_hits_ok_total":
  155. if _, ok := parsers_stats[name]; !ok {
  156. parsers_stats[name] = make(map[string]int)
  157. }
  158. parsers_stats[name]["parsed"] += ival
  159. case "cs_node_hits_ko_total":
  160. if _, ok := parsers_stats[name]; !ok {
  161. parsers_stats[name] = make(map[string]int)
  162. }
  163. parsers_stats[name]["unparsed"] += ival
  164. case "cs_lapi_route_requests_total":
  165. if _, ok := lapi_stats[route]; !ok {
  166. lapi_stats[route] = make(map[string]int)
  167. }
  168. lapi_stats[route][method] += ival
  169. case "cs_lapi_machine_requests_total":
  170. if _, ok := lapi_machine_stats[machine]; !ok {
  171. lapi_machine_stats[machine] = make(map[string]map[string]int)
  172. }
  173. if _, ok := lapi_machine_stats[machine][route]; !ok {
  174. lapi_machine_stats[machine][route] = make(map[string]int)
  175. }
  176. lapi_machine_stats[machine][route][method] += ival
  177. case "cs_lapi_bouncer_requests_total":
  178. if _, ok := lapi_bouncer_stats[bouncer]; !ok {
  179. lapi_bouncer_stats[bouncer] = make(map[string]map[string]int)
  180. }
  181. if _, ok := lapi_bouncer_stats[bouncer][route]; !ok {
  182. lapi_bouncer_stats[bouncer][route] = make(map[string]int)
  183. }
  184. lapi_bouncer_stats[bouncer][route][method] += ival
  185. case "cs_lapi_decisions_ko_total", "cs_lapi_decisions_ok_total":
  186. if _, ok := lapi_decisions_stats[bouncer]; !ok {
  187. lapi_decisions_stats[bouncer] = struct {
  188. NonEmpty int
  189. Empty int
  190. }{}
  191. }
  192. x := lapi_decisions_stats[bouncer]
  193. if fam.Name == "cs_lapi_decisions_ko_total" {
  194. x.Empty += ival
  195. } else if fam.Name == "cs_lapi_decisions_ok_total" {
  196. x.NonEmpty += ival
  197. }
  198. lapi_decisions_stats[bouncer] = x
  199. case "cs_active_decisions":
  200. if _, ok := decisions_stats[reason]; !ok {
  201. decisions_stats[reason] = make(map[string]map[string]int)
  202. }
  203. if _, ok := decisions_stats[reason][origin]; !ok {
  204. decisions_stats[reason][origin] = make(map[string]int)
  205. }
  206. decisions_stats[reason][origin][action] += ival
  207. case "cs_alerts":
  208. /*if _, ok := alerts_stats[scenario]; !ok {
  209. alerts_stats[scenario] = make(map[string]int)
  210. }*/
  211. alerts_stats[reason] += ival
  212. case "cs_cache_size":
  213. stash_stats[name] = struct {
  214. Type string
  215. Count int
  216. }{Type: mtype, Count: ival}
  217. case "cs_appsec_reqs_total":
  218. if _, ok := appsec_engine_stats[metric.Labels["appsec_engine"]]; !ok {
  219. appsec_engine_stats[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
  220. }
  221. appsec_engine_stats[metric.Labels["appsec_engine"]]["processed"] = ival
  222. case "cs_appsec_block_total":
  223. if _, ok := appsec_engine_stats[metric.Labels["appsec_engine"]]; !ok {
  224. appsec_engine_stats[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
  225. }
  226. appsec_engine_stats[metric.Labels["appsec_engine"]]["blocked"] = ival
  227. case "cs_appsec_rule_hits":
  228. appsecEngine := metric.Labels["appsec_engine"]
  229. ruleID := metric.Labels["rule_name"]
  230. if _, ok := appsec_rule_stats[appsecEngine]; !ok {
  231. appsec_rule_stats[appsecEngine] = make(map[string]map[string]int, 0)
  232. }
  233. if _, ok := appsec_rule_stats[appsecEngine][ruleID]; !ok {
  234. appsec_rule_stats[appsecEngine][ruleID] = make(map[string]int, 0)
  235. }
  236. appsec_rule_stats[appsecEngine][ruleID]["triggered"] = ival
  237. default:
  238. log.Debugf("unknown: %+v", fam.Name)
  239. continue
  240. }
  241. }
  242. }
  243. if formatType == "human" {
  244. acquisStatsTable(out, acquis_stats)
  245. bucketStatsTable(out, buckets_stats)
  246. parserStatsTable(out, parsers_stats)
  247. lapiStatsTable(out, lapi_stats)
  248. lapiMachineStatsTable(out, lapi_machine_stats)
  249. lapiBouncerStatsTable(out, lapi_bouncer_stats)
  250. lapiDecisionStatsTable(out, lapi_decisions_stats)
  251. decisionStatsTable(out, decisions_stats)
  252. alertStatsTable(out, alerts_stats)
  253. stashStatsTable(out, stash_stats)
  254. appsecMetricsToTable(out, appsec_engine_stats)
  255. appsecRulesToTable(out, appsec_rule_stats)
  256. return nil
  257. }
  258. stats := make(map[string]any)
  259. stats["acquisition"] = acquis_stats
  260. stats["buckets"] = buckets_stats
  261. stats["parsers"] = parsers_stats
  262. stats["lapi"] = lapi_stats
  263. stats["lapi_machine"] = lapi_machine_stats
  264. stats["lapi_bouncer"] = lapi_bouncer_stats
  265. stats["lapi_decisions"] = lapi_decisions_stats
  266. stats["decisions"] = decisions_stats
  267. stats["alerts"] = alerts_stats
  268. stats["stash"] = stash_stats
  269. switch formatType {
  270. case "json":
  271. x, err := json.MarshalIndent(stats, "", " ")
  272. if err != nil {
  273. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  274. }
  275. out.Write(x)
  276. case "raw":
  277. x, err := yaml.Marshal(stats)
  278. if err != nil {
  279. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  280. }
  281. out.Write(x)
  282. default:
  283. return fmt.Errorf("unknown format type %s", formatType)
  284. }
  285. return nil
  286. }
  287. var noUnit bool
  288. func runMetrics(cmd *cobra.Command, args []string) error {
  289. flags := cmd.Flags()
  290. url, err := flags.GetString("url")
  291. if err != nil {
  292. return err
  293. }
  294. if url != "" {
  295. csConfig.Cscli.PrometheusUrl = url
  296. }
  297. noUnit, err = flags.GetBool("no-unit")
  298. if err != nil {
  299. return err
  300. }
  301. if csConfig.Prometheus == nil {
  302. return fmt.Errorf("prometheus section missing, can't show metrics")
  303. }
  304. if !csConfig.Prometheus.Enabled {
  305. return fmt.Errorf("prometheus is not enabled, can't show metrics")
  306. }
  307. if err = FormatPrometheusMetrics(color.Output, csConfig.Cscli.PrometheusUrl, csConfig.Cscli.Output); err != nil {
  308. return err
  309. }
  310. return nil
  311. }
  312. func NewMetricsCmd() *cobra.Command {
  313. cmdMetrics := &cobra.Command{
  314. Use: "metrics",
  315. Short: "Display crowdsec prometheus metrics.",
  316. Long: `Fetch metrics from the prometheus server and display them in a human-friendly way`,
  317. Args: cobra.ExactArgs(0),
  318. DisableAutoGenTag: true,
  319. RunE: runMetrics,
  320. }
  321. flags := cmdMetrics.PersistentFlags()
  322. flags.StringP("url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
  323. flags.Bool("no-unit", false, "Show the real number instead of formatted with units")
  324. return cmdMetrics
  325. }