一、自定义监控进程
写脚本,脚本放在统一位置
修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
重启zabbix_agent
在web界面配置监控项和触发器
监控httpd
// 在客户端安装httpd
[root@centos ~]# yum -y install httpd
[root@centos ~]# systemctl start httpd
[root@centos ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
// 监控httpd进程
[root@centos ~]# ps -ef | grep httpd
root 36614 1 0 22:29 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 37392 36614 0 22:29 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 37393 36614 0 22:29 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 37395 36614 0 22:29 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 37396 36614 0 22:29 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 38613 1473 0 22:30 pts/0 00:00:00 grep --color=auto httpd
修改客户端zabbix_agentd文件zabbix_agentd.conf
[root@centos ~]# cd /usr/local/etc/
[root@centos etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@centos etc]# vim zabbix_agentd.conf
# Default:
UnsafeUserParameters=1
UserParameter=check_process[*],/scripts/check_process.sh $1
// 修改完配置文件重启服务
[root@centos ~]# pkill zabbix
[root@centos ~]# zabbix_agentd
编写进程然后进行测试
// 创建并进入脚本目录
[root@centos ~]# mkdir /scripts
[root@centos ~]# cd /scripts/
// 编写一个脚本
[root@centos scripts]# vim check_process.sh
#!/bin/bash
web=$( ps -ef | grep -Ev "grep|$0" | grep -c "$1" )
if [ $web -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@centos scripts]# chmod +x check_process.sh
返回服务端查看配置文件是否存在问题
[root@localhost ~]# zabbix_get -s 192.168.91.135 -k check_process[httpd]
0
[root@centos ~]# systemctl stop httpd.service
[root@localhost ~]# zabbix_get -s 192.168.91.135 -k check_process[httpd]
1
在zabbix网页上添加监控项
添加触发器
选择报警程度,填写名字,add添加
二、自定义监控日志
写脚本,脚本放在统一位置
修改被监控机zabbix_agentd.conf配置文件
UnxsafeParameters=1
UnserParameter=<key>,<command>
重启zabbix_agent
在web界面配置监控项和触发器
// 安装python3
[root@centos scripts]# yum -y install python3
// 授权
[root@centos scripts]# chmod 755 /var/log/httpd/
// 写脚本
[root@centos scripts]# vim log.py
[root@centos scripts]# cat log.py
#!/usr/bin/env python3
import sys
import re
def prePos(seekfile):
global curpos
try:
cf = open(seekfile)
except IOError:
curpos = 0
return curpos
except FileNotFoundError:
curpos = 0
return curpos
else:
try:
curpos = int(cf.readline().strip())
except ValueError:
curpos = 0
cf.close()
return curpos
cf.close()
return curpos
def lastPos(filename):
with open(filename) as lfile:
if lfile.readline():
lfile.seek(0,2)
else:
return 0
lastPos = lfile.tell()
return lastPos
def getSeekFile():
try:
seekfile = sys.argv[2]
except IndexError:
seekfile = '/tmp/logseek'
return seekfile
def getKey():
try:
tagKey = str(sys.argv[3])
except IndexError:
tagKey = 'Error'
return tagKey
def getResult(filename,seekfile,tagkey):
destPos = prePos(seekfile)
curPos = lastPos(filename)
if curPos < destPos:
curpos = 0
try:
f = open(filename)
except IOError:
print('Could not open file: %s' % filename)
except FileNotFoundError:
print('Could not open file: %s' % filename)
else:
f.seek(destPos)
while curPos != 0 and f.tell() < curPos:
rresult = f.readline().strip()
global result
if re.search(tagkey, rresult):
result = 1
break
else:
result = 0
with open(seekfile,'w') as sf:
sf.write(str(curPos))
finally:
f.close()
return result
if __name__ == "__main__":
result = 0
curpos = 0
tagkey = getKey()
seekfile = getSeekFile()
result = getResult(sys.argv[1],seekfile,tagkey)
print(result)
[root@centos scripts]# chmod +x log.py
[root@centos scripts]# ./log.py /var/log/httpd/error_log
0
//删除客户端的 logseek 文件
[root@centos scripts]# cd /tmp/
[root@centos tmp]# rm -rf logseek
// 手动添加Error再测试
[root@centos scripts]# echo "Error" >> /var/log/httpd/error_log
[root@centos scripts]# ./log.py /var/log/httpd/error_log
1
// 修改配置文件
[root@centos scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
// 重启zabbix服务
[root@centos scripts]# pkill zabbix
[root@centos scripts]# zabbix_agentd
[root@localhost ~]# zabbix_get -s 192.168.91.135 -k check_log[/var/log/httpd/error_log]
Traceback (most recent call last):
File "/scripts/log.py", line 84, in <module>
result = getResult(sys.argv[1],seekfile,tagkey)
File "/scripts/log.py", line 73, in getResult
with open(seekfile,'w') as sf:
PermissionError: [Errno 13] Permission denied: '/tmp/logseek'
// 删除 /tmp/logseek
[root@centos scripts]# rm -rf /tmp/logseek
[root@centos scripts]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# zabbix_get -s 192.168.91.135 -k check_log[/var/log/httpd/error_log]
0
手动触发报警器
[root@centos scripts]# echo "Error" >> /var/log/httpd/error_log
三、Zabbix自定义监控(主从延迟)
部署MySQL主从
主机名 | 作用 | IP |
---|---|---|
localhost | zabbix-server | 192.168.91.134 |
centos | mysql-master | 192.168.91.135 |
centos2 | mysql-slave | 192.168.91.137 |
主数据库从服务器上安装MySQL、关闭防火墙、修改配置文件、授权用户、查看状态、安装zabbix
数据库主服务器
[root@centos ~]# yum -y install mariadb mariadb-server
// 启动mysql服务
[root@centos ~]# systemctl start mariadb.service
[root@centos ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
// 数据库初始化
[root@centos ~]# mysql_secure_installation
Thanks for using MariaDB!
// 关闭防火墙和selinux
[root@centos ~]# systemctl stop firewalld.service
[root@centos ~]# systemctl disable firewalld.service
[root@centos ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
[root@centos ~]# setenforce 0
setenforce: SELinux is disabled
数据库从服务器
[root@centos2 ~]# yum -y install mariadb mariadb-server
[root@centos2 ~]# systemctl start mariadb.service
[root@centos2 ~]# systemctl enable mariadb.service
[root@centos2 ~]# mysql_secure_installation
Thanks for using MariaDB!
[root@centos2 ~]# systemctl start mariadb.service^C
[root@centos2 ~]# systemctl stop firewalld.service
[root@centos2 ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos2 ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
[root@centos2 ~]# setenforce 0
修改配置文件
数据库主服务器:
[root@centos ~]# vim /etc/my.cnf.d/mariadb-server.cnf
// 添加内容
[mysqld]
log_bin=mysql-bin //开启二进制日志
server_id=1 //服务id号,不可从复,值为0时则表示拒绝服务器连接
// 重启服务
[root@centos ~]# systemctl restart mariadb.service
数据库从服务器:
[root@centos2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
// 添加内容
[mysqld]
server_id=2
relay_log=myrelay
[root@centos2 ~]# systemctl restart mariadb.service
创建用户并授权,让从服务可以登陆主服务器
数据库主服务器:
[root@centos ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
// 创建用户
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'gf1'@'%' IDENTIFIED BY 'gf1123';
Query OK, 0 rows affected (0.000 sec)
// 刷新权限
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 757 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.001 sec)
登陆到从服务器的数据库,在数据库中配置主服务器的信息
[root@centos2 ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.91.135',MASTER_PORT=3306,MASTER_USER='gf1',MASTER_PASSWORD='gf1123';
Query OK, 0 rows affected (0.010 sec)
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.91.135
Master_User: gf1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 757
Relay_Log_File: myrelay.000003
Relay_Log_Pos: 1056
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 757
Relay_Log_Space: 1728
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 3
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
从服务器安装zabbix
下载zabbix软件包、解压、创建zabbix用户、安装依赖包、编译安装
[root@centos2 src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.4/zabbix-5.4.4.tar.gz
[root@centos2 src]# ll
total 23700
drwxr-xr-x. 2 root root 6 May 19 2020 debug
drwxr-xr-x. 2 root root 6 May 19 2020 kernels
-rw-r--r--. 1 root root 24266079 Aug 30 16:23 zabbix-5.4.4.tar.gz
[root@centos2 src]# tar xf zabbix-5.4.4.tar.gz
[root@centos2 src]# useradd -r -M -s /sbin/nologin zabbix
[root@sql-slave src]# yum -y install net-snmp-devel libevent-devel make gcc gcc-c++
[root@centos2 src]# cd zabbix-5.4.4
[root@centos2 zabbix-5.4.4]# ./configure --enable-agent
[root@centos2 zabbix-5.4.4]# make install
// 更改zabbix配置文件
[root@centos2 ~]# vim /etc/zabbix_agentd.conf
Server=192.168.91.135
ServerActive=192.168.91.135
Hostname=192.168.91.137
编写脚本文件
指定key和脚本位置
/修改配置文件,让key可以执行脚本识别MySQL状态
[root@centos2 scripts]# vim /etc/zabbix_agentd.conf
...
UnsafeUserParameters=1
...
//在文件末尾添加
UserParameter=check_replication,/scripts/check_replication.sh
// 杀死进程重新启动
[root@centos2 scripts]# pkill zabbix_agentd
[root@centos2 scripts]# zabbix_agentd
[root@cetos2 ~]# mysql -uroot -p123
//创建一个zabbix的用户,并设置权限只读和密码
MariaDB [(none)]> grant select on *.* to 'zabbix'@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.001 sec)
//刷新权限
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
[root@centos2 ~]# mkdir /scripts
[root@centos2 ~]# cd /scripts
[root@centos2 scripts]# vim check_replication.sh
#!/bin/bash
count=$(mysql -uzabbix -pzabbix -e 'show slave status\G' | grep '_Running' | grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
[root@centos2 scripts]# chmod +x check_replication.sh
// 测试脚本
[root@centos2 scripts]# ./check_replication.sh
0
// 在到zabbix服务端测试
[root@localhost ~]# zabbix_get -s 192.168.91.137 -k check_replication
0
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/5627.html