颜色变量脚本展示
123456789101112131415161718192021222324252627282930313233343536 | [root@localhost opt] #!/bin/bash BLACK= "/033[30m" RED= "/033[31m" GREEN= "/033[32m" YELLOW= "/033[33m" BLUE= "/033[34m" PURPLE= "/033[35m" SKY_GREEN= "/033[36m " WHITE= "/033[37m" BLACK_WHITE= "/033[40;37m" RED_WHITE= "/033[41;37m" GREEN_WHITE= "/033[42;37m" YELLOW_WHITE= "/033[43;37m" BLUE_WHITE= "/033[44;37m" PURPLE_WHITE= "/033[45;37m" WHITE_BLACK= "/033[47;30m" SKY_BLUE_WHITE= "/033[46;37m" FLICKER= "/033[05m" OUTPUT= "echo -e" TAILS= "/033[0m" |
把文件里的sql文件按从高到下的顺序导进数据库
############# 需求 #############
依序从上到下把文件里面的每行sql文件导进数据库
############# 执行方法 #############
sh import_ku.sh 变量1 变量2 变量3
变量1:存储数据库脚本文件名的文件
变量2:数据库用户名
变量3: 数据库密码
代码展示
1234567891011121314151617181920 | #!/bin/bash OUTPUT= "echo -e" TAILS= "/033[0m" GREEN= "/033[32m" RED= "/033[31m" FLICKER= "/033[05m" DATA=` cat $1 | wc -l` for ((i=1;i<=$DATA;i++)) do SQL=` head -$i $1 | tail -1` MySQL -u$2 -p$3 < $SQL NU=$? if [ $NU - eq 0 ]; then $OUTPUT $GREEN "$SQL exculde success" $TAILS echo else $OUTPUT $RED$FLICKER "$SQL exculde eroor!" $TAILS echo fi done |
MySQL主从同步监控
#此脚本为监控MySQL主从IO和SQL的两个YES,NO代表为不同步
#Zabbix一分钟采集一次数据
#触为器为大于1则报警
#!/bin/bash
mysql="/usr/bin/mysql"
user="root"
passWord="123456"
IO=`$mysql -u$user -p$password -e "show slave status/G" | grep 'Slave_IO_Running:' | awk '{PRint $2}'`
SQL=`$mysql -u$user -p$password -e "show slave status/G" | grep 'Slave_SQL_Running:' | awk '{print $2}'`
#[ "$IO" == "Yes" -a "$SQL" == "Yes" ] && echo "1" || echo "2"
[ "$IO" == "Yes" -o "$IO" == "Connecting" ] && [ "$SQL" == "Yes" -o "$SQL" == "Connecting" ] && echo "1" || echo "2"
#此脚本为监控MySQL主从同步的中继日志点,不变化代表不同步,
#Zabbix三分钟采集一次数据
#触为器为日志点数字不变化则报警
#!/bin/bash
mysql="/usr/bin/mysql"
user="root"
password="123456"
Relay=`$mysql -u$user -p$password -e "show slave status/G" | grep 'Relay_Log_Pos:' | awk '{print $2}'`
echo $Relay
#此脚本为监控MySQL主从同步延迟时间
#Zabbix一分钟采集一次数据
#触为器为超过3600秒延迟时间则报警
#!/bin/bash
mysql="/usr/bin/mysql"
user="root"
password="123456"
Seconds=`$mysql -u$user -p$password -e "show slave status/G" | grep 'Seconds_Behind_Master:' | awk '{print $2}'`
echo $Seconds
上述三个监控脚本写后,在Slave的机器上相应位置放置三个脚本,
再去Zabbix Web界面添加监控,设置好Item和Key即可
#telnet放到子shell执行退出
12345 | [root@localhost ~] Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]' . Connection closed by foreign host. |
#通过telnet监控22端口
12345678 | [root@ryzc32 opt] #!/bin/bash JG=` echo -e "/n" | telnet 192.168.1.30 22 | grep Connected | wc -l` if [ "$JG" == 1 ]; then echo "0" else echo "1" fi |
#通过nmap监控22端口
12345678 | [root@localhost opt] #!/bin/bash namp=`nmap 192.168.100.50 -p 22 | grep open | wc -l` if [ "$namp" == 1 ]; then echo 0 else echo 1 fi |
#通过nc监控22端口
123 | [root@localhost opt] #!/bin/bash nc -w 5 192.168.100.50 22 && echo 0 || echo 1 |
#通过wget判断www.baidu.com状态是否为200
12345678 | [root@mndg248 opt] #!/bin/bash wget -T 10 -c -q --spider www.baidu.com && RETVAL=$? if [ "$RETVAL" == 0 ]; then echo "0" else e cho "1" fi
|
一健安装Zabbix
#!/bin/bash
#定义变量
Redh="/033[31m"
Blueh="/033[36m"
Yellow="/033[33m"
Tails="/033[0m"
Output="echo -e"
Document=`dirname $0;pwd`
dir=`echo $Document | sed s'/. //'`
#加载函数库
. /etc/init.d/functions
#检查网络
$Output --------------------$Blueh【 正在检查网络,请稍候 】$Tails--------------------
echo
ping www.baidu.com -c 3 &> /dev/null
[ $? -eq 0 ] && action "网络正常" /bin/true || {
action "你的网络不通,请修复你的网络,然后再进行安装" /bin/false
exit 2
}
echo
$Output --------------------$Redh【 准备安装Zabbix 】$Tails-------------------------
sleep 3
#下载yum
rm -rf /etc/yum.repos.d/other
mkdir /etc/yum.repos.d/other
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/other
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
#关闭防火墙和selinux
service iptables stop
setenforce 0
chkconfig iptables off
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
#修改主机名
sed -i '2d' /etc/sysconfig/network
sed -i '1a HOSTNAME=zabbix' /etc/sysconfig/network
hostname zabbix
#服务端安装LAMP环境
yum install -y gcc gcc-c++ autoconf httpd php mysql mysql-server php-mysql httpd-manual mod_ssl mod_perl mod_auth_mysql php-gd php-xml php-ldap php-pear php-xmlrpc mysql-connector-odbc mysql-devel libdbi-dbd-mysql net-snmp-devel curl-devel php-mbstring php-bcmath mail
#服务端配置LAMP使用环境
sed -i 's/;date.timezone =/date.timezone = Asia//Shanghai/g' /etc/php.ini
sed -i 's#max_execution_time = 30#max_execution_time = 300#g' /etc/php.ini
sed -i 's#post_max_size = 8M#post_max_size = 32M#g' /etc/php.ini
sed -i 's#max_input_time = 60#max_input_time = 300#g' /etc/php.ini
sed -i 's/;mbstring.func_overload = 0/mbstring.func_overload = 2/g' /etc/php.ini
service mysqld start;chkconfig mysqld on
service httpd start;chkconfig httpd on
#安装Zabbix server服务端
groupadd -g 201 zabbix
useradd -g zabbix -u 201 zabbix
tar xf zabbix-2.2.2.tar.gz -C /usr/src/
cd /usr/src/zabbix-2.2.2/
./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl
make
make install
#添加Zabbix到系统服务文件
sed -i '$a zabbix-agent 10050/tcp #Zabbix Agent' /etc/services
sed -i '$a zabbix-agent 10050/udp #Zabbix Agent' /etc/services
sed -i '$a zabbix-server 10051/tcp #zabbix Trapper' /etc/services
sed -i '$a zabbix-server 10051/udp #zabbix Trapper' /etc/services
#导入zabbix数据库
mysqladmin -uroot password 'mysql_pass'
mysql -uroot -pmysql_pass -e "create database zabbix default character set utf8;"
mysql -uroot -pmysql_pass -e "grant all on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';"
mysql -uroot -pmysql_pass -e "flush privileges;"
cd /usr/src/zabbix-2.2.2/database/mysql/
mysql -uzabbix -pzabbix zabbix < schema.sql
mysql -uzabbix -pzabbix zabbix < images.sql
mysql -uzabbix -pzabbix zabbix < data.sql
#创建链接
mkdir /var/log/zabbix
chown zabbix.zabbix /var/log/zabbix
ln -s /usr/local/zabbix/etc /etc/zabbix
ln -s /usr/local/zabbix/bin/* /usr/bin/
ln -s /usr/local/zabbix/sbin/* /usr/sbin/
#拷贝service启动脚本
cp /usr/src/zabbix-2.2.2/misc/init.d/fedora/core/zabbix_* /etc/init.d/
sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#g" /etc/init.d/zabbix_server
sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#g" /etc/init.d/zabbix_agentd
#配置zabbix_server.conf服务端文件
sed -i 's#LogFile=/tmp/zabbix_server.log#LogFile=/var/log/zabbix/zabbix_server.log#' /etc/zabbix/zabbix_server.conf
sed -i 's#DBUser=root#DBUser=zabbix#' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=zabbix/' /etc/zabbix/zabbix_server.conf
service zabbix_server start
#配置zabbix_agentd.conf文件 (监控server本身,如对其他服务器进行监控,配置文件相同)
IP=`ifconfig | head -2 | tail -1 | awk '{print $2}' | awk -F':' '{print $2}'`
sed -i 's#LogFile=/tmp/zabbix_agentd.log#LogFile=/var/log/zabbix/zabbix_agentd.log#' /etc/zabbix/zabbix_agentd.conf
sed -i "s/Server=127.0.0.1/Server=127.0.0.1,$IP/" /etc/zabbix/zabbix_agentd.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$IP:10051/" /etc/zabbix/zabbix_agentd.conf
sed -i 's/# UnsafeUserParameters=0/UnsafeUserParameters=1/' /etc/zabbix/zabbix_agentd.conf
sed -i 's/Hostname=Zabbix server/Hostname=Zabbix/' /etc/zabbix/zabbix_agentd.conf
sed -i 's$# EnableRemoteCommands=0$EnableRemoteCommands=1$' /etc/zabbix/zabbix_agentd.conf
sed -i 's$# Include=/usr/local/etc/zabbix_agentd.conf.d/$Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/$' /etc/zabbix/zabbix_agentd.conf
service zabbix_agentd start
#拷贝网页文件到apache目录
cp /usr/src/zabbix-2.2.2/frontends/php/ /var/www/html/zabbix -r
chown -R apache.apache /var/www/html/zabbix
#设置zabbix开机启动
chkconfig --add zabbix_server
chkconfig --add zabbix_agentd
chkconfig zabbix_agentd on
chkconfig zabbix_server on
#配置邮件发送
cat >> /etc/mail.rc <<EOF
set from=q123456789@139.com
set smtp=smtp.139.com
set smtp-auth-user=q123456789@139.com
set smtp-auth-password=test
set smtp-auth=login
EOF
cat >> /etc/postfix/main.cf <<EOF
myhostname = zabbix.test.com
myorigin = zabbix.jenkins.com
mydomain = zabbix.jenkins.com
mynetworks = 127.0.0.0/8
EOF
#修改Hosts文件,做域名解析
sed -i '1a 127.0.0.1 localhost localhost.localdomain zabbix.test.com zabbix.wsyht.com' /etc/hosts
sed -i 1d /etc/hosts
#设置zabbix在web页面的字体
cp $dir/msyhbd.ttf /var/www/html/zabbix/fonts/
sed -i "s/define('ZBX_FONT_NAME', 'DejaVuSans');/define('ZBX_FONT_NAME', 'msyhbd');/" /var/www/html/zabbix/include/defines.inc.php
sed -i "39a define('ZBX_GRAPH_FONT_NAME', 'msyhbd');" /var/www/html/zabbix/include/defines.inc.php
sed -i 39d /var/www/html/zabbix/include/defines.inc.php
#web登陆
$Output $Yellow"[脚本功能]" $Tails
$Output $Blueh"安装Zabbix" $Tails
$Output $Blueh"安装agentd" $Tails
$Output $Blueh"邮件服务配置" $Tails
$Output $Blueh"配置PHP支持中文字体" $Tails
$Output $Yellow"[浏览器登陆]" $Tails
$Output $Redh"http://$IP/zabbix" $Tails
$Output 用户名: $Redh"admin" $Tails
$Output 密码: $Redh"zabbix" $Tails