huijial 发表于 2019-1-18 12:53:50

监控三剑客

监控三剑客
  在企业网络运维过程中,管理员闭虚随时关注服务器和网络运行情况,以便及时发现问题,尽可能减少故障的发生。当网络中的设备、服务器数量较多时,为了更加方便,快捷的获得各种监控信息,通常会借助一些集中监测软件。

下面为大家介绍一下日常生活中的三种监控软件,俗称:监控三剑客(基于CentOS7安装)

一、cacti
  cacti是一款使用PHP语言开的发性能与流量检测软件,检测对象可以是Linux、Windows服务器,也可以是路由交换机等网络设备,主要基于SNMP(simple、Network、Management、Protocol,简单网络管理协议)来搜集CPU占用、内存使用、运行进程、网卡流量等数据

1、服务器配置
  构建cacti监控平台时要先进行LAMP架构,这边实验我直接用yum一键安装、在生产环境中最好使用手工编译安装
  LAMP手工编译安装可以参考以下博客文章
  Apache手工编译安装
http://blog.运维网.com/13645280/2117794
  MySQL数据库基础
http://blog.运维网.com/13645280/2132869
  构建PHP运行环境
http://blog.运维网.com/13645280/2136415
  (1)yum安装LAMP

yum -y install httpd
yum -y install mariadb mariadb-server
yum -y install php php-mysql
#编写php首页进行测试机
cd /var/www/html/
vim index.php

#关闭防火墙、SELinux
systemctl stop firewalld
setenforce 0
  网页测试截图
http://i2.运维网.com/images/blog/201808/23/ef7418bb674a95175b1b78671bd83566.png
  (2)安装工具包,snmp协议,rrdtool

yum -y install zlib freetype libjpeg fontconfig gd libxml2 php-gd
yum -y install net-snmp net-snmp-utils
yum -y install rrdtool rrdtool-devel
  (3)安装cacti监控源码包

#用wget下载最新的源码包
wget https://www.cacti.net/downloads/cacti-1.1.38.tar.gz
#解压到/var/www/html底下然后用mv重命名为cacti
tar zxvf cacti-1.1.38.tar.gz -C /var/www/html/
mv /var/www/html/cacti-1.1.38/ /var/www/html/cacti
#创建cacti管理用户
useradd -M -s /sbin/nologin cacti
#修改cacti属主和数组
chown -R cacti.cacti /var/www/html/cacti/
#修改cacti底下include底下config.php文件
$database_type = "mysql";
$database_default = "cacti";      #修改数据库名称
$database_hostname = "localhost";
$database_username = "cacti";       #修改授权用户
$database_password = "asd123";      #修改授权密码
$database_port = "3306";
$database_ssl = false;
  (3)创建数据库斌授予权限

#首先开启数据库并数据库设置密码
systemctl start mariadb.service
mysqladmin -uroot password abc123
#进入数据库船舰cacti数据库并授予权限
mysql -u root -p
#创建名为cacti的数据库并设定字符集utf-8
create database cacti default character set utf8;
#授予cacti用户权限登陆密码为asd123
grant all on cacti.* to cacti@localhost identified by 'asd123';
quit
#把cacti目录底下的cacti.sql导入到cacti数据库里面(这边使用的用户时cacti用户授予的密码时asd123)
mysql -u cacti -p cacti < cacti.sql

  (3)修改snmp协议的配置文件

在本机模拟linux cacti被控端
vim /etc/snmp/snmpd.conf
41行:com2sec notConfigUser192.168.179.151   public
62行:accessnotConfigGroup ""      any       noauth    exactall none none
85行:view all    included.180#去掉前面的#号
#开启snmp协议
systemctl start snmpd.service
  (4)进入安装页面http://192.168.1789.151/cacti
http://i2.运维网.com/images/blog/201808/24/82bacd62e11ce2c5fdbabfcdf0c5e4ca.png
  进入下面页面后,要解决PHP一些拓展应用
http://i2.运维网.com/images/blog/201808/24/f9459dc872b86d1a55670e1340ab1d64.png
  1、解决PHP拓展模块

