架构如下:
==============================
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
}
}
}
$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"]
}
}
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"]
}
}
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 {"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内容如下:
- # $Id: snmpdiskio,v 1.5 2008/01/31 21:19:50 dverlaeckt Exp $
- # snmpdiskio v0.9.6 (c) 2008 Dieter Verlaeckt <dieter.verlaeckt@gmail.com>
- # snmpdiskio v0.9.5 (c) 2007 Pablo Destefanis <pdestefanis@gmail.com>
- # snmpdiskio v0.9.4 (c) 2006 Mikael Fridh <mikael@meanstreak.se>
- # Fields in /proc/partitions (kernel 2.4)
- # major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
- # Fields in /proc/diskstats (kernel 2.6) for disks (i.e. hda)
- # major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
- # Fields in /proc/diskstats (kernel 2.6) for partitions (i.e. hda1)
- # major minor name rio rsect wio wsect
- # InBlocks = sectors written to disk
- # OutBlocks = sectors read from disk
- # Set default procfile for kernel 2.4
- PROCFILE="/proc/partitions"
- MODE="linux24"
- # Probably kernel 2.6:
- if [ -f /proc/diskstats ]; then
- PROCFILE=/proc/diskstats
- MODE="linux26"
- fi
- function hdNum()
- {
- awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++ } END { print num } ' $PROCFILE
- }
- function hdIndex()
- {
- awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++; print num } ' $PROCFILE
- }
- function hdDescr()
- {
- if [ "$MODE" = "linux26" ]; then
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $3 }' $PROCFILE
- else
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $4 }' $PROCFILE
- fi
- }
- function hdInBlocks()
- {
- if [ "$MODE" = "linux26" ]; then
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $7 * 512 }
- $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $10 * 512 } ' $PROCFILE
- else
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $11 * 512 } ' $PROCFILE
- fi
- }
- function hdOutBlocks()
- {
- if [ "$MODE" = "linux26" ]; then
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $5 * 512 }
- $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $6 * 512 } ' $PROCFILE
- else
- awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $7 * 512 } ' $PROCFILE
- fi
- }
- function usage()
- {
- cat <<-EOUSAGE
- Usage: $0 <hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks>
- EOUSAGE
- }
- if [ 1 -ne $# ]; then
- usage
- exit 1
- fi
- case $1 in
- hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks)
- $1
- ;;
- 'hdNum')
- hdNum
- ;;
- 'hdIndex')
- hdIndex
- ;;
- 'hdDescr')
- hdDescr
- ;;
- 'hdInBlocks')
- hdInBlocks
- ;;
- 'hdOutBlocks')
- hdOutBlocks
- ;;
- *)
- usage
- exit 1
- ;;
- esac
- 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
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
#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客户端模块的编写