自宅サーバー ≫ Linux Server ≫ CentOS 7 ≫ サーバー状態監視システム (NET-SNMP + MRTG + HDDTemp)

 
サーバー状態監視システム
NET-SNMP+MRTG+HDDTempを利用しサーバー機のネットワーク・CPU使用率・メモリ使用率・ハードディスク使用率・ハードディスク温度を監視するシステムを作る
NET-SNMP のインストール
 
 
[root@centos]# yum -y install net-snmp
 
[root@centos]# yum -y install net-snmp-utils
 
snmpd.conf の設定
 
 
[root@centos]# vi /etc/snmp/snmpd.conf
 
 
途中略
 
 ####
 # First, map the community name "public" into a "security name"

 #       sec.name  source          community
 com2sec notConfigUser default public
   <--- コメント化
    

 
#com2sec notConfigUser default       public
 
com2sec local localhost private   <--- 追記
 com2sec mynetwork 192.168.1.0/24     public
   <--- 追記(public は任意な名前)
 
 ####
 # Second, map the security name into a group name:

 # groupName securityModel securityName
 group notConfigGroup v1 notConfigUser
   <--- コメント化
    

 
#group notConfigGroup v1 notConfigUser
 group notConfigGroup v2c notConfigUser
   <--- コメント化
    

 
#group notConfigGroup v2c notConfigUser
 
group  MyROGroup v1      local   <--- 追記
 group  MyROGroup v2c     local
   <--- 追記
 group  MyROGroup v1      mynetwork
   <--- 追記
 group  MyROGroup v2c     mynetwork
   <--- 追記
 
 ####
 # Third, create a view for us to let the group have rights to:

 # Make at least snmpwalk -v 1 localhost -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.1
 view    systemview    included  .1.3.6.1.2.1.25.1.1
   <--- コメント化
    

 
#view   systemview    included  .1.3.6.1.2.1.25.1.1
 
view all included .1 80   <--- 追記
 
 ####
 # 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  systemview none none
   <--- コメント化
    

 
#access notConfigGroup ""      any       noauth    exact  systemview none none
 
access  MyROGroup      ""      any       noauth    exact  all none none   <--- 追記
 
 
途中略
 
 ###############################################################################
 # disk checks
 #

 # The agent can check the amount of available disk space, and make
 # sure it is above a set limit.

 # disk PATH [MIN=100000]
 #
 # PATH: mount path to the disk in question.
 # MIN:  Disks with space below this value will have the Mib's errorFlag set.
 
#       Default value = 100000.   <--- コメント解除
         

         Default value = 100000.

 # Check the / partition and make sure it contains at least 10 megs.

 
#disk / 10000   <--- コメント解除
    

 disk / 10000
 
 
以下略
 
起動
 
 [root@
centos]# systemctl start snmpd
 
自動起動設定
 
 [root@
centos]# systemctl enable snmpd
 
自動起動設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep snmpd
 snmpd
.service                                 enabled   <--- enabled になっていればOK
 
MRTG のインストール
 
 
[root@centos]# yum -y install mrtg
 
メモリの確認
設定の際に必要になるので緑字のメモリ数値を控えておく
 
 
[root@centos]# free   <--- 入力
              total        used        free       shared       buffers       cached
 Mem:       
7882612     1897380     5985232        66920           552       797352
 -/+ buffers/cache:     1099476     6783136
 Swap:     
8142844           0     8142844
 
MRTG 用ディレクトリ作成
 
 
[root@centos]# mkdir /home/***/mrtg    ( **** はHP公開ディレクトリ)
 
アクセス権を変更
 
 
[root@centos]# chmod 755 /home/***/mrtg    ( **** はHP公開ディレクトリ)
 
所有者変更
 
 [root@centos
]# chown apache. /home/****/mrtg/    ( **** はHP公開ディレクトリ)
 
MRTG 設定ファイル作成
 
 
[root@centos]# cfgmaker --ifref=descr --ifdesc=descr public@192.168.1.*** > /etc/mrtg/mrtg.cfg
 
※上記の public@192.168.1.*** は snmpd.conf で設定した任意な名前とサーバーのアドレス
MRTG の設定
 
 
[root@centos]# vi /etc/mrtg/mrtg.cfg 
 # Created by
 # /usr/bin/cfgmaker --ifref=descr --ifdesc=descr public@192.168.1.***


 ### Global Config Options

 # for UNIX
 
# WorkDir: /home/http/mrtg   <--- コメント解除して書き換え
    

 WorkDir:
/home/****/mrtg   <--- 上記で作成した MRTG 用ディレクトリを指定  
 
 # or for NT
 # WorkDir: c:\mrtgdata

 ### Global Defaults

 # to get bits instead of bytes and graphs growing to the right
 
# Options[_]: growright, bits  <--- コメント解除して書き換え
    

 Options[_]: growright,
