搭建zabbix+grafana监控

编写一件安装脚本

#!/bin/sh
echo "\033[32;1m脚本作者:fage\033[0m"

#sleep 10
zabbix_version=4.0.2
zabbixdir=`pwd`
ip=`ip addr |grep inet |egrep -v "inet6|127.0.0.1" |awk '{print $2}' |awk -F "/" '{print $1}'`
release=`cat /etc/redhat-release | awk -F "release" '{print $2}' |awk -F "." '{print $1}' |sed 's/ //g'`
mkdir -p /application/zabbix

#sleep 10
echo "\033[32;1m当前目录为:$zabbixdir\033[0m"
echo "\033[32;1m本机ip为:$ip\033[0m"
echo "\033[32;1m安装mysql、apache、php等相关组件\033[0m"
setenforce 0 && echo "\033[32;1m临时关闭SELINUX\033[0m"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && echo "\033[32;1m永久关闭需要下次重启机器\033[0m"
sleep 3

if [ $release = 7 ];then
        rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
	yum -y install php-xml unixODBC unixODBC-devel  php-xmlrpc php-mbstring php-mhash patch java-devel wget unzip libxml2 libxml2-devel httpd mariadb mariadb-devel mariadb-server php php-mysql php-common php-mbstring php-gd php-odbc php-pear curl curl-devel net-snmp net-snmp-devel perl-DBI php-xml ntpdate  php-bcmath zlib-devel glibc-devel curl-devel gcc automake libidn-devel openssl-devel net-snmp-devel rpm-devel OpenIPMI-devel libevent-devel.x86_64
	systemctl start mariadb.service
elif [ $release = 6 ];then
	yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64 -y
	rpm -Uvh http://mirrors.isu.net.sa/pub/fedora/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
	rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
	yum install unixODBC unixODBC-devel patch java-devel wget unzip libxml2 libxml2-devel httpd mysql mysql-server  curl curl-devel net-snmp net-snmp-devel perl-DBI ntpdate zlib-devel mysql-devel glibc-devel gcc-c++ gcc automake mysql libidn-devel openssl-devel net-snmp-devel rpm-devel OpenIPMI-devel php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64 php56w-bcmath php56w-xml -y
	service mysqld start
fi

echo "\033[32;1m同步服务器时间\033[0m"
ntpdate ntp1.aliyun.com
echo "\033[32;1m设置定时同步时间\033[0m"
echo "*/5 * * * * ntpdate ntp1.aliyun.com" >> /var/spool/cron/root
echo "\033[32;1m创建zabbix用户\033[0m"
awk -F: '$3==1000{print $3}' /etc/passwd|grep 1000
if [ $? != 0 ];then
  useradd -u 1000 zabbix -s /sbin/nologin -M
fi
sleep 5


echo "\033[32;1m设置数据库root密码,默认为123456\033[0m"
sleep 3
mysqladmin  -uroot password "123456\033[0m"


echo "\033[32;1m创建zabbix数据库,和用户名密码\033[0m"
echo "create database IF NOT EXISTS zabbix default charset utf8 COLLATE utf8_general_ci;" | mysql -uroot -p123456
echo "grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';" | mysql -uroot -p123456
echo "flush privileges;" | mysql -uroot -p123456

echo "\033[32;1m安装zabbix-${zabbix_version}\033[0m"
sleep 3
if [ ! -f zabbix-${zabbix_version}.tar.gz ];then
	wget http://netix.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/${zabbix_version}/zabbix-${zabbix_version}.tar.gz
fi

tar xf $zabbixdir/zabbix-${zabbix_version}.tar.gz
cd $zabbixdir/zabbix-${zabbix_version}
echo `pwd`
./configure --prefix=/application/zabbix/ --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java
sleep 3

CPU_NUM=$(cat /proc/cpuinfo | grep processor | wc -l)
if [ $CPU_NUM -gt 1 ];then
    make -j$CPU_NUM
else
    make
fi

make install

if [ $? != 0 ];then
    exit -1
fi

