Python Selenium automatically configures zabbix mailbox alarm
Introduction:
In the last blog, I wrote about configuring mailbox alarms in zabbix, but because the configuration process is very tedious, here I wrote a script for automatic configuration (python selenium module). Running scripts directly will improve a lot of efficiency!
Experimental environment:
1.python3.7
2.selenium module
3.PyCharm development software
Step 1 of the experiment, zabbix server and agent side installation and configuration
Warm Tip: refer to blog https://blog.51cto.com/13760351/2431576 for specific steps.
2. Zabbix service web configures mailbox alarm
Write selenium automation scripts
From selenium import webdriver
Import time
Driver = webdriver.Chrome ()
# Windows window maximization
Driver.maximize_window ()
# login account password
Driver.get ("http://192.168.0.112/zabbix/")"
Driver.find_element_by_xpath ("/ / input [@ type='text']") .send_keys ("Admin")
Driver.find_element_by_id ("password") .send_keys ("zabbix")
Driver.find_element_by_id (enter) .click ()
# create a media type
Driver.get ("http://192.168.0.112/zabbix/zabbix.php?action=mediatype.edit")"
Select script
Driver.find_element_by_id (type) .click ()
Driver.find_element_by_xpath ('/ / * [@ id= "type"] / option [2]') .click ()
Name
Driver.find_element_by_id ("description") .send_keys ("mail-test")
Script name
Driver.find_element_by_id ("exec_path") .send_keys ("mailx.sh")
# add script parameters
Driver.find_element_by_id (exec_param_add) .click ()
Driver.find_element_by_id ("exec_params_0_exec_param") .send_keys ("{ALERT.SENDTO}")
Driver.find_element_by_id (exec_param_add) .click ()
Driver.find_element_by_id ("exec_params_1_exec_param") .send_keys ("{ALERT.SUBJECT}")
Driver.find_element_by_id (exec_param_add) .click ()
Driver.find_element_by_id ("exec_params_2_exec_param") .send_keys ("{ALERT.MESSAGE}")
# add completed
Driver.find_element_by_id (add) .click ()
# set alarm media
Driver.get ("http://192.168.0.112/zabbix/users.php?form=update&userid=1")"
# set up Chinese
Driver.find_element_by_id (lang) .click ()
Driver.find_element_by_xpath ('/ / [@ id= "lang"] / option [3]') .click ()
# alarm media
Driver.find_element_by_id (tab_mediaTab) .click ()
# add
Driver.find_element_by_xpath ('/ / [@ id= "userMediaFormList"] / li/div [2] / div/button') .click ()
Time.sleep (1)
# Select a type
Driver.find_element_by_xpath ('/ / [@ id= "mediatypeid"] / option [3]') .click ()
# pickup mailbox
Driver.find_element_by_id ("sendto") .send_keys ("1451772664@qq.com")
# add
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [3] / button [1]') .click ()
Time.sleep (1)
# Update
Driver.find_element_by_id (update) .click ()
# configuration Action-Action
Driver.get ('http://192.168.0.112/zabbix/actionconf.php?ddreset=1')
# Delete default template
Driver.find_element_by_id (g_actionid_3) .click ()
Driver.find_element_by_xpath ('/ / [@ id= "action_buttons"] / button [3]') .click ()
Time.sleep (1)
# confirmation dialog box
Dig_confirm = driver.switch_to.alert
Dig_confirm.accept ()
Time.sleep (1)
# Action module
Driver.get ("http://192.168.0.112/zabbix/actionconf.php?eventsource=0&form=%E5%88%9B%E5%BB%BA%E5%8A%A8%E4%BD%9C")"
# name
Driver.find_element_by_id ("name") .send_keys ("mailx")
# New trigger condition
Driver.find_element_by_id ('new_condition_conditiontype') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "new_condition_conditiontype"] / option [3]') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "actionTab"] / ul/li [4] / div [2] / div/table/tbody/tr [1] / td/div [3] / div [2] / button') .click ()
Time.sleep (1)
Driver.find_element_by_id ('item_2') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [3] / button [1]') .click ()
Driver.find_element_by_xpath ('/ / * [@ id= "actionTab"] / ul/li [4] / div [2] / div/table/tbody/tr [2] / td/button') .click ()
Knowledge explanation: confirmation dialog box part, there is no direct element can be located, can only use a fixed "dig_confirm" method
# Operation
Driver.find_element_by_id ('tab_operationTab') .click ()
# default time
Driver.find_element_by_id ('esc_period') .clear ()
Driver.find_element_by_id ('esc_period'). Send_keys ("60")
# default title
Driver.find_element_by_id ('def_shortdata') .clear ()
Driver.find_element_by_id ('def_shortdata') .send_keys ("{TRIGGER.STATUS}: {TRIGGER.NAME}")
# Operation content
Driver.find_element_by_id ('def_longdata') .clear ()
Driver.find_element_by_id ('def_longdata'). Send_keys (
'alarm host: {HOST.NAME}\ n'
'Alert IP: {HOST.IP}\ n'
'alarm time: {EVENT.DATE}-{EVENT.TIME}\ n'
'alarm level: {TRIGGER.SEVERITY}\ n'
'Alert information: {TRIGGER.NAME}: {ITEM.VALUE}\ n'
'event ID: {EVENT.ID}'
)
Time.sleep (1)
# add a new
Driver.find_element_by_xpath ('/ / [@ id= "operationTab"] / ul/li [5] / div [2] / div/button') .click ()
# steps
Driver.find_element_by_id ('new_operation_esc_step_to') .send_keys ("3")
# time
Driver.find_element_by_id ('new_operation_esc_period'). Send_keys ("60")
# send users
Driver.find_element_by_xpath ('/ / [@ id= "opmsgUserListFooter"] / td/button') .click ()
Time.sleep (1)
Driver.find_element_by_id ('item_1') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [3] / button [1]') .click ()
Driver.find_element_by_id ('new_operation_opmessage_mediatypeid') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "new_operation_opmessage_mediatypeid"] / option [4]') .click ()
# add
Driver.find_element_by_xpath ('/ / * [@ id= "operationTab"] / ul/li [6] / div [2] / div/ul [2] / li [1] / button') .click ()
Knowledge explanation: sendkey needs to add "\ n" to send newline text, and it is in quotation marks, otherwise it is useless.
# restore operation
Driver.find_element_by_id ('tab_recoveryOperationTab') .click ()
# restore the title
Driver.find_element_by_id ('ringing recent data') .clear ()
Driver.find_element_by_id ('ringing TRIGGER.NAME data') .send_keys ("{TRIGGER.STATUS}: {TRIGGER.NAME}")
# restore the content of information
Driver.find_element_by_id ('ringing long data'). Clear ()
Driver.find_element_by_id ('ringing long data'). Send_keys (
"restore host: {HOST.NAME}\ n"
"restore IP: {HOST.IP}\ n"
"recovery time: {EVENT.DATE}-{EVENT.TIME}\ n"
"restore level: {TRIGGER.SEVERITY}\ n"
"recovery information: {TRIGGER.NAME}: {ITEM.VALUE}\ n"
"restore ID: {EVENT.ID}"
)
# add "new"
Driver.find_element_by_xpath ('/ / [@ id= "recoveryOperationTab"] / ul/li [3] / div [2] / div/button') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "recOpmsgUserListFooter"] / td/button') .click ()
Time.sleep (1)
Driver.find_element_by_id ('item_1') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [3] / button [1]') .click ()
Driver.find_element_by_id ('new_recovery_operation_opmessage_mediatypeid') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "new_recovery_operation_opmessage_mediatypeid"] / option [4]') .click ()
Driver.find_element_by_xpath ('/ / * [@ id= "recoveryOperationTab"] / ul/li [4] / div [2] / div/ul [2] / li [1] / button') .click ()
# add all actions
Driver.find_element_by_id ('add') .click ()
# add a monitoring host
Driver.get ('http://192.168.0.112/zabbix/hosts.php?form=create')
# Host name
Driver.find_element_by_id ("host") .send_keys ("test")
# Group
Driver.find_element_by_xpath ('/ / [@ id= "hostlist"] / li [3] / div [2] / div/div [2] / button') .click ()
Time.sleep (2)
Driver.find_element_by_id (item_2) .click ()
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [3] / button [1]') .click ()
# add ip address
Driver.find_element_by_name ('interfaces [1] [ip]') .clear ()
Driver.find_element_by_name ('interfaces [1] [ip]'). Send_keys ('192.168.0.200')
# template
Driver.find_element_by_id ('tab_templateTab') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "templateTab"] / ul/li [2] / div [2] / div/table/tbody/tr [1] / td/div/div [2] / button') .click ()
Time.sleep (2)
Driver.find_element_by_id ('item_10095') .click ()
Driver.find_element_by_id ('item_10050') .click ()
Driver.find_element_by_xpath ('/ / [@ id= "overlay_dialogue"] / div [4] / button [1]') .click ()
Driver.find_element_by_xpath ('/ / * [@ id= "templateTab"] / ul/li [2] / div [2] / div/table/tbody/tr [2] / td/button') .click ()
# Click to add
Driver.find_element_by_id ('add') .click ()
# go back to the dashboard
Driver.get ('http://192.168.0.112/zabbix/zabbix.php?action=dashboard.view&ddreset=1')
Third, web page verification
Summary
1. The script written here is only a template. It is not recommended to take it and use it directly.
two。 There are many details to pay special attention to, such as pop-up dialog box processing, sending text wrapping, time pause and so on.