当前位置:   article > 正文

zabbix监控进程,日志,主从状态和主从延迟

zabbix监控进程,日志,主从状态和主从延迟

自定义监控进程

使用httpd服务为例,监控httpd的进程

在zabbix-agent上安装httpd

yum -y install httpd

重启httpd

systemctl restart httpd

systemtctl enable httpd

查看httpd的进程

  1. [root@zabbix-agent ~]# ps -ef | grep httpd
  2. root 2407458 1 0 16:56 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  3. apache 2407459 2407458 0 16:56 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  4. apache 2407460 2407458 0 16:56 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  5. apache 2407461 2407458 0 16:56 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  6. apache 2407462 2407458 0 16:56 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  7. root 2424256 749323 0 17:02 pts/2 00:00:00 grep --color=auto httpd
  8. [root@zabbix-agent ~]# ps -ef | grep httpd | grep -v grep | wc -l
  9. 5
  10. [root@zabbix-agent ~]# ps -ef | grep -v grep | grep -c httpd
  11. 5
  12. [root@zabbix-agent ~]#

 新建脚本存放目录

  1. [root@zabbix-agent ~]# mkdir /etc/zabbix/script
  2. [root@zabbix-agent ~]# cd /etc/zabbix/script/
  3. [root@zabbix-agent script]# vim check_httpd.sh //新建脚本//
  4. [root@zabbix-agent script]# cat check_httpd.sh
  5. count=$(ps -ef | grep -Ev "grep|$0" | grep -c httpd)
  6. if [ $count -eq 0 ];then
  7. echo '1'
  8. else
  9. echo '0'
  10. fi

设置属主,权限

chmod +x check_httpd.sh

chown -R zabbix.zabbix /etc/zabbix/script/

 测试脚本

0是httpd服务开启,1为关闭

  1. [root@zabbix-agent script]# ./check_httpd.sh
  2. 0
  3. [root@zabbix-agent script]# systemctl stop httpd //此时停止httpd服务
  4. [root@zabbix-agent script]# ./check_httpd.sh // 脚本测试出来是1
  5. 1
  6. [root@zabbix-agent script]#
  7. 关闭后记得启动httpd服务

修改zabbix-agentd.conf文件

  1. vim /etc/zabbix/zabbix_agentd.conf
  2. 添加:
  3. UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh

重启服务

systemctl restart zabbix-agent

 zabbix server端测试脚本

  1. [root@zabbix-server ~]# zabbix_get -s 192.168.100.30 -k check_httpd
  2. 0
  3. [root@zabbix-server ~]#
  4. //0代表httpd服务已启动/

zabbix web平台配置

新建监控项

配置触发器

 测试

关闭httpd服务,测试告警信息
[root@zabbix-agent script]# systemctl stop httpd

 此时查看告警信息和邮箱

邮箱

 开启httpd服务,查看邮箱是否收到恢复邮件

[root@zabbix-agent ~]# systemctl restart zabbix-agent.service

查看邮箱

自定义监控日志

下载log.py来协助我们进行测试,以httpd服务为例

将log.py上传到/etc/zabbix/script/目录下,然后给执行权限,修改所有者和所属组为zabbix