1:
安装ldap
yum install php-ldap
打开php.ini配置文件找到ldap那个区域模块添加
extension=ldap.so
2:
安装mbstring拓展模块
yum install php-mbstring
打开php.ini配置在配置文件最后一行添加
extension=mbstring.so
3:
安装posix模块
yum -y install php-process
查看验证是否安装成功
php -m|grep posix
posix
显示红色字体posix就表示安装成功

  2、解决数据库时间问题

#进入数据库授权
GRANT SELECT ON mysql.time_zone_name TO cacti@localhost IDENTIFIED BY 'cactiwhsir';
#再把以下命令重新授权一下
grant all on cacti.* to cacti@localhost identified by 'asd123';
#刷新
flush privileges;
#又会出现下面报错
ERROR: Your MySQL TimeZone database is not populated. Please populate this database before proceeding.
#解决办法
mysql_tzinfo_to_sql /usr/share/zoneinfo/Asia/Shanghai Shanghai | mysql -u root -p mysql
#重启数据库
systemctl restart mariadb.service
  3、我们再看问题都解决了还是不能安装因为还有一个cacti报错看下图:
http://i2.运维网.com/images/blog/201808/24/0f99cdba08f955feb8b15bebeabd9028.png
报错的意思是:错误:您的Web服务器没有设置PHP时区设置。请编辑PHP。然后取消对日期的评论。在安装Cacti之前,根据PHP安装说明将其设置为Web服务器时区。
  解决方法:

#打开php.ini配置文件找到date.timezone原本是注释状态需要去掉前面的;分号然后在=等于后面空格加上Asia/Shanghai
date.timezone = Asia/Shanghai
重启HTTP服务
  点击next下一步安装好后进入以下页面:
http://i2.运维网.com/images/blog/201808/24/08a69f6871dbe1032e3e3f9885fc8006.png
  默认账号密码admin,登陆进去后会提示当前密码不安全重新设置密码,密码要求符合复杂性要求:大写字母加数字加特殊符号一般我用的符合复杂性密码是ABcd123!
  下面是进入了监控页面,监控那台主机需要自行添加
http://i2.运维网.com/images/blog/201808/24/f597cc097a143052cb61e461a3314ffa.png

添加被控端
console->Create devices ->Add->前两项输入服务端IP、模板选择ucd/net SNMP Host->Create->Save
生成监控图像
console->Create graphs ->勾选需监控的对象(CPU、内存、网卡等)->Create
Graph Trees->Default Tree->Add->Type选择Host->Create->Save
执行采集数据
/usr/bin/php /var/www/html/cacti/poller.php
计划性任务
echo '*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php > /dev/null' >> /var/spool/cron/root
graphs->Default Tree->查看监测结果图像
二、nagios
  Nagios是一款开源的计算机系统和网络监视工具,能有效地监控Windows,Linux和UNIX的主机服务状态,在系统服务发生异常时会发出电子邮件和短信通知报警第一时间通知网站维护人员

Nagios可识别的四种监控状态返回信息:
  0(ok)表示状态正常/绿色
  1(WARNING)表示出现警告/×××
  2(CRITICAL)表示出先严重的错误/红色
  3(UNKNOWN)表示位置错误/深×××
  Nagios通过插件的返回值,来判断监控对象的状态,并通过Web显示出来

Nagios通过nrpe插件来远程管理服务


[*]Nagios执行安装在它里面的check_nrpe插件,并告诉check_nrpe来检测那些服务
[*]通过SSH,check_nrpe连接远端机器上的NRPE daemon
[*]NRPE运行本地各种插件检测本地服务器状态(check_disk,...etc)
[*]NRPE把检测结果传给主机端的check_nrpe,check_nrpe再把结果送到Nagios状态队列中
[*]Nagios依次读取队列中的信息。再把结果显示出来

案例实施

实验环境:
linux:192.168.179.151(监控主机)
linux:192.168.179.134(被监控机)
  1:安装工具开发包

yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel
  2:创建nagios用户和组

useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
  3:同不系统时间(两台都执行)

ntpdate pool.ntp.org
27 Aug 11:30:44 ntpdate: adjust time server 85.199.214.100 offset 0.002483 sec
  4:编译安装Nagios

tar zxvf nagios-4.0.1.tar.gz -C /opt/
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
chkconfig --add /etc/rc.d/init.d/nagios
chkconfig --level 35 nagios on
chkconfig --list nagios
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies '。
nagios          0:关 1:关 2:关 3:开 4:关 5:开 6:关

  5:检查 /usr/local/nagios 是否存在etc、bin、sbin、share、var 这五个目录

