工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆

Mrtg系统状态监控

Mrtg系统状态监控[CPU 内存 网卡流量 系统进程 硬盘空间 TCP连接数]

----------------------------
1) 安装所需rpm包
----------------------------
net-snmp-perl-5.1.2-11.EL4.6.x86_64.rpm
net-snmp-libs-5.1.2-11.EL4.6.x86_64.rpm
net-snmp-utils-5.1.2-11.EL4.6.x86_64.rpm
net-snmp-devel-5.1.2-11.EL4.6.x86_64.rpm
   !!需要预先安装以下rpm包
   beecrypt-devel-3.1.0-6.x86_64.rpm
   elfutils-devel-0.97-5.x86_64.rpm(在第5张安装光盘上)
net-snmp-5.1.2-11.EL4.6.x86_64.rpm
net-snmp-utils-5.1.2-11.EL4.6.x86_64.rpm
安装mrtg-2.10.15-1.x86_64.rpm

----------------------------
2) 修改/etc/snmp/snmpd.conf
----------------------------
找到这行:
# Make at least  snmpwalk -v 1 59.57.251.56 -c public system fast again
#       name           incl/excl     subtree         mask(optional)
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
在view几行的下面,加上这段文字:
# For Mrtg Add start ####################################
view    all           included   .1.3.6
# For Mrtg Add end   ####################################
找到这行:
####
# Finally, grant the group read-only access to the systemview view.
#       group          context sec.model sec.level prefix read   write  notif
access  notConfigGroup ""      any       noauth    exact  mib2   none   none
把 access  notConfigGroup 里的mib2(也可能是systemview) 更改为all
如果需要监控硬盘容量使用状况,还需要进行以下操作:
用 df -a 看到你要监控的硬盘分卷方式以及容量大小(-am:以M为单位; -ak:以K为单位 -ag:以G为单位)
比如:
#df -am
Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/sda2                63993     10284     50459  17% /
/dev/sda1                  981        24       908   3% /boot

然后修改/etc/snmp/snmpd.conf,在刚才修改的view行最下面加入:
disk / 63993
disk /boot 981
然后重启snmp服务
# service snmpd restart

----------------------------
3) 修改mrtg配置文件 mrtg.cfg
----------------------------
使用Redhat AS4u5 自带的mrtg
安装以后的配置文件是/usr/local/apache/htdocs/mrtg

我们预设要监控的服务器以下参数:
连接公网网卡的流量;
主机连续运行时间;
系统负载;
CPU负载;
内存使用量;
系统进程数;
硬盘空间;
打开的TCP连接数。