# log.py

  1. try:
  2. seekfile = sys.argv[2]
  3. except IndexError:
  4. seekfile = '/tmp/logseek'
  5. return seekfile
  6. def getKey():
  7. try:
  8. tagKey = str(sys.argv[3])
  9. except IndexError:
  10. tagKey = 'Error'
  11. return tagKey
  12. def getResult(filename,seekfile,tagkey):
  13. destPos = prePos(seekfile)
  14. curPos = lastPos(filename)
  15. if curPos < destPos:
  16. curpos = 0
  17. try:
  18. f = open(filename)
  19. except IOError:
  20. print('Could not open file: %s' % filename)
  21. except FileNotFoundError:
  22. print('Could not open file: %s' % filename)
  23. #!/usr/bin/env python3
  24. import sys
  25. import re
  26. def prePos(seekfile):
  27. global curpos
  28. try:
  29. cf = open(seekfile)
  30. except IOError:
  31. curpos = 0
  32. return curpos
  33. except FileNotFoundError:
  34. curpos = 0
  35. return curpos
  36. else:
  37. try:
  38. curpos = int(cf.readline().strip())
  39. except ValueError:
  40. curpos = 0
  41. cf.close()
  42. return curpos
  43. cf.close()
  44. return curpos
  45. def lastPos(filename):
  46. with open(filename) as lfile:
  47. if lfile.readline():
  48. lfile.seek(0,2)
  49. else:
  50. return 0
  51. lastPos = lfile.tell()
  52. return lastPos
  53. def getSeekFile():
  54. try:
  55. seekfile = sys.argv[2]
  56. except IndexError:
  57. seekfile = '/tmp/logseek'
  58. return seekfile
  59. def getKey():
  60. try:
  61. tagKey = str(sys.argv[3])
  62. except IndexError:
  63. tagKey = 'Error'
  64. return tagKey
  65. def getResult(filename,seekfile,tagkey):
  66. destPos = prePos(seekfile)
  67. curPos = lastPos(filename)
  68. if curPos < destPos:
  69. curpos = 0
  70. try:
  71. f = open(filename)
  72. except IOError:
  73. print('Could not open file: %s' % filename)
  74. except FileNotFoundError:
  75. print('Could not open file: %s' % filename)
  76. else:
  77. f.seek(destPos)
  78. while curPos != 0 and f.tell() < curPos:
  79. rresult = f.readline().strip()
  80. global result
  81. if re.search(tagkey, rresult):
  82. result = 1
  83. break
  84. else:
  85. result = 0
  86. with open(seekfile,'w') as sf:
  87. sf.write(str(curPos))
  88. finally:
  89. f.close()
  90. return result
  91. if __name__ == "__main__":
  92. result = 0
  93. curpos = 0
  94. tagkey = getKey()
  95. seekfile = getSeekFile()
  96. result = getResult(sys.argv[1],seekfile,tagkey)
  97. print(result)

### 作用:检查日志文件中是否有指定的关键字

#### 第一个参数为日志文件名(必须有,相对路径、绝对路径均可)

#### 第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)

#### 第三个参数为搜索关键字,默认为 Error

 设置属主和权限

chmod +x log.py

chown zabbix.zabbix log.py

httpd服务的日志文件在/var/log/httpd/目录下,首先我们需要给这个目录设置一个ACL权限,让zabbix用户有权限去访问该目录 

[root@zabbix-agent httpd]# setfacl -m u:zabbix:r-x /var/log/httpd/

下载python3来执行log.py脚本

[root@zabbix-agent ~]# yum -y install python3

修改zabbix_agentd.conf文件,并重启服务

  1. vim /etc/zabbix/zabbix_agentd.conf
  2. UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
  3. UserParameter=check_logs[*],/usr/bin/python3 /etc/zabbix/script/log.py $1 $2 $3

重启zabbix-agent服务

systemctl restart zabbix-agent.service

 测试脚本

  1. [root@zabbix-agent script]# python3 log.py /var/log/httpd/error_log
  2. 0
  3. [root@zabbix-agent script]# echo 'Error'>> /var/log/httpd/error_log
  4. / 追加重定向一个Error到日志文件中做实验
  5. [root@zabbix-agent script]# python3 log.py /var/log/httpd/error_log
  6. 1
  7. [root@zabbix-agent script]#
  8. / 0为没有Error日志信息,1为有Error日志信息///

测试完成后将写入的Error内容删除

而且因文件/tmp/logseek属于root账户,在web端写入写不进去,所以删除

rm -rf /tmp/logseek

配置监控项

 创建触发器

测试

 向httpd日志文件输入一个error

  1. [root@zabbix-agent script]# echo 'Error'>> /var/log/httpd/error_log
  2. [root@zabbix-agent script]# python3 log.py /var/log/httpd/error_log
  3. 1
  4. // 0为没有Error日志信息,1为有Error日志信息

查看是否有告警信息

zabbix监控 mysql主从

