test_zabbix.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. from flexmock import flexmock
  2. import borgmatic.hooks.monitor
  3. from borgmatic.hooks import zabbix as module
  4. SERVER = 'https://zabbix.com/zabbix/api_jsonrpc.php'
  5. ITEMID = 55105
  6. USERNAME = 'testuser'
  7. PASSWORD = 'fakepassword'
  8. API_KEY = 'fakekey'
  9. HOST = 'borg-server'
  10. KEY = 'borg.status'
  11. VALUE = 'fail'
  12. DATA_HOST_KEY = {
  13. 'jsonrpc': '2.0',
  14. 'method': 'history.push',
  15. 'params': {'host': HOST, 'key': KEY, 'value': VALUE},
  16. 'id': 1,
  17. }
  18. DATA_HOST_KEY_WITH_KEY_VALUE = {
  19. 'jsonrpc': '2.0',
  20. 'method': 'history.push',
  21. 'params': {'host': HOST, 'key': KEY, 'value': VALUE},
  22. 'id': 1,
  23. 'auth': '3fe6ed01a69ebd79907a120bcd04e494',
  24. }
  25. DATA_ITEMID = {
  26. 'jsonrpc': '2.0',
  27. 'method': 'history.push',
  28. 'params': {'itemid': ITEMID, 'value': VALUE},
  29. 'id': 1,
  30. }
  31. DATA_HOST_KEY_WITH_ITEMID = {
  32. 'jsonrpc': '2.0',
  33. 'method': 'history.push',
  34. 'params': {'itemid': ITEMID, 'value': VALUE},
  35. 'id': 1,
  36. 'auth': '3fe6ed01a69ebd79907a120bcd04e494',
  37. }
  38. DATA_USER_LOGIN = {
  39. 'jsonrpc': '2.0',
  40. 'method': 'user.login',
  41. 'params': {'username': USERNAME, 'password': PASSWORD},
  42. 'id': 1,
  43. }
  44. AUTH_HEADERS_API_KEY = {
  45. 'Content-Type': 'application/json-rpc',
  46. 'Authorization': f'Bearer {API_KEY}',
  47. }
  48. AUTH_HEADERS_USERNAME_PASSWORD = {'Content-Type': 'application/json-rpc'}
  49. def test_ping_monitor_with_non_matching_state_exits_early():
  50. hook_config = {'api_key': API_KEY}
  51. flexmock(module.requests).should_receive('post').never()
  52. module.ping_monitor(
  53. hook_config,
  54. {},
  55. 'config.yaml',
  56. borgmatic.hooks.monitor.State.START,
  57. monitoring_log_level=1,
  58. dry_run=False,
  59. )
  60. def test_ping_monitor_config_with_api_key_only_exit_early():
  61. # This test should exit early since only providing an API KEY is not enough
  62. # for the hook to work
  63. hook_config = {'api_key': API_KEY}
  64. flexmock(module.logger).should_receive('warning').once()
  65. flexmock(module.requests).should_receive('post').never()
  66. module.ping_monitor(
  67. hook_config,
  68. {},
  69. 'config.yaml',
  70. borgmatic.hooks.monitor.State.FAIL,
  71. monitoring_log_level=1,
  72. dry_run=False,
  73. )
  74. def test_ping_monitor_config_with_host_only_exit_early():
  75. # This test should exit early since only providing a HOST is not enough
  76. # for the hook to work
  77. hook_config = {'host': HOST}
  78. flexmock(module.logger).should_receive('warning').once()
  79. flexmock(module.requests).should_receive('post').never()
  80. module.ping_monitor(
  81. hook_config,
  82. {},
  83. 'config.yaml',
  84. borgmatic.hooks.monitor.State.FAIL,
  85. monitoring_log_level=1,
  86. dry_run=False,
  87. )
  88. def test_ping_monitor_config_with_key_only_exit_early():
  89. # This test should exit early since only providing a KEY is not enough
  90. # for the hook to work
  91. hook_config = {'key': KEY}
  92. flexmock(module.logger).should_receive('warning').once()
  93. flexmock(module.requests).should_receive('post').never()
  94. module.ping_monitor(
  95. hook_config,
  96. {},
  97. 'config.yaml',
  98. borgmatic.hooks.monitor.State.FAIL,
  99. monitoring_log_level=1,
  100. dry_run=False,
  101. )
  102. def test_ping_monitor_config_with_server_only_exit_early():
  103. # This test should exit early since only providing a SERVER is not enough
  104. # for the hook to work
  105. hook_config = {'server': SERVER}
  106. flexmock(module.logger).should_receive('warning').once()
  107. flexmock(module.requests).should_receive('post').never()
  108. module.ping_monitor(
  109. hook_config,
  110. {},
  111. 'config.yaml',
  112. borgmatic.hooks.monitor.State.FAIL,
  113. monitoring_log_level=1,
  114. dry_run=False,
  115. )
  116. def test_ping_monitor_config_user_password_no_zabbix_data_exit_early():
  117. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  118. hook_config = {'server': SERVER, 'username': USERNAME, 'password': PASSWORD}
  119. flexmock(module.logger).should_receive('warning').once()
  120. flexmock(module.requests).should_receive('post').never()
  121. module.ping_monitor(
  122. hook_config,
  123. {},
  124. 'config.yaml',
  125. borgmatic.hooks.monitor.State.FAIL,
  126. monitoring_log_level=1,
  127. dry_run=False,
  128. )
  129. def test_ping_monitor_config_api_key_no_zabbix_data_exit_early():
  130. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  131. hook_config = {'server': SERVER, 'api_key': API_KEY}
  132. flexmock(module.logger).should_receive('warning').once()
  133. flexmock(module.requests).should_receive('post').never()
  134. module.ping_monitor(
  135. hook_config,
  136. {},
  137. 'config.yaml',
  138. borgmatic.hooks.monitor.State.FAIL,
  139. monitoring_log_level=1,
  140. dry_run=False,
  141. )
  142. def test_ping_monitor_config_itemid_no_auth_data_exit_early():
  143. # This test should exit early since there is no authentication provided
  144. # and Zabbix requires authentication to use it's API
  145. hook_config = {'server': SERVER, 'itemid': ITEMID}
  146. flexmock(module.logger).should_receive('warning').once()
  147. flexmock(module.requests).should_receive('post').never()
  148. module.ping_monitor(
  149. hook_config,
  150. {},
  151. 'config.yaml',
  152. borgmatic.hooks.monitor.State.FAIL,
  153. monitoring_log_level=1,
  154. dry_run=False,
  155. )
  156. def test_ping_monitor_config_host_and_key_no_auth_data_exit_early():
  157. # This test should exit early since there is no authentication provided
  158. # and Zabbix requires authentication to use it's API
  159. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY}
  160. flexmock(module.logger).should_receive('warning').once()
  161. flexmock(module.requests).should_receive('post').never()
  162. module.ping_monitor(
  163. hook_config,
  164. {},
  165. 'config.yaml',
  166. borgmatic.hooks.monitor.State.FAIL,
  167. monitoring_log_level=1,
  168. dry_run=False,
  169. )
  170. def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful():
  171. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  172. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  173. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  174. flexmock(module.requests).should_receive('post').with_args(
  175. f'{SERVER}',
  176. headers=AUTH_HEADERS_API_KEY,
  177. json=DATA_HOST_KEY,
  178. ).and_return(flexmock(ok=True)).once()
  179. flexmock(module.logger).should_receive('warning').never()
  180. module.ping_monitor(
  181. hook_config,
  182. {},
  183. 'config.yaml',
  184. borgmatic.hooks.monitor.State.FAIL,
  185. monitoring_log_level=1,
  186. dry_run=False,
  187. )
  188. def test_ping_monitor_config_host_and_missing_key_exits_early():
  189. hook_config = {'server': SERVER, 'host': HOST, 'api_key': API_KEY}
  190. flexmock(module.logger).should_receive('warning').once()
  191. flexmock(module.requests).should_receive('post').never()
  192. module.ping_monitor(
  193. hook_config,
  194. {},
  195. 'config.yaml',
  196. borgmatic.hooks.monitor.State.FAIL,
  197. monitoring_log_level=1,
  198. dry_run=False,
  199. )
  200. def test_ping_monitor_config_key_and_missing_host_exits_early():
  201. hook_config = {'server': SERVER, 'key': KEY, 'api_key': API_KEY}
  202. flexmock(module.logger).should_receive('warning').once()
  203. flexmock(module.requests).should_receive('post').never()
  204. module.ping_monitor(
  205. hook_config,
  206. {},
  207. 'config.yaml',
  208. borgmatic.hooks.monitor.State.FAIL,
  209. monitoring_log_level=1,
  210. dry_run=False,
  211. )
  212. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_successful():
  213. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  214. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  215. hook_config = {
  216. 'server': SERVER,
  217. 'host': HOST,
  218. 'key': KEY,
  219. 'username': USERNAME,
  220. 'password': PASSWORD,
  221. }
  222. auth_response = flexmock(ok=True)
  223. auth_response.should_receive('json').and_return(
  224. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  225. )
  226. flexmock(module.requests).should_receive('post').with_args(
  227. f'{SERVER}',
  228. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  229. json=DATA_USER_LOGIN,
  230. ).and_return(auth_response).once()
  231. flexmock(module.logger).should_receive('warning').never()
  232. flexmock(module.requests).should_receive('post').with_args(
  233. f'{SERVER}',
  234. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  235. json=DATA_HOST_KEY_WITH_KEY_VALUE,
  236. ).and_return(flexmock(ok=True)).once()
  237. module.ping_monitor(
  238. hook_config,
  239. {},
  240. 'config.yaml',
  241. borgmatic.hooks.monitor.State.FAIL,
  242. monitoring_log_level=1,
  243. dry_run=False,
  244. )
  245. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_and_auth_post_error_exits_early():
  246. hook_config = {
  247. 'server': SERVER,
  248. 'host': HOST,
  249. 'key': KEY,
  250. 'username': USERNAME,
  251. 'password': PASSWORD,
  252. }
  253. auth_response = flexmock(ok=False)
  254. auth_response.should_receive('json').and_return(
  255. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  256. )
  257. auth_response.should_receive('raise_for_status').and_raise(
  258. module.requests.ConnectionError
  259. ).once()
  260. flexmock(module.requests).should_receive('post').with_args(
  261. f'{SERVER}',
  262. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  263. json=DATA_USER_LOGIN,
  264. ).and_return(auth_response).once()
  265. flexmock(module.logger).should_receive('warning').once()
  266. flexmock(module.requests).should_receive('post').with_args(
  267. f'{SERVER}',
  268. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  269. json=DATA_HOST_KEY_WITH_KEY_VALUE,
  270. ).never()
  271. module.ping_monitor(
  272. hook_config,
  273. {},
  274. 'config.yaml',
  275. borgmatic.hooks.monitor.State.FAIL,
  276. monitoring_log_level=1,
  277. dry_run=False,
  278. )
  279. def test_ping_monitor_config_host_and_key_with_username_and_missing_password_exits_early():
  280. hook_config = {
  281. 'server': SERVER,
  282. 'host': HOST,
  283. 'key': KEY,
  284. 'username': USERNAME,
  285. }
  286. flexmock(module.logger).should_receive('warning').once()
  287. flexmock(module.requests).should_receive('post').never()
  288. module.ping_monitor(
  289. hook_config,
  290. {},
  291. 'config.yaml',
  292. borgmatic.hooks.monitor.State.FAIL,
  293. monitoring_log_level=1,
  294. dry_run=False,
  295. )
  296. def test_ping_monitor_config_host_and_key_with_passing_and_missing_username_exits_early():
  297. hook_config = {
  298. 'server': SERVER,
  299. 'host': HOST,
  300. 'key': KEY,
  301. 'password': PASSWORD,
  302. }
  303. flexmock(module.logger).should_receive('warning').once()
  304. flexmock(module.requests).should_receive('post').never()
  305. module.ping_monitor(
  306. hook_config,
  307. {},
  308. 'config.yaml',
  309. borgmatic.hooks.monitor.State.FAIL,
  310. monitoring_log_level=1,
  311. dry_run=False,
  312. )
  313. def test_ping_monitor_config_itemid_with_api_key_auth_data_successful():
  314. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  315. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  316. hook_config = {'server': SERVER, 'itemid': ITEMID, 'api_key': API_KEY}
  317. flexmock(module.requests).should_receive('post').with_args(
  318. f'{SERVER}',
  319. headers=AUTH_HEADERS_API_KEY,
  320. json=DATA_ITEMID,
  321. ).and_return(flexmock(ok=True)).once()
  322. flexmock(module.logger).should_receive('warning').never()
  323. module.ping_monitor(
  324. hook_config,
  325. {},
  326. 'config.yaml',
  327. borgmatic.hooks.monitor.State.FAIL,
  328. monitoring_log_level=1,
  329. dry_run=False,
  330. )
  331. def test_ping_monitor_config_itemid_with_username_password_auth_data_successful():
  332. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  333. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  334. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  335. auth_response = flexmock(ok=True)
  336. auth_response.should_receive('json').and_return(
  337. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  338. )
  339. flexmock(module.requests).should_receive('post').with_args(
  340. f'{SERVER}',
  341. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  342. json=DATA_USER_LOGIN,
  343. ).and_return(auth_response).once()
  344. flexmock(module.logger).should_receive('warning').never()
  345. flexmock(module.requests).should_receive('post').with_args(
  346. f'{SERVER}',
  347. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  348. json=DATA_HOST_KEY_WITH_ITEMID,
  349. ).and_return(flexmock(ok=True)).once()
  350. module.ping_monitor(
  351. hook_config,
  352. {},
  353. 'config.yaml',
  354. borgmatic.hooks.monitor.State.FAIL,
  355. monitoring_log_level=1,
  356. dry_run=False,
  357. )
  358. def test_ping_monitor_config_itemid_with_username_password_auth_data_and_push_post_error_exits_early():
  359. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  360. auth_response = flexmock(ok=True)
  361. auth_response.should_receive('json').and_return(
  362. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  363. )
  364. flexmock(module.requests).should_receive('post').with_args(
  365. f'{SERVER}',
  366. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  367. json=DATA_USER_LOGIN,
  368. ).and_return(auth_response).once()
  369. push_response = flexmock(ok=False)
  370. push_response.should_receive('raise_for_status').and_raise(
  371. module.requests.ConnectionError
  372. ).once()
  373. flexmock(module.requests).should_receive('post').with_args(
  374. f'{SERVER}',
  375. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  376. json=DATA_HOST_KEY_WITH_ITEMID,
  377. ).and_return(push_response).once()
  378. flexmock(module.logger).should_receive('warning').once()
  379. module.ping_monitor(
  380. hook_config,
  381. {},
  382. 'config.yaml',
  383. borgmatic.hooks.monitor.State.FAIL,
  384. monitoring_log_level=1,
  385. dry_run=False,
  386. )