mrtg.cfg配置如下:

  1. ###################### Configuration Being #########################  
  2. #---------------------------------  
  3. # filename : mrtg.cfg  
  4. # 注意如果文件夹不存在,需要先创建  
  5. #---------------------------------  
  6. ### Global Config Options  
  7. #  to get bits instead of bytes and graphs growing to the right  
  8. #Options[_]: growright, bits  
  9. EnableIPv6: no  
  10. WorkDir: /usr/local/apache/htdocs/mrtg  
  11. Language: Chinese  
  12. HtmlDir: /usr/local/apache/htdocs/mrtg  
  13. ImageDir: /usr/local/apache/htdocs/mrtg  
  14. LogDir: /var/log/mrtg  
  15. ThreshDir: /var/lib/mrtg  
  16. LoadMIBs:/usr/share/snmp/mibs/UCD-SNMP-MIB.txt,/usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt,/usr/share/snmp/mibs/TCP-MIB.txt  
  17. #================================================================================  
  18. #监控eth0网卡(连接公网的)  
  19. #================================================================================  
  20. Target[eth0_lan]: /59.57.251.56:public@59.57.251.56:  
  21. Options[eth0_lan]: growright  
  22. Directory[eth0_lan]: eth0  
  23. MaxBytes[eth0_lan]: 100000000  
  24. Kmg[eth0_lan]: ,k,M,G,T,P  
  25. YLegend[eth0_lan]: Bytes per Second  
  26. ShortLegend[eth0_lan]: B/s  
  27. Legend1[eth0_lan]: 每秒流入量 (单位 Bytes)  
  28. Legend2[eth0_lan]: 每秒流出量 (单位 Bytes)  
  29. LegendI[eth0_lan]: 流入:  
  30. LegendO[eth0_lan]: 流出:  
  31. Title[eth0_lan]: eth0网络流量[流入+流出]  
  32. PageTop[eth0_lan]: <H1>eth0网络流量[流入+流出]</H1> 
  33. <TABLE> 
  34.   <TR><TD>描述 :</TD><TD>LAN网络接口eth0的网络流量(Bytes/s)</TD></TR> 
  35. </TABLE> 
  36. #================================================================================  
  37. #监控主机连续运行时间[运行天数] 实际操作过程中此节未配置,没有多大用途  
  38. #================================================================================  
  39. Target[upday]: `/usr/local/mrtg/bin/mrtg-updays.pl`  
  40. Options[upday]: gauge,nopercent,growright  
  41. Directory[upday]: upday  
  42. MaxBytes[upday]: 1000  
  43. YLegend[upday]: Up Days  
  44. ShortLegend[upday]: 天  
  45. Legend1[upday]: 主机连续运行时间(天)  
  46. Legend2[upday]:  
  47. LegendI[upday]: 运行时间:  
  48. LegendO[upday]:  
  49. Title[upday]: 主机连续运行时间[运行天数]  
  50. PageTop[upday]: <h1>主机连续运行时间[运行天数]</h1> 
  51. <TABLE> 
  52.   <TR><TD>描述:</TD><TD>主机连续运行的时间(天)</TD></TR> 
  53. </TABLE> 
  54. #================================================================================  
  55. #监控系统负载[1分钟+15分钟]  
  56. #================================================================================  
  57. Target[systemload]: .1.3.6.1.4.1.2021.10.1.5.1&.1.3.6.1.4.1.2021.10.1.5.3:public@59.57.251.56:  
  58. Options[systemload]: gauge,nopercent,growright  
  59. Directory[systemload]: load  
  60. MaxBytes[systemload]: 3000  
  61. YLegend[systemload]: System Load  
  62. ShortLegend[systemload]:  
  63. Legend1[systemload]: 最近1分钟系统负载(x100)  
  64. Legend2[systemload]: 最近15分钟系统负载(x100)  
  65. LegendI[systemload]: 1分钟负载:  
  66. LegendO[systemload]: 15分钟负载:  
  67. Title[systemload]: 系统负载(x100)[1分钟+15分钟]  
  68. PageTop[systemload]: <h1>系统负载(x100)[1分钟+15分钟]</h1> 
  69. <TABLE> 
  70.   <TR><TD>描述:</TD><TD>系统负载(x100)[1分钟+15分钟]</TD></TR> 
  71. </TABLE> 
  72. #================================================================================  
  73. #监控CPU负载[用户+闲置]  
  74. #================================================================================  
  75. Target[cpuload]: .1.3.6.1.4.1.2021.11.50.0&1.3.6.1.4.1.2021.11.53.0:public@59.57.251.56:  
  76. Options[cpuload]: nopercent,growright  
  77. Directory[cpuload]: cpu  
  78. MaxBytes[cpuload]: 100  
  79. Unscaled[cpuload]: dwym  
  80. YLegend[cpuload]: CPU Utilization  
  81. ShortLegend[cpuload]: %;  
  82. Legend1[cpuload]: CPU用户负载(%)  
  83. Legend2[cpuload]: CPU闲置(%)  
  84. LegendI[cpuload]: 用户:  
  85. LegendO[cpuload]: 闲置:  
  86. Title[cpuload]: CPU负载[用户+闲置]  
  87. PageTop[cpuload]: <h1>CPU负载[用户+闲置]</h1> 
  88. <TABLE> 
  89.   <TR><TD>描述:</TD><TD>CPU负载[用户+闲置]</TD></TR> 
  90. </TABLE> 
  91. #================================================================================  
  92. #监控内存使用量[Mem+Swap]  
  93. #================================================================================  
  94. Target[memory]: .1.3.6.1.2.1.25.2.3.1.6.2&.1.3.6.1.2.1.25.2.3.1.6.3:public@59.57.251.56:  
  95. Options[memory]: gauge,growright  
  96. Directory[memory]: mem  
  97. MaxBytes1[memory]: 4045336  
  98. MaxBytes2[memory]: 2097152  
  99. Kmg[memory]: k,M,G,T,P  
  100. Kilo[memory]: 1024  
  101. Unscaled[memory]: dwym  
  102. YLegend[memory]: Bytes  
  103. ShortLegend[memory]: B  
  104. Legend1[memory]: 已用Mem (Bytes)  
  105. Legend2[memory]: 已用Swap(Bytes)  
  106. LegendI[memory]: 已用Mem :  
  107. LegendO[memory]: 已用Swap:  
  108. Title[memory]: 内存使用量[Mem+Swap]  
  109. PageTop[memory]: <h1>内存使用量[Mem+Swap]</h1> 
  110. <TABLE> 
  111.   <TR><TD>描述:</TD><TD>Memory和Swap的使用量(Bytes)</TD></TR> 
  112. </TABLE> 
  113. #================================================================================  
  114. #监控系统进程数[进程数]  
  115. #================================================================================  
  116. Target[process]: .1.3.6.1.2.1.25.1.6.0&.1.3.6.1.2.1.25.1.6.0:public@59.57.251.56:  
  117. Options[process]: gauge,nopercent,growright  
  118. Directory[process]: process  
  119. MaxBytes[process]: 1000  
  120. YLegend[process]: Processes  
  121. ShortLegend[process]: 个  
  122. Legend1[process]: 系统进程数(个)  
  123. Legend2[process]:  
  124. LegendI[process]: 进程数:  
  125. LegendO[process]:  
  126. Title[process]: 系统进程数[进程数]  
  127. PageTop[process]: <h1>系统进程数[进程数]</h1> 
  128. <TABLE> 
  129.   <TR><TD>描述:</TD><TD>系统进程数(个)</TD></TR> 
  130. </TABLE> 
  131. #==================================================================================  
  132. # 监控硬盘空间[系统盘+数据盘]  !!参照前面的2) 这里需要与修改后snmpd.conf 的硬盘参数一致  
  133. #==================================================================================  
  134. 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@59.57.251.56:  
  135. Options[disk]: gauge,growright  
  136. Directory[disk]: disk  
  137. MaxBytes1[disk]: 10080520  
  138. MaxBytes2[disk]: 46251780  
  139. Kmg[disk]: k,M,G,T,P  
  140. Kilo[disk]: 1024  
  141. Unscaled[disk]: dwym  
  142. YLegend[disk]: Bytes  
  143. ShortLegend[disk]: B  
  144. Legend1[disk]: 系统盘已用空间  
  145. Legend2[disk]: 数据盘已用空间  
  146. LegendI[disk]: 系统已用:  
  147. LegendO[disk]: 数据已用:  
  148. Title[disk]: 硬盘空间[系统盘+数据盘]  
  149. PageTop[disk]: <h1>硬盘空间[系统盘+数据盘]</h1> 
  150. <TABLE> 
  151.   <TR><TD>描述:</TD><TD>监控硬盘空间</TD></TR> 
  152. </TABLE> 
  153. #================================================================================  
  154. #监控打开的TCP连接数[TCP连接数]  
  155. #================================================================================  
  156. Target[tcpopen]: .1.3.6.1.2.1.6.9.0&.1.3.6.1.2.1.6.9.0:public@59.57.251.56:  
  157. Options[tcpopen]: gauge,nopercent,growright  
  158. Directory[tcpopen]: tcpopen  
  159. MaxBytes[tcpopen]: 1000  
  160. YLegend[tcpopen]: Tcp Connections  
  161. ShortLegend[tcpopen]: 个  
  162. Legend1[tcpopen]: 打开的TCP连接数(个)  
  163. Legend2[tcpopen]:  
  164. LegendI[tcpopen]: TCP连接数:  
  165. LegendO[tcpopen]:  
  166. Title[tcpopen]: TCP连接数[TCP连接数]  
  167. PageTop[tcpopen]: <h1>TCP连接数[TCP连接数]</h1> 
  168. <TABLE> 
  169.   <TR><TD>描述:</TD><TD>打开的TCP连接数(个)</TD></TR> 
  170. </TABLE> 
  171. ###################### Configuration End ######################### 