部署mysql主从

  1. 1、部署mysql主从,使用mariadb进行操作
  2. 192.168.100.10 master.example.com master
  3. 192.168.100.20 slave.example.com slave
  4. 2、将server、agent1、master、slave主机的/etc/hosts文件全部设置为
  5. 192.168.100.40 server.example.com server
  6. 192.168.100.30 agent1.example.com agent1
  7. 192.168.100.10 master.example.com master
  8. 192.168.100.20 slave.example.com slave
  9. 3、然后两台主机都安装mariadb mariadb-server
  10. yum install -y mariadb mariadb-server
  11. systemctl restart mariadb;systemctl enable mariadb
  12. 4、两台主机都初始化mysql数据库
  13. mysql_secure_installation
  14. [root@master ~]# mysql_secure_installation
  15. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  16. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  17. In order to log into MariaDB to secure it, we'll need the current
  18. password for the root user. If you've just installed MariaDB, and
  19. haven't set the root password yet, you should just press enter here.
  20. Enter current password for root (enter for none):
  21. OK, successfully used password, moving on...
  22. Setting the root password or using the unix_socket ensures that nobody
  23. can log into the MariaDB root user without the proper authorisation.
  24. You already have your root account protected, so you can safely answer 'n'.
  25. Switch to unix_socket authentication [Y/n] y
  26. Enabled successfully!
  27. Reloading privilege tables..
  28. ... Success!
  29. You already have your root account protected, so you can safely answer 'n'.
  30. Change the root password? [Y/n] y
  31. New password:
  32. Re-enter new password:
  33. Password updated successfully!
  34. Reloading privilege tables..
  35. ... Success!
  36. By default, a MariaDB installation has an anonymous user, allowing anyone
  37. to log into MariaDB without having to have a user account created for
  38. them. This is intended only for testing, and to make the installation
  39. go a bit smoother. You should remove them before moving into a
  40. production environment.
  41. Remove anonymous users? [Y/n] y
  42. ... Success!
  43. Normally, root should only be allowed to connect from 'localhost'. This
  44. ensures that someone cannot guess at the root password from the network.
  45. Disallow root login remotely? [Y/n] n
  46. ... skipping.
  47. By default, MariaDB comes with a database named 'test' that anyone can
  48. access. This is also intended only for testing, and should be removed
  49. before moving into a production environment.
  50. Remove test database and access to it? [Y/n] y
  51. - Dropping test database...
  52. ... Success!
  53. - Removing privileges on test database...
  54. ... Success!
  55. Reloading the privilege tables will ensure that all changes made so far
  56. will take effect immediately.
  57. Reload privilege tables now? [Y/n] y
  58. ... Success!
  59. Cleaning up...
  60. All done! If you've completed all of the above steps, your MariaDB
  61. installation should now be secure.
  62. Thanks for using MariaDB!
  63. 5、修改数据库配置文件,然后两台主机都重启mariadb服务
  64. Master:
  65. vim /etc/my.cnf
  66. [mysqld]
  67. #添加两行数据
  68. log_bin=mysql-bin
  69. server_id=20
  70. systemctl restart mariadb
  71. Slave:
  72. vim /etc/my.cnf
  73. [mysqld]
  74. #添加两行数据
  75. log_bin=mysql-bin
  76. server_id=30
  77. systemctl restart mariadb
  78. 6、进入数据库配置主从
  79. Master:
  80. mysql -u root -p #密码为redhat
  81. grant all privileges on *.* to root@'%' identified by "redhat";
  82. grant replication slave on *.* to 'user'@'slave' identified by 'redhat';
  83. Slave:
  84. grant all privileges on *.* to root@'%' identified by "redhat";
  85. change master to master_host='master',master_user='user',master_password='redhat';
  86. start slave;
  87. show slave status\G
  88. Slave_IO_Running: Connecting
  89. Slave_SQL_Running: Yes

 安装zabbix-agent

 slave主机中安装zabbix-agent软件包,将slave添加到zabbix web监控平台中

将server主机的zabbix.repo复制过来,接着安装zabbix-agent

更改yum源

1

2

  1. [root@slave yum.repos.d]# vim zabbix.repo
  2. [root@slave yum.repos.d]# cat zabbix.repo
  3. [zabbix]
  4. name=Zabbix Official Repository - $basearch
  5. baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rocky/9/$basearch/
  6. enabled=1
  7. gpgcheck=1
  8. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-B5333005
  9. [zabbix-non-supported]
  10. name=Zabbix Official Repository non-supported - $basearch
  11. baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/9/$basearch/
  12. enabled=1
  13. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-08EFA7DD
  14. gpgcheck=1
  15. [zabbix-sources]
  16. name=Zabbix Official Repository source code - $basearch
  17. baseurl=https://repo.zabbix.com/zabbix/7.0/rocky/9/SRPMS
  18. enabled=0
  19. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-B5333005
  20. gpgcheck=1
  21. [root@slave yum.repos.d]#

 安装zabbix-agent

