zabbix项目实战

第一步
到了企业中如果没有监控,我们要进行那几个方面规划,要写一个项目案例,项目案例分类,实施,成本,ip规划,硬件,配置,架构图这是一个项目实施准备,给boss看

zabbix可以对mysql,tomcat,nginx,php实现监控。

先弄个项目规划

主机分组 (名字:linux-MySQL-node01)

  • 交换机(网络设备,内网的,交换机也需要监控的)
  • nginx(web方向,反向代理)
  • tomcat
  • MySQL
  • Apache
  • php-fpm
  • redis
  • memcache

监控对象识别:

  • 使用SNMP监控交换(交换机也是能开启的)
  • 使用IPMI监控服务器硬件(监控服务器硬件的温度,主板,内存条)
  • 使用agent监控服务器(这些软件服务都可以使用agent来进行监控,agent主被动监控的,是有soket进程的)
  • 使用jmx监控java (监控tomcat等java服务的)
SNMP监控流程

1.交换机上开启snmp
2.在zabbix上添加监控(设置SNMP interfaces)
3.关联监控模版

IPMI

建议使用自定义item监控服务器硬件,在本地执行ipmitool命令来获取数据

可以在本地执行ipmitool命令获取数据(命令需要安装)

安装上了命令但是还是获取不了数据,关键有一个东西没有,那是真的服务器上是要有一个东西的叫做远程控制卡这个东西是要服务器硬件提供数据输出的,找远程控制卡来获取数据的

[root@localhost ~]# yum install -y ipmitool		
[root@localhost ~]# ipmitool 
No command provided!
Commands:
	raw           Send a RAW IPMI request and print response
	i2c           Send an I2C Master Write-Read command and print response
	spd           Print SPD info from remote I2C device
	lan           Configure LAN Channels
	chassis       Get chassis status and set power state
	power         Shortcut to chassis power commands
	event         Send pre-defined events to MC
	mc            Management Controller status and global enables
	sdr           Print Sensor Data Repository entries and readings
	sensor        Print detailed sensor information
	fru           Print built-in FRU and scan SDR for FRU locators
	gendev        Read/Write Device associated with Generic Device locators sdr
	sel           Print System Event Log (SEL)
	pef           Configure Platform Event Filtering (PEF)
	sol           Configure and connect IPMIv2.0 Serial-over-LAN
	tsol          Configure and connect with Tyan IPMIv1.5 Serial-over-LAN
	isol          Configure IPMIv1.5 Serial-over-LAN
	user          Configure Management Controller users
	channel       Configure Management Controller channels
	session       Print session information
	sunoem        OEM Commands for Sun servers
	kontronoem    OEM Commands for Kontron devices
	picmg         Run a PICMG/ATCA extended cmd
	fwum          Update IPMC using Kontron OEM Firmware Update Manager
	firewall      Configure Firmware Firewall
	delloem       OEM Commands for Dell systems
	shell         Launch interactive IPMI shell
	exec          Run list of commands from file
	set           Set runtime variable for shell and exec
	hpm           Update HPM components using PICMG HPM.1 file
	ekanalyzer    run FRU-Ekeying analyzer using FRU files
JMX(使用Zabbix Java Gateway代理)

用来获取java数据的

zabbix监控MySQL数据库

编写监控脚本
MySQL_USER="root"
MySQL_PWD="123123"
MySQL_HOST="127.0.0.1"
MySQL_PORT="3306"
MySQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MySQL_USER} -h${MySQL_HOST} -P${MySQL_PORT} -p${MySQL_PWD}"
if [ $# -ne "1" ];then
    echo "arg error!"
fi
case $1 in
	Uptime)
		result=`${MySQL_CONN} status | cut -f2 -d":" | cut -f1 -d "T"`		#cut相当于是缩减版的awk	-d指定分隔符,-f是取第几咧 | 以t作为分割
		echo $result
        ;;
    Com_update)
        result=`${MySQL_CONN} extended-status | grep -w "Com_update" | cut -d"|" -f3`
        echo $result
		;;
	Slow_querles)
		result=`${MySQL_CONN} status | cut -f5 -d":" | cut -f1 -d"O"`
		echo $result
        ;;
    Com_select)
        result=`${MySQL_CONN} extended-status | grep -w "Com_select" | cut -d "|" -f3`
        echo $result
		;;
	Com_rollback)
		result=`${MySQL_CONN} extended-status | grep -w "Com_rollback" | cut -d"|" -f3`
		echo $result
        ;;
    Questions)
        result=`${MySQL_CONN} status | cut -f4 -d":" | cut -f1 -d"S"`
        echo $result
		;;
	Com_insert)
		result=`${MySQL_CONN} extended-status | grep -w "Com_insert" | cut -d"|" -f3`
		echo $result
        ;;
    Com_delete)
        result=`${MySQL_CONN} extended-status | grep -w "Com_delete" | cut -d"|" -f3`
        echo $result
		;;
	Com_commit)
		result=`${MySQL_CONN} extended-status | grep -w "Com_commit" | cut -d"|" -f3`
		echo $result
        ;;
    Bytes_sent)
        result=`${MySQL_CONN} extended-status | grep -w "Bytes_sent" | cut -d"|" -f3`
        echo $result
		;;
	Bytes_received)
		result=`${MySQL_CONN} extended-status | grep -w "Bytes_received" | cut -d"|" -f3`
		echo $result
        ;;
    Com_begin)
        result=`${MySQL_CONN} extended-status | grep -w "Com_begin" | cut -d"|" -f3`
        echo $result
		;;
	*)
		echo "Usage:$0(Uptime|Com_update|Slow_querles|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
        ;;