注意<一>
在上面监控主机连续运行时间的配置中,有提到要使用/usr/local/mrtg/bin/mrtg-updays.pl这个文件。需要手动创建,内容如下:

  1. #!/usr/bin/perl  
  2. $machine = `/bin/hostname`;  
  3. $uptime1 = `/usr/bin/uptime`;  
  4. $uptime2 = $uptime1;  
  5. $uptime1 =~ /up (.*?) day/;  
  6. $upday = int($1);  
  7. $uptime2 =~ /up (.*?) load/;  
  8. $uptime = $1;  
  9. print "$upday\n";  
  10. print "$upday\n";  
  11. print "$uptime\n";  
  12. print $machine."\n"


保存以后修改权限为可执行:

  1. #chmod +x /usr/local/mrtg/bin/mrtg-updays.pl 

注意<二>
关于硬盘参数,一定要注意
snmp.conf与mrtg.cfg中的格式、参数一定要一致,并且完全跟df -a*的命令结果相符。否则会得到输入错误的提示信息。
 
----------------------------
4) 生成工作目录及相关文件
----------------------------
env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/apache/htdocs/mrtg/mrtg.cfg
 
----------------------------
5) 生成监控的页面文件
----------------------------
env LANG=C /usr/local/mrtg/bin/indexmaker --output /usr/local/apache/htdocs/mrtg/index.html --title="System state Monitor" /usr/local/apache/htdocs/mrtg/mrtg.cfg