mkdir /var/www/html/zabbix
cp -r $zabbixdir/zabbix-${zabbix_version}/frontends/php/* /var/www/html/zabbix
cp $zabbixdir/simkai.ttf /var/www/html/zabbix/fonts
sed -i "s/DejaVuSans/simkai/g" /var/www/html/zabbix/include/defines.inc.php

echo "\033[32;1m创建zabbix数据库配置档\033[0m"
rm -f /var/www/html/zabbix/conf/zabbix.conf.php
cat > /var/www/html/zabbix/conf/zabbix.conf.php <<END
<?php
// Zabbix GUI configuration file.
global \$DB;

\$DB['TYPE']     = 'MYSQL';
\$DB['SERVER']   = 'localhost';
\$DB['PORT']     = '0';
\$DB['DATABASE'] = 'zabbix';
\$DB['USER']     = 'zabbix';
\$DB['PASSWORD'] = 'zabbix';

// Schema name. Used for IBM DB2 and PostgreSQL.
\$DB['SCHEMA'] = '';

\$ZBX_SERVER      = 'localhost';
\$ZBX_SERVER_PORT = '10051';
\$ZBX_SERVER_NAME = '';

\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
?>
END

echo "\033[32;1m导入zabbix数据库\033[0m"
sleep 3

cd $zabbixdir/zabbix-${zabbix_version}
mysql -uzabbix -pzabbix -hlocalhost zabbix < database/mysql/schema.sql
mysql -uzabbix -pzabbix -hlocalhost zabbix < database/mysql/images.sql
mysql -uzabbix -pzabbix -hlocalhost zabbix < database/mysql/data.sql
echo "\033[32;1m创建启动init"
sleep 3
cp misc/init.d/tru64/zabbix_agentd /etc/init.d/
cp misc/init.d/tru64/zabbix_server /etc/init.d/
chmod +x /etc/init.d/zabbix_*
sed -i 's:DAEMON=/usr/local/sbin/zabbix_server:DAEMON=/application/zabbix/sbin/zabbix_server:g' /etc/init.d/zabbix_server
sed -i 's:DAEMON=/usr/local/sbin/zabbix_agentd:DAEMON=/application/zabbix/sbin/zabbix_agentd:g' /etc/init.d/zabbix_agentd
sed -i 's:DBUser=root:DBUser=zabbix:g' /application/zabbix/etc/zabbix_server.conf
sed -i '/# DBPassword=/a\DBPassword=zabbix' /application/zabbix/etc/zabbix_server.conf
echo "\033[32;1m设置php.ini相关参数\033[0m"
sleep 3
cp /etc/php.ini /etc/php.ini.zabbixbak
sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /etc/php.ini
sed -i '/max_input_time =/s/60/300/' /etc/php.ini
sed -i '/mbstring.func_overload = 0/a\mbstring.func_overload = 1' /etc/php.ini
sed -i '/post_max_size =/s/8M/32M/' /etc/php.ini
sed -i '/;always_populate_raw_post_data = -1/a\always_populate_raw_post_data = -1' /etc/php.ini
sed -i '/;date.timezone =/a\date.timezone = PRC' /etc/php.ini

echo "\033[32;1m设置apache\033[0m"
sleep 3
sed -i '/#ServerName www.example.com:80/a\ServerName zabbix' /etc/httpd/conf/httpd.conf 
if [ $release = 7 ];then
	systemctl start httpd.service
	systemctl enable httpd.service
        systemctl enable mariadb.service
elif [ $release = 6 ];then
	service httpd start
fi


echo "\033[32;1m启动zabbix\033[0m"
/etc/init.d/zabbix_server restart
/etc/init.d/zabbix_agentd restart
/application/zabbix/sbin/zabbix_java/startup.sh

echo "\033[32;1m数据库默认root密码zabbix 123456;zabbix-Database name:zabbix/User:zabbix/Password:zabbix\033[0m"

cat > /usr/lib/systemd/system/zabbix_agent.service <<END
[Unit]
Description=The Zabbix-Agent Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/etc/init.d/zabbix_agentd start
ExecReload=/etc/init.d/zabbix_agentd restart
ExecStop=/etc/init.d/zabbix_agentd stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
END

systemctl enable zabbix_agent

cat > /usr/lib/systemd/system/zabbix_service.servicea <<END
[Unit]
Description=The Zabbix-Server Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/etc/init.d/zabbix_server start
ExecReload=/etc/init.d/zabbix_server restart
ExecStop=/etc/init.d/zabbix_server stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
END

systemctl enable zabbix_service
echo "打开http://$ip/zabbix,进行下一步安装"

 

 

 

 

 

 zabbix-agent安装

sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
useradd -u 1001 -M -s /sbin/nologin zabbix
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.2-1.el7.x86_64.rpm
sed -i 's/127.0.0.1/10.0.0.70/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/# ListenPort=10050/ListenPort=10050/g' /etc/zabbix/zabbix_agentd.conf 
systemctl start zabbix-agent.service 
systemctl enable zabbix-agent.service

 

 

 

 

 

 

自定义监控业务

如果监控一些自定的监控比如请求数量,未超时的数量,并发数量,使用netstat命令报错的话需要授权用户有s 粘滞位权限(chmod au+s /usr/bin/netstat)

tail -3 /etc/zabbix/zabbix_agentd.conf

 

 

 

Grafana安装及配置

wget https://dl.grafana.com/oss/release/grafana-6.7.1-1.x86_64.rpm
yum install -y grafana-6.7.1-1.x86_64.rpm
systemctl enable grafana-server.service 
systemctl start grafana-server.service

获取插件

grafana-cli plugins list-remote

安装zabbix插件

grafana-cli plugins install alexanderzobnin-zabbix-app
systemctl restart grafana-server.service

图形配置

 

 

 

 

 

打开页面输入:http://192.168.10.101/zabbix/api_jsonrpc.php

 

posted @ 2020-10-06 23:49  缺个好听的昵称  阅读(113)  评论(0编辑  收藏  举报