test_zabbix.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. from flexmock import flexmock
  2. import borgmatic.hooks.monitoring.monitor
  3. from borgmatic.hooks.monitoring 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. }
  24. DATA_ITEMID = {
  25. 'jsonrpc': '2.0',
  26. 'method': 'history.push',
  27. 'params': {'itemid': ITEMID, 'value': VALUE},
  28. 'id': 1,
  29. }
  30. DATA_HOST_KEY_WITH_ITEMID = {
  31. 'jsonrpc': '2.0',
  32. 'method': 'history.push',
  33. 'params': {'itemid': ITEMID, 'value': VALUE},
  34. 'id': 1,
  35. }
  36. DATA_USER_LOGIN = {
  37. 'jsonrpc': '2.0',
  38. 'method': 'user.login',
  39. 'params': {'username': USERNAME, 'password': PASSWORD},
  40. 'id': 1,
  41. }
  42. DATA_USER_LOGOUT = {
  43. 'jsonrpc': '2.0',
  44. 'method': 'user.logout',
  45. 'params': [],
  46. 'id': 1,
  47. }
  48. AUTH_HEADERS_LOGIN = {
  49. 'Content-Type': 'application/json-rpc',
  50. }
  51. AUTH_HEADERS = {
  52. 'Content-Type': 'application/json-rpc',
  53. 'Authorization': f'Bearer {API_KEY}',
  54. }
  55. def test_send_zabbix_request_with_post_error_bails():
  56. server = flexmock()
  57. headers = flexmock()
  58. data = {'method': 'do.stuff'}
  59. response = flexmock(ok=False)
  60. response.should_receive('raise_for_status').and_raise(
  61. module.requests.exceptions.RequestException
  62. )
  63. flexmock(module.requests).should_receive('post').with_args(
  64. server, headers=headers, json=data
  65. ).and_return(response)
  66. assert module.send_zabbix_request(server, headers, data) is None
  67. def test_send_zabbix_request_with_invalid_json_response_bails():
  68. server = flexmock()
  69. headers = flexmock()
  70. data = {'method': 'do.stuff'}
  71. flexmock(module.requests.exceptions.JSONDecodeError).should_receive('__init__')
  72. response = flexmock(ok=True)
  73. response.should_receive('json').and_raise(module.requests.exceptions.JSONDecodeError)
  74. flexmock(module.requests).should_receive('post').with_args(
  75. server, headers=headers, json=data
  76. ).and_return(response)
  77. assert module.send_zabbix_request(server, headers, data) is None
  78. def test_send_zabbix_request_with_success_returns_response_result():
  79. server = flexmock()
  80. headers = flexmock()
  81. data = {'method': 'do.stuff'}
  82. response = flexmock(ok=True)
  83. response.should_receive('json').and_return({'result': {'foo': 'bar'}})
  84. flexmock(module.requests).should_receive('post').with_args(
  85. server, headers=headers, json=data
  86. ).and_return(response)
  87. assert module.send_zabbix_request(server, headers, data) == {'foo': 'bar'}
  88. def test_send_zabbix_request_with_success_passes_through_missing_result():
  89. server = flexmock()
  90. headers = flexmock()
  91. data = {'method': 'do.stuff'}
  92. response = flexmock(ok=True)
  93. response.should_receive('json').and_return({})
  94. flexmock(module.requests).should_receive('post').with_args(
  95. server, headers=headers, json=data
  96. ).and_return(response)
  97. assert module.send_zabbix_request(server, headers, data) is None
  98. def test_send_zabbix_request_with_error_bails():
  99. server = flexmock()
  100. headers = flexmock()
  101. data = {'method': 'do.stuff'}
  102. response = flexmock(ok=True)
  103. response.should_receive('json').and_return({'result': {'data': [{'error': 'oops'}]}})
  104. flexmock(module.requests).should_receive('post').with_args(
  105. server, headers=headers, json=data
  106. ).and_return(response)
  107. assert module.send_zabbix_request(server, headers, data) is None
  108. def test_ping_monitor_with_non_matching_state_bails():
  109. hook_config = {'api_key': API_KEY}
  110. flexmock(module).should_receive('send_zabbix_request').never()
  111. module.ping_monitor(
  112. hook_config,
  113. {},
  114. 'config.yaml',
  115. borgmatic.hooks.monitoring.monitor.State.START,
  116. monitoring_log_level=1,
  117. dry_run=False,
  118. )
  119. def test_ping_monitor_config_with_api_key_only_bails():
  120. # This test should exit early since only providing an API KEY is not enough
  121. # for the hook to work
  122. hook_config = {'api_key': API_KEY}
  123. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  124. 'resolve_credential'
  125. ).replace_with(lambda value, config: value)
  126. flexmock(module.logger).should_receive('warning').once()
  127. flexmock(module).should_receive('send_zabbix_request').never()
  128. module.ping_monitor(
  129. hook_config,
  130. {},
  131. 'config.yaml',
  132. borgmatic.hooks.monitoring.monitor.State.FAIL,
  133. monitoring_log_level=1,
  134. dry_run=False,
  135. )
  136. def test_ping_monitor_config_with_host_only_bails():
  137. # This test should exit early since only providing a HOST is not enough
  138. # for the hook to work
  139. hook_config = {'host': HOST}
  140. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  141. 'resolve_credential'
  142. ).replace_with(lambda value, config: value)
  143. flexmock(module.logger).should_receive('warning').once()
  144. flexmock(module).should_receive('send_zabbix_request').never()
  145. module.ping_monitor(
  146. hook_config,
  147. {},
  148. 'config.yaml',
  149. borgmatic.hooks.monitoring.monitor.State.FAIL,
  150. monitoring_log_level=1,
  151. dry_run=False,
  152. )
  153. def test_ping_monitor_config_with_key_only_bails():
  154. # This test should exit early since only providing a KEY is not enough
  155. # for the hook to work
  156. hook_config = {'key': KEY}
  157. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  158. 'resolve_credential'
  159. ).replace_with(lambda value, config: value)
  160. flexmock(module.logger).should_receive('warning').once()
  161. flexmock(module).should_receive('send_zabbix_request').never()
  162. module.ping_monitor(
  163. hook_config,
  164. {},
  165. 'config.yaml',
  166. borgmatic.hooks.monitoring.monitor.State.FAIL,
  167. monitoring_log_level=1,
  168. dry_run=False,
  169. )
  170. def test_ping_monitor_config_with_server_only_bails():
  171. # This test should exit early since only providing a SERVER is not enough
  172. # for the hook to work
  173. hook_config = {'server': SERVER}
  174. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  175. 'resolve_credential'
  176. ).replace_with(lambda value, config: value)
  177. flexmock(module.logger).should_receive('warning').once()
  178. flexmock(module).should_receive('send_zabbix_request').never()
  179. module.ping_monitor(
  180. hook_config,
  181. {},
  182. 'config.yaml',
  183. borgmatic.hooks.monitoring.monitor.State.FAIL,
  184. monitoring_log_level=1,
  185. dry_run=False,
  186. )
  187. def test_ping_monitor_config_user_password_no_zabbix_data_bails():
  188. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  189. hook_config = {'server': SERVER, 'username': USERNAME, 'password': PASSWORD}
  190. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  191. 'resolve_credential'
  192. ).replace_with(lambda value, config: value)
  193. flexmock(module.logger).should_receive('warning').once()
  194. flexmock(module).should_receive('send_zabbix_request').never()
  195. module.ping_monitor(
  196. hook_config,
  197. {},
  198. 'config.yaml',
  199. borgmatic.hooks.monitoring.monitor.State.FAIL,
  200. monitoring_log_level=1,
  201. dry_run=False,
  202. )
  203. def test_ping_monitor_config_api_key_no_zabbix_data_bails():
  204. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  205. hook_config = {'server': SERVER, 'api_key': API_KEY}
  206. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  207. 'resolve_credential'
  208. ).replace_with(lambda value, config: value)
  209. flexmock(module.logger).should_receive('warning').once()
  210. flexmock(module).should_receive('send_zabbix_request').never()
  211. module.ping_monitor(
  212. hook_config,
  213. {},
  214. 'config.yaml',
  215. borgmatic.hooks.monitoring.monitor.State.FAIL,
  216. monitoring_log_level=1,
  217. dry_run=False,
  218. )
  219. def test_ping_monitor_config_itemid_no_auth_data_bails():
  220. # This test should exit early since there is no authentication provided
  221. # and Zabbix requires authentication to use it's API
  222. hook_config = {'server': SERVER, 'itemid': ITEMID}
  223. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  224. 'resolve_credential'
  225. ).replace_with(lambda value, config: value)
  226. flexmock(module.logger).should_receive('warning').once()
  227. flexmock(module).should_receive('send_zabbix_request').never()
  228. module.ping_monitor(
  229. hook_config,
  230. {},
  231. 'config.yaml',
  232. borgmatic.hooks.monitoring.monitor.State.FAIL,
  233. monitoring_log_level=1,
  234. dry_run=False,
  235. )
  236. def test_ping_monitor_config_host_and_key_no_auth_data_bails():
  237. # This test should exit early since there is no authentication provided
  238. # and Zabbix requires authentication to use it's API
  239. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY}
  240. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  241. 'resolve_credential'
  242. ).replace_with(lambda value, config: value)
  243. flexmock(module.logger).should_receive('warning').once()
  244. flexmock(module).should_receive('send_zabbix_request').never()
  245. module.ping_monitor(
  246. hook_config,
  247. {},
  248. 'config.yaml',
  249. borgmatic.hooks.monitoring.monitor.State.FAIL,
  250. monitoring_log_level=1,
  251. dry_run=False,
  252. )
  253. def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful():
  254. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  255. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  256. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  257. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  258. 'resolve_credential'
  259. ).replace_with(lambda value, config: value)
  260. flexmock(module).should_receive('send_zabbix_request').with_args(
  261. f'{SERVER}',
  262. headers=AUTH_HEADERS,
  263. data=DATA_HOST_KEY,
  264. ).once()
  265. flexmock(module.logger).should_receive('warning').never()
  266. module.ping_monitor(
  267. hook_config,
  268. {},
  269. 'config.yaml',
  270. borgmatic.hooks.monitoring.monitor.State.FAIL,
  271. monitoring_log_level=1,
  272. dry_run=False,
  273. )
  274. def test_ping_monitor_config_adds_missing_api_endpoint_to_server_url():
  275. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  276. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  277. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  278. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  279. 'resolve_credential'
  280. ).replace_with(lambda value, config: value)
  281. flexmock(module).should_receive('send_zabbix_request').with_args(
  282. f'{SERVER}',
  283. headers=AUTH_HEADERS,
  284. data=DATA_HOST_KEY,
  285. ).once()
  286. flexmock(module.logger).should_receive('warning').never()
  287. module.ping_monitor(
  288. hook_config,
  289. {},
  290. 'config.yaml',
  291. borgmatic.hooks.monitoring.monitor.State.FAIL,
  292. monitoring_log_level=1,
  293. dry_run=False,
  294. )
  295. def test_ping_monitor_config_host_and_missing_key_bails():
  296. hook_config = {'server': SERVER, 'host': HOST, 'api_key': API_KEY}
  297. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  298. 'resolve_credential'
  299. ).replace_with(lambda value, config: value)
  300. flexmock(module.logger).should_receive('warning').once()
  301. flexmock(module).should_receive('send_zabbix_request').never()
  302. module.ping_monitor(
  303. hook_config,
  304. {},
  305. 'config.yaml',
  306. borgmatic.hooks.monitoring.monitor.State.FAIL,
  307. monitoring_log_level=1,
  308. dry_run=False,
  309. )
  310. def test_ping_monitor_config_key_and_missing_host_bails():
  311. hook_config = {'server': SERVER, 'key': KEY, 'api_key': API_KEY}
  312. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  313. 'resolve_credential'
  314. ).replace_with(lambda value, config: value)
  315. flexmock(module.logger).should_receive('warning').once()
  316. flexmock(module).should_receive('send_zabbix_request').never()
  317. module.ping_monitor(
  318. hook_config,
  319. {},
  320. 'config.yaml',
  321. borgmatic.hooks.monitoring.monitor.State.FAIL,
  322. monitoring_log_level=1,
  323. dry_run=False,
  324. )
  325. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_successful():
  326. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  327. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  328. hook_config = {
  329. 'server': SERVER,
  330. 'host': HOST,
  331. 'key': KEY,
  332. 'username': USERNAME,
  333. 'password': PASSWORD,
  334. }
  335. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  336. 'resolve_credential'
  337. ).replace_with(lambda value, config: value)
  338. flexmock(module).should_receive('send_zabbix_request').with_args(
  339. f'{SERVER}',
  340. headers=AUTH_HEADERS_LOGIN,
  341. data=DATA_USER_LOGIN,
  342. ).and_return('fakekey').once()
  343. flexmock(module.logger).should_receive('warning').never()
  344. flexmock(module).should_receive('send_zabbix_request').with_args(
  345. f'{SERVER}',
  346. headers=AUTH_HEADERS,
  347. data=DATA_HOST_KEY_WITH_KEY_VALUE,
  348. ).once()
  349. flexmock(module).should_receive('send_zabbix_request').with_args(
  350. f'{SERVER}',
  351. headers=AUTH_HEADERS,
  352. data=DATA_USER_LOGOUT,
  353. ).once()
  354. module.ping_monitor(
  355. hook_config,
  356. {},
  357. 'config.yaml',
  358. borgmatic.hooks.monitoring.monitor.State.FAIL,
  359. monitoring_log_level=1,
  360. dry_run=False,
  361. )
  362. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_and_auth_post_error_bails():
  363. hook_config = {
  364. 'server': SERVER,
  365. 'host': HOST,
  366. 'key': KEY,
  367. 'username': USERNAME,
  368. 'password': PASSWORD,
  369. }
  370. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  371. 'resolve_credential'
  372. ).replace_with(lambda value, config: value)
  373. flexmock(module).should_receive('send_zabbix_request').with_args(
  374. f'{SERVER}',
  375. headers=AUTH_HEADERS_LOGIN,
  376. data=DATA_USER_LOGIN,
  377. ).and_return(None).once()
  378. flexmock(module).should_receive('send_zabbix_request').with_args(
  379. f'{SERVER}',
  380. headers=AUTH_HEADERS,
  381. data=DATA_HOST_KEY_WITH_KEY_VALUE,
  382. ).never()
  383. flexmock(module).should_receive('send_zabbix_request').with_args(
  384. f'{SERVER}',
  385. headers=AUTH_HEADERS,
  386. data=DATA_USER_LOGOUT,
  387. ).never()
  388. module.ping_monitor(
  389. hook_config,
  390. {},
  391. 'config.yaml',
  392. borgmatic.hooks.monitoring.monitor.State.FAIL,
  393. monitoring_log_level=1,
  394. dry_run=False,
  395. )
  396. def test_ping_monitor_config_host_and_key_with_username_and_missing_password_bails():
  397. hook_config = {
  398. 'server': SERVER,
  399. 'host': HOST,
  400. 'key': KEY,
  401. 'username': USERNAME,
  402. }
  403. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  404. 'resolve_credential'
  405. ).replace_with(lambda value, config: value)
  406. flexmock(module.logger).should_receive('warning').once()
  407. flexmock(module).should_receive('send_zabbix_request').never()
  408. module.ping_monitor(
  409. hook_config,
  410. {},
  411. 'config.yaml',
  412. borgmatic.hooks.monitoring.monitor.State.FAIL,
  413. monitoring_log_level=1,
  414. dry_run=False,
  415. )
  416. def test_ping_monitor_config_host_and_key_with_password_and_missing_username_bails():
  417. hook_config = {
  418. 'server': SERVER,
  419. 'host': HOST,
  420. 'key': KEY,
  421. 'password': PASSWORD,
  422. }
  423. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  424. 'resolve_credential'
  425. ).replace_with(lambda value, config: value)
  426. flexmock(module.logger).should_receive('warning').once()
  427. flexmock(module).should_receive('send_zabbix_request').never()
  428. module.ping_monitor(
  429. hook_config,
  430. {},
  431. 'config.yaml',
  432. borgmatic.hooks.monitoring.monitor.State.FAIL,
  433. monitoring_log_level=1,
  434. dry_run=False,
  435. )
  436. def test_ping_monitor_config_itemid_with_api_key_auth_data_successful():
  437. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  438. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  439. hook_config = {'server': SERVER, 'itemid': ITEMID, 'api_key': API_KEY}
  440. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  441. 'resolve_credential'
  442. ).replace_with(lambda value, config: value)
  443. flexmock(module).should_receive('send_zabbix_request').with_args(
  444. f'{SERVER}',
  445. headers=AUTH_HEADERS,
  446. data=DATA_ITEMID,
  447. ).once()
  448. flexmock(module.logger).should_receive('warning').never()
  449. module.ping_monitor(
  450. hook_config,
  451. {},
  452. 'config.yaml',
  453. borgmatic.hooks.monitoring.monitor.State.FAIL,
  454. monitoring_log_level=1,
  455. dry_run=False,
  456. )
  457. def test_ping_monitor_config_itemid_with_username_password_auth_data_successful():
  458. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  459. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  460. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  461. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  462. 'resolve_credential'
  463. ).replace_with(lambda value, config: value)
  464. flexmock(module).should_receive('send_zabbix_request').with_args(
  465. f'{SERVER}',
  466. headers=AUTH_HEADERS_LOGIN,
  467. data=DATA_USER_LOGIN,
  468. ).and_return('fakekey').once()
  469. flexmock(module.logger).should_receive('warning').never()
  470. flexmock(module).should_receive('send_zabbix_request').with_args(
  471. f'{SERVER}',
  472. headers=AUTH_HEADERS,
  473. data=DATA_HOST_KEY_WITH_ITEMID,
  474. ).once()
  475. flexmock(module).should_receive('send_zabbix_request').with_args(
  476. f'{SERVER}',
  477. headers=AUTH_HEADERS,
  478. data=DATA_USER_LOGOUT,
  479. ).once()
  480. module.ping_monitor(
  481. hook_config,
  482. {},
  483. 'config.yaml',
  484. borgmatic.hooks.monitoring.monitor.State.FAIL,
  485. monitoring_log_level=1,
  486. dry_run=False,
  487. )
  488. def test_ping_monitor_with_credential_error_bails():
  489. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  490. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  491. 'resolve_credential'
  492. ).and_raise(ValueError)
  493. flexmock(module).should_receive('send_zabbix_request').never()
  494. flexmock(module.logger).should_receive('warning').once()
  495. module.ping_monitor(
  496. hook_config,
  497. {},
  498. 'config.yaml',
  499. borgmatic.hooks.monitoring.monitor.State.FAIL,
  500. monitoring_log_level=1,
  501. dry_run=False,
  502. )