Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3056011
  • 博文数量: 535
  • 博客积分: 15788
  • 博客等级: 上将
  • 技术积分: 6507
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 09:11
文章分类

全部博文(535)

文章存档

2016年(1)

2015年(1)

2014年(10)

2013年(26)

2012年(43)

2011年(86)

2010年(76)

2009年(136)

2008年(97)

2007年(59)

分类: 系统运维

2011-09-21 11:22:16

插件下载:

一、被监控端
  1. cp scripts/iostat-persist.pl /usr/local/bin/
  2. chmod +x /usr/local/bin/iostat-persist.pl
 添加计划任务:
  1. */2 * * * * cd /tmp && iostat -xkd 30 2 | sed 's/,/\./g' > io.tmp && mv io.tmp iostat.cache
 
 注:我试图不用添加计划任务,功能可以实现,但是数据断断续续。详细见后面,。
 
 添加oid
pass_persist .1.3.6.1.3.1 /usr/bin/perl /usr/local/bin/iostat-persist.pl
 
 iostat-persist.pl和iostat.pl的区别:
 There is also a much improved persistent script which involves a lot less forking,
and also a caching mechanism. If you would like to use this version (recommended)

二、cacti服务器
1、测试
  1. snmpwalk -v 2c 59.51.24.18 -c public .1.3.6.1.3.1.13
关于ubuntu,可以取到数据,但是不是利用率%util而是前面的w_await,是因为iostat的返回多了两列r_await和w_await,所以需要修改iostat-persist.pl,详细的修改见文章后面。

SNMPv2-SMI::experimental.1.13 = No Such Instance currently exists at this OID
  
如果snmp中已经添加了上面的pass_persist,则有可能是selinux造成的,查看/var/log/audit/audit.log,有下面的日志(对于RHEL,注意查看/var/log/message)
type=AVC msg=audit(1316500461.989:3076): avc:  denied  { read } for  pid=17209 comm="perl" name="iostat.cache" dev=dm-0 ino=1177351 scontext=user_u:system_r:snmpd_t:s0 tcontext=root:object_r:tmp_t:s0 tclass=file
看不到该日志?
  1. Turn on all AVC Messages for which

  2. SELinux currently is "dontaudit"ing.

  3. semodule -DB

  4. Turn "dontaudit" rules back on.

  5. semodule -B

  1. ls -Z /tmp/iostat.cache
可以看到权限确实有问题。

解决办法一:不要放到/tmp目录

解决办法二:
将上面的日志保存到一个文件中,如1.log,执行
  1. audit2allow -m local -l -i ./1.log >local.te
audit2allow:
rhel5:policycoreutils
rhel6:policycoreutils-python.x86_64

local.te的内容类似于
 
module local 1.0;
require {
type snmpd_t;
        
type tmp_t;
        
class file  read ;
}
#============= snmpd_t =============
allow snmpd_t tmp_t:file  read ;

继续:
  1. checkmodule -M -m -o local.mod local.te
  2. semodule_package -o local.pp -m local.mod
  3. semodule -i local.pp

成功后,重新执行snmpwalk -v 2c  59.51.24.18 -c public .1.3.6.1.3.1.13,发现又有新的报错,
type=AVC msg=audit(1316500461.989:3076): avc:  denied  { read } for  pid=17209 comm="perl" name="/tmp/iostat.cache" dev=dm-0 ino=1177351 scontext=user_u:system_r:snmpd_t:s0 tcontext=root:object_r:tmp_t:s0 tclass=file
type=AVC msg=audit(1316501743.698:109): avc:  denied  { ioctl } for  pid=3294 comm="perl" path="/tmp/iostat.cache" dev=dm-0 ino=1177349 scontext=user_u:system_r:snmpd_t:s0 tcontext=root:object_r:tmp_t:s0 tclass=file
type=AVC msg=audit(1316501743.698:110): avc:  denied  { getattr } for  pid=3294 comm="perl" path="/tmp/iostat.cache" dev=dm-0 ino=1177349 scontext=user_u:system_r:snmpd_t:s0 tcontext=root:object_r:tmp_t:s0 tclass=file
 
重复上面的步骤,这次把新的两条和上面的一条错误一起放到log文件里。
  1. audit2allow -m local -l -i ./1.log >local.te
local.te内容为:
module local 1.0;
require {
type snmpd_t;
        
type tmp_t;
        
class file { read ioctl getattr };
}
#============= snmpd_t =============
allow snmpd_t tmp_t:file { read ioctl getattr };
 
  1. checkmodule -M -m -o local.mod local.te && semodule_package -o local.pp -m local.mod && semodule -i local.pp

执行snmpwalk,这次成功取到了数据
snmpwalk -v 2c  1.1.1.1 -c public .1.3.6.1.3.1.13
SNMPv2-SMI::experimental.1.13.1 = STRING: "0.11"
SNMPv2-SMI::experimental.1.13.2 = STRING: "0.00"
SNMPv2-SMI::experimental.1.13.3 = STRING: "0.11"
SNMPv2-SMI::experimental.1.13.4 = STRING: "0.11"
SNMPv2-SMI::experimental.1.13.5 = STRING: "0.00"