noinfo

 EnableIPv6: no
 
 
Language: iso2022jp  <--- 追記(日本語化)
 
 ######################################################################
 # System: papa-net.info
 # Description: Linux papa-net.info 2.6.32-220.7.1.el6.i686 #1 SMP Tue Mar 6 21:21:22 GMT 2012 i686
 # Contact: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
 # Location: Unknown (edit /etc/snmp/snmpd.conf)
 ######################################################################
 
 
途中略
 
 ### Interface 2 >> Descr: 'enp3s0' | Name: 'enp3s0' | Ip: '192.168.1.111' | Eth: '40-16-7e-6e-0a-40'   ###
 Target[192.168.1.111_enp3s0]: \enp3s0:public@192.168.1.***:
 SetEnv[192.168.1.111_enp3s0]: MRTG_INT_IP="192.168.1.111" MRTG_INT_DESCR="enp3s0"
 MaxBytes[192.168.1.***_enp3s0]: 12500000
 Title[192.168.1.***_enp3s0]:
enp3s0 -- papa-net.info  <--- コ書き換え
    

 Title[192.168.1.***_enp3s0]:
NetWork Information
 PageTop[192.168.1.***_enp3s0]: <h1>enp3s0 -- papa-net.info</h1>  <--- 書き換え
    

 PageTop[192.168.1.***_enp3s0]: <h1>
NetWork Information</h1>
 
 
 
途中略
 

 以下最終行に追記

 
 
 
#--- CPU ---
 Target[cpu]:
.1.3.6.1.4.1.2021.10.1.5.1&.1.3.6.1.4.1.2021.10.1.5.2:public@192.168.1.***  <--- *** は
 MaxBytes[cpu]:100                                                                
サーバーのアドレス
 Unscaled[cpu]:dwmy
 Options[cpu]:gauge, absolute, growright, noinfo, nopercent
 YLegend[cpu]:CPU Load(%)
 ShortLegend[cpu]:(%)
 LegendI[cpu]:1 minute average
 LegendO[cpu]:5 minute average
 Legend1[cpu]:1 minute average(%)
 Legend2[cpu]:5 minute average(%)
 Title[cpu]:CPU Utilization Information
 PageTop[cpu]:<H1>CPU Utilization Information</H1>
 
 #--- Memory ---
 Target[mem]:
.1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.4.0:public@192.168.1.***   <--- *** は
 MaxBytes1[mem]:7882612
   <--- メモリ確認で表示された men の total 値              サーバーのアドレス
 MaxBytes2[mem]:8142844
   <--- メモリ確認で表示された Swap の total 値
 Unscaled[mem]:dwmy
 Options[mem]:gauge, absolute, growright, noinfo
 YLegend[mem]:Mem Free(Bytes)
 ShortLegend[mem]:Bytes
 kilo[mem]:1024
 kMG[mem]:k,M,G,T,P
 LegendI[mem]:Real
 LegendO[mem]:Swap
 Legend1[mem]:Real Memory[MBytes]
 Legend2[mem]:Swap Memory[MBytes]
 Title[mem]:Free Memory Information
 PageTop[mem]:<H1>Free Memory Information</H1>
 
 #---
HD Disk ---
 Target[disk]:.1.3.6.1.4.1.2021.9.1.9.1&.1.3.6.1.4.1.2021.9.1.9.1:public@192.168.1.111
 MaxBytes[disk]:100
 Unscaled[disk]:dwmy
 Options[disk]:gauge, absolute, growright, nopercent, noinfo
 YLegend[disk]:Disk Used(%)
 ShortLegend[disk]: (%)
 LegendI[disk]:/ Disk used
 LegendO[disk]:/ Disk UsedLegend1[disk]: / Disk used
 Legend2[disk]:/ Disk used
 Title[disk]:Disk Information
 PageTop[disk]:<H1>Disk Information</H1>

 
Apache 用 MRTG の設定
 
 
[root@centos]# vi /etc/httpd/conf.d/mrtg.conf
 #
 # This configuration file maps the mrtg output (generated daily)
 # into the URL space. By default these results are only accessible
 # from the local host.
 #
 Alias /mrtg /
var/www/mrtg  <--- 書き換え  作成した MRTG 用ディレクトリを指定
    

 Alias /mrtg /
home/***/mrtg

 <Location /mrtg>
     Require local
 <--- コメント化
    

    
#Require local
    # Require ip 10.1.2.3
    # Require host example.org
    
以下追記
    
Order deny,allow   <--- 一旦全てを拒否してから下記を許可する
    
Allow from 127.0.0.1   <--- ローカルからの閲覧を許可
    
Allow from 192.168.1.0/24   <--- ネットワーク内からの閲覧を許可
 </Location>
 
