轻量级自动化监控 | 基于MySQL+PHP+Python
简介
一款轻量级os系统可视化监控指标工具,采集的指标有cpu idle空闲使用率,cpu load负载使用率,内存使用率,磁盘空间使用率。
(其他监控指标可以根据需求添加)
整个监控流程:
Agent端从Server端os_status_info表中,获取被监控主机的各项系统阀值,采集客户端主机资源信息完成入库和报警,
Server端用来监控客户端主机ssh是否存活和页面信息展示,可实现微信和邮件报警。
-----------------------------------------------------------------------------
可视化界面
1、监控首页
可以查看主机的存活状态,也可以点击图表,查看历史曲线图
2、cpu idle空闲使用率
4、内存使用率
5、不同的磁盘空间使用率
微信报警和邮件报警有兴趣的同学可以深入研究
-----------------------------------------------------------------------------
一、环境搭建
【监控管理端】
# yum install httpd MysqL PHP PHP-MysqL MysqL-server* -y
# yum -y install perl-IO-Socket-SSL openssl-perl openssl-devel python-simplejson -y
# service httpd restart
# servuce MysqLd restart
【被监控端】
# yum install PHP PHP-MysqL -y
# yum -y install perl-IO-Socket-SSL openssl-perl openssl-devel python-simplejson -y
二、os_monitor监控工具搭建
【监控管理端】
1、把os_monitor.zip
安装包解压缩到/var/www/html/目录下
# cd /var/www/html/os_monitor/
# chmod 755 ./mail/sendEmail
# chmod 755 ./weixin/wechat.py
(注:邮件和微信报警调用的第三方工具,所以这里要赋予可执行权限755)
2、导入os_monitor监控工具表结构(testdb库)
# cd /var/www/html/os_monitor/
创建testdb数据库
MysqL -uroot -p123
create database testdb
use testdb
# MysqL -uroot -p123 testdb < os_monitor_schema.sql
3、录入被监控主机的信息
1 MysqL> insert into os_status_info 2 (host, ssh_port, tag, monitor, send_mail, send_mail_to_list, send_weixin, send_weixin_to_list, threshold_alarm_cpu_idle, threshold_alarm_cpu_load, threshold_alarm_memory_usage, threshold_alarm_disk_free) 3 values 4 ('127.0.0.1', 22, 'testdb', 1, 1, 'xxx@163.com', 1, 'shujuyr', 60, 6, 80, 85);
PS:以下字段可以按照需求变更:
host字段含义:输入被监控主机的IP地址
ssh_port字段含义:输入被监控主机的ssh端口
tag字段含义:输入被监控主机的名字
monitor字段含义:0为关闭监控(也不采集数据,直接跳过);1为开启监控(采集数据)
send_mail字段含义:0为关闭邮件报警;1为开启邮件报警
send_mail_to_list字段含义:邮件人列表,多个邮件用逗号分隔
send_weixin字段含义:0为关闭微信报警;1为开启微信报警
send_weixin_to_list字段含义:微信公众号
threshold_alarm_cpu_idle字段含义:设置空闲cpu使用率阀值,即cpu处于空闲状态时间比例
threshold_alarm_cpu_load字段含义:设置cpu load负载使用率阀值
threshold_alarm_memory_usage字段含义:设置memory内存使用率阀值
threshold_alarm_disk_free字段含义:设置磁盘空间使用率阀值
# vim /var/www/html/os_monitor/conn.PHP
$conn = MysqLi_connect("127.0.0.1","root","passwd","testdb","3306") or die("数据库链接错误" . PHP_EOL
.MysqLi_connect_error());
改成你的os_monitor监控工具表结构(testdb库)连接信息
5、crontab定时任务每分钟抓取一次
*/1 * * * * cd /var/www/html/os_monitor/; /usr/bin/PHP /var/www/html/os_monitor/check_os_server.PHP > /dev/null 2 >&1
*/1 * * * * cd /var/www/html/os_monitor/; /usr/bin/PHP /var/www/html/os_monitor/check_os_agent.PHP > /dev/null 2 >&1
# vim os_status_monitor.PHP
http-equiv="refresh" content="600"
7、页面访问
http://yourIP/os_monitor/os_status_monitor.PHP
-----------------------------------------------------------------------------
【被监控端Agent】
需要check_os_agent.PHP和conn.PHP文件
crontab定时任务每分钟抓取一次
*/1 * * * * cd /var/www/html/os_monitor/; /usr/bin/PHP /var/www/html/os_monitor/check_os_agent.PHP > /dev/null 2 >&1
下载地址
https://pan.baidu.com/s/1oqMwoRKl7yIAPi8eRq0R5w