Ubuntu取不到数据
iso.3.6.1.3.1 = No Such Instance currently exists at this OID
可能原因1
/etc/snmp/snmpd.conf
pass_persist .1.3.6.1.3.1 /usr/local/bin/iostat-persist.pl
OID和脚本中间不要额外写名字

可能原因2
发现snmp的pass_persist脚本没有起来
https://community.opmantek.com/display/NMIS/Extending+SNMPd+for+custom+monitoring
pass_persist的脚本一直运行,如果ps -ef是可以看到该脚本一直在运行着。及时不是随snmp启动,至少第一次请求后将一直存在。
It's also very efficient because the pass_persist program
is running permanently and there is no repeated startup overhead, and
the program can do whatever it needs to do, whenever and however it
wants to.

Debug:
/usr/sbin/snmpd -D -f  -u snmp -g snmp >./11111111 2>&1
查看日志
加载了pass_persist模块
snmpd_register_app_config_handler: registering .conf token for "pass_persist"
trace: internal_register_config_handler(): read_config.c, 217:
9:read_config:register_handler: registering snmpd pass_persist

启动snmpd不会立刻启动iostat脚本,通过snmpwalk请求一遍,可以看到log
ucd-snmp/pass_persist: open_persist_pipe(1,'/usr/bin/perl /usr/local/bin/iostat-persist.pl') recurse=0
trace: open_persist_pipe(): ucd-snmp/pass_persist.c, 543:
ucd-snmp/pass_persist: open_persist_pipe: opened the pipes
trace: open_persist_pipe(): ucd-snmp/pass_persist.c, 579:
ucd-snmp/pass_persist: open_persist_pipe: Got perl: warning: Setting locale failed.
 instead of PONG!

# /usr/local/bin/iostat-persist.pl
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
所以问题还是perl的LC_ALL问题,为最小化减小对现有系统影响,将该环境变量添加到/etc/ini.d/snmpd,重启snmpd,OK

2、
导入cacti的模板
  1. cp /opt/markround-Cacti-iostat-templates-7394c7b/snmp_queries/linux/iostat.xml /usr/local/nginx/html/cacti/resource/snmp_queries/

  2. chown cacti.cacti/usr/local/nginx/html/cacti/resource/snmp_queries/iostat.xml
 

我只想监控磁盘利用率,所以我只导入了以下几个模板
Import Templates菜单下导入:
cacti_data_query_iostat_-_utilisation.xml
cacti_graph_template_iostat_-_utilisation.xml

注:导如后,建议修改一下
原始模板是在Current:后面换行,所以造成监控图中看不到current的数据,更改为在max后换行
graph templates-> iostat - Utilisation ->
将Item # 5 中的Insert Hard Return的对勾去掉。
将Item # 4 中的Insert Hard Return的对勾勾选上。


画图
Associated Data Queries下添加即可



关于时无法获取信息:

  1. + Running data query [12].
  2. + Found type = '3' [SNMP Query].
  3. + Found data query XML file at '/usr/local/nginx/html/cacti/resource/snmp_queries/iostat.xm'
  4. + XML file parsed ok.
  5. + Invalid field <index_order>ioDescr:ioName:ioIndex</index_order>
  6. + Must contain <direction>input</direction> fields only
解决办法:
+ Invalid field <index_order>ioDescr:ioName:ioIndex</index_order>
更改为
+ Invalid field <index_order>ioDescr:ioIndex</index_order>
即去掉ioName:


