test_zabbix.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. 'User-Agent': 'borgmatic',
  51. }
  52. AUTH_HEADERS = {
  53. 'Content-Type': 'application/json-rpc',
  54. 'Authorization': f'Bearer {API_KEY}',
  55. 'User-Agent': 'borgmatic',
  56. }
  57. def test_send_zabbix_request_with_post_error_bails():
  58. server = flexmock()
  59. headers = flexmock()
  60. data = {'method': 'do.stuff'}
  61. response = flexmock(ok=False)
  62. response.should_receive('raise_for_status').and_raise(
  63. module.requests.exceptions.RequestException,
  64. )
  65. flexmock(module.requests).should_receive('post').with_args(
  66. server,
  67. headers=headers,
  68. json=data,
  69. timeout=int,
  70. ).and_return(response)
  71. assert module.send_zabbix_request(server, headers, data) is None
  72. def test_send_zabbix_request_with_invalid_json_response_bails():
  73. server = flexmock()
  74. headers = flexmock()
  75. data = {'method': 'do.stuff'}
  76. flexmock(module.requests.exceptions.JSONDecodeError).should_receive('__init__')
  77. response = flexmock(ok=True)
  78. response.should_receive('json').and_raise(module.requests.exceptions.JSONDecodeError)
  79. flexmock(module.requests).should_receive('post').with_args(
  80. server,
  81. headers=headers,
  82. json=data,
  83. timeout=int,
  84. ).and_return(response)
  85. assert module.send_zabbix_request(server, headers, data) is None
  86. def test_send_zabbix_request_with_success_returns_response_result():
  87. server = flexmock()
  88. headers = flexmock()
  89. data = {'method': 'do.stuff'}
  90. response = flexmock(ok=True)
  91. response.should_receive('json').and_return({'result': {'foo': 'bar'}})
  92. flexmock(module.requests).should_receive('post').with_args(
  93. server,
  94. headers=headers,
  95. json=data,
  96. timeout=int,
  97. ).and_return(response)
  98. assert module.send_zabbix_request(server, headers, data) == {'foo': 'bar'}
  99. def test_send_zabbix_request_with_success_passes_through_missing_result():
  100. server = flexmock()
  101. headers = flexmock()
  102. data = {'method': 'do.stuff'}
  103. response = flexmock(ok=True)
  104. response.should_receive('json').and_return({})
  105. flexmock(module.requests).should_receive('post').with_args(
  106. server,
  107. headers=headers,
  108. json=data,
  109. timeout=int,
  110. ).and_return(response)
  111. assert module.send_zabbix_request(server, headers, data) is None
  112. def test_send_zabbix_request_with_error_bails():
  113. server = flexmock()
  114. headers = flexmock()
  115. data = {'method': 'do.stuff'}
  116. response = flexmock(ok=True)
  117. response.should_receive('json').and_return({'result': {'data': [{'error': 'oops'}]}})
  118. flexmock(module.requests).should_receive('post').with_args(
  119. server,
  120. headers=headers,
  121. json=data,
  122. timeout=int,
  123. ).and_return(response)
  124. assert module.send_zabbix_request(server, headers, data) is None
  125. def test_ping_monitor_with_non_matching_state_bails():
  126. hook_config = {'api_key': API_KEY}
  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.START,
  133. monitoring_log_level=1,
  134. dry_run=False,
  135. )
  136. def test_ping_monitor_config_with_api_key_only_bails():
  137. # This test should exit early since only providing an API KEY is not enough
  138. # for the hook to work
  139. hook_config = {'api_key': API_KEY}
  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_host_only_bails():
  154. # This test should exit early since only providing a HOST is not enough
  155. # for the hook to work
  156. hook_config = {'host': HOST}
  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_key_only_bails():
  171. # This test should exit early since only providing a KEY is not enough
  172. # for the hook to work
  173. hook_config = {'key': KEY}
  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_with_server_only_bails():
  188. # This test should exit early since only providing a SERVER is not enough
  189. # for the hook to work
  190. hook_config = {'server': SERVER}
  191. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  192. 'resolve_credential',
  193. ).replace_with(lambda value, config: value)
  194. flexmock(module.logger).should_receive('warning').once()
  195. flexmock(module).should_receive('send_zabbix_request').never()
  196. module.ping_monitor(
  197. hook_config,
  198. {},
  199. 'config.yaml',
  200. borgmatic.hooks.monitoring.monitor.State.FAIL,
  201. monitoring_log_level=1,
  202. dry_run=False,
  203. )
  204. def test_ping_monitor_config_user_password_no_zabbix_data_bails():
  205. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  206. hook_config = {'server': SERVER, 'username': USERNAME, 'password': PASSWORD}
  207. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  208. 'resolve_credential',
  209. ).replace_with(lambda value, config: value)
  210. flexmock(module.logger).should_receive('warning').once()
  211. flexmock(module).should_receive('send_zabbix_request').never()
  212. module.ping_monitor(
  213. hook_config,
  214. {},
  215. 'config.yaml',
  216. borgmatic.hooks.monitoring.monitor.State.FAIL,
  217. monitoring_log_level=1,
  218. dry_run=False,
  219. )
  220. def test_ping_monitor_config_api_key_no_zabbix_data_bails():
  221. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  222. hook_config = {'server': SERVER, 'api_key': API_KEY}
  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_itemid_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, 'itemid': ITEMID}
  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_no_auth_data_bails():
  254. # This test should exit early since there is no authentication provided
  255. # and Zabbix requires authentication to use it's API
  256. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY}
  257. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  258. 'resolve_credential',
  259. ).replace_with(lambda value, config: value)
  260. flexmock(module.logger).should_receive('warning').once()
  261. flexmock(module).should_receive('send_zabbix_request').never()
  262. module.ping_monitor(
  263. hook_config,
  264. {},
  265. 'config.yaml',
  266. borgmatic.hooks.monitoring.monitor.State.FAIL,
  267. monitoring_log_level=1,
  268. dry_run=False,
  269. )
  270. def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful():
  271. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  272. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  273. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  274. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  275. 'resolve_credential',
  276. ).replace_with(lambda value, config: value)
  277. flexmock(module).should_receive('send_zabbix_request').with_args(
  278. f'{SERVER}',
  279. headers=AUTH_HEADERS,
  280. data=DATA_HOST_KEY,
  281. ).once()
  282. flexmock(module.logger).should_receive('warning').never()
  283. module.ping_monitor(
  284. hook_config,
  285. {},
  286. 'config.yaml',
  287. borgmatic.hooks.monitoring.monitor.State.FAIL,
  288. monitoring_log_level=1,
  289. dry_run=False,
  290. )
  291. def test_ping_monitor_config_adds_missing_api_endpoint_to_server_url():
  292. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  293. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  294. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  295. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  296. 'resolve_credential',
  297. ).replace_with(lambda value, config: value)
  298. flexmock(module).should_receive('send_zabbix_request').with_args(
  299. f'{SERVER}',
  300. headers=AUTH_HEADERS,
  301. data=DATA_HOST_KEY,
  302. ).once()
  303. flexmock(module.logger).should_receive('warning').never()
  304. module.ping_monitor(
  305. hook_config,
  306. {},
  307. 'config.yaml',
  308. borgmatic.hooks.monitoring.monitor.State.FAIL,
  309. monitoring_log_level=1,
  310. dry_run=False,
  311. )
  312. def test_ping_monitor_config_host_and_missing_key_bails():
  313. hook_config = {'server': SERVER, 'host': HOST, 'api_key': API_KEY}
  314. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  315. 'resolve_credential',
  316. ).replace_with(lambda value, config: value)
  317. flexmock(module.logger).should_receive('warning').once()
  318. flexmock(module).should_receive('send_zabbix_request').never()
  319. module.ping_monitor(
  320. hook_config,
  321. {},
  322. 'config.yaml',
  323. borgmatic.hooks.monitoring.monitor.State.FAIL,
  324. monitoring_log_level=1,
  325. dry_run=False,
  326. )
  327. def test_ping_monitor_config_key_and_missing_host_bails():
  328. hook_config = {'server': SERVER, 'key': KEY, 'api_key': API_KEY}
  329. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  330. 'resolve_credential',
  331. ).replace_with(lambda value, config: value)
  332. flexmock(module.logger).should_receive('warning').once()
  333. flexmock(module).should_receive('send_zabbix_request').never()
  334. module.ping_monitor(
  335. hook_config,
  336. {},
  337. 'config.yaml',
  338. borgmatic.hooks.monitoring.monitor.State.FAIL,
  339. monitoring_log_level=1,
  340. dry_run=False,
  341. )
  342. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_successful():
  343. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  344. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  345. hook_config = {
  346. 'server': SERVER,
  347. 'host': HOST,
  348. 'key': KEY,
  349. 'username': USERNAME,
  350. 'password': PASSWORD,
  351. }
  352. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  353. 'resolve_credential',
  354. ).replace_with(lambda value, config: value)
  355. flexmock(module).should_receive('send_zabbix_request').with_args(
  356. f'{SERVER}',
  357. headers=AUTH_HEADERS_LOGIN,
  358. data=DATA_USER_LOGIN,
  359. ).and_return('fakekey').once()
  360. flexmock(module.logger).should_receive('warning').never()
  361. flexmock(module).should_receive('send_zabbix_request').with_args(
  362. f'{SERVER}',
  363. headers=AUTH_HEADERS,
  364. data=DATA_HOST_KEY_WITH_KEY_VALUE,
  365. ).once()
  366. flexmock(module).should_receive('send_zabbix_request').with_args(
  367. f'{SERVER}',
  368. headers=AUTH_HEADERS,
  369. data=DATA_USER_LOGOUT,
  370. ).once()
  371. module.ping_monitor(
  372. hook_config,
  373. {},
  374. 'config.yaml',
  375. borgmatic.hooks.monitoring.monitor.State.FAIL,
  376. monitoring_log_level=1,
  377. dry_run=False,
  378. )
  379. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_and_auth_post_error_bails():
  380. hook_config = {
  381. 'server': SERVER,
  382. 'host': HOST,
  383. 'key': KEY,
  384. 'username': USERNAME,
  385. 'password': PASSWORD,
  386. }
  387. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  388. 'resolve_credential',
  389. ).replace_with(lambda value, config: value)
  390. flexmock(module).should_receive('send_zabbix_request').with_args(
  391. f'{SERVER}',
  392. headers=AUTH_HEADERS_LOGIN,
  393. data=DATA_USER_LOGIN,
  394. ).and_return(None).once()
  395. flexmock(module).should_receive('send_zabbix_request').with_args(
  396. f'{SERVER}',
  397. headers=AUTH_HEADERS,
  398. data=DATA_HOST_KEY_WITH_KEY_VALUE,
  399. ).never()
  400. flexmock(module).should_receive('send_zabbix_request').with_args(
  401. f'{SERVER}',
  402. headers=AUTH_HEADERS,
  403. data=DATA_USER_LOGOUT,
  404. ).never()
  405. module.ping_monitor(
  406. hook_config,
  407. {},
  408. 'config.yaml',
  409. borgmatic.hooks.monitoring.monitor.State.FAIL,
  410. monitoring_log_level=1,
  411. dry_run=False,
  412. )
  413. def test_ping_monitor_config_host_and_key_with_username_and_missing_password_bails():
  414. hook_config = {
  415. 'server': SERVER,
  416. 'host': HOST,
  417. 'key': KEY,
  418. 'username': USERNAME,
  419. }
  420. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  421. 'resolve_credential',
  422. ).replace_with(lambda value, config: value)
  423. flexmock(module.logger).should_receive('warning').once()
  424. flexmock(module).should_receive('send_zabbix_request').never()
  425. module.ping_monitor(
  426. hook_config,
  427. {},
  428. 'config.yaml',
  429. borgmatic.hooks.monitoring.monitor.State.FAIL,
  430. monitoring_log_level=1,
  431. dry_run=False,
  432. )
  433. def test_ping_monitor_config_host_and_key_with_password_and_missing_username_bails():
  434. hook_config = {
  435. 'server': SERVER,
  436. 'host': HOST,
  437. 'key': KEY,
  438. 'password': PASSWORD,
  439. }
  440. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  441. 'resolve_credential',
  442. ).replace_with(lambda value, config: value)
  443. flexmock(module.logger).should_receive('warning').once()
  444. flexmock(module).should_receive('send_zabbix_request').never()
  445. module.ping_monitor(
  446. hook_config,
  447. {},
  448. 'config.yaml',
  449. borgmatic.hooks.monitoring.monitor.State.FAIL,
  450. monitoring_log_level=1,
  451. dry_run=False,
  452. )
  453. def test_ping_monitor_config_itemid_with_api_key_auth_data_successful():
  454. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  455. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  456. hook_config = {'server': SERVER, 'itemid': ITEMID, 'api_key': API_KEY}
  457. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  458. 'resolve_credential',
  459. ).replace_with(lambda value, config: value)
  460. flexmock(module).should_receive('send_zabbix_request').with_args(
  461. f'{SERVER}',
  462. headers=AUTH_HEADERS,
  463. data=DATA_ITEMID,
  464. ).once()
  465. flexmock(module.logger).should_receive('warning').never()
  466. module.ping_monitor(
  467. hook_config,
  468. {},
  469. 'config.yaml',
  470. borgmatic.hooks.monitoring.monitor.State.FAIL,
  471. monitoring_log_level=1,
  472. dry_run=False,
  473. )
  474. def test_ping_monitor_config_itemid_with_username_password_auth_data_successful():
  475. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  476. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  477. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  478. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  479. 'resolve_credential',
  480. ).replace_with(lambda value, config: value)
  481. flexmock(module).should_receive('send_zabbix_request').with_args(
  482. f'{SERVER}',
  483. headers=AUTH_HEADERS_LOGIN,
  484. data=DATA_USER_LOGIN,
  485. ).and_return('fakekey').once()
  486. flexmock(module.logger).should_receive('warning').never()
  487. flexmock(module).should_receive('send_zabbix_request').with_args(
  488. f'{SERVER}',
  489. headers=AUTH_HEADERS,
  490. data=DATA_HOST_KEY_WITH_ITEMID,
  491. ).once()
  492. flexmock(module).should_receive('send_zabbix_request').with_args(
  493. f'{SERVER}',
  494. headers=AUTH_HEADERS,
  495. data=DATA_USER_LOGOUT,
  496. ).once()
  497. module.ping_monitor(
  498. hook_config,
  499. {},
  500. 'config.yaml',
  501. borgmatic.hooks.monitoring.monitor.State.FAIL,
  502. monitoring_log_level=1,
  503. dry_run=False,
  504. )
  505. def test_ping_monitor_with_credential_error_bails():
  506. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  507. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  508. 'resolve_credential',
  509. ).and_raise(ValueError)
  510. flexmock(module).should_receive('send_zabbix_request').never()
  511. flexmock(module.logger).should_receive('warning').once()
  512. module.ping_monitor(
  513. hook_config,
  514. {},
  515. 'config.yaml',
  516. borgmatic.hooks.monitoring.monitor.State.FAIL,
  517. monitoring_log_level=1,
  518. dry_run=False,
  519. )