架构如下:

 

puppet系列之之snmpd模块_第1张图片

 


==============================
manifests
=============================
 
1,at /etc/puppet/modules
-----------------------
init.pp
+++++++++++++
class cacti (
  $monitor_ip = "xxx.xxx.xxx.xxx",
  $snmp_passwd = "xxxx"
){
        case $operatingsystem {
                centos:{
                        include cacti::base
                        include cacti::diskio
                        include cacti::service
                }
        }
}
 
----------------------
base.pp
++++++++++
class cacti::base {
    package {"net-snmp":
            ensure => present
    }
    package {"net-snmp-utils":
            ensure  =>  present,
            require =>  package["net-snmp"]
    }
}
-----------------------------------
diskio.pp
++++++++++++
class cacti::diskio {
         file {"snmpdiskio":
            mode    => 755,
            owner   => root,
            group   => root,
            path    => "/usr/local/bin/snmpdiskio",
            source  => "puppet:///cacti/snmpdiskio",
            require => Class["cacti::base"],
            notify  => Class["cacti::service"]
         }
         file { "webconn.sh":
            mode    => 755,
            owner   => root,
            group   => root,
            path    => "/etc/snmp/webconn.sh",
            content => template('cacti/webconn.erb'),
            require => Class["cacti::base"],
            notify  => Class["cacti::service"]
         }
}
----------------------------
service.pp
++++++++++++++
class cacti::service {
      file {"snmpd.conf":
        mode    => 644,
        owner   => root,
        group   => root,
        path    => "/etc/snmp/snmpd.conf",
        content => template('cacti/snmpd-conf.erb'),
        require => Class["cacti::base","cacti::diskio"],
        notify  => Service['snmpd']
      }

      service { "snmpd":
        ensure  => running,
        enable  => true,
        subscribe => File['snmpd.conf']
      }
   }
 
=========================
file
=========================
snmpdiskio内容如下:
 
    
    
    
    
  1. # $Id: snmpdiskio,v 1.5 2008/01/31 21:19:50 dverlaeckt Exp $ 
  2. # snmpdiskio v0.9.6 (c) 2008 Dieter Verlaeckt <dieter.verlaeckt@gmail.com> 
  3. # snmpdiskio v0.9.5 (c) 2007 Pablo Destefanis <pdestefanis@gmail.com> 
  4. # snmpdiskio v0.9.4 (c) 2006 Mikael Fridh <mikael@meanstreak.se> 
  5.  
  6. # Fields in /proc/partitions (kernel 2.4) 
  7. # major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq 
  8.  
  9. # Fields in /proc/diskstats (kernel 2.6) for disks (i.e. hda) 
  10. # major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq 
  11.  
  12. # Fields in /proc/diskstats (kernel 2.6) for partitions (i.e. hda1) 
  13. # major minor name rio rsect wio wsect 
  14.  
  15. InBlocks = sectors written to disk 
  16. OutBlocks = sectors read from disk 
  17.  
  18. # Set default procfile for kernel 2.4 
  19. PROCFILE="/proc/partitions" 
  20. MODE="linux24" 
  21.  
  22. # Probably kernel 2.6: 
  23. if [ -f /proc/diskstats ]; then 
  24.     PROCFILE=/proc/diskstats 
  25.     MODE="linux26" 
  26. fi 
  27.  
  28.  
  29. function hdNum() 
  30.         awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++ } END { print num } ' $PROCFILE 
  31.  
  32. function hdIndex() 
  33.         awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++; print num } ' $PROCFILE 
  34.  
  35. function hdDescr() 
  36.         if [ "$MODE" = "linux26" ]; then 
  37.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $3 }' $PROCFILE 
  38.         else 
  39.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $4 }' $PROCFILE 
  40.         fi 
  41.  
  42. function hdInBlocks() 
  43.         if [ "$MODE" = "linux26" ]; then 
  44.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $7 * 512 } 
  45.                       $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $10 * 512 } ' $PROCFILE 
  46.         else 
  47.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $11 * 512 } ' $PROCFILE 
  48.         fi 
  49.  
  50. function hdOutBlocks() 
  51.         if [ "$MODE" = "linux26" ]; then 
  52.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $5 * 512 } 
  53.                       $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $6 * 512 } ' $PROCFILE 
  54.         else 
  55.                 awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $7 * 512 } ' $PROCFILE 
  56.         fi 
  57.  
  58. function usage() 
  59.         cat <<-EOUSAGE 
  60. Usage: $0 <hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks> 
  61. EOUSAGE 
  62.  
  63. if [ 1 -ne $# ]; then 
  64.         usage 
  65.         exit 1 
  66. fi 
  67.  
  68. case $1 in 
  69.         hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks) 
  70.                 $1 
  71.         ;; 
  72.         'hdNum') 
  73.                 hdNum 
  74.         ;; 
  75.         'hdIndex') 
  76.                 hdIndex 
  77.         ;; 
  78.         'hdDescr') 
  79.                 hdDescr 
  80.         ;; 
  81.         'hdInBlocks') 
  82.                 hdInBlocks 
  83.         ;; 
  84.         'hdOutBlocks') 
  85.                 hdOutBlocks 
  86.         ;; 
  87.         *) 
  88.                 usage 
  89.                 exit 1 
  90.         ;; 
  91. esac 
  92.  
  93. exit 0 

 

 

============================
templates
============================
1,at /etc/puppet/modules/cacti/templates
------------------------------
snmpd-conf.erb
++++++++++++
com2sec localhost  <%= ipaddress_lo %>       public
com2sec monitorhost  <%= monitor_ip %>       <%= snmp_passwd %>
group   myhost         v2c           localhost
group   mygroup        v2c           monitorhost
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
access  myhost         ""      any       noauth    exact  all    none none
access  mygroup        ""      any       noauth    exact  all    none none
view all    included  .1                               80
pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat
extend .1.3.6.1.4.1.2021.21 webconn /bin/bash /etc/snmp/webconn.sh
exec .1.3.6.1.4.1.2021.54 hdNum /bin/sh /usr/local/bin/snmpdiskio hdNum
exec .1.3.6.1.4.1.2021.55 hdIndex /bin/sh /usr/local/bin/snmpdiskio hdIndex
exec .1.3.6.1.4.1.2021.56 hdDescr /bin/sh /usr/local/bin/snmpdiskio hdDescr
exec .1.3.6.1.4.1.2021.57 hdInBlocks /bin/sh /usr/local/bin/snmpdiskio hdInBlocks
exec .1.3.6.1.4.1.2021.58 hdOutBlocks /bin/sh /usr/local/bin/snmpdiskio hdOutBlocks
 
------------------------------------
webconn.erb
++++++++++++++++++++
#!/bin/sh
#If more than one IP or port Please '|' separated
ip="<%= ipaddress_eth0 %>"
port="80|443"
webconn=`netstat -nt |grep ESTABLISHED |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`
websyn=`netstat -nt |grep SYN_RECV |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`
weback=`netstat -nt |grep LAST_ACK |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`
webwait=`netstat -nt |grep TIME_WAI |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`
echo $webconn
echo $websyn
echo $weback
echo $webwait

 

----------------------------------------------------------------------

webconn.sh的作用是用来监控tcp连接数

另外在webconn.erb中如果客户端有双网卡则ip="<%= ipaddress_eth0 %>|<%= ipaddress_eth1 %>" 以此类推

snmpdiskio的作用是用来监控磁盘io

 

下一篇:salt客户端模块的编写