脚本环境
1.linux centos 7 系统
2.光盘镜像文件已挂载
3.系统可连接外网
4.字体链接:https://pan.baidu.com/s/1m9WJys5aGuUi-WgBG-zrxw
提取码:qq6r
实验步骤
1.上传字体文件和zabbix脚本到Linux系统/root目录下
方法一:挂载 方法二:通过Xftp软件上传
2.脚本zabbix.sh详解
#!/bin/bash
#this is auto install lamp + zabbix shell!
#
#获取ip地址
ip=`ifconfig ens33 | grep "netmask" | awk ‘{print $2}‘`
#获取主机名
name=`hostname`
#关闭防火墙、安全性
systemctl stop firewalld.service
setenforce 0
#
#下载所有安装包
if [ $? -eq 0 ];then
rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm && yum install zabbix-server-mysql zabbix-web-mysql mariadb mariadb-server php-bcmath php-mbstring httpd php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash -y && sleep 5
#
if [ $? -eq 0 ];then
#修改httpd,php 配置文件
sed -i "s/^Listen 80/Listen $ip:80/g" /etc/httpd/conf/httpd.conf
sed -i ‘164s/$/ index.php/‘ /etc/httpd/conf/httpd.conf
sed -i "s/^;date.timezone =/date.timezone = PRC/g" /etc/php.ini#启动httpd,mariadb 服务
systemctl start httpd.service
systemctl start mariadb
else echo -e "\e\t\t\t[31m package down error,check! \e[0m"
exit 0
fi
#
#检查mysql,http服务启动状态
port=`netstat -ntap | egrep ‘(80|3306)‘|wc -l`
if [ $? -eq 0 ]&&[ $port -gt "2" ];then
echo -e "\033\t\t\t[32m mysql run success!! \033[0m"
#
#创建zabbix库,密码
mysql -e "create database zabbix character set utf8 collate utf8_bin;grant all privileges ON . TO ‘zabbix‘@‘%‘ identified by ‘admin123‘;flush privileges;drop user ‘‘@localhost;drop user ‘‘@$name;"
#
#数据库设置密码(可自定义)
# mysqladmin -u root password ‘admin123‘
else
echo -e "\033\t\t\t[5;31m mysql start error,check!! \033[0m"
exit 0
fi
#
#zabbix安装、配置
if [ $? -eq 0 ];then
#zabbix连接数据库
echo -e ‘<?php\n$link=mysql_connect(localhost);\nif($link) echo "Success!!";\nelse echo "Fail!!";\nmysql_close();\n?>‘ >>/var/www/html/index.php
sed -i "s/localhost/‘$ip‘,‘zabbix‘,‘admin123‘/g" /var/www/html/index.php
echo -e "\e\t\t\t[32m test http://$ip/index.php \e[0m"
#
#zabbix 导入数据库
zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz |mysql -uzabbix -padmin123 zabbix
#
#修改zabbix配置文件
sed -i "s/^# DBPassword=/DBPassword=admin123/g" /etc/zabbix/zabbix_server.conf
sed -i "20iphp_value date.timezone Asia/Shanghai" /etc/httpd/conf.d/zabbix.conf
#
#修改字体
sed -i "s/graphfont/kaiti/g" /usr/share/zabbix/include/defines.inc.php
cp /root/STKAITI.TTF /usr/share/zabbix/fonts/
#
#开启zabbix 服务
systemctl start zabbix-server.service && systemctl enable zabbix-server.service
systemctl restart httpd && sleep 3
else echo -e "\e\t\t\t[31m zabbix set error,check! \e[0m"
exit 0
fi
#
#检查zabbix服务端口
http_port=`netstat -antp | grep :80 | wc -l`
zabbix_port=`netstat -antp | grep :10051 | wc -l`
if [ $? -eq 0 ]&&[ $http_port -ne 0 ]&& [ $zabbix_port -ne 0 ];then
echo -e "\033\t\t\t[32m http and zabbix run success!! \033[0m"
echo -e "\033\t\t\t[32m install web http:$ip/zabbix \033[0m"
else
echo -e "\033\t\t\t[5;31m zabbix install fail,check!! \033[0m"
exit 0
fi
#防火墙停止错误
else
echo -e "\e\t\t\t[31m stop firewalld error,check! \e[0m"
fi
3.添加脚本执行权限
chmod +x zabbix.sh
4.编写python selenium 模块
from selenium import webdriver
import time
#采用谷歌浏览器
driver = webdriver.Chrome()
#进入安装链接
driver.get("http://192.168.22.136/zabbix")
print(driver.title)
#定位“next”元素,点击鼠标(就是点击下一步)
driver.find_element_by_id("next_0").click()
#点击下一步
driver.find_element_by_id("next_1").click()
#清除文本后
driver.find_element_by_id("port").clear()
#输入新文本
driver.find_element_by_name("port").send_keys("3306")
driver.find_element_by_name("password").send_keys("admin123")
driver.find_element_by_id("next_2").click()
#填写name,点击下一步
driver.find_element_by_id("zbx_server_name").send_keys("zabbix")
driver.find_element_by_id("next_3").click()
#点击下一步
driver.find_element_by_id("next_4").click()
#点击完成
driver.find_element_by_id("finish").click()
#填写用户登陆信息
driver.find_element_by_id("name").send_keys("Admin")
driver.find_element_by_id("password").send_keys("zabbix")
driver.find_element_by_id("enter").click()
5.先执行shell 脚本
sh zabbix.sh
6.执行python selenium , 保存后点击F5.
总结:
1.编写shell脚本必须让有些命令顺序运行,该判断的地方一定要有!
2.在下载软件包时要休眠几秒(seep 5)否则没下载完成就运行后面命令。
3.这里的zabbix 安装基于lamp 架构上,lamp都是yum安装的,不建议采用源码包安装,太费时间了!
4.这里的数据库设置都是默认的,为了安全,后期应该加密码(脚本中有命令,注释了,去#就可以了)
5.在网页上安装zabbix 时,定位元素直接找"id"即可。
6.脚本文件不建议直接拷贝使用,可以去我的资料下载脚本http://down.51cto.com/data/2460609
7.此脚本只是监控端安装,后期会有被监控端安装脚本,敬请期待!
原文地址:https://blog.51cto.com/13760351/2379953