上記設定はローカル(サーバー機自身)と同一ネットワーク内からの閲覧しか許可していないが外部からの閲覧を許可する場合の設定

 
[root@centos]# vi /etc/httpd/conf.d/mrtg.conf
 #
 # This configuration file maps the mrtg output (generated daily)
 # into the URL space. By default these results are only accessible
 # from the local host.
 #
 Alias /mrtg /home/***/mrtg
 
 
以下全てコメント化
 
 
#<Location /mrtg>
    
# Require local
   
# Require ip 10.1.2.3
   
# Require host example.org
    
# Order deny,allow
    
# Allow from 127.0.0.1
    
# Allow from 192.168.1.0/24
 
#</Location>
 
Apache の再起動
 
 
[root@centos]# systemctl restart httpd
 
ハードディスク温度表示追加
ハードディスク温度監視システム hddtemp をインストール
hddtempパッケージは EPELリポジトリにあるので、EPELを利用できるようにするには本サイトの yum用リポジトリの追加 を参照
hddtemp をインストール
 
 
[root@centos]# yum -y --enablerepo=epel install hddtemp
 
ハードディスク温度の取得確認
 
 
[root@centos]# hddtemp /dev/sda
 /dev/sda: Samsung SSD 840 EVO 120GB: 30°C
  ← ハードディスク情報と温度(30°C)が表示
 
hddtemp の起動
 
 
[root@centos]# systemctl start hddtemp
 
クリプトの作成
 
 
[root@centos]# vi /usr/share/hddtemp.sh
 
 
以下新規記述
 
 
#!/bin/sh
 /usr/sbin/hddtemp -n /dev/sda
 
/usr/sbin/hddtemp -n /dev/sda
 
アクセス権を変更
 
 
[root@centos]# /chmod 755 /usr/share/hddtemp.sh
 
ハードディスク温度表示設定
 
 
[root@centos]# vi /etc/mrtg/mrtg.cfg 
 
 
 
途中略
 

 以下最終行に追記

 
 
 
#--- HD Disk Temp ---
 Target[hddtemp]: `/usr/share/hddtemp.sh `
 MaxBytes[hddtemp]: 90
 Options[hddtemp]: growright,absolute,gauge,nopercent,noinfo
 WithPeak[hddtemp]: ymw
 YLegend[hddtemp]: Temperature
 ShortLegend[hddtemp]: 'C
 LegendI[hddtemp]: Temp:
 LegendO[hddtemp]:
 Legend1[hddtemp]: Disk Temp
 Legend2[hddtemp]:
 Title[hddtemp]: Disk Temp Information
 PageTop[hddtemp]:<h1>Disk Temp Information</h1>
 
インデックスページの作成
 
 
[root@centos]# indexmaker --columns=1 /etc/mrtg/mrtg.cfg > /home/***/mrtg/index.html
 
作成したインデックスページを日本語表示できるように修正
 
 
[root@centos]# vi /home/***/mrtg/index.html    ( **** はHP公開ディレクトリ)
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <HTML>
 <HEAD>
 <TITLE>MRTG Index Page</TITLE>
 <!-- Command line is easier to read using "View Page Properties" of your browser -->
 <!-- But not all browsers show that information. :-( -->
 <meta http-equiv="content-type" content="text/html; charset=
iso-8859-15" >  <--- 書き換え
    

 <meta http-equiv="content-type" content="text/html; charset=
iso2022jp" >
 <META NAME="Command-Line" CONTENT="/usr/bin/indexmaker --columns=1 /etc/mrtg/mrtg.cfg" >
 <META HTTP-EQUIV="Refresh" CONTENT="300" >
 <META HTTP-EQUIV="Cache-Control" content="no-cache" >
 <META HTTP-EQUIV="Pragma" CONTENT="no-cache" >
 <META HTTP-EQUIV="Expires" CONTENT="Fri, 31 Oct 2014 14:15:45 GMT" >
 <LINK HREF="favicon.ico" rel="shortcut icon" >

 <style type="text/css">
 /* commandline was: /usr/bin/indexmaker --columns=1 /etc/mrtg/mrtg.cfg */
 /* sorry, no style, just abusing this to place the commandline and pass validation */
 </style>
 </HEAD>
 
 
以下省略
 
MRTG の実行
 
 
[root@centos]# LANG=ja_JP.iso2022jp mrtg /etc/mrtg/mrtg.cfg ※3回行う
 
MRTG は過去2回の起動時ログを使用するため、mrtg.cfg を修正した場合は必ず3回実行する
MRTG の自動実行
crontab に5分毎の実行を記述
 
 
[root@centos]# vi /etc/cron.d/mrtg
 
以下新規記述
 
*/5 * * * * root LANG=ja_JP.iso2022jp mrtg /etc/mrtg/mrtg.cfg
 
ブラウザ確認
http://<サーバーのアドレス>/mrtg

各グラフをクリックする事により詳細が表示される
参考としてCPUの画像を表示

戻る