cd /usr/local/nagios/
ls
binetclibexecsbinsharevar
  6:编译安装 nagios-plugins

tar zxvf /abc/nagios-plugins-1.5.tar.gz -C /opt/
cd /opt/nagios-plugins-1.5
./configure --prefix=/usr/local/nagios
make && make install
  7:yum安装LAMP架构

yum -y install httpd
yum -y install mariadb mariadb-server
yum -y install php php-mysql
  8:修改httpd.conf配置文件

找到:
User apache
Group apache
修改为
User nagios
Group nagios
然后找到

  DirectoryIndex index.html

修改为

  DirectoryIndex index.html index.php

接着增加如下内容:
AddType application/x-httpd-php .php
为了安全起见,一般情况下要让nagios 的web 监控页面必须经过授权才能访问,
这需要增加验证配置,即在httpd.conf 文件最后添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd            
Require valid-user

Alias /nagios "/usr/local/nagios/share"

AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user

  9:设置网页访问密码

# htpasswd -c /usr/local/nagios/etc/htpasswd jack
New password: 321321
Re-type new password: 321321
Adding password for user jack
# cat /usr/local/nagios/etc/htpasswd
jack:$apr1$G40eVtso$JGHxlhg0dolYoeGWZ4pkA0
  10:配置nagios一般在目录/usr/local/nagios/etc/下

cd /usr/local/nagios/etc/
ls
cgi.cfghtpasswdnagios.cfgobjectsresource.cfg
cd objects/
ls
commands.cfglocalhost.cfgswitch.cfg   timeperiods.cfg
contacts.cfgprinter.cfg    templates.cfgwindows.cfg
  1.templates.cfi nagios
  主要用于监控主机资源以及服务,在nagios配置中称为对象,
为了不必重复定义一些监控对象,Nagios引入了一个模板配置文件,
将一些共性的属性定义成模板,以便于多次引用。
  contact_groups联系组属性改成 ts 将在后面的contacts.cfg文件中定义

contact_groups          ts 注:所有contact_group的admins都要改成ts

[*]hosts.cfg文件
  此文件默认不存在,需要手动创建,hosts.cfg主要用来指定被监控的主机地址以及相关属性信息(不能有任何空格)

define host{   
use                     linux-server#引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。
host_name               Nagios-Linux#主机名
alias                   Nagios-Linux#主机别名
address               192.168.179.134#被监控的主机地址,这个地址可以是ip,也可以是域名。
}   
#定义一个主机组   
define hostgroup{      
hostgroup_name          bsmart-servers#主机组名称,可以随意指定。
alias                   bsmart servers#主机组别名
members               Nagios-Linux#主机组成员,其中“Nagios-Linux”就是上面定义的主机。   
}
3.services.cfg文件         此文件默认也不存在,需要手动创建
主要用于定义监控的服务和主机资源
define service{
use                     local-service#引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。
host_name               Nagios-Linux#指定要监控哪个主机上的服务,“Nagios-Server”在hosts.cfg文件中进行了定义。
service_description   check-host-alive#对监控服务内容的描述,以供维护人员参考。
check_command         check-host-alive#指定检查的命令。
}
4.contacts.cfg文件   contacts.cfg是一个定义联系人和联系人组的配置文件(不能有空格)
define contact{
contact_name                  jack#联系人的名称,这个地方不要有空格
use                           generic-contact#引用generic-contact的属性信息,其中“generic-contact”在templates.cfg文件中进行定义
alias                           Nagios Admin
email                           13951868284@139.com
}
define contactgroup{
contactgroup_name       ts#联系人组的名称,同样不能空格
alias                   Technical Support#联系人组描述
members               jack#联系人组成员,其中“jack”就是上面定义的联系人,如果有多个联系人则以逗号相隔
}
  5.cgi.cfg文件 此文件用来控制相关cgi脚本
由于nagios的web监控界面验证用户为jack,所以只需在cgi.cfg文件中添加此用户的执行权限
在最后加入

