MRTG+SNMP+http图形化监控linux服务器性能(网络流量,CPU,磁盘等)

 yum -y install mrtg*

 

 mrtg的工作目录

 网卡

 cpu参数

 内存

 硬盘

 

 

[root@gitlab ~]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 2001100 0 2001100 0% /dev
tmpfs 2013108 0 2013108 0% /dev/shm
tmpfs 2013108 10040 2003068 1% /run
tmpfs 2013108 0 2013108 0% /sys/fs/cgroup
/dev/mapper/centos-root 27245572 2165392 25080180 8% /
/dev/sda1 1038336 140308 898028 14% /boot
tmpfs 402624 0 402624 0% /run/user/0
[root@gitlab ~]# free -k
total used free shared buff/cache available
Mem: 4026216 624704 2905032 10040 496480 3171200
Swap: 3145724 0 3145724

准备mrt文件:mrtg-new.cfg

###Minimal mrtg.cfg
#-------------------------------------------
HtmlDir: /var/www/mrtg
ImageDir: /var/www/mrtg
LogDir: /var/lib/mrtg
ThreshDir: /var/lib/mrtg
#================================

#=====================================
#监视eth0网卡
#======================================

Target[eth0_lan]: /192.168.8.201:public@localhost:
Options[eth0_lan]: growright
Directory[eth0_lan]: 100000000
MaxBytes[eth0_lan]: K,M,G
YLegend[eth0_lan]: Bytes per Second
ShortLegend[eth0_lan]: B/s
Legend1[eth0_lan]: 每秒流入量(单位 Bytes)
Legend2[eth0_lan]: 每秒流出量(单位 Bytes)
LegendI[eth0_lan]: 流入:
LegendO[eth0_lan]: 流出:
Title[eth0_lan]: eth0网络流量[流入+流出]
PageTop[eth0_lan]: <h1>eth0网络流量[流入+流出]</h1>
#=====================================
#监视CPU负载[用户+闲置]
#======================================
Target[cpuload]: .1.3.6.1.4.1.2021.11.50.0&.1.3.6.1.4.1.2021.11.53.0:public@localhost:
Options[cpuload]: nopercent,growright
Directory[cpuload]: cpu
MaxBytes[cpuload]: 100
Unscaled[cpuload]: dwym
YLegend[cpuload]: CPU Utilization
ShortLegend[cpuload]: %
Legend1[cpuload]: CPU用户负载(%)
Legend2[cpuload]: CPU闲置(%)
LegendI[cpuload]: 用户:
LegendO[cpuload]: 闲置:
Title[cpuload]: CPU负载[用户+闲置]
PageTop[cpuload]: <h1>CPU负载[用户+闲置]</h1>
#=====================================
#监视内存使用量[Mem+Swap]
#======================================
Target[memory]: .1.3.6.1.2.25.2.3.1.6.2&.1.3.6.1.2.1.25.2.3.1.6.3:public@localhost:
Options[memory]: gauge,growright
Directory[memory]: mem
MaxBytes1[memory]: 255412
MaxBytes2:[memory]: 524280
Kmg[memory]: K,M,G
Kilo[memory]: 1024
Unscaled[memory]: dwym
YLegend[memory]: Memory Bytes
ShortLegend[memory]: B
Legend1[memory]: 已用Mem(Bytes)
Legend2[memory]: 已用Swap(Bytes)
LegendI[memory]: 已用Mem:
LegendO[memory]: 已用Swap:
Title[memory]: 内存使用量[Mem+Swap]
PageTop[memory]: <h1>内存使用量[Mem+Swap]</h1>
#=====================================
#监视硬盘空间[根分区+boot分区]
#======================================
Target[disk]: .1.3.6.1.4.1.2021.9.1.8.1&.1.3.6.1.4.1.2021.9.1.8.2:public@localhost:
Options[disk]: gauge,growright
Directory[disk]: disk
MaxBytes1[disk]: 19679908
MaxBytes2[disk]: 101086
Kmg[disk]: K,M,G
Kilo[disk]: 1024
Unscaled[disk]: dwym
YLegend[disk]: Disk Bytes
ShortLegend[disk]: B
Legend1[disk]: 根分区已用空间
Legend2[disk]: boot分区已用空间
LegendI[disk]: 根分区已用:
LegendO[disk]: boot分区已用:
Title[disk]: 硬盘空间[根分区+boot分区]
PageTop[disk]: <h1>硬盘空间[根分区+boot分区]</h1>

 

 

[root@gitlab mrtg]# ls
mrtg.cfg mrtg-new.cfg
[root@gitlab mrtg]# cp mrtg.cfg mrtg.cfg.bakup
[root@gitlab mrtg]# ls
mrtg.cfg mrtg.cfg.bakup mrtg-new.cfg
[root@gitlab mrtg]# rm mrtg.cfg
rm:是否删除普通文件 "mrtg.cfg"?y
[root@gitlab mrtg]# mv mrtg-new.cfg mrtg.cfg
[root@gitlab mrtg]# ls
mrtg.cfg mrtg.cfg.bakup

开始生成图片:

/etc/cron.d/  下面生产mrtg文件,写下面内容

*/2 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg  /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_1 --confcache-file /var/lib/mrtg/mrtg.ok

 17行报错处理

 

 执行3遍以上测试:

[root@gitlab cron.d]# LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_1 --confcache-file /var/lib/mrtg/mrtg.ok
WARNING: /var/www/mrtg/eth0/ did not exist I will create it now
WARNING: /var/lib/mrtg/eth0/ did not exist I will create it now
WARNING: /var/www/mrtg/cpu/ did not exist I will create it now
WARNING: /var/lib/mrtg/cpu/ did not exist I will create it now
WARNING: /var/www/mrtg/mem/ did not exist I will create it now
WARNING: /var/lib/mrtg/mem/ did not exist I will create it now
WARNING: /var/www/mrtg/disk/ did not exist I will create it now
WARNING: /var/lib/mrtg/disk/ did not exist I will create it now

