metrics.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. type (
  19. statAcquis map[string]map[string]int
  20. statParser map[string]map[string]int
  21. statBucket map[string]map[string]int
  22. statLapi map[string]map[string]int
  23. statLapiMachine map[string]map[string]map[string]int
  24. statLapiBouncer map[string]map[string]map[string]int
  25. statLapiDecision map[string]struct {
  26. NonEmpty int
  27. Empty int
  28. }
  29. statDecision map[string]map[string]map[string]int
  30. statAppsecEngine map[string]map[string]int
  31. statAppsecRule map[string]map[string]map[string]int
  32. statAlert map[string]int
  33. statStash map[string]struct {
  34. Type string
  35. Count int
  36. }
  37. )
  38. type cliMetrics struct {
  39. cfg configGetter
  40. }
  41. func NewCLIMetrics(getconfig configGetter) *cliMetrics {
  42. return &cliMetrics{
  43. cfg: getconfig,
  44. }
  45. }
  46. // FormatPrometheusMetrics is a complete rip from prom2json
  47. func FormatPrometheusMetrics(out io.Writer, url string, formatType string, noUnit bool) error {
  48. mfChan := make(chan *dto.MetricFamily, 1024)
  49. errChan := make(chan error, 1)
  50. // Start with the DefaultTransport for sane defaults.
  51. transport := http.DefaultTransport.(*http.Transport).Clone()
  52. // Conservatively disable HTTP keep-alives as this program will only
  53. // ever need a single HTTP request.
  54. transport.DisableKeepAlives = true
  55. // Timeout early if the server doesn't even return the headers.
  56. transport.ResponseHeaderTimeout = time.Minute
  57. go func() {
  58. defer trace.CatchPanic("crowdsec/ShowPrometheus")
  59. err := prom2json.FetchMetricFamilies(url, mfChan, transport)
  60. if err != nil {
  61. errChan <- fmt.Errorf("failed to fetch prometheus metrics: %w", err)
  62. return
  63. }
  64. errChan <- nil
  65. }()
  66. result := []*prom2json.Family{}
  67. for mf := range mfChan {
  68. result = append(result, prom2json.NewFamily(mf))
  69. }
  70. if err := <-errChan; err != nil {
  71. return err
  72. }
  73. log.Debugf("Finished reading prometheus output, %d entries", len(result))
  74. /*walk*/
  75. mAcquis := statAcquis{}
  76. mParser := statParser{}
  77. mBucket := statBucket{}
  78. mLapi := statLapi{}
  79. mLapiMachine := statLapiMachine{}
  80. mLapiBouncer := statLapiBouncer{}
  81. mLapiDecision := statLapiDecision{}
  82. mDecision := statDecision{}
  83. mAppsecEngine := statAppsecEngine{}
  84. mAppsecRule := statAppsecRule{}
  85. mAlert := statAlert{}
  86. mStash := statStash{}
  87. for idx, fam := range result {
  88. if !strings.HasPrefix(fam.Name, "cs_") {
  89. continue
  90. }
  91. log.Tracef("round %d", idx)
  92. for _, m := range fam.Metrics {
  93. metric, ok := m.(prom2json.Metric)
  94. if !ok {
  95. log.Debugf("failed to convert metric to prom2json.Metric")
  96. continue
  97. }
  98. name, ok := metric.Labels["name"]
  99. if !ok {
  100. log.Debugf("no name in Metric %v", metric.Labels)
  101. }
  102. source, ok := metric.Labels["source"]
  103. if !ok {
  104. log.Debugf("no source in Metric %v for %s", metric.Labels, fam.Name)
  105. } else {
  106. if srctype, ok := metric.Labels["type"]; ok {
  107. source = srctype + ":" + source
  108. }
  109. }
  110. value := m.(prom2json.Metric).Value
  111. machine := metric.Labels["machine"]
  112. bouncer := metric.Labels["bouncer"]
  113. route := metric.Labels["route"]
  114. method := metric.Labels["method"]
  115. reason := metric.Labels["reason"]
  116. origin := metric.Labels["origin"]
  117. action := metric.Labels["action"]
  118. mtype := metric.Labels["type"]
  119. fval, err := strconv.ParseFloat(value, 32)
  120. if err != nil {
  121. log.Errorf("Unexpected int value %s : %s", value, err)
  122. }
  123. ival := int(fval)
  124. switch fam.Name {
  125. /*buckets*/
  126. case "cs_bucket_created_total":
  127. if _, ok := mBucket[name]; !ok {
  128. mBucket[name] = make(map[string]int)
  129. }
  130. mBucket[name]["instantiation"] += ival
  131. case "cs_buckets":
  132. if _, ok := mBucket[name]; !ok {
  133. mBucket[name] = make(map[string]int)
  134. }
  135. mBucket[name]["curr_count"] += ival
  136. case "cs_bucket_overflowed_total":
  137. if _, ok := mBucket[name]; !ok {
  138. mBucket[name] = make(map[string]int)
  139. }
  140. mBucket[name]["overflow"] += ival
  141. case "cs_bucket_poured_total":
  142. if _, ok := mBucket[name]; !ok {
  143. mBucket[name] = make(map[string]int)
  144. }
  145. if _, ok := mAcquis[source]; !ok {
  146. mAcquis[source] = make(map[string]int)
  147. }
  148. mBucket[name]["pour"] += ival
  149. mAcquis[source]["pour"] += ival
  150. case "cs_bucket_underflowed_total":
  151. if _, ok := mBucket[name]; !ok {
  152. mBucket[name] = make(map[string]int)
  153. }
  154. mBucket[name]["underflow"] += ival
  155. /*acquis*/
  156. case "cs_parser_hits_total":
  157. if _, ok := mAcquis[source]; !ok {
  158. mAcquis[source] = make(map[string]int)
  159. }
  160. mAcquis[source]["reads"] += ival
  161. case "cs_parser_hits_ok_total":
  162. if _, ok := mAcquis[source]; !ok {
  163. mAcquis[source] = make(map[string]int)
  164. }
  165. mAcquis[source]["parsed"] += ival
  166. case "cs_parser_hits_ko_total":
  167. if _, ok := mAcquis[source]; !ok {
  168. mAcquis[source] = make(map[string]int)
  169. }
  170. mAcquis[source]["unparsed"] += ival
  171. case "cs_node_hits_total":
  172. if _, ok := mParser[name]; !ok {
  173. mParser[name] = make(map[string]int)
  174. }
  175. mParser[name]["hits"] += ival
  176. case "cs_node_hits_ok_total":
  177. if _, ok := mParser[name]; !ok {
  178. mParser[name] = make(map[string]int)
  179. }
  180. mParser[name]["parsed"] += ival
  181. case "cs_node_hits_ko_total":
  182. if _, ok := mParser[name]; !ok {
  183. mParser[name] = make(map[string]int)
  184. }
  185. mParser[name]["unparsed"] += ival
  186. case "cs_lapi_route_requests_total":
  187. if _, ok := mLapi[route]; !ok {
  188. mLapi[route] = make(map[string]int)
  189. }
  190. mLapi[route][method] += ival
  191. case "cs_lapi_machine_requests_total":
  192. if _, ok := mLapiMachine[machine]; !ok {
  193. mLapiMachine[machine] = make(map[string]map[string]int)
  194. }
  195. if _, ok := mLapiMachine[machine][route]; !ok {
  196. mLapiMachine[machine][route] = make(map[string]int)
  197. }
  198. mLapiMachine[machine][route][method] += ival
  199. case "cs_lapi_bouncer_requests_total":
  200. if _, ok := mLapiBouncer[bouncer]; !ok {
  201. mLapiBouncer[bouncer] = make(map[string]map[string]int)
  202. }
  203. if _, ok := mLapiBouncer[bouncer][route]; !ok {
  204. mLapiBouncer[bouncer][route] = make(map[string]int)
  205. }
  206. mLapiBouncer[bouncer][route][method] += ival
  207. case "cs_lapi_decisions_ko_total", "cs_lapi_decisions_ok_total":
  208. if _, ok := mLapiDecision[bouncer]; !ok {
  209. mLapiDecision[bouncer] = struct {
  210. NonEmpty int
  211. Empty int
  212. }{}
  213. }
  214. x := mLapiDecision[bouncer]
  215. if fam.Name == "cs_lapi_decisions_ko_total" {
  216. x.Empty += ival
  217. } else if fam.Name == "cs_lapi_decisions_ok_total" {
  218. x.NonEmpty += ival
  219. }
  220. mLapiDecision[bouncer] = x
  221. case "cs_active_decisions":
  222. if _, ok := mDecision[reason]; !ok {
  223. mDecision[reason] = make(map[string]map[string]int)
  224. }
  225. if _, ok := mDecision[reason][origin]; !ok {
  226. mDecision[reason][origin] = make(map[string]int)
  227. }
  228. mDecision[reason][origin][action] += ival
  229. case "cs_alerts":
  230. /*if _, ok := mAlert[scenario]; !ok {
  231. mAlert[scenario] = make(map[string]int)
  232. }*/
  233. mAlert[reason] += ival
  234. case "cs_cache_size":
  235. mStash[name] = struct {
  236. Type string
  237. Count int
  238. }{Type: mtype, Count: ival}
  239. case "cs_appsec_reqs_total":
  240. if _, ok := mAppsecEngine[metric.Labels["appsec_engine"]]; !ok {
  241. mAppsecEngine[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
  242. }
  243. mAppsecEngine[metric.Labels["appsec_engine"]]["processed"] = ival
  244. case "cs_appsec_block_total":
  245. if _, ok := mAppsecEngine[metric.Labels["appsec_engine"]]; !ok {
  246. mAppsecEngine[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
  247. }
  248. mAppsecEngine[metric.Labels["appsec_engine"]]["blocked"] = ival
  249. case "cs_appsec_rule_hits":
  250. appsecEngine := metric.Labels["appsec_engine"]
  251. ruleID := metric.Labels["rule_name"]
  252. if _, ok := mAppsecRule[appsecEngine]; !ok {
  253. mAppsecRule[appsecEngine] = make(map[string]map[string]int, 0)
  254. }
  255. if _, ok := mAppsecRule[appsecEngine][ruleID]; !ok {
  256. mAppsecRule[appsecEngine][ruleID] = make(map[string]int, 0)
  257. }
  258. mAppsecRule[appsecEngine][ruleID]["triggered"] = ival
  259. default:
  260. log.Debugf("unknown: %+v", fam.Name)
  261. continue
  262. }
  263. }
  264. }
  265. if formatType == "human" {
  266. mAcquis.table(out, noUnit)
  267. mBucket.table(out, noUnit)
  268. mParser.table(out, noUnit)
  269. mLapi.table(out)
  270. mLapiMachine.table(out)
  271. mLapiBouncer.table(out)
  272. mLapiDecision.table(out)
  273. mDecision.table(out)
  274. mAlert.table(out)
  275. mStash.table(out)
  276. mAppsecEngine.table(out, noUnit)
  277. mAppsecRule.table(out, noUnit)
  278. return nil
  279. }
  280. stats := make(map[string]any)
  281. stats["acquisition"] = mAcquis
  282. stats["buckets"] = mBucket
  283. stats["parsers"] = mParser
  284. stats["lapi"] = mLapi
  285. stats["lapi_machine"] = mLapiMachine
  286. stats["lapi_bouncer"] = mLapiBouncer
  287. stats["lapi_decisions"] = mLapiDecision
  288. stats["decisions"] = mDecision
  289. stats["alerts"] = mAlert
  290. stats["stash"] = mStash
  291. switch formatType {
  292. case "json":
  293. x, err := json.MarshalIndent(stats, "", " ")
  294. if err != nil {
  295. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  296. }
  297. out.Write(x)
  298. case "raw":
  299. x, err := yaml.Marshal(stats)
  300. if err != nil {
  301. return fmt.Errorf("failed to unmarshal metrics : %v", err)
  302. }
  303. out.Write(x)
  304. default:
  305. return fmt.Errorf("unknown format type %s", formatType)
  306. }
  307. return nil
  308. }
  309. func (cli *cliMetrics) run(url string, noUnit bool) error {
  310. cfg := cli.cfg()
  311. if url != "" {
  312. cfg.Cscli.PrometheusUrl = url
  313. }
  314. if cfg.Prometheus == nil {
  315. return fmt.Errorf("prometheus section missing, can't show metrics")
  316. }
  317. if !cfg.Prometheus.Enabled {
  318. return fmt.Errorf("prometheus is not enabled, can't show metrics")
  319. }
  320. if err := FormatPrometheusMetrics(color.Output, cfg.Cscli.PrometheusUrl, cfg.Cscli.Output, noUnit); err != nil {
  321. return err
  322. }
  323. return nil
  324. }
  325. func (cli *cliMetrics) NewCommand() *cobra.Command {
  326. var (
  327. url string
  328. noUnit bool
  329. )
  330. cmd := &cobra.Command{
  331. Use: "metrics",
  332. Short: "Display crowdsec prometheus metrics.",
  333. Long: `Fetch metrics from the prometheus server and display them in a human-friendly way`,
  334. Args: cobra.ExactArgs(0),
  335. DisableAutoGenTag: true,
  336. RunE: func(cmd *cobra.Command, args []string) error {
  337. return cli.run(url, noUnit)
  338. },
  339. }
  340. flags := cmd.Flags()
  341. flags.StringVarP(&url, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
  342. flags.BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")
  343. return cmd
  344. }