cwx 发表于 2018-5-7 13:07:51

在ubuntu上安装服务器监视软件MRTG

二、安装CPU负载监视[默认5分钟采集一次]  切换到超级用户:
  sudo -sH
  安装软件:
  apt-get install sysstat
  建立CPU脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.cpu
  #!/bin/bash
  cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'`
  cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'`
  UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
  echo $cpuusr
  echo $cpusys
  echo $UPtime
  hostname
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.cpu
  修改 /etc/mrtg.cfg 在文件最后加入cpu项目
  Target: `/opt/mrtg/mrtg.cpu`
  MaxBytes: 100
  Options: gauge, nopercent, growright
  YLegend: CPU loading (%)
  ShortLegend: %
  LegendO:   CPU us;
  LegendI:   CPU sy;
  Title: CPU Loading
  PageTop: <H1>CPU Loading</H1>
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  三、安装WWW连接数监视[默认5分钟采集一次]
  切换到超级用户:
  sudo -sH
  建立WWW脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.www
  #!/bin/bash
  all=`netstat -a | grep www|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
  user=`netstat -a | grep www|awk '{print $5}'|cut -d&quot;:&quot; -f1|sort| uniq |wc -l | awk '{print $1 - 1}'`
  if [ &quot;$all&quot; = &quot;-1&quot; ]; then
  echo 0
  else
  echo $all
  fi
  if [ &quot;$user&quot; = &quot;-1&quot; ]; then
  echo 0
  else
  echo $user
  fi
  UPtime=`/usr/bin/uptime | awk '{print $3 &quot; &quot; $4 &quot; &quot; $5}'`
  echo $UPtime
  hostname
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.www
  修改 /etc/mrtg.cfg 在文件最后加入www项目
  Target: `/opt/mrtg/mrtg.www`
  MaxBytes: 500
  Options: nopercent, growright
  YLegend: Online Users
  ShortLegend: %
  LegendI: &nbsp; Connect :
  LegendO: &nbsp; Online :
  Title: WWW Connect
  PageTop: <H1> WWW Connect </H1>
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  四、安装内存使用监视[默认5分钟采集一次]
  切换到超级用户:
  sudo -sH
  建立RAM脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.ram
  #!/bin/bash
  # run this script to check the mem usage.
  totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
  usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
  UPtime=`/usr/bin/uptime | awk '{print $3&quot;&quot;$4&quot;&quot;$5}'`
  echo $totalmem
  echo $usedmem
  echo $UPtime
  hostname
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.ram
  修改 /etc/mrtg.cfg 在文件最后加入ram项目
  Target: `/opt/mrtg/mrtg.ram`
  #Unscaled: dwym
  MaxBytes: 2048000
  Title:Memory
  ShortLegend: &
  kmg:kB,MB
  kilo:1024
  YLegend: &nbsp; Memory Usage :
  Legend1: &nbsp; Total Memory :
  Legend2: &nbsp; Used Memory :
  LegendI: &nbsp; Total Memory :
  LegendO: &nbsp; Used Memory :
  Options: growright,gauge,nopercent
  PageTop:<H1>Memory</H1>
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  五、安装FTP连接数监视[默认5分钟采集一次]
  切换到超级用户:
  sudo -sH
  建立FTP脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.ftp
  #!/bin/bash
  all=`netstat -a | grep ftp|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
  user=`netstat -a | grep ftp|awk '{print $5}'|cut -d&quot;:&quot; -f1|sort| uniq |wc -l | awk '{print $1 - 1}'`
  if [ &quot;$all&quot; = &quot;-1&quot; ]; then
  echo 0
  else
  echo $all
  fi
  if [ &quot;$user&quot; = &quot;-1&quot; ]; then
  echo 0
  else
  echo $user
  fi
  UPtime=`/usr/bin/uptime | awk '{print $3 &quot; &quot; $4 &quot; &quot; $5}'`
  echo $UPtime
  hostname
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.ftp
  修改 /etc/mrtg.cfg 在文件最后加入ftp项目
  Target: `/opt/mrtg/mrtg.ftp`
  MaxBytes: 500
  Options: nopercent, growright
  YLegend: Online Users
  ShortLegend: %
  LegendI: &nbsp; Connect :
  LegendO: &nbsp; Online :
  Title: FTP Connect
  PageTop: <H1> FTP Connect </H1>
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  六、安装CPU温度监视[默认5分钟采集一次]
  切换到超级用户:
  sudo -sH
  安装软件:
  apt-get install mbmon
  建立CPU温度脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.temp
  #!/bin/bash
  /usr/bin/mbmon -c 1 -i -T 4 -u -n
  exit 0
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.temp
  修改 /etc/mrtg.cfg 在文件最后加入cpu项目
  Target: `/opt/mrtg/mrtg.temp`
  MaxBytes: 100
  Title: CPU Temperature
  PageTop: <H1>CPU Temperature</H1>
  Options: gauge,absolute,unknaszero,growright
  YLegend: Temperature(C)
  ShortLegend: (C)
  Legend1: CPU Temperature
  Legend2: M/B Temperature
  LegendI: CPU Temp.
  LegendO: M/B Temp.
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  七、硬盘读写监视[默认5分钟采集一次]
  切换到超级用户:
  sudo -sH
  建立硬盘读写脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.disk
  #!/bin/bash
  hd=sda
  disk=/dev/$hd
  UPtime=`/usr/bin/uptime |awk '{print $3&quot;&quot;$4&quot;&quot;$5}'`
  KBread_sec=`iostat -x $disk|grep $hd |awk '{print 8$}'`
  KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print 9$}'`
  echo $KBread_sec
  echo $KBwrite_sec
  echo $UPtime
  hostname
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.disk
  修改 /etc/mrtg.cfg 在文件最后加入硬盘读写
  Target: `/opt/mrtg/mrtg.disk`
  Title: Disk HDA I/O Utilization Report
  #Unscaled: dwym
  MaxBytes: 10240000
  PageTop: <H1>Disk I/O Utilization Report</H1>
  kmg: KB,MB,GB
  LegendI: Disk I/O KBread/sec
  LegendO: Disk I/O KBwrite/sec
  Legend1: Disk I/O KBread/sec
  Legend2: Disk I/O KBwrite/sec
  YLegend: Megabytes
  ShortLegend: &
  Options: growright,gauge,nopercent
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
  八、交换分区
  切换到超级用户:
  sudo -sH
  建立交换分区脚本:
  mkdir /opt/mrtg
  vim /opt/mrtg/mrtg.swap
  #!/bin/bash
  # This script to monitor the swap usage.
  totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'`
  usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'`
  echo &quot;$totalswap&quot;
  echo &quot;$usedswap&quot;
  使脚本可以执行:
  chmod +755 /opt/mrtg/mrtg.swap
  修改 /etc/mrtg.cfg 在文件最后加入
  Target: `/opt/mrtg/mrtg.swap`
  MaxBytes: 2048000
  Title:Memory State of Server
  ShortLegend: &
  kmg:kB,MB
  kilo:1024
  YLegend: Swap Usage
  Legend1: Total Swap
  Legend2: Used Swap
  LegendI: Total Swap
  LegendO: Used Swap
  Options: growright,gauge,nopercent
  PageTop:<H1>Swap</H1>
  重新生成索引页面:
  indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
  访问:
  http://localhost/mrtg/
页: [1]
查看完整版本: 在ubuntu上安装服务器监视软件MRTG