executable_prometheus-billing2 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env -S guile --no-auto-compile -e main -s
  2. !#
  3. (use-modules (mjru config)
  4. (guile mariadb)
  5. (guix records)
  6. (json)
  7. (ice-9 match)
  8. (ice-9 popen)
  9. (ice-9 pretty-print)
  10. (ice-9 rdelim)
  11. (srfi srfi-1)
  12. (srfi srfi-26)
  13. (srfi srfi-37))
  14. (define %options
  15. (let ((display-and-exit-proc (lambda (msg)
  16. (lambda (opt name arg loads)
  17. (display msg) (quit)))))
  18. (list (option '(#\v "version") #f #f
  19. (display-and-exit-proc "prometheus-billing2 version 0.0.1\n"))
  20. (option '(#\h "help") #f #f
  21. (display-and-exit-proc
  22. "Usage: prometheus-billing2 ...\n"))
  23. (option '(#\d "debug") #f #f
  24. (lambda (opt name arg result . rest)
  25. (apply values
  26. (alist-cons 'debug? #t
  27. (alist-delete 'debug? result eq?))
  28. rest))))))
  29. (define %default-options
  30. '())
  31. (define-json-mapping <vds> make-vds vds?
  32. json->vds
  33. (id vds-id "vds_id")
  34. (ip-address vds-ip-address "ip_address"))
  35. (define %excluded-accounts
  36. (append (list (match %billing2-account ((email account-id) account-id)))
  37. (match %billing2-support (((email account-id) ...) account-id))))
  38. (define (main args)
  39. (define opts
  40. (args-fold (cdr (program-arguments))
  41. %options
  42. (lambda (opt name arg loads)
  43. (error "Unrecognized option `~A'" name))
  44. (lambda (op loads)
  45. (cons op loads))
  46. %default-options))
  47. (define debug? (assoc-ref opts 'debug?))
  48. (define connection
  49. (mariadb-connection
  50. (inherit mariadb-connection-billing2)
  51. (statement (let ((tables '("equip_ip_addresses" "vds_accounts")))
  52. (string-join `("SELECT"
  53. "JSON_OBJECT ('vds_id', vds_account_id, 'ip_address', equip_ip_addresses.address)"
  54. "FROM"
  55. ,(string-join tables ",")
  56. "WHERE"
  57. ,(string-join (map (lambda (account)
  58. (string-join (list "vds_accounts.client_id" "!=" account)))
  59. (map number->string %excluded-accounts))
  60. " AND ")
  61. "AND" "equip_ip_addresses.equip_ip_address_id" "=" "vds_accounts.equip_ip_address_id"
  62. "AND" "(vds_accounts.vds_plan_id = 40 OR vds_accounts.vds_plan_id = 41 OR vds_accounts.vds_plan_id = 42)"))))))
  63. (when debug?
  64. (format #t "Executing statement: ~s.~%"
  65. (mariadb-connection-statement connection)))
  66. (match-record connection <mariadb-connection>
  67. (user password host database statement arguments)
  68. (let* ((command `("--silent"
  69. ,@(if user
  70. (list (string-append "--user=" user))
  71. '())
  72. ,@(if password
  73. (list (string-append "--password=" password))
  74. '())
  75. ,@(if host
  76. (list (string-append "--host=" host))
  77. '())
  78. ,@(if database
  79. (list (string-append "--database=" database))
  80. '())
  81. ,@(if statement
  82. (list (string-append "--execute=" statement))
  83. '())
  84. ,@arguments))
  85. (port (apply open-pipe* OPEN_READ "mysql" command))
  86. (output (read-string port)))
  87. (close-port port)
  88. (format #t
  89. "{hostname=~~~s}~%"
  90. (string-join
  91. (map (compose (lambda (vds)
  92. (string-append "vm" (number->string (vds-id vds)) "." %vm-domain))
  93. json->vds)
  94. (string-split (string-trim-right output
  95. #\newline)
  96. #\newline))
  97. "|")))))