esac
在zabbix-agent端创建自定义键值配置文件
    #创建mysql.status的键值
    [root@nginx scripts]# chmod +x check_mysql.sh 
    [root@nginx scripts]# chown zabbix.zabbix check_mysql.sh
    [root@nginx zabbix]# cd /etc/zabbix/zabbix_agentd.d/
    [root@nginx zabbix_agentd.d]# cat mysql_status.conf 
    UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
    #删除旧键值模版
    [root@nginx zabbix_agentd.d]# rm -rf userparameter_mysql.conf 
    #创建mysql.ping和mysql.version的键值
    [root@nginx zabbix_agentd.d]# cat mysql_status.conf 
    UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
    UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p123123 ping | grep -c alive
    UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V
    #重启zabbix-agentd服务
    [root@nginx zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
    Shutting down Zabbix agent:                                [  OK  ]
    Starting Zabbix agent:                                     [  OK  ]
在zabbix-server端测试键值
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.status[Uptime]"
92
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.status[Com_commit]"
70
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.status[Bytes_sent]"
218317
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.status[Bytes_received]"
86664
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.ping"
1
[root@localhost zabbix_agentd.d]# zabbix_get -s 192.168.182.137 -p 10050 -k "mysql.version"
/usr/local/mysql/bin/mysql  Ver 14.14 Distrib 5.5.32, for linux2.6 (x86_64) using readline 5.1
在zabbix-server端Web前台引入zabbix监控MySQL的模版

zabbix自带监控MySQL的模版,我们只需要将模版引入到主机即可
配置–>主机–>选择主机–>模板,然后将MySQL模版连接到此主机即可

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

实战监控Apache

源码编译构建apache服务
    #为了避免冲突,若系统中已经安装httpd服务,务必删除
    [root@apache ~]# rpm -q httpd
    package httpd is not installed
    [root@apache ~]# /etc/init.d/iptables stop
    [root@apache ~]# setenforce 0
    setenforce: SELinux is disabled
    #源码构建apache
    [root@apache httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
    [root@apache httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
    [root@apache httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
    [root@apache httpd-2.2.17]# cd /usr/local/httpd/
    [root@apache httpd]# ls
    bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
    [root@apache httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
    [root@apache httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
    [root@apache httpd]# chmod +x /etc/init.d/httpd
    [root@apache ~]# cd /usr/local/httpd/htdocs/
    [root@apache htdocs]# echo "`hostname -I` I am apache" > index.html
    [root@localhost htdocs]# cat index.html 
    192.168.182.137  I am apache
    [root@localhost conf]# cd /usr/local/httpd/conf/
    [root@apache conf]# awk '/#ServerName/{print NR,$0}' httpd.conf 
    97 #ServerName www.example.com:8081
    [root@apache conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf 
    [root@apache conf]# sed -n '97p' httpd.conf 
    ServerName www.yunjisuan.com:8081
    [root@apache conf]# /etc/init.d/httpd start
    [root@localhost conf]# ss -antup | grep 8081
    tcp    LISTEN     0      128                   :::8081                 :::*      users:(("httpd",55012,4),("httpd",55013,4),("httpd",55014,4),         	("httpd",55015,4),("httpd",55016,4),("httpd",55017,4))
    [root@localhost conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
    [root@localhost conf]# curl www.yunjisuan.com:8081
     192.168.182.137  I am apache    
开启apache状态页

要打开apache的ServerStatus页面,需要在httpd.conf文件最下边加入代码段

    ExtendedStatus On
    <location /server-status>
    SetHandler server-status
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    </location>

最后,重启动apache即可

下载apache zabbix模版并解压
    #从如下地址下载apache zabbix模版:
    [root@apache ~]# wget https://github.com/lorf/zapache/archive/master.zip
    #解压zip格式压缩文件
    [root@localhost ~]# yum install  -y unzip
    [root@apache ~]# unzip master
    [root@apache ~]# ls -d zapache-master
    zapache-master
    [root@apache ~]# cd zapache-master/
    [root@apache ~]# tree zapache-master/
    zapache-master/
    ├── httpd-server-status.conf.sample
    ├── README.md
    ├── userparameter_zapache.conf.sample
    ├── zapache
    ├── zapache-template-active.xml
    └── zapache-template.xml
    0 directories, 6 files
  • 此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。
  • 首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:
    [root@apache zapache-master]# chmod 755 /etc/zabbix/scripts/zapache     #如果没有scripts那就创建这个目录
    [root@apache scripts]# cd /etc/zabbix/scripts/
    [root@apache scripts]# ./zapache 
    zapache version: 1.5
    usage:
      ./zapache [<url>] TotalAccesses                 - Check total accesses.
      ./zapache [<url>] TotalKBytes                   - Check total KBytes.
      ./zapache [<url>] CPULoad                       - Check CPU load.
      ./zapache [<url>] Uptime                        - Check uptime.
      ./zapache [<url>] ReqPerSec                     - Check requests per second.
      ./zapache [<url>] BytesPerSec                   - Check Bytes per second.
      ./zapache [<url>] BytesPerReq                   - Check Bytes per request.
      ./zapache [<url>] BusyWorkers                   - Check busy workers.
      ./zapache [<url>] IdleWorkers                   - Check idle workers.
      ./zapache [<url>] version                       - Version of this script.
      ./zapache [<url>] ping                          - Check if Apache is up.
      ./zapache [<url>] WaitingForConnection          - Check Waiting for Connection processess.
      ./zapache [<url>] StartingUp                    - Check Starting Up processess.
      ./zapache [<url>] ReadingRequest                - Check Reading Request processess.
      ./zapache [<url>] SendingReply                  - Check Sending Reply processess.
      ./zapache [<url>] KeepAlive                     - Check KeepAlive Processess.
      ./zapache [<url>] DNSLookup                     - Check DNSLookup Processess.
      ./zapache [<url>] ClosingConnection             - Check Closing Connection Processess.
      ./zapache [<url>] Logging                       - Check Logging Processess.
      ./zapache [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess.
      ./zapache [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess.
      ./zapache [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process.
  • 接着将userparameter_zapache.conf.sample文件放到/etc/zabbix/zabbix_agentd.d目录下,并重命名为userparameter_zapache.conf文件,然后修改键值命令的路径。
  • 最后,重启zabbix-agent服务
    [root@apache zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
    [root@apache zabbix_agentd.d]# tail -1 userparameter_zapache.conf 
    UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1    #本行要修改
    [root@apache zapache-master]# /etc/init.d/zabbix-agent restart
    Shutting down Zabbix agent:                                [  OK  ]
    Starting Zabbix agent:                                     [  OK  ]
测试键值
    #在zabbix-agent端查看脚本用法
    [root@apache zabbix_agentd.d]# cd /etc/zabbix/scripts/
    [root@apache scripts]# ./zapache 
    zapache version: 1.5
    usage:
      ./zapache [<url>] TotalAccesses                 - Check total accesses.
      ./zapache [<url>] TotalKBytes                   - Check total KBytes.
      ./zapache [<url>] CPULoad                       - Check CPU load.
      ./zapache [<url>] Uptime                        - Check uptime.
      ./zapache [<url>] ReqPerSec                     - Check requests per second.
      ./zapache [<url>] BytesPerSec                   - Check Bytes per second.
      ./zapache [<url>] BytesPerReq                   - Check Bytes per request.
      ./zapache [<url>] BusyWorkers                   - Check busy workers.
      ./zapache [<url>] IdleWorkers                   - Check idle workers.
      ./zapache [<url>] version                       - Version of this script.
      ./zapache [<url>] ping                          - Check if Apache is up.
      ./zapache [<url>] WaitingForConnection          - Check Waiting for Connection processess.
      ./zapache [<url>] StartingUp                    - Check Starting Up processess.
      ./zapache [<url>] ReadingRequest                - Check Reading Request processess.
      ./zapache [<url>] SendingReply                  - Check Sending Reply processess.
      ./zapache [<url>] KeepAlive                     - Check KeepAlive Processess.
      ./zapache [<url>] DNSLookup                     - Check DNSLookup Processess.
      ./zapache [<url>] ClosingConnection             - Check Closing Connection Processess.
      ./zapache [<url>] Logging                       - Check Logging Processess.
      ./zapache [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess.
      ./zapache [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess.
      ./zapache [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process.

我们发现脚本需要传递两个参数,即url和选项
然后我们在zabbix-server端进行键值测试

    [root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"
    9
    [root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com version]"
    1.5
    [root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com ping]"
    1
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值