6) 多服务器环境下的集中管理
mrtg主目录:/usr/local/apache/htdocs/mrtg
如果存在多服务器,建议用创建ip文件夹,方便识别:
实例:
创建一IP文件夹(117.25.130.49),
(1) 生成CFG配置文件
/usr/local/mrtg/bin/cfgmaker public@117.25.130.49 --global 'workdir: /usr/local/apache/htdocs/mrtg/117.25.130.49' --output /usr/local/apache/htdocs/mrtg/117.25.130.49/mrtg.cfg
这样生成的配置文件只是MRTG默认的配置,可以不用操作这一步,直接COPY上面的配置模板,将其中IP更改为要监控的服务器IP

(2) 生成监控页面文件
/usr/local/mrtg/bin/indexmaker --output=/usr/local/apache/htdocs/mrtg/117.25.130.49/index.html --title="System Monitor" /usr/local/apache/htdocs/mrtg/117.25.130.49/mrtg.cfg

(3) 生成工作目录及相关文件
env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/apache/htdocs/mrtg/117.25.130.49/mrtg.cfg

(4) 更改模板文件的字符集,
上面生成的html文件中,默认字符集为iso-8859-1,浏览时可能会出现乱码,可以将其更改为utf-8
# sed -i "s/charset=iso-8859-15/charset=utf-8/" filename
# sed -i "s/charset=iso-8859-1/charset=utf-8/" filename

(5) 定时执行
*/5 * * * * env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/apache/htdocs/mrtg/117.25.130.49/mrtg.cfg

(6) 浏览
http://59.57.251.56/mrtg/117.25.130.49/


(7) 防火墙
iptables -A INPUT -s 59.57.251.56 -p udp --destination-port 161 -j ACCEPT
iptables -A INPUT -s 59.57.251.56 -p udp --destination-port 162 -j ACCEPT

