monitoring.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Gábor Boskovits <boskovits@gmail.com>
  3. ;;; Copyright © 2018, 2019 Oleg Pykhalov <go.wigust@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu tests monitoring)
  20. #:use-module (gnu packages databases)
  21. #:use-module (gnu packages monitoring)
  22. #:use-module (gnu packages php)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services monitoring)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu services databases)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu services web)
  29. #:use-module (gnu system vm)
  30. #:use-module (gnu system)
  31. #:use-module (gnu tests)
  32. #:use-module (guix gexp)
  33. #:export (%test-prometheus-node-exporter
  34. %test-zabbix))
  35. ;;;
  36. ;;; Prometheus Node Exporter
  37. ;;;
  38. (define* (run-prometheus-node-exporter-server-test name test-os)
  39. "Run tests in %PROMETHEUS-NODE-EXPORTER-OS, which has prometheus-node-exporter running."
  40. (define os
  41. (marionette-operating-system
  42. test-os
  43. #:imported-modules '((gnu services herd))))
  44. (define vm
  45. (virtual-machine
  46. (operating-system os)
  47. (port-forwardings '((8080 . 9100)))))
  48. (define test
  49. (with-imported-modules '((gnu build marionette))
  50. #~(begin
  51. (use-modules (srfi srfi-11)
  52. (srfi srfi-64)
  53. (gnu build marionette)
  54. (web client)
  55. (web response))
  56. (define marionette
  57. (make-marionette (list #$vm)))
  58. (mkdir #$output)
  59. (chdir #$output)
  60. (test-begin #$name)
  61. (test-assert "prometheus-node-exporter running"
  62. (marionette-eval
  63. '(begin
  64. (use-modules (gnu services herd))
  65. (match (start-service 'prometheus-node-exporter)
  66. (#f #f)
  67. (('service response-parts ...)
  68. (match (assq-ref response-parts 'running)
  69. ((pid) (number? pid))))))
  70. marionette))
  71. (test-equal "http-get"
  72. 200
  73. (begin
  74. (wait-for-tcp-port 9100 marionette)
  75. (let-values (((response text)
  76. (http-get "http://localhost:8080")))
  77. (response-code response))))
  78. (test-end)
  79. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  80. (gexp->derivation (string-append name "-test") test))
  81. (define %prometheus-node-exporter-os
  82. (simple-operating-system
  83. (service dhcp-client-service-type)
  84. (service prometheus-node-exporter-service-type
  85. (prometheus-node-exporter-configuration))))
  86. (define %test-prometheus-node-exporter
  87. (system-test
  88. (name "prometheus-node-exporter")
  89. (description "Connect to a running prometheus-node-exporter server.")
  90. (value (run-prometheus-node-exporter-server-test
  91. name %prometheus-node-exporter-os))))
  92. ;;;
  93. ;;; Zabbix
  94. ;;;
  95. (define %psql-user-create-zabbix
  96. "\
  97. sudo -u postgres psql <<< \"create user zabbix password 'zabbix';\"
  98. ")
  99. (define %psql-db-zabbix-create-script
  100. "\
  101. sudo -u postgres psql --no-align <<< \\\\du
  102. ")
  103. (define %psql-db-create-zabbix
  104. "\
  105. sudo -u postgres createdb -O zabbix -E Unicode -T template0 zabbix
  106. ")
  107. (define %psql-db-import-zabbix
  108. #~(format #f "\
  109. cat ~a | sudo -u zabbix psql zabbix;
  110. cat ~a | sudo -u zabbix psql zabbix;
  111. cat ~a | sudo -u zabbix psql zabbix;
  112. "
  113. (string-append #$zabbix-server:schema
  114. "/database/postgresql/schema.sql")
  115. (string-append #$zabbix-server:schema
  116. "/database/postgresql/images.sql")
  117. (string-append #$zabbix-server:schema
  118. "/database/postgresql/data.sql")))
  119. (define* (run-zabbix-server-test name test-os)
  120. "Run tests in %ZABBIX-OS, which has zabbix running."
  121. (define os
  122. (marionette-operating-system
  123. test-os
  124. #:imported-modules '((gnu services herd))))
  125. (define vm
  126. (virtual-machine
  127. (operating-system os)
  128. (port-forwardings '((8080 . 80)))
  129. (memory-size 1024)))
  130. (define test
  131. (with-imported-modules '((gnu build marionette))
  132. #~(begin
  133. (use-modules (srfi srfi-11)
  134. (srfi srfi-64)
  135. (gnu build marionette)
  136. (web client)
  137. (web response)
  138. (ice-9 popen)
  139. (ice-9 rdelim))
  140. (define marionette
  141. (make-marionette (list #$vm)))
  142. (mkdir #$output)
  143. (chdir #$output)
  144. (test-begin #$name)
  145. ;; XXX: Shepherd reads the config file *before* binding its control
  146. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  147. ;; 'marionette' service is started.
  148. (test-assert "shepherd socket ready"
  149. (marionette-eval
  150. `(begin
  151. (use-modules (gnu services herd))
  152. (let loop ((i 10))
  153. (cond ((file-exists? (%shepherd-socket-file))
  154. #t)
  155. ((> i 0)
  156. (sleep 1)
  157. (loop (- i 1)))
  158. (else
  159. 'failure))))
  160. marionette))
  161. (test-assert "postgres service running"
  162. (marionette-eval
  163. '(begin
  164. (use-modules (gnu services herd))
  165. (start-service 'postgres))
  166. marionette))
  167. ;; Add /run/setuid-programs to $PATH so that the scripts passed to
  168. ;; 'system' can find 'sudo'.
  169. (marionette-eval
  170. '(setenv "PATH"
  171. "/run/setuid-programs:/run/current-system/profile/bin")
  172. marionette)
  173. (test-eq "postgres create zabbix user"
  174. 0
  175. (marionette-eval '(begin (system #$%psql-user-create-zabbix))
  176. marionette))
  177. (test-equal "postgres find zabbix user"
  178. "List of roles
  179. Role name|Attributes|Member of
  180. postgres|Superuser, Create role, Create DB, Replication, Bypass RLS|{}
  181. zabbix||{}
  182. "
  183. (marionette-eval
  184. '(begin (let* ((port (open-pipe #$%psql-db-zabbix-create-script
  185. OPEN_READ))
  186. (output (read-string port))
  187. (status (close-pipe port)))
  188. output))
  189. marionette))
  190. (test-eq "postgres create zabbix db"
  191. 0
  192. (marionette-eval '(begin (system #$%psql-db-create-zabbix))
  193. marionette))
  194. (test-eq "postgres import zabbix db"
  195. 0
  196. (marionette-eval '(begin (system #$%psql-db-import-zabbix))
  197. marionette))
  198. ;; Wait for zabbix-server to be up and running.
  199. (test-assert "zabbix-server running"
  200. (marionette-eval
  201. '(begin
  202. (use-modules (gnu services herd))
  203. (start-service 'zabbix-server))
  204. marionette))
  205. ;; Make sure the PID file is created.
  206. (test-assert "zabbix-server PID file"
  207. (marionette-eval
  208. '(file-exists? "/var/run/zabbix/zabbix_server.pid")
  209. marionette))
  210. ;; Wait for zabbix-agent to be up and running.
  211. (test-assert "zabbix-agent running"
  212. (marionette-eval
  213. '(begin
  214. (use-modules (gnu services herd))
  215. (start-service 'zabbix-agent))
  216. marionette))
  217. ;; Make sure the PID file is created.
  218. (test-assert "zabbix-agent PID file"
  219. (marionette-eval
  220. '(file-exists? "/var/run/zabbix/zabbix_agent.pid")
  221. marionette))
  222. ;; Wait for php-fpm to be up and running.
  223. (test-assert "php-fpm running"
  224. (marionette-eval
  225. '(begin
  226. (use-modules (gnu services herd))
  227. (start-service 'php-fpm))
  228. marionette))
  229. ;; Wait for nginx to be up and running.
  230. (test-assert "nginx running"
  231. (marionette-eval
  232. '(begin
  233. (use-modules (gnu services herd))
  234. (start-service 'nginx))
  235. marionette))
  236. ;; Make sure the PID file is created.
  237. (test-assert "nginx PID file"
  238. (marionette-eval
  239. '(file-exists? "/var/run/nginx/pid")
  240. marionette))
  241. ;; Make sure we can access pages that correspond to our Zabbix instance.
  242. (letrec-syntax ((test-url
  243. (syntax-rules ()
  244. ((_ path code)
  245. (test-equal (string-append "GET " path)
  246. code
  247. (let-values (((response body)
  248. (http-get (string-append
  249. "http://localhost:8080"
  250. path))))
  251. (response-code response))))
  252. ((_ path)
  253. (test-url path 200)))))
  254. (test-url "/")
  255. (test-url "/does-not-exist" 404))
  256. (test-end)
  257. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  258. (gexp->derivation (string-append name "-test") test))
  259. (define %zabbix-os
  260. ;; Return operating system under test.
  261. (let ((base-os
  262. (simple-operating-system
  263. (service dhcp-client-service-type)
  264. (service postgresql-service-type
  265. (postgresql-configuration
  266. (postgresql postgresql)))
  267. (service zabbix-front-end-service-type
  268. (zabbix-front-end-configuration
  269. (db-password "zabbix")))
  270. (service php-fpm-service-type
  271. (php-fpm-configuration
  272. (timezone "Europe/Paris")))
  273. (service zabbix-server-service-type
  274. (zabbix-server-configuration
  275. (db-password "zabbix")
  276. (log-type "console")))
  277. (service zabbix-agent-service-type))))
  278. (operating-system
  279. (inherit base-os)
  280. (packages (cons* postgresql (operating-system-packages base-os))))))
  281. (define %test-zabbix
  282. (system-test
  283. (name "zabbix")
  284. (description "Connect to a running Zabbix")
  285. (value (run-zabbix-server-test name %zabbix-os))))