关于ubuntu,可以取到数据,但是不是利用率%util而是前面的w_await,是因为iostat的返回多了两列r_await和w_await,所以需要修改iostat-persist.pl,详细的修改见文章后面。

            if ($ostype eq 'linux') {
               /^([a-z0-9\-\/]+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[
    \.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)/;

  1.            $stats{"$base_oid.1.$devices"} = $devices; # index
  2.            $stats{"$base_oid.2.$devices"} = $1; # device name
  3.            $stats{"$base_oid.3.$devices"} = $2; # rrqm/s
  4.            $stats{"$base_oid.4.$devices"} = $3; # wrqm/s
  5.            $stats{"$base_oid.5.$devices"} = $4; # r/s
  6.            $stats{"$base_oid.6.$devices"} = $5; # w/s
  7.            $stats{"$base_oid.7.$devices"} = $6; # rkB/s
  8.            $stats{"$base_oid.8.$devices"} = $7; # wkB/s
  9.            $stats{"$base_oid.9.$devices"} = $8; # avgrq-sz
  10.            $stats{"$base_oid.10.$devices"} = $9; # avgqu-sz
  11.            $stats{"$base_oid.11.$devices"} = $10; # await
  12.            $stats{"$base_oid.12.$devices"} = $11; # r_await
  13.            $stats{"$base_oid.13.$devices"} = $12; # w_await
  14.            $stats{"$base_oid.14.$devices"} = $13; # svctm
  15.            $stats{"$base_oid.15.$devices"} = $14; # %util

        if ($ostype eq 'linux') {
           /^([a-z0-9\-\/]+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[
\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)/;
更改为
        if ($ostype eq 'linux') {
           /^([a-z0-9\-\/]+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[
\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)/;
并增加$11,$12两行,并重启snmpd。



关于nagios check_snmp
使用snmpwalk可以取到数据,使用check_snmp有报错:
SNMP OK -  = No Such Instance currently exists at this OID |
原因:
check_snmp doesn't do a walk. You must be very specific to the exact OID you  want to check. You'll also need to specify failure criteria if you want to get any kind of alert.
即一定要指定准确的OID
如.1.3.6.1.3.1.13.1




关于不cache iostat的数据,每次snmp来取数时再用iostat获取数据,这样就不需要添加cron计划任务,也不需要iostat.cache文件了,但是缺点是经常会取不到数据。

1、snmpd.conf中添加:pass .1.3.6.1.3.1 /usr/bin/perl /usr/local/bin/iostat.pl
2、修改iostat.pl
iostat.pl修改为:
  1. #!/usr/bin/env perl
  2. use strict;

  3. use constant debug => 0;
  4. my $base_oid = ".1.3.6.1.3.1";
  5. my $req;
  6. my %stats;
  7. my $devices;

  8. process();

  9. my $mode = shift(@ARGV);
  10. if ( $mode eq "-g" ) {
  11.     $req = shift(@ARGV);
  12.     getoid($req);
  13. }
  14. elsif ( $mode eq "-n" ) {
  15.     $req = shift(@ARGV);
  16.     my $next = getnextoid($req);
  17.     getoid($next);
  18. }
  19. else {
  20.     $req = $mode;
  21.     getoid($req);
  22. }

  23. sub process {
  24.     $devices = 1;
  25.     open( IOSTAT, "iostat -xkd 1 2 | sed 's/,/\./g'|" )
  26.       or return ("Could not run iostat : $!");

  27.     my $header_seen = 0;

  28.     while (<IOSTAT>) {
  29.         if (/^[D|d]evice/) {
  30.             $header_seen++;
  31.             next;
  32.         }
  33.         next if ( $header_seen < 2 );
  34.         next if (/^$/);
  35.         
  36.            /^([a-z0-9\-\/]+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)\s+(\d+[\.,]\d+)/;

  37.            $stats{"$base_oid.1.$devices"} = $devices;    # index
  38.            $stats{"$base_oid.2.$devices"} = $1;        # device name
  39.            $stats{"$base_oid.3.$devices"} = $2;        # rrqm/s
  40.            $stats{"$base_oid.4.$devices"} = $3;        # wrqm/s
  41.            $stats{"$base_oid.5.$devices"} = $4;        # r/s
  42.            $stats{"$base_oid.6.$devices"} = $5;        # w/s
  43.            $stats{"$base_oid.7.$devices"} = $6;        # rkB/s
  44.            $stats{"$base_oid.8.$devices"} = $7;        # wkB/s
  45.            $stats{"$base_oid.9.$devices"} = $8;        # avgrq-sz
  46.            $stats{"$base_oid.10.$devices"} = $9;        # avgqu-sz
  47.            $stats{"$base_oid.11.$devices"} = $10;        # await
  48.            $stats{"$base_oid.12.$devices"} = $11;        # svctm
  49.            $stats{"$base_oid.13.$devices"} = $12;        # %util


  50.         $devices++;
  51.     }

  52. }

  53. sub getoid {
  54.     my $oid = shift(@_);
  55.     print "Fetching oid : $oid\n" if (debug);
  56.     if ( $oid =~ /^$base_oid\.(\d+)\.(\d+).*/ && exists( $stats{$oid} ) ) {
  57.         print $oid. "\n";
  58.         if ( $1 == 1 ) {
  59.             print "integer\n";
  60.         }
  61.         else {
  62.             print "string\n";
  63.         }

  64.         print $stats{$oid} . "\n";
  65.     }
  66. }

  67. sub getnextoid {
  68.     my $first_oid = shift(@_);
  69.     my $next_oid = '';
  70.     my $count_id;
  71.     my $index;

  72.     if ( $first_oid =~ /$base_oid\.(\d+)\.(\d+).*/ ) {
  73.         print("getnextoid($first_oid): index: $2, count_id: $1\n") if (debug);
  74.         if ( $2 + 1 >= $devices ) {
  75.             $count_id = $1 + 1;
  76.             $index = 1;
  77.         }
  78.         else {
  79.             $index = $2 + 1;
  80.             $count_id = $1;
  81.         }
  82.         print(
  83.             "getnextoid($first_oid): NEW - index: $index, count_id: $count_id\n"
  84.         ) if (debug);
  85.         $next_oid = "$base_oid.$count_id.$index";
  86.     }
  87.     elsif ( $first_oid =~ /$base_oid\.(\d+).*/ ) {
  88.         $next_oid = "$base_oid.$1.1";
  89.     }
  90.     elsif ( $first_oid eq $base_oid ) {
  91.         $next_oid = "$base_oid.1.1";
  92.     }
  93.     print("getnextoid($first_oid): returning $next_oid\n") if (debug);
  94.     return $next_oid;
  95. }
阅读(4806) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~