0、常见的监控软件
cacti, nagios, zabbix, smokeping, open-falcon,prometheus,grafana
基于web的开源软件,开源监控系统状态也可以监控网络设备。Zabbix 可以自动发现主机和网络设备,支持邮件和短信告警。
C/S 架构,基于 C++ 开发,监控中心支持 web 界面配置和管理,单 server 节点可以支持上万台客户端
5 个组件:
- zabbix-server 监控中心,接收客户端上报信息,负责配置、统计、操作数据
- web UI 在 web 界面下操作配置是 zabbix 简单易用的主要原因
- zabbix-proxy 可选组件,可以代替 zabbix-server 的功能,减轻 server 的压力
- zabbix-agent 客户端软件,负责采集各个监控服务或项目的数据并上报
- 数据存储,存放数据,如 mysql
$ rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
$ yum clean all
$ yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent [zabbix-get zabbix-web]
$ mysql -uroot -p
$ mysql> create database zabbix;
$ mysql> grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'password';
$ vim /etc/zabbix/zabbix_server.conf
93 DBHost=127.0.0.1
128 DBPassword=xing/*-
$ zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -h127.0.0.1 -p zabbix
$ vim /etc/httpd/conf.d/zabbix.conf
20 php_value date.timezone Asia/Shanghai
# vim /etc/php.ini
# 878 date.timezone = Asia/Shanghai
$ vim /etc/zabbix/zabbix_agentd.conf
98 Server=127.0.0.1 //白名单,主动模式,默认 server 会到 agent 端取数据
139 ServerActive=127.0.0.1 //被动模式
$ systemctl start zabbix-server zabbix-agent httpd
$ systemctl enable zabbix-server zabbix-agent httpd
# zabbix-server 监听端口为10051
# zabbix-agentd 监听端口为10050
# 浏览器 http://192.168.6.165/zabbix 安装
# The default user name is Admin, password zabbix.
# 客户端只需安装 zabbix-agent
$ rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
$ yum install zabbix-agent -y
$ vim /etc/zabbix/zabbix_agentd.conf
98 Server=192.168.6.165 //服务端IP
139 ServerActive=192.168.6.165
$ systemctl start zabbix-agent
$ iptables -I INPUT -s 192.168.6.165 -j ACCEPT //或者放行 10050 端口
Configuration --> Host groups
Configuration --> Hosts --> Create Host
- Host name:Tester
- Groups:Linux servers
- Agent interfaces:IP address:192.168.6.128 Port:10050
Configuration --> Templates --> Create Template
- Template name:test
- Groups:Templates
- Items --> Copy --> Templates Target:test(进入一个 Templates --> Full clone 一个新模板)
进入新增的监控主机 --> Templates --> Link new templates:test --> Add
Monitoring --> Latest data --> Hosts:Tester --> Apply
# 测试监控项
$ zabbix_get -s 192.168.6.128 -k "system.cpu.load[all,avg1]"
$ zabbix_get -s 192.168.6.128 -k "system.hostname"
Monitoring --> Screens --> Create Screen --> Constructor --> 右上角 Add to Favourites
- Name:Tester
- Columns * Rows: 2 * 2
Configuration --> Hosts --> Tester:Triggers --> Create Trigger
- Name:net_if_in
- Expression:Add Conditions: {Tester:net.if.in[ens33].last()}>=10000000
- Item:Incoming network traffic on ens33
- Function:Last()-Last(most recent) T Value
- Result:>= 10000000 bit (10M)
# 设置utf8字符集
$ vim /etc/my.cnf
character_set_database = utf8
character_set_server = utf8
$ mysqldump -uroot -paminglinux --default-character-set=utf8 zabbix > zabbix.sql
$ vim zabbix.sql
:1,$s/latin1/utf8/g
$ mysql -uroot -paminglinux --default-character-set=utf8 zabbix < zabbix.sql
$ vim /usr/share/zabbix/include/defines.inc.php
68 define('ZBX_FONTPATH', realpath('fonts'));
69 define('ZBX_GRAPH_FONT_NAME', 'graphfont');
# 从 windows 系统拷贝字体并改名到 /usr/share/zabbix/fonts/graphfont.ttf
# windows:C:\Windows\Fonts\simsun.ttc
发邮件 python 脚本
$ vi mail.py
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
mail_user = '[email protected]'
mail_pass = 'xxxxxxxx'
def send_mail(to_list,subject,content):
me = "zabbix 监控告警平台"+"<"+mail_user+">"
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
s = smtplib.SMTP("smtpdm.aliyun.com", 25)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.close()
return True
except Exception,e:
print str(e)
return False
if __name__ == "__main__":
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
$ python mail.py [email protected] "测试邮件" "TEST TEST TEST"
$ vim mailto.py
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
gserver = 'smtp.qq.com'
gport = 25
try:
# msg = MIMEText(unicode(content).encode('utf-8')) //如果发送的邮件有乱码,可以尝试把这行改成如下:
msg = MIMEText(content,'plan','utf-8')
msg['from'] = mailfrom
msg['to'] = mailto
msg['Reply-To'] = mailfrom
msg['Subject'] = subject
smtp = smtplib.SMTP(gserver, gport)
smtp.set_debuglevel(0)
smtp.ehlo()
smtp.login(username,password)
smtp.sendmail(mailfrom, mailto, msg.as_string())
smtp.close()
except Exception,err:
print "Send mail failed. Error: %s" % err
def main():
to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)
sendqqmail('[email protected]','aaaaaaaaaa','[email protected]',to,subject,content)
if __name__ == "__main__":
main()
$ vim /etc/zabbix/zabbix_server.conf
520 AlertScriptsPath=/usr/lib/zabbix/alertscripts
$ mv mail.py /usr/lib/zabbix/alertscripts
$ chmod 755 mail.py
Administration --> Media types --> Create Media type
- Name:AlertAlertAlert
- Type:Script
- Script parameters:{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
Administration --> Users --> Admin (或新建用户) --> Media
- Type:AlertAlertAlert
- Send to:[email protected]
Configuration --> Actions --> Create action
- Name: 发送告警邮件
- Operations --> New
- Send to Users -> Admin
- Send only to -> AlertAlertAlert
- Conditions -> new -> Add
- Recovery operations --> New
- Send to Users -> Admin
- Send only to -> AlertAlertAlert
配置Nginx状态信息(客户端)
$ vi /usr/local/nginx/conf/vhosts/aaa.com.conf #增加
location /nginx_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
$ nginx -t
$ service nginx restart
nginx: [emerg] unknown directive "stub_status" in /usr/local/nginx/conf/vhosts/aaa.com.conf:13
# 报错 是因为 没装 modules/ngx_http_stub_status_module.o 模块
# 解决 在编译安装时 加参数 --with-http_stub_status_module
# 测试
$ curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
字段 | 含义 |
---|---|
Active Connection | 当前活动连接数,其中也包括了等待状态的连接 |
accepts | 接收到的连接数 |
handled | 已经处理完的连接数,该数字一般和accepts一致,如果不一致那么说明Nginx出错了 |
requests | 总共处理的请求数,一个连接可以有多个请求,所以该值比accpets要大 |
Reading | 正在读取请求头信息的连接数 |
Writing | 正在发送响应报文的连接数 |
Waiting | 处于闲置状态,等待客户端发送请求的连接数 |
监控脚本(客户端)
$ vim /usr/local/sbin/ngx_status.sh
#!/bin/bash
url="http://127.0.0.1/nginx_status"
curl=/usr/bin/curl
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
$curl $url 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
$curl $url 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
$curl $url 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
$curl $url 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $3}'
}
$1
$ chmod 755 /usr/local/sbin/ngx_status.sh
$ vim /etc/zabbix/zabbix_agentd.conf # 增加
297 UserParameter=nginx.status[*],/usr/local/sbin/ngx_status.sh $1
# nginx.status 自定义名称
# [*] 表示有参数
$ systemctl restart zabbix-agent
# 在服务端执行测试
$ zabbix_get -s 192.168.222.129 -k 'nginx.status[accepts]
链接模板
Configuration --> Templates --> Import --> Import file:zabbix_nginx_template.xml
Configuration --> Hosts --> Tester --> Templates --> Link new templates:Template App NGINX
https://github.com/ziluoxingjun/linux2019/tree/master/zabbix_nginx_template
在 zabbix_server 机器上安装 zabbix_java_gateway
$ yum install -y zabbix-java-gateway
$ vim /etc/zabbix/zabbix_java_gateway.conf
9 LISTEN_IP="0.0.0.0"
17 LISTEN_PORT=10052
35 START_POLLERS=5
$ vim /etc/zabbix/zabbix_server.conf
292 JavaGateway=127.0.0.1
300 JavaGatewayPort=10052
308 StartJavaPollers=5
$ systemctl start zabbix-java-gateway
$ systemctl start zabbix-server
开启JMX
$ vim /usr/local/tomcat/bin/catalina.sh # 在#!/bin/sh下增加
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.6.165
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"
# 重启tomcat
$ /usr/local/tomcat/bin/shutdown.sh
$ /usr/local/tomcat/bin/startup.sh
添加 Host 和链接模板
Configuration -> Hosts -> Create host
- Host name:Tester_tomcat
- Groups:Linux servers
- Agent interfaces:Remove
- JMX interfaces:IP address:127.0.0.1 Port:9999
Templates --> Link new templates:Template App Apache Tomcat JMX,Template App Generic Java JMX
$ vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
$ mysql -uroot -p
$ mysql> grant usage, process, replication client on *.* to 'mon'@'localhost' identified by 'xing/*-';// 授权一个监控用户
$ mkdir /var/lib/zabbix
$ vim /var/lib/zabbix/.my.cnf
[mysql]
host=localhost
user=mon
password='xing/*-'
socket=/tmp/mysql.sock
[mysqladmin]
host=localhost
user=mon
password='xing/*-'
socket=/tmp/mysql.sock
$ systemctl restart zabbix-agent
# 测试
$ zabbix_get -s 127.0.0.1 -p 10050 -k mysql.ping
$ zabbix_get -s 127.0.0.1 -p 10050 -k mysql.version
$ zabbix_get -s 127.0.0.1 -p 10050 -k mysql.size
# 报错:sh: mysqladmin: 未找到命令
# 解决:
$ ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/
$ vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
17 /usr/local/mysql/bin/mysqladmin
添加 Host 和链接模板
Configuration -> Hosts -> Create host
- Host name:Tester_mysql
- Groups:Linux servers
- Agent interfaces:address:127.0.0.1 Port:10050
Templates --> Link new templates:Template DB MySQL