文件mrt.cfg最终版:

[root@gitlab cron.d]# cat /etc/mrtg/mrtg.cfg
###Minimal mrtg.cfg
#-------------------------------------------
HtmlDir: /var/www/mrtg
ImageDir: /var/www/mrtg
LogDir: /var/lib/mrtg
ThreshDir: /var/lib/mrtg
#================================

#=====================================
#监视eth0网卡
#======================================

Target[eth0_lan]: /192.168.8.201:public@localhost:
Options[eth0_lan]: growright
Directory[eth0_lan]: eth0
MaxBytes[eth0_lan]: 100000000
Kmg[eth0_lan]: K,M,G
YLegend[eth0_lan]: Bytes per Second
ShortLegend[eth0_lan]: B/s
Legend1[eth0_lan]: 每秒流入量(单位 Bytes)
Legend2[eth0_lan]: 每秒流出量(单位 Bytes)
LegendI[eth0_lan]: 流入:
LegendO[eth0_lan]: 流出:
Title[eth0_lan]: eth0网络流量[流入+流出]
PageTop[eth0_lan]: <h1>eth0网络流量[流入+流出]</h1>
#=====================================
#监视CPU负载[用户+闲置]
#======================================
Target[cpuload]: .1.3.6.1.4.1.2021.11.50.0&.1.3.6.1.4.1.2021.11.53.0:public@localhost:
Options[cpuload]: nopercent,growright
Directory[cpuload]: cpu
MaxBytes[cpuload]: 100
Unscaled[cpuload]: dwym
YLegend[cpuload]: CPU Utilization
ShortLegend[cpuload]: %
Legend1[cpuload]: CPU用户负载(%)
Legend2[cpuload]: CPU闲置(%)
LegendI[cpuload]: 用户:
LegendO[cpuload]: 闲置:
Title[cpuload]: CPU负载[用户+闲置]
PageTop[cpuload]: <h1>CPU负载[用户+闲置]</h1>
#=====================================
#监视内存使用量[Mem+Swap]
#======================================
Target[memory]: .1.3.6.1.2.25.2.3.1.6.2&.1.3.6.1.2.1.25.2.3.1.6.3:public@localhost:
Options[memory]: gauge,growright
Directory[memory]: mem
MaxBytes1[memory]: 4026216
MaxBytes2[memory]: 3145724
Kmg[memory]: K,M,G
Kilo[memory]: 1024
Unscaled[memory]: dwym
YLegend[memory]: Memory Bytes
ShortLegend[memory]: B
Legend1[memory]: 已用Mem(Bytes)
Legend2[memory]: 已用Swap(Bytes)
LegendI[memory]: 已用Mem:
LegendO[memory]: 已用Swap:
Title[memory]: 内存使用量[Mem+Swap]
PageTop[memory]: <h1>内存使用量[Mem+Swap]</h1>
#=====================================
#监视硬盘空间[根分区+boot分区]
#======================================
Target[disk]: .1.3.6.1.4.1.2021.9.1.8.1&.1.3.6.1.4.1.2021.9.1.8.2:public@localhost:
Options[disk]: gauge,growright
Directory[disk]: disk
MaxBytes1[disk]: 27245572
MaxBytes2[disk]: 1038336
Kmg[disk]: K,M,G
Kilo[disk]: 1024
Unscaled[disk]: dwym
YLegend[disk]: Disk Bytes
ShortLegend[disk]: B
Legend1[disk]: 根分区已用空间
Legend2[disk]: boot分区已用空间
LegendI[disk]: 根分区已用:
LegendO[disk]: boot分区已用:
Title[disk]: 硬盘空间[根分区+boot分区]
PageTop[disk]: <h1>硬盘空间[根分区+boot分区]</h1>

 

 

 

 

 

[root@gitlab cron.d]#

[root@gitlab cron.d]# LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_1 --confcache-file /var/lib/mrtg/mrtg.ok
2023-10-10 22:28:09: ERROR: I guess another mrtg is running. A lockfile (/var/lock/mrtg/mrtg_1) aged
7 seconds is hanging around. If you are sure that no other mrtg
is running you can remove the lockfile

 

 下面执行没有报错,命令行执行没有报错

 snmp配置

 

[root@zabbix-agent www]# cd /etc/snmp/
[root@zabbix-agent snmp]# pwd
/etc/snmp
[root@zabbix-agent snmp]# ls
snmpd.conf snmpd.conf.bak snmptrapd.conf
[root@zabbix-agent snmp]# cat snmpd.conf
access notConfigGroup "" any noauth exact all none none
view all included .1 80
rocommunity public
disk / 17811456
disk /boot 1038336
[root@zabbix-agent snmp]#

 下一步:生成监控首页文件

 

indexmaker --output /var/www/mrtg/index.html --title="System state Monitor" /etc/mrtg/mrtg.cfg

 安装apache的httpd软件

 

 使用httpd创建虚拟主机去展示mrtg的监控图形:::

在子配置目录/etc/httpd/conf.d/下面去配置虚拟主机的文件virhost.conf

<VirtualHost 192.168.8.230:80>
ServerName zabbix-agent
DocumentRoot "/var/www/mrtg"
DirectoryIndex index.html
<Directory "/var/www/mrtg">
Require all granted
</Directory>
</VirtualHost>

 访问192.168.8.230,说明mrtg的网络监控图形创建成功!!!!!!!!!!!!!

 

posted @ 2023-10-10 19:54  往事已成昨天  阅读(123)  评论(0编辑  收藏  举报