yum -y install zabbix-agent

修改/etc/zabbix/zabbix_agentd.conf

  1. vim /etc/zabbix/zabbix_agentd.conf
  2. Server=192.168.100.10
  3. ServerActive=192.168.100.40
  4. Hostname=slave

重启服务

[root@slave ~]# systemctl restart zabbix-agent.service 
[root@slave ~]# systemctl enable zabbix-agent.service 

在监控平台添加主机

 

 在slave上配置脚本

  1. [root@slave ~]# cd /etc/zabbix/
  2. [root@slave zabbix]# mkdir script
  3. [root@slave zabbix]# cd script/
  4. [root@slave script]# cat mysql_slave_status.sh
  5. #!/bin/bash
  6. USER="root"
  7. PASSWD="redhat"
  8. NAME=$1
  9. function IO {
  10. Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
  11. if [ $Slave_IO_Running == "Connecting" ];then
  12. echo 0
  13. else
  14. echo 1
  15. fi
  16. }
  17. function SQL {
  18. Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
  19. if [ $Slave_SQL_Running == "Yes" ];then
  20. echo 0
  21. else
  22. echo 1
  23. fi
  24. }
  25. case $NAME in
  26. io)
  27. IO
  28. ;;
  29. sql)
  30. SQL
  31. ;;
  32. *)
  33. echo -e "Usage: $0 [io | sql]"

修改属主和权限

chmod +x mysql_slave_status.sh

chown -R zabbix.zabbix /etc/zabbix/script/

 验证脚本是否正确

  1. [root@slave script]# ./mysql_slave_status.sh io
  2. 0
  3. [root@slave script]# ./mysql_slave_status.sh sql
  4. 0
  5. [root@slave script]#

编写一个自配置文件, 里面指定上面编写的脚本的路径,然后重启服务

  1. [root@slave ~]# cd /etc/zabbix/zabbix_agentd.d/
  2. [root@slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
  3. [root@slave zabbix_agentd.d]# cat userparameter_mysql_slave.conf
  4. UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1

设置属主

  1. [root@slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf[root@slave zabbix_agentd.d]# ll
  2. total 4
  3. -rw-r--r-- 1 zabbix zabbix 73 Aug 14 11:21 userparameter_mysql_slave.conf

 重启服务

systemctl restart zabbix-agent.service

去zabbix server验证状态,使用zabbix_get命令验证,需要下载zibbix-get包

验证的结果如果是0,为正常,如果为1,则异常

  1. [root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k mysql.slave[io]
  2. 0
  3. [root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k mysql.slave[sql]
  4. 0
  5. [root@zabbix-server ~]#

在zabbix web平台配置监控项

新建监控项

1

2

 新建触发器

测试

 将mysql主从关闭

查看zabbix告警信息,验证邮箱是由能接收到邮件

mysql -u root -plinux -e "stop slave;"

告警

 邮箱

恢复mysql主从 

mysql -u root -plinux -e "start slave;"

zabbix监控主从延迟 

cd /etc/zabbix/script

#Behind:落后主库多少秒,存在秒数则出现主库复制之间的延迟   

#只要当延迟数据为NULL,以及0-200是正常的,否则其他数字输入1表示错误

  1. vim mysql_delay.sh
  2. #!/bin/bash
  3. delay=$(mysql -uroot -predhat -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
  4. if [ $delay == "NULL" ];then
  5. echo 0
  6. elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
  7. echo 0
  8. else
  9. echo $delay
  10. fi

设置属主和权限

chown -R zabbix.zabbix mysql_delay.sh

chmod +x mysql_delay.sh

配置agentd文件,并重启服务

  1. vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
  2. UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
  3. UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh

重启服务

systemctl restart zabbix-agent.service

测试 mysql_delay.sh脚本

  1. [root@slave script]# pwd
  2. /etc/zabbix/script
  3. [root@slave script]# ./mysql_delay.sh
  4. 0
  5. [root@slave script]#

在zabbix-server进行脚本测试

  1. [root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k check_mysql_delay
  2. 0

 在zabbix web平台操作

添加监控项

 验证

配置触发器

查看告警信息

邮箱 

 监控mysql主从延迟成功,将触发器的值修改回来

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/1004531
推荐阅读
相关标签
  

闽ICP备14008679号