Skip to content

Latest commit

 

History

History
406 lines (339 loc) · 12.7 KB

监控平台.md

File metadata and controls

406 lines (339 loc) · 12.7 KB

Linux 监控

0、常见的监控软件

cacti, nagios, zabbix, smokeping, open-falcon,prometheus,grafana

1、zabbix 监控介绍

基于web的开源软件,开源监控系统状态也可以监控网络设备。Zabbix 可以自动发现主机和网络设备,支持邮件和短信告警。
C/S 架构,基于 C++ 开发,监控中心支持 web 界面配置和管理,单 server 节点可以支持上万台客户端

5 个组件:

  • zabbix-server 监控中心,接收客户端上报信息,负责配置、统计、操作数据
  • web UI 在 web 界面下操作配置是 zabbix 简单易用的主要原因
  • zabbix-proxy 可选组件,可以代替 zabbix-server 的功能,减轻 server 的压力
  • zabbix-agent 客户端软件,负责采集各个监控服务或项目的数据并上报
  • 数据存储,存放数据,如 mysql

2、zabbix 4.0 安装

$ 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 端口

3、监控客户机

添加主机

Configuration --> Host groups

Configuration --> Hosts --> Create Host

  1. Host name:Tester
  2. Groups:Linux servers
  3. Agent interfaces:IP address:192.168.6.128 Port:10050

管理模板

Configuration --> Templates --> Create Template

  1. Template name:test
  2. Groups:Templates
  3. 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

  1. Name:Tester
  2. Columns * Rows: 2 * 2

配置触发器

Configuration --> Hosts --> Tester:Triggers --> Create Trigger

  1. Name:net_if_in
  2. Expression:Add Conditions: {Tester:net.if.in[ens33].last()}>=10000000
    1. Item:Incoming network traffic on ens33
    2. Function:Last()-Last(most recent) T Value
    3. Result:>= 10000000 bit (10M)

4、中文名称不能写数据库

# 设置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

5、图形中文乱码

$ 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

6、zabbix 邮件告警

发邮件 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()

配置 zabbix_server 配置文件

$ vim /etc/zabbix/zabbix_server.conf
520 AlertScriptsPath=/usr/lib/zabbix/alertscripts
$ mv mail.py /usr/lib/zabbix/alertscripts
$ chmod 755 mail.py

zabbix WEB UI 设置告警

设置报警媒介

Administration --> Media types --> Create Media type

  1. Name:AlertAlertAlert
  2. Type:Script
  3. Script parameters:{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}

设置接收邮件地址

Administration --> Users --> Admin (或新建用户) --> Media

  1. Type:AlertAlertAlert
  2. Send to:[email protected]

配置动作

Configuration --> Actions --> Create action

  1. Name: 发送告警邮件
  2. Operations --> New
    1. Send to Users -> Admin
    2. Send only to -> AlertAlertAlert
    3. Conditions -> new -> Add
  3. Recovery operations --> New
    1. Send to Users -> Admin
    2. Send only to -> AlertAlertAlert

7、zabbix 监控 nginx

配置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

8、zabbix 监控 Tomcat

在 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

  1. Host name:Tester_tomcat
  2. Groups:Linux servers
  3. Agent interfaces:Remove
  4. JMX interfaces:IP address:127.0.0.1 Port:9999

Templates --> Link new templates:Template App Apache Tomcat JMX,Template App Generic Java JMX

9、zabbix 监控 MySQL

$ 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

  1. Host name:Tester_mysql
  2. Groups:Linux servers
  3. Agent interfaces:address:127.0.0.1 Port:10050

Templates --> Link new templates:Template DB MySQL