在目标主机上安装zabbix_agentd用与收集信息
#安装依赖
yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel libxslt-devel
# zabbix仓库连接 http://repo.zabbix.com/
#部署yum源
rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
#安装
yum install -y zabbix-agent
#编辑命令
vim /etc/zabbix/zabbix_agentd.conf
#配置内容
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
DebugLevel=3
ListenPort=10050
ListenIP=192.168.44.101
Hostname=192.168.44.101
#zabbix server 地址
Server=192.168.44.100
#主动监测 zabbix server地址
ServerActive=192.168.44.100
Include=/etc/zabbix/zabbix_agentd.d/*.conf
# UserParameter=
#开机自启
systemctl enable zabbix-agent
#Agentd启停相关
systemctl start zabbix-agent
systemctl stop zabbix-agent
systemctl restart zabbix-agent
#查看进程
ps -ef|grep zabbix
#连通性测试 zabbix-server主机上测试
/usr/local/zabbix/bin/zabbix_get -s <被监控主机IP> -p 10050 -k "system.hostname"
WEB 端配置监控主机
模板配置
Email 告警
自定义脚本告警sendEmail
#安装mailx
yum install -y mailx dos2unix
#配置邮件服务器
vim /etc/mail.rc
#Mail Server info
set [email protected] smtp=smtp.163.com
set [email protected] smtp-auth-password=xxxxxxx123
set smtp-auth=login
#测试邮件发送
echo "邮件内容" |mail -s "邮件标题" [email protected]
#在zabbix_server.conf中配置脚本路径
vim /usr/local/zabbix/etc/zabbix_server.conf
#指定脚本目录
AlertScriptsPath=${datadir}/zabbix/alertscripts
#创建脚本
cd /usr/local/zabbix/share/zabbix/alertscripts/
touch sendMail.sh
vim sendMail.sh
#脚本内容
#!/bin/bash
to=$1
subject=$2
body=$3
FILE=/tmp/mail.tmp
echo "$body" > $FILE
# 解决正文变成附件.bin
dos2unix -k $FILE
mail -s "$subject" "$to" < $FILE
#加入可执行权限
chmod +x sendMail.sh
#为mail授予zabbix权限
chown zabbix.zabbix /tmp/mail.tmp
#测试脚本
./sendMail.sh 目标邮件地址 邮件标题 '邮件内容'
./sendMail.sh [email protected] zabbix 'zabbix 123456'
钉钉告警配置
#钉钉机器人
https://oapi.dingtalk.com/robot/sendaccess_token=97cbd23c133e7bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#安装依赖
yum install -y python-requests
#创建脚本
cd /usr/local/zabbix/share/zabbix/alertscripts/
touch DingDing.py
vim DingDing.py
#脚本内容
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
headers = {'Content-Type': 'application/json;charset=utf-8'}
#钉钉机器人地址
api_url = "https://oapi.dingtalk.com/robot/send?access_token=97cbd23c133e7bbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
def msg(text):
json_text= {
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
"166..."
],
"isAtAll": False
}
}
print requests.post(api_url,json.dumps(json_text),headers=headers).content
if __name__ == '__main__':
text = sys.argv[1]
msg(text)
#加入可执行权限
chmod +x DingDing.py
./DingDing.py 'Problem'
#上传字体并拷贝至目标目录
cp STXINWEI.TTF /usr/local/nginx/html/fonts/
#重命名
mv STXINWEI.TTF DejaVuSans.ttf
Tomcat 指标监控 【堆内存 类加载 线程 GC】
为Tomcat开启JMX监控
#编辑命令
vim catalina.sh
#开启JMX监控
CATALINA_OPTS="-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.44.101
-Dcom.sun.management.jmxremote.port=12355
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"
#查看监听端口
netstat -antp|grep 12355
#启动查看zabbix命令目录
cd /usr/local/zabbix/sbin/ && ll
cd zabbix_java
./startup.sh
ps -ef|grep zabbix_java
#监听端口
netstat -antp|grep 10052
#配置zabbix_server 连接java
vim /usr/local/zabbix/etc/zabbix_server.conf
#配置内容
JavaGateway=192.168.44.100
JavaGatewayPort=10052
#配置Java线程数
StartJavaPollers=4
#重启动zabbix_server
systemctl restart zabbix_server
#创建脚本
touch userparameter_mysql.conf
#脚本内容
# 连接数
UserParameter=mysql.max_connections,echo "show variables where Variable_name='max_connections';" | mysql -N | awk '{print $2}'
UserParameter=mysql.current_connections,echo "show global status where Variable_name='Threads_connected';" | mysql -N | awk '{print $2}'
# 缓冲池
UserParameter=mysql.buffer_pool_size,echo "show variables where Variable_name='innodb_buffer_pool_size';" | mysql -N |awk '{printf "%.2f",$2/1024/1024/1024}'
UserParameter=mysql.buffer_pool_usage_percent,echo "show global status where Variable_name='Innodb_buffer_pool_pages_free' or Variable_name='Innodb_buffer_pool_pages_total';" | mysql -N | awk '{a[NR]=$2}END{printf "%.1f",100-((a[1]/a[2])*100)}'
# 增删改查
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | mysql -N | awk '{print $$2}'
# 实例状态
UserParameter=mysql.ping,HOME=/usr/local/zabbix_agent mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
#编辑命令
vim /etc/my.cnf
#内容
[mysql]
default-character-set=utf8
host=192.168.44.101
user=zabbix_sql
password=SYS_666_zabbix_sql
[mysqladmin]
host=192.168.44.101
user=zabbix_sql
password=SYS_666_zabbix_sql
#获取链接数
echo "show variables where Variable_name='max_connections';" | mysql -N | awk '{print $2}'
#查看zabbix_agentd配置信息
cat /etc/zabbix/zabbix_agentd.conf
#拷贝
mv userparameter_mysql.conf /etc/zabbix/zabbix_agentd.conf.d/
#重启agentd
systemctl restart zabbix-agent
参数 | 描述 |
---|---|
active | 当前活动的客户端链接数,包括waiting等待数 |
accepts | 已接受客户端连接数 |
handled | 已处理的连接总数 |
requests | 客户端请求的总数 |
reading | 正在读取请求头的当前连接数 |
writing | 将响应写会客户端的当前连接数 |
waiting | 等待请求空闲客户端的当前连接数 |
#编辑配置
vim /usr/local/nginx/conf/nginx.conf
#开启nginx状态手机
server {
listen 8099;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.44.101;
allow 1127.0.0.1;
deny all;
}
}
#重启Nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl http://127.0.0.1:8099/nginx_status
#创建脚本
mkdir -p /opt/zabbix_scripts
cd /opt/zabbix_scripts && touch nginx_status.sh
vim nginx_status.sh
#脚本内容
#!/bin/bash
result="/usr/bin/curl -s http://127.0.0.1:8099/nginx_status"
case $1 in
active)
$result |awk '/Active/ {print $NF}'
;;
accepts)
$result |awk 'NR==3 {print $1}'
;;
handled)
$result |awk 'NR==3 {print $2}'
;;
requests)
$result |awk 'NR==3 {print $3}'
;;
reading)
$result |awk '/Reading/ {print $2}'
;;
writing)
$result |awk '/Writing/ {print $4}'
;;
waiting)
$result |awk '/Waiting/ {print $6}'
;;
*)
echo "USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac
#授权
chmod +x nginx_status.sh
#编辑命令
vim /etc/zabbix/zabbix_agentd.conf
#配置脚本位置
UserParameter=nginx.status[*],/opt/zabbix_scripts/nginx_status.sh $1
#重启zabbix_agentd
systemctl restart zabbix-agent
TCP11种状态信息
配置TCP监听信息
#编辑命令
vim /etc/zabbix/zabbix_agentd.conf
#配置参数
UserParameter=tcp.status[*],ss -antp | awk '{a[$$1]++}END{print a["'$1'"]}'
#重启agentd
systemctl restart zabbix-agent
/usr/local/zabbix/bin/zabbix_get -s 192.168.44.101 -p 10050 -k 'tcp.status[LISTEN]'
#安装Ansible
yum install -y openssh-server ansible
#配置目标主机
vim /etc/ansible/hosts
#可配置多个目标主机IP
[webservers]
192.168.44.102
#生成秘钥
ssh-keygen
#拷贝秘钥至目标主机
ssh-copy-id 目标主机IP
#切换目录
cd /opt/tel
touch zabbix_agent.yml
vim zabbix_agent.yml
#内容
---
- hosts: webservers
vars:
- zabbix_server: 192.168.44.100
tasks:
- name: Install zabbix agent - CentOS6
yum: name=https://hf-file-bucket.oss-cn-beijing.aliyuncs.com/zabbix/4.0/rhel/6/zabbix-agent-4.0.0-2.el6.x86_64.rpm state=present
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
- name: Install zabbix agent - CentOS7
yum: name=https://hf-file-bucket.oss-cn-beijing.aliyuncs.com/zabbix/4.0/rhel/7/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
- name: Copy zabbix agent configuration file
template: src=/opt/tel/zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
- name: Start zabbix agent
service: name=zabbix-agent state=started enabled=true
#切换目录
cd /opt/tel
touch zabbix_agentd.conf.j2
vim zabbix_agentd.conf.j2
#内容
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
DebugLevel=3
Server={{ zabbix_server }}
ListenPort=10050
ListenIP={{ ansible_default_ipv4.address }}
ServerActive={{ zabbix_server }}
Hostname={{ ansible_default_ipv4.address }}
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
#TCP链接
UserParameter=tcp.status[*],ss -antp |awk '{a[$$1]++}END{print a["'$1'"]}'
#远程安装
ansible-playbook /opt/tel/zabbix_agent.yml
#编辑
vim /etc/zabbix/zabbix_agentd.conf
#内容
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
DebugLevel=3
Server=192.168.44.100
ListenPort=10050
ListenIP=192.168.44.102
ServerActive=192.168.44.100
Hostname=192.168.44.102
#元数据信息
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
#TCP链接
UserParameter=tcp.status[*],ss -antp |awk '{a[$$1]++}END{print a["'$1'"]}'