test_zabbix.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. '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_bails():
  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.monitoring.monitor.State.START,
  57. monitoring_log_level=1,
  58. dry_run=False,
  59. )
  60. def test_ping_monitor_config_with_api_key_only_bails():
  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.borgmatic.hooks.credential.parse).should_receive(
  65. 'resolve_credential'
  66. ).replace_with(lambda value, config: value)
  67. flexmock(module.logger).should_receive('warning').once()
  68. flexmock(module.requests).should_receive('post').never()
  69. module.ping_monitor(
  70. hook_config,
  71. {},
  72. 'config.yaml',
  73. borgmatic.hooks.monitoring.monitor.State.FAIL,
  74. monitoring_log_level=1,
  75. dry_run=False,
  76. )
  77. def test_ping_monitor_config_with_host_only_bails():
  78. # This test should exit early since only providing a HOST is not enough
  79. # for the hook to work
  80. hook_config = {'host': HOST}
  81. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  82. 'resolve_credential'
  83. ).replace_with(lambda value, config: value)
  84. flexmock(module.logger).should_receive('warning').once()
  85. flexmock(module.requests).should_receive('post').never()
  86. module.ping_monitor(
  87. hook_config,
  88. {},
  89. 'config.yaml',
  90. borgmatic.hooks.monitoring.monitor.State.FAIL,
  91. monitoring_log_level=1,
  92. dry_run=False,
  93. )
  94. def test_ping_monitor_config_with_key_only_bails():
  95. # This test should exit early since only providing a KEY is not enough
  96. # for the hook to work
  97. hook_config = {'key': KEY}
  98. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  99. 'resolve_credential'
  100. ).replace_with(lambda value, config: value)
  101. flexmock(module.logger).should_receive('warning').once()
  102. flexmock(module.requests).should_receive('post').never()
  103. module.ping_monitor(
  104. hook_config,
  105. {},
  106. 'config.yaml',
  107. borgmatic.hooks.monitoring.monitor.State.FAIL,
  108. monitoring_log_level=1,
  109. dry_run=False,
  110. )
  111. def test_ping_monitor_config_with_server_only_bails():
  112. # This test should exit early since only providing a SERVER is not enough
  113. # for the hook to work
  114. hook_config = {'server': SERVER}
  115. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  116. 'resolve_credential'
  117. ).replace_with(lambda value, config: value)
  118. flexmock(module.logger).should_receive('warning').once()
  119. flexmock(module.requests).should_receive('post').never()
  120. module.ping_monitor(
  121. hook_config,
  122. {},
  123. 'config.yaml',
  124. borgmatic.hooks.monitoring.monitor.State.FAIL,
  125. monitoring_log_level=1,
  126. dry_run=False,
  127. )
  128. def test_ping_monitor_config_user_password_no_zabbix_data_bails():
  129. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  130. hook_config = {'server': SERVER, 'username': USERNAME, 'password': PASSWORD}
  131. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  132. 'resolve_credential'
  133. ).replace_with(lambda value, config: value)
  134. flexmock(module.logger).should_receive('warning').once()
  135. flexmock(module.requests).should_receive('post').never()
  136. module.ping_monitor(
  137. hook_config,
  138. {},
  139. 'config.yaml',
  140. borgmatic.hooks.monitoring.monitor.State.FAIL,
  141. monitoring_log_level=1,
  142. dry_run=False,
  143. )
  144. def test_ping_monitor_config_api_key_no_zabbix_data_bails():
  145. # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to
  146. hook_config = {'server': SERVER, 'api_key': API_KEY}
  147. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  148. 'resolve_credential'
  149. ).replace_with(lambda value, config: value)
  150. flexmock(module.logger).should_receive('warning').once()
  151. flexmock(module.requests).should_receive('post').never()
  152. module.ping_monitor(
  153. hook_config,
  154. {},
  155. 'config.yaml',
  156. borgmatic.hooks.monitoring.monitor.State.FAIL,
  157. monitoring_log_level=1,
  158. dry_run=False,
  159. )
  160. def test_ping_monitor_config_itemid_no_auth_data_bails():
  161. # This test should exit early since there is no authentication provided
  162. # and Zabbix requires authentication to use it's API
  163. hook_config = {'server': SERVER, 'itemid': ITEMID}
  164. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  165. 'resolve_credential'
  166. ).replace_with(lambda value, config: value)
  167. flexmock(module.logger).should_receive('warning').once()
  168. flexmock(module.requests).should_receive('post').never()
  169. module.ping_monitor(
  170. hook_config,
  171. {},
  172. 'config.yaml',
  173. borgmatic.hooks.monitoring.monitor.State.FAIL,
  174. monitoring_log_level=1,
  175. dry_run=False,
  176. )
  177. def test_ping_monitor_config_host_and_key_no_auth_data_bails():
  178. # This test should exit early since there is no authentication provided
  179. # and Zabbix requires authentication to use it's API
  180. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY}
  181. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  182. 'resolve_credential'
  183. ).replace_with(lambda value, config: value)
  184. flexmock(module.logger).should_receive('warning').once()
  185. flexmock(module.requests).should_receive('post').never()
  186. module.ping_monitor(
  187. hook_config,
  188. {},
  189. 'config.yaml',
  190. borgmatic.hooks.monitoring.monitor.State.FAIL,
  191. monitoring_log_level=1,
  192. dry_run=False,
  193. )
  194. def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful():
  195. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  196. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  197. hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
  198. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  199. 'resolve_credential'
  200. ).replace_with(lambda value, config: value)
  201. flexmock(module.requests).should_receive('post').with_args(
  202. f'{SERVER}',
  203. headers=AUTH_HEADERS_API_KEY,
  204. json=DATA_HOST_KEY,
  205. ).and_return(flexmock(ok=True)).once()
  206. flexmock(module.logger).should_receive('warning').never()
  207. module.ping_monitor(
  208. hook_config,
  209. {},
  210. 'config.yaml',
  211. borgmatic.hooks.monitoring.monitor.State.FAIL,
  212. monitoring_log_level=1,
  213. dry_run=False,
  214. )
  215. def test_ping_monitor_config_host_and_missing_key_bails():
  216. hook_config = {'server': SERVER, 'host': HOST, 'api_key': API_KEY}
  217. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  218. 'resolve_credential'
  219. ).replace_with(lambda value, config: value)
  220. flexmock(module.logger).should_receive('warning').once()
  221. flexmock(module.requests).should_receive('post').never()
  222. module.ping_monitor(
  223. hook_config,
  224. {},
  225. 'config.yaml',
  226. borgmatic.hooks.monitoring.monitor.State.FAIL,
  227. monitoring_log_level=1,
  228. dry_run=False,
  229. )
  230. def test_ping_monitor_config_key_and_missing_host_bails():
  231. hook_config = {'server': SERVER, 'key': KEY, 'api_key': API_KEY}
  232. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  233. 'resolve_credential'
  234. ).replace_with(lambda value, config: value)
  235. flexmock(module.logger).should_receive('warning').once()
  236. flexmock(module.requests).should_receive('post').never()
  237. module.ping_monitor(
  238. hook_config,
  239. {},
  240. 'config.yaml',
  241. borgmatic.hooks.monitoring.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_successful():
  246. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  247. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  248. hook_config = {
  249. 'server': SERVER,
  250. 'host': HOST,
  251. 'key': KEY,
  252. 'username': USERNAME,
  253. 'password': PASSWORD,
  254. }
  255. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  256. 'resolve_credential'
  257. ).replace_with(lambda value, config: value)
  258. auth_response = flexmock(ok=True)
  259. auth_response.should_receive('json').and_return(
  260. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  261. )
  262. flexmock(module.requests).should_receive('post').with_args(
  263. f'{SERVER}',
  264. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  265. json=DATA_USER_LOGIN,
  266. ).and_return(auth_response).once()
  267. flexmock(module.logger).should_receive('warning').never()
  268. flexmock(module.requests).should_receive('post').with_args(
  269. f'{SERVER}',
  270. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  271. json=DATA_HOST_KEY_WITH_KEY_VALUE,
  272. ).and_return(flexmock(ok=True)).once()
  273. module.ping_monitor(
  274. hook_config,
  275. {},
  276. 'config.yaml',
  277. borgmatic.hooks.monitoring.monitor.State.FAIL,
  278. monitoring_log_level=1,
  279. dry_run=False,
  280. )
  281. def test_ping_monitor_config_host_and_key_with_username_password_auth_data_and_auth_post_error_bails():
  282. hook_config = {
  283. 'server': SERVER,
  284. 'host': HOST,
  285. 'key': KEY,
  286. 'username': USERNAME,
  287. 'password': PASSWORD,
  288. }
  289. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  290. 'resolve_credential'
  291. ).replace_with(lambda value, config: value)
  292. auth_response = flexmock(ok=False)
  293. auth_response.should_receive('json').and_return(
  294. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  295. )
  296. auth_response.should_receive('raise_for_status').and_raise(
  297. module.requests.ConnectionError
  298. ).once()
  299. flexmock(module.requests).should_receive('post').with_args(
  300. f'{SERVER}',
  301. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  302. json=DATA_USER_LOGIN,
  303. ).and_return(auth_response).once()
  304. flexmock(module.logger).should_receive('warning').once()
  305. flexmock(module.requests).should_receive('post').with_args(
  306. f'{SERVER}',
  307. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  308. json=DATA_HOST_KEY_WITH_KEY_VALUE,
  309. ).never()
  310. module.ping_monitor(
  311. hook_config,
  312. {},
  313. 'config.yaml',
  314. borgmatic.hooks.monitoring.monitor.State.FAIL,
  315. monitoring_log_level=1,
  316. dry_run=False,
  317. )
  318. def test_ping_monitor_config_host_and_key_with_username_and_missing_password_bails():
  319. hook_config = {
  320. 'server': SERVER,
  321. 'host': HOST,
  322. 'key': KEY,
  323. 'username': USERNAME,
  324. }
  325. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  326. 'resolve_credential'
  327. ).replace_with(lambda value, config: value)
  328. flexmock(module.logger).should_receive('warning').once()
  329. flexmock(module.requests).should_receive('post').never()
  330. module.ping_monitor(
  331. hook_config,
  332. {},
  333. 'config.yaml',
  334. borgmatic.hooks.monitoring.monitor.State.FAIL,
  335. monitoring_log_level=1,
  336. dry_run=False,
  337. )
  338. def test_ping_monitor_config_host_and_key_with_password_and_missing_username_bails():
  339. hook_config = {
  340. 'server': SERVER,
  341. 'host': HOST,
  342. 'key': KEY,
  343. 'password': PASSWORD,
  344. }
  345. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  346. 'resolve_credential'
  347. ).replace_with(lambda value, config: value)
  348. flexmock(module.logger).should_receive('warning').once()
  349. flexmock(module.requests).should_receive('post').never()
  350. module.ping_monitor(
  351. hook_config,
  352. {},
  353. 'config.yaml',
  354. borgmatic.hooks.monitoring.monitor.State.FAIL,
  355. monitoring_log_level=1,
  356. dry_run=False,
  357. )
  358. def test_ping_monitor_config_itemid_with_api_key_auth_data_successful():
  359. # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
  360. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  361. hook_config = {'server': SERVER, 'itemid': ITEMID, 'api_key': API_KEY}
  362. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  363. 'resolve_credential'
  364. ).replace_with(lambda value, config: value)
  365. flexmock(module.requests).should_receive('post').with_args(
  366. f'{SERVER}',
  367. headers=AUTH_HEADERS_API_KEY,
  368. json=DATA_ITEMID,
  369. ).and_return(flexmock(ok=True)).once()
  370. flexmock(module.logger).should_receive('warning').never()
  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_itemid_with_username_password_auth_data_successful():
  380. # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD
  381. # to authenticate and HOST/KEY to know which item to populate in Zabbix.
  382. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  383. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  384. 'resolve_credential'
  385. ).replace_with(lambda value, config: value)
  386. auth_response = flexmock(ok=True)
  387. auth_response.should_receive('json').and_return(
  388. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  389. )
  390. flexmock(module.requests).should_receive('post').with_args(
  391. f'{SERVER}',
  392. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  393. json=DATA_USER_LOGIN,
  394. ).and_return(auth_response).once()
  395. flexmock(module.logger).should_receive('warning').never()
  396. flexmock(module.requests).should_receive('post').with_args(
  397. f'{SERVER}',
  398. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  399. json=DATA_HOST_KEY_WITH_ITEMID,
  400. ).and_return(flexmock(ok=True)).once()
  401. module.ping_monitor(
  402. hook_config,
  403. {},
  404. 'config.yaml',
  405. borgmatic.hooks.monitoring.monitor.State.FAIL,
  406. monitoring_log_level=1,
  407. dry_run=False,
  408. )
  409. def test_ping_monitor_config_itemid_with_username_password_auth_data_and_push_post_error_bails():
  410. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  411. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  412. 'resolve_credential'
  413. ).replace_with(lambda value, config: value)
  414. auth_response = flexmock(ok=True)
  415. auth_response.should_receive('json').and_return(
  416. {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
  417. )
  418. flexmock(module.requests).should_receive('post').with_args(
  419. f'{SERVER}',
  420. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  421. json=DATA_USER_LOGIN,
  422. ).and_return(auth_response).once()
  423. push_response = flexmock(ok=False)
  424. push_response.should_receive('raise_for_status').and_raise(
  425. module.requests.ConnectionError
  426. ).once()
  427. flexmock(module.requests).should_receive('post').with_args(
  428. f'{SERVER}',
  429. headers=AUTH_HEADERS_USERNAME_PASSWORD,
  430. json=DATA_HOST_KEY_WITH_ITEMID,
  431. ).and_return(push_response).once()
  432. flexmock(module.logger).should_receive('warning').once()
  433. module.ping_monitor(
  434. hook_config,
  435. {},
  436. 'config.yaml',
  437. borgmatic.hooks.monitoring.monitor.State.FAIL,
  438. monitoring_log_level=1,
  439. dry_run=False,
  440. )
  441. def test_ping_monitor_with_credential_error_bails():
  442. hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
  443. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  444. 'resolve_credential'
  445. ).and_raise(ValueError)
  446. flexmock(module.requests).should_receive('post').never()
  447. flexmock(module.logger).should_receive('warning').once()
  448. module.ping_monitor(
  449. hook_config,
  450. {},
  451. 'config.yaml',
  452. borgmatic.hooks.monitoring.monitor.State.FAIL,
  453. monitoring_log_level=1,
  454. dry_run=False,
  455. )