default_user_name=jack
authorized_for_system_information=nagiosadmin,jack
authorized_for_configuration_information=nagiosadmin,jack
authorized_for_system_commands=jack
authorized_for_all_services=nagiosadmin,jack
authorized_for_all_hosts=nagiosadmin,jack
authorized_for_all_service_commands=nagiosadmin,jack
authorized_for_all_host_commands=nagiosadmin,jack
  6.nagios.cfg文件

将对象配置文件在Nagios.cfg文件中进行引用
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
command_check_interval=10s该变量用于设置nagios对外部命令检测的时间间隔
  7.用/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 验证配置文件

Nagios Core 4.0.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 10-15-2013
License: GPL
Website: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
Checked 9 services.
Checked 2 hosts.
Checked 2 host groups.
Checked 0 service groups.
Checked 1 contacts.
Checked 1 contact groups.
Checked 24 commands.
Checked 5 time periods.
Checked 0 host escalations.
Checked 0 service escalations.
Checking for circular paths...
Checked 2 hosts
Checked 0 service dependencies
Checked 0 host dependencies
Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check
  启动nagios

# cd /usr/local/nagios/
# ls
binetclibexecsbinsharevar
# cd bin/
# ls
nagiosnagiostats
# ./nagios -d ../etc/nagios.cfg

  重启httpd服务
  验证:
http://192.168.179.151/nagios/
http://i2.运维网.com/images/blog/201808/27/3cc1f0a2a9ac6d02011f7de7586f7c2f.png
  登陆页面:
http://i2.运维网.com/images/blog/201808/27/60229250e6490f5b5276844d8cf8bf5d.png
  下面是监控本机与192.168.179.134的监控页面
http://i2.运维网.com/images/blog/201808/27/6e49edb0c7154fae08dd9aec0955a64d.png

三、zabbix
  Zabblx是一个高度集成的企业级开源网络监控解决方案,与Cacti、Nagios提供分布式监控以以及集中的Web管理界面。Zabbix具备常见商业监控软件所具备的功能,例如主机性能监控、网络设备性能监控、数据库性能监控、ftp等通用协议的监控,能够利用灵活的可定制警告机制、允许用户对事件发送基于E-mail 的警告,保证相关维护人员对问题做出快速解决;还能够利用存储数据提供杰出的报表及实时的图形化效据处理,实现对Linux、Windows 主机的7x24小时集中监控。

Zabbix具备常见商业监控软件所具备的功能:


[*]主机性能监控
[*]网络设备性能监控
[*]数据库性能监控
[*]ftp等通用协议监控

安装配置Zabbix
  Zabbix官方首页:https://www.zabbix.com/
  1-安装LAMP架构

yum install -y \
httpd \
mariadb-server mariadb \
php \
php-mysql \
php-gd \
libjpeg* \
php-ldap \
php-odbc \
php-pear \
php-xml \
php-xmlrpc \
php-mhash
  设置httpd配置文件域名和支持添加php首页存放点,设置php.ini设置时区

vim /etc/httpd/conf/httpd.conf
ServerName www.benet.com
DirectoryIndex index.html index.php
vi /etc/php.ini
date.timezone = PRC    //设置中国时区
  关闭防火墙和SElinux

systemctl stop firewalld.service
setenforce 0
  启动httpd、mariadb,设置数据库密码,添加php首页验证php主页面

systemctl start httpd.service
systemctl start mariadb.service
mysql_secure_installation
vi /var/www/html/index.php

http://i2.运维网.com/images/blog/201808/23/ef7418bb674a95175b1b78671bd83566.png
  2-进入数据库创建zabbix数据库,授予zabbix用户操作权限

mysql -u root -p
CREATE DATABASE zabbix character set utf8 collate utf8_bin;
GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
flush privileges;
  编辑index.php测试能否连接数据库,连接成功页面显示Success!!连接失败显示Fail!!

vim /var/www/html/index.php

http://i2.运维网.com/images/blog/201808/28/0ba713f8d7b3d4dee48beea4804cf050.png
  3-部署Zabbix-Sever

yum install php-bcmath php-mbstring -y
rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm //会自动生成yum源文件,保证系统可以上网
yum install zabbix-server-mysql zabbix-web-mysql -y
zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz | mysql -uzabbix -pzabbix //生成数据库文件,注意密码不要输成root的
#//生成数据库文件,注意密码不要输成root的
# zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz | mysql -uzabbix -pzabbix
Enter password: admin123(授权的zabbix密码)

  查看/etc/zabbix底下zabbix_server.conf 文件(grep过滤查看)

