1. 程式人生 > >shell 搭配 python 自動安裝zabbix監控端詳解

shell 搭配 python 自動安裝zabbix監控端詳解

導入 maria httpd 步驟 web http user follow 自定義 c89

技術分享圖片

腳本環境

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.此腳本只是監控端安裝,後期會有被監控端安裝腳本,敬請期待!

shell 搭配 python 自動安裝zabbix監控端詳解