附:NET-SNMP Tutorial -- MRTG

  1. ### Global Config Options  
  2. EnableIPv6: no  
  3. Options[_]: growright, bits  
  4. WorkDir: /usr/local/apache/htdocs/mrtg  
  5. Language: Chinese  
  6. HtmlDir: /usr/local/apache/htdocs/mrtg  
  7. ImageDir: /usr/local/apache/htdocs/mrtg  
  8. LogDir: /var/log/mrtg  
  9. ThreshDir: /var/lib/mrtg  
  10. LoadMIBs:/usr/share/snmp/mibs/UCD-SNMP-MIB.txt,/usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt,/usr/share/snmp/mibs/TCP-MIB.txt  
  11.  
  12. # User vs Idle CPU usage  
  13. Target[kontor.cpu]:ssCpuRawUser.0&ssCpuRawIdle.0:public@59.57.251.56  
  14. RouterUptime[kontor.cpu]: public@59.57.251.56  
  15. MaxBytes[kontor.cpu]: 100  
  16. Title[kontor.cpu]: CPU LOAD  
  17. PageTop[kontor.cpu]: <H1>User CPU Load %</H1> 
  18. Unscaled[kontor.cpu]: ymwd  
  19. ShortLegend[kontor.cpu]: %  
  20. YLegend[kontor.cpu]: CPU Utilization  
  21. Legend1[kontor.cpu]: User CPU in % (Load)  
  22. Legend2[kontor.cpu]: Idle CPU in % (Load)  
  23. Legend3[kontor.cpu]:  
  24. Legend4[kontor.cpu]:  
  25. LegendI[kontor.cpu]:  User  
  26. LegendO[kontor.cpu]:  Idle  
  27. Options[kontor.cpu]: growright,nopercent  
  28.  
  29. # User vs System CPU usage  
  30. Target[kontor.usrsys]:ssCpuRawUser.0&ssCpuRawSystem.0:public@59.57.251.56  
  31. RouterUptime[kontor.usrsys]: public@59.57.251.56  
  32. MaxBytes[kontor.usrsys]: 100  
  33. Title[kontor.usrsys]: CPU LOAD  
  34. PageTop[kontor.usrsys]: <H1>CPU (user and system) Load %</H1> 
  35. Unscaled[kontor.usrsys]: ymwd  
  36. ShortLegend[kontor.usrsys]: %  
  37. YLegend[kontor.usrsys]: CPU Utilization  
  38. Legend1[kontor.usrsys]: User CPU in % (Load)  
  39. Legend2[kontor.usrsys]: System CPU in % (Load)  
  40. Legend3[kontor.usrsys]:  
  41. Legend4[kontor.usrsys]:  
  42. LegendI[kontor.usrsys]:  User  
  43. LegendO[kontor.usrsys]:  System  
  44. Options[kontor.usrsys]: growright,nopercent  
  45.  
  46. ### Active CPU usage  
  47. Target[kontor.cpusum]:ssCpuRawUser.0&ssCpuRawUser.0:public@59.57.251.56 + ssCpuRawSystem.0&ssCpuRawSystem.0:public@59.57.251.56 + ssCpuRawNice.0&ssCpuRawNice.0:public@59.57.251.56  
  48. RouterUptime[kontor.cpusum]: public@59.57.251.56  
  49. MaxBytes[kontor.cpusum]: 100  
  50. Title[kontor.cpusum]: CPU LOAD  
  51. PageTop[kontor.cpusum]: <H1>Active CPU Load %</H1> 
  52. Unscaled[kontor.cpusum]: ymwd  
  53. ShortLegend[kontor.cpusum]: %  
  54. YLegend[kontor.cpusum]: CPU Utilization  
  55. Legend1[kontor.cpusum]: Active CPU in % (Load)  
  56. Legend2[kontor.cpusum]:  
  57. Legend3[kontor.cpusum]:  
  58. Legend4[kontor.cpusum]:  
  59. LegendI[kontor.cpusum]:  Active  
  60. LegendO[kontor.cpusum]:  
  61. Options[kontor.cpusum]: growright,nopercent  
  62.  
  63. ###Monitoring DISK space  
  64. ###Monitoring from dskTable  
  65.  
  66. Target[kontor.root]:dskPercent.1&dskPercent.2:public@59.57.251.56  
  67. RouterUptime[kontor.root]: public@59.57.251.56  
  68. MaxBytes[kontor.root]: 100  
  69. Title[kontor.root]: DISK USAGE  
  70. PageTop[kontor.root]: <H1>DISK / and /usr Usage %</H1> 
  71. Unscaled[kontor.root]: ymwd  
  72. ShortLegend[kontor.root]: %  
  73. YLegend[kontor.root]: DISK Utilization  
  74. Legend1[kontor.root]: Root disk  
  75. Legend2[kontor.root]: /usr disk  
  76. Legend3[kontor.root]:  
  77. Legend4[kontor.root]:  
  78. LegendI[kontor.root]:  Root disk  
  79. LegendO[kontor.root]:  /usr disk  
  80. Options[kontor.root]: growright,gauge,nopercent  
  81.  
  82. ###Monitoring from hrStorageTable  
  83.  
  84. Target[kontor.hrroot]:hrStorageSize.1&hrStorageUsed.1:public@59.57.251.56  
  85. RouterUptime[kontor.hrroot]: public@59.57.251.56  
  86. MaxBytes[kontor.hrroot]: 300000  
  87. Title[kontor.hrroot]: DISK / USAGE  
  88. PageTop[kontor.hrroot]: <H1>DISK / Usage</H1> 
  89. ShortLegend[kontor.hrroot]: B  
  90. kMG[kontor.hrroot]: k,M,G,T,P  
  91. kilo[kontor.hrroot]: 1024  
  92. YLegend[kontor.hrroot]: DISK / Utilization  
  93. Legend1[kontor.hrroot]: Root disk size  
  94. Legend2[kontor.hrroot]: Root disk usage  
  95. Legend3[kontor.hrroot]:  
  96. Legend4[kontor.hrroot]:  
  97. LegendI[kontor.hrroot]:  Root disk size  
  98. LegendO[kontor.hrroot]:  Root disk usage  
  99. Options[kontor.hrroot]: growright,gauge,nopercent  
  100. Two further examples that have been offered:   
  101.  
  102. ### Monitoring TCP connections  
  103.  
  104. Target[tcpopen]:   
  105. .1.3.6.1.2.1.6.9.0&.1.3.6.1.2.1.6.9.0:public@localhost   
  106. Options[tcpopen]: nopercent,growright,gauge,noinfo   
  107. Title[tcpopen]: Open TCP connections   
  108. PageTop[tcpopen]: Open TCP connections  
  109. MaxBytes[tcpopen]: 1000000   
  110. YLegend[tcpopen]: # conns   
  111. ShortLegend[tcpopen]: connections   
  112. LegendI[tcpopen]:  Connections:   
  113. LegendO[tcpopen]:   
  114. Legend1[tcpopen]: Open TCP connections   
  115.  
  116.  
  117. ### Monitoring Free Memory  
  118. Target[freemem]: .1.3.6.1.4.1.2021.4.11.0&.1.3.6.1.4.1.2021.4.11.0:public@localhost   
  119. Options[freemem]: nopercent,growright,gauge,noinfo   
  120. Title[freemem]: Free Memory   
  121. PageTop[freemem]: Free Memory  
  122. MaxBytes[freemem]: 1000000   
  123. kMG[freemem]: k,M,G,T,P,X   
  124. YLegend[freemem]: bytes   
  125. ShortLegend[freemem]: bytes   
  126. LegendI[freemem]:  Free Memory:   
  127. LegendO[freemem]:   
  128. Legend1[freemem]: Free memory, not including swap, in bytes 

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):