38:LogFile=/var/log/zabbix/zabbix_server.log
49:LogFileSize=0
72:PidFile=/var/run/zabbix/zabbix_server.pid
82:SocketDir=/var/run/zabbix
101:DBName=zabbix
117:DBUser=zabbix
125:DBPassword=admin123 //修改本行 (配置文件里面没有此行需要手动添加)
347:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
465:Timeout=4
507:AlertScriptsPath=/usr/lib/zabbix/alertscripts
517:ExternalScripts=/usr/lib/zabbix/externalscripts
553:LogSlowQueries=3000
  修改zabbix.conf配置文件里面的时区

vi /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
  修正图表中的乱码

vi /usr/share/zabbix/include/defines.inc.php //修正图表中文乱码
:%s /graphfont/kaiti/g//从微软系统下复制相应的字体文件到 /usr/share/zabbix/fonts 目录中注意字体名称要对应配置文件,且注意大小写
cp STKAITI.TTF /usr/share/zabbix/fonts/
#STKAITI.TTF 如果需要可留言,也可以在网上查找
  启动zabbix-server

systemctl enable zabbix-server
systemctl start zabbix-server
netstat -anpt | grep zabbix//监听在10051端口上      
  重启httpd服务登陆zabbix页面

systemctl restart httpd.service
http://192.168.175.155/zabbix///安装后登录 用户名Admin 密码:zabbix
设置中文环境
Administrator-Users-点击用户-语言中设置
http://i2.运维网.com/images/blog/201808/28/b2201120d328740933383f364828df58.png
http://i2.运维网.com/images/blog/201808/28/41b9684749ae2592921dc346d8947539.png
http://i2.运维网.com/images/blog/201808/28/95db4fb0aab9455ac3ac2aa7f79436d7.png
http://i2.运维网.com/images/blog/201808/28/916690cfaabb954824fdfcf9f29b1a1e.png
http://i2.运维网.com/images/blog/201808/28/83b2e75e68f537770c16c8054b494170.png
http://i2.运维网.com/images/blog/201808/28/118758a1076efd08ce2a023980716f91.png
http://i2.运维网.com/images/blog/201808/28/ed00ec98bc2feb2241d15c41cc6148f4.png
  下面是监控页面,不过没有添加被监控端
http://i2.运维网.com/images/blog/201808/28/29dd2a09bf75f5a5e8a48f32196e8a37.png

添加被监控端
  1-被监控端安装zabbix-agent

rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
yum install -y zabbix-agent
  2-查看/etc/zabbix/zabbix_agentd.conf配置文件

grep -n '^' /etc/zabbix/zabbix_agentd.conf
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=192.168.175.155
139:ServerActive=192.168.175.155
150:Hostname=test
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf
  被监控端关闭防火墙SElinux,开启zabbix-agent

service firewalld stop
setenforce 0
systemctl enable zabbix-agent.service
systemctl restart zabbix-agent.service
#被监控端端口为10050
netstat -anpt | grep zabbix
tcp      0      0 0.0.0.0:10050         0.0.0.0:*               LISTEN      2850/zabbix_agentd
增加被控主机在WEB平台上做
配置-主机-创建主机:
主机页面根据需求配置
模板页需要链接模板,根据需要链接相应的模板
0K
  监控页面切换中文设置
http://i2.运维网.com/images/blog/201808/28/ac1ef9eb5ba2e3998f394f50a778b1e5.png
  添加被监控主机
http://i2.运维网.com/images/blog/201808/28/3ff754aba57214de7d642bc48e60a1b5.png
  进入到下面添加页面:(红色※代表必填)
http://i2.运维网.com/images/blog/201808/28/fdf2adebb4d6ec0ed2f12c0e8559f9ad.png
http://i2.运维网.com/images/blog/201808/28/dd68ab20847bb0400870b464baaed27b.png
  添加完后刷新页面,过一会就会报错
http://i2.运维网.com/images/blog/201808/28/67114cc4be4a00021f35063833135995.png

这边cacti,nagios,zabbix安装就写到这,下一篇zabbix邮箱服务尽请关注



页: [1]
查看完整版本: 监控三剑客